jset.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. interface jlib_decl IBitSet : public IInterface
  17. {
  18. virtual void set(unsigned n,bool val=true) = 0;
  19. virtual bool invert(unsigned n) = 0; // returns inverted value
  20. virtual bool test(unsigned n) = 0;
  21. virtual bool testSet(unsigned n,bool val=true) = 0; // returns prev val
  22. virtual unsigned scan(unsigned from,bool tst) = 0; // returns index of first = val >= from
  23. virtual unsigned scanInvert(unsigned from,bool tst) = 0; // like scan but inverts bit as well
  24. virtual void incl(unsigned lo, unsigned hi) = 0;
  25. virtual void excl(unsigned lo, unsigned hi) = 0;
  26. virtual void reset() = 0;
  27. virtual void serialize(MemoryBuffer &buffer) const = 0;
  28. };
  29. // type of underlying bit storage, exposed so thread-unsafe version can know boundaries
  30. typedef unsigned bits_t;
  31. enum { BitsPerItem = sizeof(bits_t) * 8 };
  32. // Simple BitSet // 0 based, all intermediate items exist, operations threadsafe and atomic
  33. extern jlib_decl IBitSet *createThreadSafeBitSet();
  34. extern jlib_decl IBitSet *deserializeThreadSafeBitSet(MemoryBuffer &mb);
  35. /* Not thread safe, but can be significantly faster than createThreadSafeBitSet
  36. * Client provides a fixed block of memory used for the bit set, threads must ensure they do not set bits
  37. * in parallel within the same bits_t space.
  38. * IOW, e.g. bits 0-sizeof(bits_t) must be set from only 1 thread at a time.
  39. */
  40. extern jlib_decl IBitSet *createBitSet(size32_t memSize, const void *mem, bool reset=true);
  41. // This form allows the size of the bit set to be dynamic. No guarantees about threading.
  42. extern jlib_decl IBitSet *createBitSet();
  43. extern jlib_decl IBitSet *deserializeBitSet(MemoryBuffer &mb);
  44. // returns number of bytes required to represent numBits in memory
  45. extern jlib_decl size32_t getBitSetMemoryRequirement(unsigned numBits);
  46. #endif