jset.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. extern jlib_decl IBitSet *deserializeIBitSet(MemoryBuffer &mb);
  30. // type of underlying bit storage, exposed so thread-unsafe version can know boundaries
  31. typedef unsigned bits_t;
  32. enum { BitsPerItem = sizeof(bits_t) * 8 };
  33. // returns number of bytes required to represent numBits in memory
  34. extern jlib_decl size32_t getBitSetMemoryRequirement(unsigned numBits);
  35. // Simple BitSet // 0 based, all intermediate items exist, operations threadsafe and atomic
  36. extern jlib_decl IBitSet *createBitSet();
  37. /* Thread unsafe, but can be significantly faster.
  38. * Client provide a fixed block of memory used for the bit set, threads must ensure they do not set bits
  39. * in parallel within the same bits_t space.
  40. * IOW, e.g. bits 0-sizeof(bits_t) must be set from only 1 thread at a time.
  41. */
  42. extern jlib_decl IBitSet *createBitSetThreadUnsafe(size32_t memSize, const void *mem, bool reset=true);
  43. // This form allows the size of the bit set to be dynamic, but there are no guarantees about threading
  44. extern jlib_decl IBitSet *createBitSetThreadUnsafe();
  45. #endif