tsIntegerSet.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _TSINTEGERSET_H_
  23. #define _TSINTEGERSET_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. #ifndef _TVECTOR_H_
  28. #include "core/util/tVector.h"
  29. #endif
  30. #if defined(TORQUE_MAX_LIB)
  31. #define MAX_TS_SET_DWORDS 32
  32. #else
  33. #define MAX_TS_SET_DWORDS 64
  34. #endif
  35. #define MAX_TS_SET_SIZE (32*MAX_TS_SET_DWORDS)
  36. class Stream;
  37. /// The standard mathmatical set, where there are no duplicates. However,
  38. /// this set uses bits instead of numbers.
  39. class TSIntegerSet
  40. {
  41. /// The bits!
  42. U32 bits[MAX_TS_SET_DWORDS];
  43. public:
  44. /// Sets this bit to false
  45. void clear(S32 index);
  46. /// Set this bit to true
  47. void set(S32 index);
  48. /// Is this bit true?
  49. bool test(S32 index) const;
  50. /// Sets all bits to false
  51. void clearAll(S32 upto = MAX_TS_SET_SIZE);
  52. /// Sets all bits to true
  53. void setAll(S32 upto = MAX_TS_SET_SIZE);
  54. /// Tests all bits for true
  55. bool testAll(S32 upto = MAX_TS_SET_SIZE) const;
  56. /// Counts set bits
  57. S32 count(S32 upto = MAX_TS_SET_SIZE) const;
  58. /// intersection (a & b)
  59. void intersect(const TSIntegerSet&);
  60. /// union (a | b)
  61. void overlap(const TSIntegerSet&);
  62. /// xor (a | b) & ( !(a & b) )
  63. void difference(const TSIntegerSet&);
  64. /// subtraction (a - b)
  65. void takeAway(const TSIntegerSet&);
  66. /// copy one integer set into another
  67. void copy(const TSIntegerSet&);
  68. void insert(S32 index, bool value);
  69. void erase(S32 index);
  70. void operator=(const TSIntegerSet& otherSet) { copy(otherSet); }
  71. S32 start() const;
  72. S32 end() const;
  73. void next(S32 & i) const;
  74. void read(Stream *);
  75. void write(Stream *) const;
  76. TSIntegerSet();
  77. TSIntegerSet(const TSIntegerSet&);
  78. };
  79. inline void TSIntegerSet::clear(S32 index)
  80. {
  81. AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::clear");
  82. bits[index>>5] &= ~(1 << (index & 31));
  83. }
  84. inline void TSIntegerSet::set(S32 index)
  85. {
  86. AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::set");
  87. bits[index>>5] |= 1 << (index & 31);
  88. }
  89. inline bool TSIntegerSet::test(S32 index) const
  90. {
  91. AssertFatal(index>=0 && index<MAX_TS_SET_SIZE,"TS::IntegerSet::test");
  92. return ((bits[index>>5] & (1 << (index & 31)))!=0);
  93. }
  94. #endif