1
0

TerrainTaintsArray.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Runtime.CompilerServices;
  28. using System.Threading;
  29. namespace OpenSim.Framework
  30. {
  31. public class TerrainTaintsArray
  32. {
  33. public const int VectorNumberBits = 32;
  34. public const int VectorNumberBitsLog2 = 5;
  35. public const int FALSEWORD = 0;
  36. public const int TRUEWORD = ~FALSEWORD;
  37. private int[] m_data;
  38. private readonly int m_nbits;
  39. private volatile int m_ntainted;
  40. private object m_mainlock = new object();
  41. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  42. public TerrainTaintsArray(int lenght) : this(lenght, false) { }
  43. public TerrainTaintsArray(int lenght, bool preset)
  44. {
  45. m_nbits = lenght;
  46. int nInts = calclen(m_nbits);
  47. m_data = new int[nInts];
  48. if (preset)
  49. {
  50. for (int i = 0; i < m_data.Length; i++)
  51. m_data[i] = TRUEWORD;
  52. m_ntainted = m_nbits;
  53. }
  54. else
  55. m_ntainted = 0;
  56. }
  57. public int Length
  58. {
  59. get
  60. {
  61. return m_nbits;
  62. }
  63. }
  64. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  65. public int TaitedCount()
  66. {
  67. return m_ntainted;
  68. }
  69. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  70. public bool IsTaited()
  71. {
  72. return m_ntainted > 0;
  73. }
  74. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  75. public bool Get(int bitindex)
  76. {
  77. int indexh = bitindex >> VectorNumberBitsLog2;
  78. int mask = 1 << (bitindex & (VectorNumberBits - 1));
  79. return (m_data[indexh] & mask) != 0;
  80. }
  81. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  82. public bool Get(int bitindex, bool clear)
  83. {
  84. int indexh = bitindex >> VectorNumberBitsLog2;
  85. int mask = 1 << (bitindex & (VectorNumberBits - 1));
  86. lock (m_mainlock)
  87. {
  88. if ((m_data[indexh] & mask) != 0)
  89. {
  90. if (clear)
  91. {
  92. m_data[indexh] ^= mask;
  93. --m_ntainted;
  94. }
  95. return true;
  96. }
  97. }
  98. return false;
  99. }
  100. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public unsafe bool GetAndClear(int bitindex)
  102. {
  103. int indexh = bitindex >> VectorNumberBitsLog2;
  104. int mask = 1 << (bitindex & (VectorNumberBits - 1));
  105. lock (m_mainlock)
  106. {
  107. if ((m_data[indexh] & mask) != 0)
  108. {
  109. m_data[indexh] ^= mask;
  110. --m_ntainted;
  111. return true;
  112. }
  113. }
  114. return false;
  115. }
  116. public void Set(int bitindex, bool val)
  117. {
  118. int indexh = bitindex >> VectorNumberBitsLog2;
  119. int mask = 1 << (bitindex & (VectorNumberBits - 1));
  120. lock (m_mainlock)
  121. {
  122. if (val)
  123. {
  124. if ((m_data[indexh] & mask) == 0)
  125. {
  126. m_data[indexh] |= mask;
  127. ++m_ntainted;
  128. }
  129. }
  130. else
  131. {
  132. if ((m_data[indexh] & mask) != 0)
  133. {
  134. m_data[indexh] ^= mask;
  135. --m_ntainted;
  136. }
  137. }
  138. }
  139. }
  140. public bool this[int bitindex]
  141. {
  142. get
  143. {
  144. return Get(bitindex);
  145. }
  146. set
  147. {
  148. int indexh = bitindex >> VectorNumberBitsLog2;
  149. int mask = 1 << (bitindex & (VectorNumberBits - 1));
  150. lock (m_mainlock)
  151. {
  152. if (value)
  153. {
  154. if ((m_data[indexh] & mask) == 0)
  155. {
  156. m_data[indexh] |= mask;
  157. ++m_ntainted;
  158. }
  159. }
  160. else
  161. {
  162. if ((m_data[indexh] & mask) != 0)
  163. {
  164. m_data[indexh] ^= mask;
  165. --m_ntainted;
  166. }
  167. }
  168. }
  169. }
  170. }
  171. public void SetAll(bool val)
  172. {
  173. lock (m_mainlock)
  174. {
  175. if (val)
  176. {
  177. for (int i = 0; i < m_data.Length; ++i)
  178. m_data[i] = TRUEWORD;
  179. m_ntainted = m_nbits;
  180. }
  181. else
  182. {
  183. for (int i = 0; i < m_data.Length; ++i)
  184. m_data[i] = 0;
  185. m_ntainted = 0;
  186. }
  187. }
  188. }
  189. public bool IsVectorOfFalse(int vectorIndex)
  190. {
  191. return m_data[vectorIndex] == 0;
  192. }
  193. public bool IsVectorOfBitFalse(int bitindex)
  194. {
  195. return m_data[(bitindex >> VectorNumberBitsLog2)] == 0;
  196. }
  197. public bool IsVectorTrue(int vectorIndex)
  198. {
  199. return m_data[vectorIndex] == unchecked(((int)0xffffffff));
  200. }
  201. public bool IsVectorOfBitTrue(int bitindex)
  202. {
  203. return m_data[(bitindex >> VectorNumberBitsLog2)] == unchecked(((int)0xffffffff));
  204. }
  205. public void Or(TerrainTaintsArray other)
  206. {
  207. if (m_nbits != other.m_nbits)
  208. return;
  209. lock (m_mainlock)
  210. {
  211. lock (other.m_mainlock)
  212. {
  213. for (int i = 0; i < m_data.Length; ++i)
  214. {
  215. int tr = other.m_data[i];
  216. if (tr == 0)
  217. continue;
  218. int tt = m_data[i] | tr;
  219. tr ^= tt;
  220. if (tr != 0)
  221. {
  222. m_data[i] = tt;
  223. for (int j = 1; j != 0; j <<= 1)
  224. {
  225. if ((tr & j) != 0)
  226. ++m_ntainted;
  227. }
  228. }
  229. }
  230. }
  231. }
  232. }
  233. public int GetNextTrue(int startBitIndex)
  234. {
  235. if (startBitIndex < 0 || startBitIndex >= m_nbits)
  236. return -1;
  237. int indexh = startBitIndex >> VectorNumberBitsLog2;
  238. int j = startBitIndex & (VectorNumberBits - 1);
  239. int cur = m_data[indexh];
  240. if (cur != 0)
  241. {
  242. for (; j < VectorNumberBits; ++j)
  243. {
  244. if ((cur & (1 << j)) != 0)
  245. return (indexh << VectorNumberBitsLog2) | j;
  246. }
  247. }
  248. while (++indexh < m_data.Length)
  249. {
  250. cur = m_data[indexh];
  251. if (cur != 0)
  252. {
  253. for (j = 0; j < VectorNumberBits; ++j)
  254. {
  255. if ((cur & (1 << j)) != 0)
  256. return (indexh << VectorNumberBitsLog2) | j;
  257. }
  258. }
  259. }
  260. return -1;
  261. }
  262. public int GetAndClearNextTrue(int startBitIndex)
  263. {
  264. if (m_ntainted <= 0 || startBitIndex < 0 || startBitIndex >= m_nbits)
  265. return -1;
  266. int indexh = startBitIndex >> VectorNumberBitsLog2;
  267. int j = startBitIndex & (VectorNumberBits - 1);
  268. lock (m_mainlock)
  269. {
  270. int cur = m_data[indexh];
  271. if (cur != 0)
  272. {
  273. for (; j < VectorNumberBits; ++j)
  274. {
  275. int mask = (1 << j);
  276. if ((cur & mask) != 0)
  277. {
  278. m_data[indexh] ^= mask;
  279. --m_ntainted;
  280. return (indexh << VectorNumberBitsLog2) | j;
  281. }
  282. }
  283. }
  284. while (++indexh < m_data.Length)
  285. {
  286. cur = m_data[indexh];
  287. if (cur != 0)
  288. {
  289. for (j = 0; j < VectorNumberBits; ++j)
  290. {
  291. int mask = (1 << j);
  292. if ((cur & mask) != 0)
  293. {
  294. m_data[indexh] ^= mask;
  295. --m_ntainted;
  296. return (indexh << VectorNumberBitsLog2) | j;
  297. }
  298. }
  299. }
  300. }
  301. }
  302. return -1;
  303. }
  304. private int calclen(int bitsLen)
  305. {
  306. return bitsLen > 0 ? ((bitsLen - 1) >> VectorNumberBitsLog2) + 1 : 0;
  307. }
  308. }
  309. }