jset.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #ifndef __JSET__
  14. #define __JSET__
  15. #include "jiface.hpp"
  16. #if defined (_WIN32)
  17. #include <intrin.h>
  18. #endif
  19. //Return the number of trailing zeros. Deliberately undefined if value == 0
  20. inline unsigned countTrailingUnsetBits(unsigned value)
  21. {
  22. dbgassertex(value != 0);
  23. #if defined(__GNUC__)
  24. return __builtin_ctz(value);
  25. #elif defined (_WIN32)
  26. unsigned long index;
  27. _BitScanForward(&index, value);
  28. return (unsigned)index;
  29. #else
  30. unsigned mask = 1U;
  31. unsigned i;
  32. for (i=0; i < sizeof(unsigned)*8; i++)
  33. {
  34. if (value & mask)
  35. return i;
  36. mask = mask << 1;
  37. }
  38. return i;
  39. #endif
  40. }
  41. //Return the number of leading zeros. Deliberately undefined if value == 0
  42. inline unsigned countLeadingUnsetBits(unsigned value)
  43. {
  44. dbgassertex(value != 0);
  45. #if defined(__GNUC__)
  46. return __builtin_clz(value);
  47. #elif defined (_WIN32)
  48. unsigned long index;
  49. _BitScanReverse(&index, value);
  50. return (unsigned)((sizeof(unsigned)*8)-1 - index);
  51. #else
  52. unsigned mask = 1U << ((sizeof(unsigned)*8)-1);
  53. unsigned i;
  54. for (i=0; i < sizeof(unsigned)*8; i++)
  55. {
  56. if (value & mask)
  57. return i;
  58. mask = mask >> 1;
  59. }
  60. return i;
  61. #endif
  62. }
  63. //Return the number of bits including the first non-zero bit. Undefined if value == 0
  64. inline unsigned getMostSignificantBit(unsigned value)
  65. {
  66. dbgassertex(value != 0);
  67. #if defined(__GNUC__)
  68. return (sizeof(unsigned)*8) - __builtin_clz(value);
  69. #elif defined (_WIN32)
  70. unsigned long index;
  71. _BitScanReverse(&index, value);
  72. return (unsigned)index+1;
  73. #else
  74. unsigned mask = 1U << ((sizeof(unsigned)*8)-1);
  75. unsigned i;
  76. for (i=0; i < sizeof(unsigned)*8; i++)
  77. {
  78. if (value & mask)
  79. return sizeof(unsigned)*8-i;
  80. mask = mask >> 1;
  81. }
  82. return 0;
  83. #endif
  84. }
  85. interface jlib_decl IBitSet : public IInterface
  86. {
  87. virtual void set(unsigned n,bool val=true) = 0;
  88. virtual bool invert(unsigned n) = 0; // returns inverted value
  89. virtual bool test(unsigned n) = 0;
  90. virtual bool testSet(unsigned n,bool val=true) = 0; // returns prev val
  91. virtual unsigned scan(unsigned from,bool tst) = 0; // returns index of first = val >= from
  92. virtual unsigned scanInvert(unsigned from,bool tst) = 0; // like scan but inverts bit as well
  93. virtual void incl(unsigned lo, unsigned hi) = 0;
  94. virtual void excl(unsigned lo, unsigned hi) = 0;
  95. virtual void reset() = 0;
  96. virtual void serialize(MemoryBuffer &buffer) const = 0;
  97. };
  98. // type of underlying bit storage, exposed so thread-unsafe version can know boundaries
  99. typedef unsigned bits_t;
  100. enum { BitsPerItem = sizeof(bits_t) * 8 };
  101. // Simple BitSet // 0 based, all intermediate items exist, operations threadsafe and atomic
  102. extern jlib_decl IBitSet *createThreadSafeBitSet();
  103. extern jlib_decl IBitSet *deserializeThreadSafeBitSet(MemoryBuffer &mb);
  104. /* Not thread safe, but can be significantly faster than createThreadSafeBitSet
  105. * Client provides a fixed block of memory used for the bit set, threads must ensure they do not set bits
  106. * in parallel within the same bits_t space.
  107. * IOW, e.g. bits 0-sizeof(bits_t) must be set from only 1 thread at a time.
  108. */
  109. extern jlib_decl IBitSet *createBitSet(size32_t memSize, const void *mem, bool reset=true);
  110. // This form allows the size of the bit set to be dynamic. No guarantees about threading.
  111. extern jlib_decl IBitSet *createBitSet();
  112. extern jlib_decl IBitSet *deserializeBitSet(MemoryBuffer &mb);
  113. // returns number of bytes required to represent numBits in memory
  114. extern jlib_decl size32_t getBitSetMemoryRequirement(unsigned numBits);
  115. #endif