jset.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. inline unsigned countTrailingUnsetBits(unsigned __int64 value)
  42. {
  43. dbgassertex(value != 0);
  44. #if defined(__GNUC__)
  45. return __builtin_ctzll(value);
  46. #elif defined (_WIN32)
  47. //There doesn't seem to be a 64bit version of _BitScanForward() generally available
  48. unsigned long index;
  49. if ((unsigned)value)
  50. {
  51. _BitScanForward(&index, (unsigned)value);
  52. return (unsigned)index;
  53. }
  54. else
  55. {
  56. _BitScanForward(&index, (unsigned)(value >> 32));
  57. return (unsigned)index+32;
  58. }
  59. #else
  60. unsigned __int64 mask = 1U;
  61. unsigned i;
  62. for (i=0; i < sizeof(mask)*8; i++)
  63. {
  64. if (value & mask)
  65. return i;
  66. mask = mask << 1;
  67. }
  68. return i;
  69. #endif
  70. }
  71. //Return the number of leading zeros. Deliberately undefined if value == 0
  72. inline unsigned countLeadingUnsetBits(unsigned value)
  73. {
  74. dbgassertex(value != 0);
  75. #if defined(__GNUC__)
  76. return __builtin_clz(value);
  77. #elif defined (_WIN32)
  78. unsigned long index;
  79. _BitScanReverse(&index, value);
  80. return (unsigned)((sizeof(unsigned)*8)-1 - index);
  81. #else
  82. unsigned mask = 1U << ((sizeof(unsigned)*8)-1);
  83. unsigned i;
  84. for (i=0; i < sizeof(unsigned)*8; i++)
  85. {
  86. if (value & mask)
  87. return i;
  88. mask = mask >> 1;
  89. }
  90. return i;
  91. #endif
  92. }
  93. //Return the number of bits including the first non-zero bit. Undefined if value == 0
  94. inline unsigned getMostSignificantBit(unsigned value)
  95. {
  96. dbgassertex(value != 0);
  97. #if defined(__GNUC__)
  98. return (sizeof(unsigned)*8) - __builtin_clz(value);
  99. #elif defined (_WIN32)
  100. unsigned long index;
  101. _BitScanReverse(&index, value);
  102. return (unsigned)index+1;
  103. #else
  104. unsigned mask = 1U << ((sizeof(unsigned)*8)-1);
  105. unsigned i;
  106. for (i=0; i < sizeof(unsigned)*8; i++)
  107. {
  108. if (value & mask)
  109. return sizeof(unsigned)*8-i;
  110. mask = mask >> 1;
  111. }
  112. return 0;
  113. #endif
  114. }
  115. interface jlib_decl IBitSet : public IInterface
  116. {
  117. virtual void set(unsigned n,bool val=true) = 0;
  118. virtual bool invert(unsigned n) = 0; // returns inverted value
  119. virtual bool test(unsigned n) = 0;
  120. virtual bool testSet(unsigned n,bool val=true) = 0; // returns prev val
  121. virtual unsigned scan(unsigned from,bool tst) = 0; // returns index of first = val >= from
  122. virtual unsigned scanInvert(unsigned from,bool tst) = 0; // like scan but inverts bit as well
  123. virtual void incl(unsigned lo, unsigned hi) = 0;
  124. virtual void excl(unsigned lo, unsigned hi) = 0;
  125. virtual void reset() = 0;
  126. virtual void serialize(MemoryBuffer &buffer) const = 0;
  127. };
  128. // type of underlying bit storage, exposed so thread-unsafe version can know boundaries
  129. typedef unsigned bits_t;
  130. enum { BitsPerItem = sizeof(bits_t) * 8 };
  131. // Simple BitSet // 0 based, all intermediate items exist, operations threadsafe and atomic
  132. extern jlib_decl IBitSet *createThreadSafeBitSet();
  133. extern jlib_decl IBitSet *deserializeThreadSafeBitSet(MemoryBuffer &mb);
  134. /* Not thread safe, but can be significantly faster than createThreadSafeBitSet
  135. * Client provides a fixed block of memory used for the bit set, threads must ensure they do not set bits
  136. * in parallel within the same bits_t space.
  137. * IOW, e.g. bits 0-sizeof(bits_t) must be set from only 1 thread at a time.
  138. */
  139. extern jlib_decl IBitSet *createBitSet(size32_t memSize, const void *mem, bool reset=true);
  140. // This form allows the size of the bit set to be dynamic. No guarantees about threading.
  141. extern jlib_decl IBitSet *createBitSet();
  142. extern jlib_decl IBitSet *deserializeBitSet(MemoryBuffer &mb);
  143. // returns number of bytes required to represent numBits in memory
  144. extern jlib_decl size32_t getBitSetMemoryRequirement(unsigned numBits);
  145. #endif