tsIntegerSet.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. #include "ts/tsIntegerSet.h"
  23. #include "platform/platform.h"
  24. #include "core/stream/stream.h"
  25. #define SETUPTO(upto) ( ((1<<(upto&31))-1)*2+1 ) // careful not to shift more than 31 times
  26. void TSIntegerSet::clearAll(S32 upto)
  27. {
  28. AssertFatal(upto<=MAX_TS_SET_SIZE,"TSIntegerSet::clearAll: out of range");
  29. dMemset(bits,0,(upto>>5)*4);
  30. if (upto&31)
  31. bits[upto>>5] &= ~SETUPTO(upto);
  32. }
  33. void TSIntegerSet::setAll(S32 upto)
  34. {
  35. AssertFatal(upto<=MAX_TS_SET_SIZE,"TSIntegerSet::setAll: out of range");
  36. dMemset(bits,0xFFFFFFFF,(upto>>5)*4);
  37. if (upto&31)
  38. bits[upto>>5] |= SETUPTO(upto);
  39. }
  40. bool TSIntegerSet::testAll(S32 upto) const
  41. {
  42. AssertFatal(upto<=MAX_TS_SET_SIZE,"TSIntegerSet::testAll: out of range");
  43. S32 i;
  44. for (i=0; i<(upto>>5); i++)
  45. if (bits[i])
  46. return true;
  47. if (upto&31)
  48. return (bits[upto>>5] & SETUPTO(upto)) != 0;
  49. return false;
  50. }
  51. S32 TSIntegerSet::count(S32 upto) const
  52. {
  53. AssertFatal(upto<=MAX_TS_SET_SIZE,"TSIntegerSet::count: out of range");
  54. S32 count = 0;
  55. U32 dword = bits[0];
  56. for (S32 i = 0; i < upto; i++)
  57. {
  58. // get next word
  59. if (!(i & 0x1F))
  60. dword = bits[i>>5];
  61. // test bits in word
  62. if (dword & (1 << (i & 0x1F)))
  63. count++;
  64. }
  65. return count;
  66. }
  67. void TSIntegerSet::intersect(const TSIntegerSet & otherSet)
  68. {
  69. for (S32 i=0; i<MAX_TS_SET_DWORDS; i++)
  70. bits[i] &= otherSet.bits[i];
  71. }
  72. void TSIntegerSet::overlap(const TSIntegerSet & otherSet)
  73. {
  74. for (S32 i=0; i<MAX_TS_SET_DWORDS; i++)
  75. bits[i] |= otherSet.bits[i];
  76. }
  77. void TSIntegerSet::difference(const TSIntegerSet & otherSet)
  78. {
  79. for (S32 i=0; i<MAX_TS_SET_DWORDS; i++)
  80. bits[i] = (bits[i] | otherSet.bits[i]) & ~(bits[i] & otherSet.bits[i]);
  81. }
  82. void TSIntegerSet::takeAway(const TSIntegerSet & otherSet)
  83. {
  84. for (S32 i=0; i<MAX_TS_SET_DWORDS; i++)
  85. bits[i] &= ~otherSet.bits[i];
  86. }
  87. S32 TSIntegerSet::start() const
  88. {
  89. for (S32 i=0; i<MAX_TS_SET_DWORDS; i++)
  90. {
  91. // search for set bit one dword at a time
  92. U32 dword = bits[i];
  93. if (dword!=0)
  94. {
  95. // got dword, now search one byte at a time
  96. S32 j = 0;
  97. U32 mask = 0xFF;
  98. do
  99. {
  100. if (dword&mask)
  101. {
  102. // got byte, now search one bit at a time
  103. U32 bit = mask & ~(mask<<1); // grabs the smallest bit
  104. do
  105. {
  106. if (dword&bit)
  107. return (i<<5)+j;
  108. j++;
  109. bit <<= 1;
  110. } while (1);
  111. }
  112. mask <<= 8;
  113. j += 8;
  114. } while (1);
  115. }
  116. }
  117. return MAX_TS_SET_SIZE;
  118. }
  119. S32 TSIntegerSet::end() const
  120. {
  121. for (S32 i=MAX_TS_SET_DWORDS-1; i>=0; i--)
  122. {
  123. // search for set bit one dword at a time
  124. U32 dword = bits[i];
  125. if (bits[i])
  126. {
  127. // got dword, now search one byte at a time
  128. S32 j = 31;
  129. U32 mask = 0xFF000000;
  130. do
  131. {
  132. if (dword&mask)
  133. {
  134. // got byte, now one bit at a time
  135. U32 bit = mask & ~(mask>>1); // grabs the highest bit
  136. do
  137. {
  138. if (dword&bit)
  139. return (i<<5)+j+1;
  140. j--;
  141. bit >>= 1;
  142. } while (1);
  143. }
  144. mask >>= 8;
  145. j -= 8;
  146. } while (1);
  147. }
  148. }
  149. return 0;
  150. }
  151. void TSIntegerSet::next(S32 & i) const
  152. {
  153. i++;
  154. U32 idx = i>>5;
  155. U32 bit = 1 << (i&31);
  156. U32 dword = bits[idx] & ~(bit-1);
  157. while (dword==0)
  158. {
  159. i = (i+32) & ~31;
  160. if (i>=MAX_TS_SET_SIZE)
  161. return;
  162. dword=bits[++idx];
  163. bit = 1;
  164. }
  165. dword = bits[idx];
  166. while ( (bit & dword) == 0)
  167. {
  168. bit <<= 1;
  169. i++;
  170. }
  171. }
  172. /* Or would one byte at a time be better...
  173. void TSIntegerSet::next(S32 & i)
  174. {
  175. U32 idx = i>>3;
  176. U8 bit = 1 << (i&7);
  177. U8 byte = ((U8*)bits)[idx] & ~(bit*2-1);
  178. while (byte==0)
  179. {
  180. i = (i+8) & ~7;
  181. if (i>=MAX_TS_SET_SIZE)
  182. return;
  183. byte=((U8*)bits)[++idx];
  184. bit = 1;
  185. }
  186. byte = ((U8*)bits)[idx];
  187. while (bit & byte == 0)
  188. {
  189. bit <<= 1;
  190. i++;
  191. }
  192. }
  193. */
  194. void TSIntegerSet::copy(const TSIntegerSet & otherSet)
  195. {
  196. dMemcpy(bits,otherSet.bits,MAX_TS_SET_DWORDS*4);
  197. }
  198. void TSIntegerSet::insert(S32 index, bool value)
  199. {
  200. AssertFatal(index<MAX_TS_SET_SIZE,"TSIntegerSet::insert: out of range");
  201. // shift bits in words after the insertion point
  202. U32 endWord = (end() >> 5) + 1;
  203. if (endWord >= MAX_TS_SET_DWORDS)
  204. endWord = MAX_TS_SET_DWORDS-1;
  205. for (S32 i = endWord; i > (index >> 5); i--)
  206. {
  207. bits[i] = bits[i] << 1;
  208. if (bits[i-1] & 0x80000000)
  209. bits[i] |= 0x1;
  210. }
  211. // shift to create space in target word
  212. U32 lowMask = (1 << (index & 0x1f)) - 1; // bits below the insert point
  213. U32 highMask = ~(lowMask | (1 << (index & 0x1f))); // bits above the insert point
  214. S32 word = index >> 5;
  215. bits[word] = ((bits[word] << 1) & highMask) | (bits[word] & lowMask);
  216. // insert new value
  217. if (value)
  218. set(index);
  219. }
  220. void TSIntegerSet::erase(S32 index)
  221. {
  222. AssertFatal(index<MAX_TS_SET_SIZE,"TSIntegerSet::erase: out of range");
  223. // shift to erase bit in target word
  224. S32 word = index >> 5;
  225. U32 lowMask = (1 << (index & 0x1f)) - 1; // bits below the erase point
  226. bits[word] = ((bits[word] >> 1) & ~lowMask) | (bits[word] & lowMask);
  227. // shift bits in words after the erase point
  228. U32 endWord = (end() >> 5) + 1;
  229. if (endWord >= MAX_TS_SET_DWORDS)
  230. endWord = MAX_TS_SET_DWORDS-1;
  231. for (S32 i = (index >> 5) + 1; i <= endWord; i++)
  232. {
  233. if (bits[i] & 0x1)
  234. bits[i-1] |= 0x80000000;
  235. bits[i] = bits[i] >> 1;
  236. }
  237. }
  238. TSIntegerSet::TSIntegerSet()
  239. {
  240. clearAll();
  241. }
  242. TSIntegerSet::TSIntegerSet(const TSIntegerSet & otherSet)
  243. {
  244. copy(otherSet);
  245. }
  246. void TSIntegerSet::read(Stream * s)
  247. {
  248. clearAll();
  249. S32 numInts;
  250. s->read(&numInts); // don't care about this
  251. S32 sz;
  252. s->read(&sz);
  253. AssertFatal(sz<=MAX_TS_SET_DWORDS,"TSIntegerSet:: set too large...increase max set size and re-compile");
  254. for (S32 i=0; i<sz; i++) // now mirrors the write code...
  255. s->read(&(bits[i]));
  256. }
  257. void TSIntegerSet::write(Stream * s) const
  258. {
  259. s->write((S32)0); // don't do this anymore, keep in to avoid versioning
  260. S32 i,sz=0;
  261. for (i=0; i<MAX_TS_SET_DWORDS; i++)
  262. if (bits[i]!=0)
  263. sz=i+1;
  264. s->write(sz);
  265. for (i=0; i<sz; i++)
  266. s->write(bits[i]);
  267. }