antlr3bitset.inl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. namespace antlr3 {
  2. template <class ImplTraits>
  3. ANTLR_INLINE BitsetList<ImplTraits>::BitsetList()
  4. {
  5. m_bits = NULL;
  6. m_length = 0;
  7. }
  8. template <class ImplTraits>
  9. ANTLR_INLINE BitsetList<ImplTraits>::BitsetList( ANTLR_BITWORD* bits, ANTLR_UINT32 length )
  10. {
  11. m_bits = bits;
  12. m_length = length;
  13. }
  14. template <class ImplTraits>
  15. ANTLR_INLINE ANTLR_BITWORD* BitsetList<ImplTraits>::get_bits() const
  16. {
  17. return m_bits;
  18. }
  19. template <class ImplTraits>
  20. ANTLR_INLINE ANTLR_UINT32 BitsetList<ImplTraits>::get_length() const
  21. {
  22. return m_length;
  23. }
  24. template <class ImplTraits>
  25. ANTLR_INLINE void BitsetList<ImplTraits>::set_bits( ANTLR_BITWORD* bits )
  26. {
  27. m_bits = bits;
  28. }
  29. template <class ImplTraits>
  30. ANTLR_INLINE void BitsetList<ImplTraits>::set_length( ANTLR_UINT32 length )
  31. {
  32. m_length = length;
  33. }
  34. template <class ImplTraits>
  35. typename BitsetList<ImplTraits>::BitsetType* BitsetList<ImplTraits>::bitsetLoad()
  36. {
  37. // Allocate memory for the bitset structure itself
  38. // the input parameter is the bit number (0 based)
  39. // to include in the bitset, so we need at at least
  40. // bit + 1 bits. If any arguments indicate a
  41. // a bit higher than the default number of bits (0 means default size)
  42. // then Add() will take care
  43. // of it.
  44. //
  45. BitsetType* bitset = new BitsetType();
  46. if (this != NULL)
  47. {
  48. // Now we can add the element bits into the set
  49. //
  50. ANTLR_UINT32 count=0;
  51. while (count < m_length)
  52. {
  53. if( bitset->get_blist().get_length() <= count)
  54. bitset->grow(count+1);
  55. typename ImplTraits::BitsetListType& blist = bitset->get_blist();
  56. blist.m_bits[count] = *(m_bits+count);
  57. count++;
  58. }
  59. }
  60. // return the new bitset
  61. //
  62. return bitset;
  63. }
  64. template <class ImplTraits>
  65. typename BitsetList<ImplTraits>::BitsetType* BitsetList<ImplTraits>::bitsetCopy()
  66. {
  67. BitsetType* bitset;
  68. ANTLR_UINT32 numElements = m_length;
  69. // Avoid memory thrashing at the expense of a few more bytes
  70. //
  71. if (numElements < 8)
  72. numElements = 8;
  73. // Allocate memory for the bitset structure itself
  74. //
  75. bitset = new Bitset<ImplTraits>(numElements);
  76. memcpy(bitset->get_blist().get_bits(), m_bits, numElements * sizeof(ANTLR_BITWORD));
  77. // All seems good
  78. //
  79. return bitset;
  80. }
  81. template <class ImplTraits>
  82. Bitset<ImplTraits>::Bitset( ANTLR_UINT32 numBits )
  83. {
  84. // Avoid memory thrashing at the up front expense of a few bytes
  85. if (numBits < (8 * ANTLR_BITSET_BITS))
  86. numBits = 8 * ANTLR_BITSET_BITS;
  87. // No we need to allocate the memory for the number of bits asked for
  88. // in multiples of ANTLR3_UINT64.
  89. //
  90. ANTLR_UINT32 numelements = ((numBits -1) >> ANTLR_BITSET_LOG_BITS) + 1;
  91. m_blist.set_bits( (ANTLR_BITWORD*) AllocPolicyType::alloc0(numelements * sizeof(ANTLR_BITWORD)));
  92. m_blist.set_length( numelements );
  93. }
  94. template <class ImplTraits>
  95. Bitset<ImplTraits>::Bitset( const Bitset& bitset )
  96. :m_blist(bitset.m_blist)
  97. {
  98. }
  99. template <class ImplTraits>
  100. ANTLR_INLINE Bitset<ImplTraits>* Bitset<ImplTraits>::clone() const
  101. {
  102. Bitset* bitset;
  103. // Allocate memory for the bitset structure itself
  104. //
  105. bitset = new Bitset( ANTLR_BITSET_BITS * m_blist.get_length() );
  106. // Install the actual bits in the source set
  107. //
  108. memcpy(bitset->m_blist.get_bits(), m_blist.get_bits(),
  109. m_blist.get_length() * sizeof(ANTLR_BITWORD) );
  110. // All seems good
  111. //
  112. return bitset;
  113. }
  114. template <class ImplTraits>
  115. Bitset<ImplTraits>* Bitset<ImplTraits>::bor(Bitset* bitset2)
  116. {
  117. Bitset* bitset;
  118. if (this == NULL)
  119. return bitset2->clone();
  120. if (bitset2 == NULL)
  121. return this->clone();
  122. // Allocate memory for the newly ordered bitset structure itself.
  123. //
  124. bitset = this->clone();
  125. bitset->bitsetORInPlace(bitset2);
  126. return bitset;
  127. }
  128. template <class ImplTraits>
  129. void Bitset<ImplTraits>::borInPlace(Bitset* bitset2)
  130. {
  131. ANTLR_UINT32 minimum;
  132. if (bitset2 == NULL)
  133. return;
  134. // First make sure that the target bitset is big enough
  135. // for the new bits to be ored in.
  136. //
  137. if ( m_blist.get_length() < bitset2->m_blist.get_length() )
  138. this->growToInclude( bitset2->m_blist.get_length() * sizeof(ANTLR_BITWORD) );
  139. // Or the miniimum number of bits after any resizing went on
  140. //
  141. if ( m_blist.get_length() < bitset2->m_blist.get_length() )
  142. minimum = m_blist.get_length();
  143. else
  144. minimum = bitset2->m_blist.get_length();
  145. ANTLR_BITWORD* bits1 = m_blist.get_bits();
  146. ANTLR_BITWORD* bits2 = bitset2->m_blist.get_bits();
  147. for (ANTLR_UINT32 i = minimum; i > 0; i--)
  148. bits1[i-1] |= bits2[i-1];
  149. }
  150. template <class ImplTraits>
  151. ANTLR_UINT32 Bitset<ImplTraits>::size() const
  152. {
  153. ANTLR_UINT32 degree;
  154. ANTLR_INT32 i;
  155. ANTLR_INT8 bit;
  156. // TODO: Come back to this, it may be faster to & with 0x01
  157. // then shift right a copy of the 4 bits, than shift left a constant of 1.
  158. // But then again, the optimizer might just work this out
  159. // anyway.
  160. //
  161. degree = 0;
  162. ANTLR_BITWORD* bits = m_blist.get_bits();
  163. for (i = m_blist.get_length() - 1; i>= 0; i--)
  164. {
  165. if (bits[i] != 0)
  166. {
  167. for(bit = ANTLR_BITSET_BITS - 1; bit >= 0; bit--)
  168. {
  169. if((bits[i] & (((ANTLR_BITWORD)1) << bit)) != 0)
  170. {
  171. degree++;
  172. }
  173. }
  174. }
  175. }
  176. return degree;
  177. }
  178. template <class ImplTraits>
  179. ANTLR_INLINE void Bitset<ImplTraits>::add(ANTLR_INT32 bit)
  180. {
  181. ANTLR_UINT32 word = Bitset::WordNumber(bit);
  182. if (word >= m_blist.get_length() )
  183. this->growToInclude(bit);
  184. ANTLR_BITWORD* bits = m_blist.get_bits();
  185. bits[word] |= Bitset::BitMask(bit);
  186. }
  187. template <class ImplTraits>
  188. void Bitset<ImplTraits>::grow(ANTLR_INT32 newSize)
  189. {
  190. ANTLR_BITWORD* newBits;
  191. // Space for newly sized bitset - TODO: come back to this and use realloc?, it may
  192. // be more efficient...
  193. //
  194. newBits = (ANTLR_BITWORD*) AllocPolicyType::alloc0(newSize * sizeof(ANTLR_BITWORD) );
  195. if ( m_blist.get_bits() != NULL)
  196. {
  197. // Copy existing bits
  198. //
  199. memcpy( newBits, m_blist.get_bits(), m_blist.get_length() * sizeof(ANTLR_BITWORD) );
  200. // Out with the old bits... de de de derrr
  201. //
  202. AllocPolicyType::free( m_blist.get_bits() );
  203. }
  204. // In with the new bits... keerrrang.
  205. //
  206. m_blist.set_bits(newBits);
  207. m_blist.set_length(newSize);
  208. }
  209. template <class ImplTraits>
  210. bool Bitset<ImplTraits>::equals(Bitset* bitset2) const
  211. {
  212. ANTLR_UINT32 minimum;
  213. ANTLR_UINT32 i;
  214. if (this == NULL || bitset2 == NULL)
  215. return false;
  216. // Work out the minimum comparison set
  217. //
  218. if ( m_blist.get_length() < bitset2->m_blist.get_length() )
  219. minimum = m_blist.get_length();
  220. else
  221. minimum = bitset2->m_blist.get_length();
  222. // Make sure explict in common bits are equal
  223. //
  224. for (i = minimum - 1; i < minimum ; i--)
  225. {
  226. ANTLR_BITWORD* bits1 = m_blist.get_bits();
  227. ANTLR_BITWORD* bits2 = bitset2->m_blist.get_bits();
  228. if ( bits1[i] != bits2[i])
  229. return false;
  230. }
  231. // Now make sure the bits of the larger set are all turned
  232. // off.
  233. //
  234. if ( m_blist.get_length() > minimum)
  235. {
  236. for (i = minimum ; i < m_blist.get_length(); i++)
  237. {
  238. ANTLR_BITWORD* bits = m_blist.get_bits();
  239. if(bits[i] != 0)
  240. return false;
  241. }
  242. }
  243. else if (bitset2->m_blist.get_length() > minimum)
  244. {
  245. ANTLR_BITWORD* bits = m_blist.get_bits();
  246. for (i = minimum; i < bitset2->m_blist.get_length(); i++)
  247. {
  248. if ( bits[i] != 0 )
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. template <class ImplTraits>
  255. bool Bitset<ImplTraits>::isMember(ANTLR_UINT32 bit) const
  256. {
  257. ANTLR_UINT32 wordNo = Bitset::WordNumber(bit);
  258. if (wordNo >= m_blist.get_length())
  259. return false;
  260. ANTLR_BITWORD* bits = m_blist.get_bits();
  261. if ( (bits[wordNo] & Bitset::BitMask(bit)) == 0)
  262. return false;
  263. else
  264. return true;
  265. }
  266. template <class ImplTraits>
  267. ANTLR_INLINE ANTLR_UINT32 Bitset<ImplTraits>::numBits() const
  268. {
  269. return m_blist.get_length() << ANTLR_BITSET_LOG_BITS;
  270. }
  271. template <class ImplTraits>
  272. ANTLR_INLINE typename ImplTraits::BitsetListType& Bitset<ImplTraits>::get_blist()
  273. {
  274. return m_blist;
  275. }
  276. template <class ImplTraits>
  277. ANTLR_INLINE void Bitset<ImplTraits>::remove(ANTLR_UINT32 bit)
  278. {
  279. ANTLR_UINT32 wordNo = Bitset::WordNumber(bit);
  280. if (wordNo < m_blist.get_length())
  281. {
  282. ANTLR_BITWORD* bits = m_blist.get_bits();
  283. bits[wordNo] &= ~(Bitset::BitMask(bit));
  284. }
  285. }
  286. template <class ImplTraits>
  287. ANTLR_INLINE bool Bitset<ImplTraits>::isNilNode() const
  288. {
  289. ANTLR_UINT32 i;
  290. ANTLR_BITWORD* bits = m_blist.get_bits();
  291. for (i = m_blist.get_length() -1 ; i < m_blist.get_length(); i--)
  292. {
  293. if(bits[i] != 0)
  294. return false;
  295. }
  296. return true;
  297. }
  298. template <class ImplTraits>
  299. ANTLR_INT32* Bitset<ImplTraits>::toIntList() const
  300. {
  301. ANTLR_UINT32 numInts; // How many integers we will need
  302. ANTLR_UINT32 numBits; // How many bits are in the set
  303. ANTLR_UINT32 i;
  304. ANTLR_UINT32 index;
  305. ANTLR_INT32* intList;
  306. numInts = this->size() + 1;
  307. numBits = this->numBits();
  308. intList = (ANTLR_INT32*) AllocPolicyType::alloc(numInts * sizeof(ANTLR_INT32));
  309. intList[0] = numInts;
  310. // Enumerate the bits that are turned on
  311. //
  312. for (i = 0, index = 1; i<numBits; i++)
  313. {
  314. if (this->isMember(i) == true)
  315. intList[index++] = i;
  316. }
  317. // Result set
  318. //
  319. return intList;
  320. }
  321. template <class ImplTraits>
  322. ANTLR_INLINE Bitset<ImplTraits>::~Bitset()
  323. {
  324. if (m_blist.get_bits() != NULL)
  325. AllocPolicyType::free(m_blist.get_bits());
  326. return;
  327. }
  328. template <class ImplTraits>
  329. void Bitset<ImplTraits>::growToInclude(ANTLR_INT32 bit)
  330. {
  331. ANTLR_UINT32 bl;
  332. ANTLR_UINT32 nw;
  333. bl = (m_blist.get_length() << 1);
  334. nw = Bitset::NumWordsToHold(bit);
  335. if (bl > nw)
  336. this->grow(bl);
  337. else
  338. this->grow(nw);
  339. }
  340. template <class ImplTraits>
  341. ANTLR_INLINE ANTLR_UINT64 Bitset<ImplTraits>::BitMask(ANTLR_UINT32 bitNumber)
  342. {
  343. return ((ANTLR_UINT64)1) << (bitNumber & (ANTLR_BITSET_MOD_MASK));
  344. }
  345. template <class ImplTraits>
  346. ANTLR_INLINE ANTLR_UINT32 Bitset<ImplTraits>::NumWordsToHold(ANTLR_UINT32 bit)
  347. {
  348. return (bit >> ANTLR_BITSET_LOG_BITS) + 1;
  349. }
  350. template <class ImplTraits>
  351. ANTLR_INLINE ANTLR_UINT32 Bitset<ImplTraits>::WordNumber(ANTLR_UINT32 bit)
  352. {
  353. return bit >> ANTLR_BITSET_LOG_BITS;
  354. }
  355. template <class ImplTraits>
  356. void Bitset<ImplTraits>::bitsetORInPlace(Bitset* bitset2)
  357. {
  358. ANTLR_UINT32 minimum;
  359. ANTLR_UINT32 i;
  360. if (bitset2 == NULL)
  361. return;
  362. // First make sure that the target bitset is big enough
  363. // for the new bits to be ored in.
  364. //
  365. if ( m_blist.get_length() < bitset2->m_blist.get_length() )
  366. this->growToInclude( bitset2->m_blist.get_length() * sizeof(ANTLR_BITWORD) );
  367. // Or the miniimum number of bits after any resizing went on
  368. //
  369. if ( m_blist.get_length() < bitset2->m_blist.get_length() )
  370. minimum = m_blist.get_length();
  371. else
  372. minimum = bitset2->m_blist.get_length();
  373. ANTLR_BITWORD* bits1 = m_blist.get_bits();
  374. ANTLR_BITWORD* bits2 = bitset2->m_blist.get_bits();
  375. for (i = minimum; i > 0; i--)
  376. bits1[i-1] |= bits2[i-1];
  377. }
  378. template <class ImplTraits>
  379. Bitset<ImplTraits>* Bitset<ImplTraits>::BitsetOf(ANTLR_INT32 bit)
  380. {
  381. // Allocate memory for the bitset structure itself
  382. // the input parameter is the bit number (0 based)
  383. // to include in the bitset, so we need at at least
  384. // bit + 1 bits. If any arguments indicate a
  385. // a bit higher than the default number of bits (0 menas default size)
  386. // then Add() will take care
  387. // of it.
  388. //
  389. Bitset<ImplTraits>* bitset = new Bitset<ImplTraits>(0);
  390. bitset->add(bit);
  391. return bitset;
  392. }
  393. template <class ImplTraits>
  394. Bitset<ImplTraits>* Bitset<ImplTraits>::BitsetOf(ANTLR_INT32 bit1, ANTLR_INT32 bit2)
  395. {
  396. Bitset<ImplTraits>* bitset = Bitset<ImplTraits>::BitsetOf(bit1);
  397. bitset->add(bit2);
  398. return bitset;
  399. }
  400. //static
  401. template <class ImplTraits>
  402. Bitset<ImplTraits>* Bitset<ImplTraits>::BitsetFromList(const IntListType& list)
  403. {
  404. // We have no idea what exactly is in the list
  405. // so create a default bitset and then just add stuff
  406. // as we enumerate.
  407. //
  408. Bitset<ImplTraits>* bitset = new Bitset<ImplTraits>(0);
  409. for( int i = 0; i < list.size(); ++i )
  410. bitset->add( list[i] );
  411. return bitset;
  412. }
  413. }