jset.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 nunber of trailing zeros. Deliberately undefined if value == 0
  20. inline unsigned countTrailingZeros(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 nunber of leading zeros. Deliberately undefined if value == 0
  42. inline unsigned countLeadingZeros(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)index;
  51. #else
  52. unsigned mask = 1U << 31;
  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. interface jlib_decl IBitSet : public IInterface
  64. {
  65. virtual void set(unsigned n,bool val=true) = 0;
  66. virtual bool invert(unsigned n) = 0; // returns inverted value
  67. virtual bool test(unsigned n) = 0;
  68. virtual bool testSet(unsigned n,bool val=true) = 0; // returns prev val
  69. virtual unsigned scan(unsigned from,bool tst) = 0; // returns index of first = val >= from
  70. virtual unsigned scanInvert(unsigned from,bool tst) = 0; // like scan but inverts bit as well
  71. virtual void incl(unsigned lo, unsigned hi) = 0;
  72. virtual void excl(unsigned lo, unsigned hi) = 0;
  73. virtual void reset() = 0;
  74. virtual void serialize(MemoryBuffer &buffer) const = 0;
  75. };
  76. // type of underlying bit storage, exposed so thread-unsafe version can know boundaries
  77. typedef unsigned bits_t;
  78. enum { BitsPerItem = sizeof(bits_t) * 8 };
  79. // Simple BitSet // 0 based, all intermediate items exist, operations threadsafe and atomic
  80. extern jlib_decl IBitSet *createThreadSafeBitSet();
  81. extern jlib_decl IBitSet *deserializeThreadSafeBitSet(MemoryBuffer &mb);
  82. /* Not thread safe, but can be significantly faster than createThreadSafeBitSet
  83. * Client provides a fixed block of memory used for the bit set, threads must ensure they do not set bits
  84. * in parallel within the same bits_t space.
  85. * IOW, e.g. bits 0-sizeof(bits_t) must be set from only 1 thread at a time.
  86. */
  87. extern jlib_decl IBitSet *createBitSet(size32_t memSize, const void *mem, bool reset=true);
  88. // This form allows the size of the bit set to be dynamic. No guarantees about threading.
  89. extern jlib_decl IBitSet *createBitSet();
  90. extern jlib_decl IBitSet *deserializeBitSet(MemoryBuffer &mb);
  91. // returns number of bytes required to represent numBits in memory
  92. extern jlib_decl size32_t getBitSetMemoryRequirement(unsigned numBits);
  93. #endif