antlr3collections.inl 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  1. namespace antlr3 {
  2. template< class ImplTraits, class DataType >
  3. ANTLR_INLINE TrieEntry<ImplTraits, DataType>::TrieEntry(const DataType& data, TrieEntry* next)
  4. :m_data(data)
  5. {
  6. m_next = next;
  7. }
  8. template< class ImplTraits, class DataType >
  9. ANTLR_INLINE DataType& TrieEntry<ImplTraits, DataType>::get_data()
  10. {
  11. return m_data;
  12. }
  13. template< class ImplTraits, class DataType >
  14. ANTLR_INLINE const DataType& TrieEntry<ImplTraits, DataType>::get_data() const
  15. {
  16. return m_data;
  17. }
  18. template< class ImplTraits, class DataType >
  19. ANTLR_INLINE TrieEntry<ImplTraits, DataType>* TrieEntry<ImplTraits, DataType>::get_next() const
  20. {
  21. return m_next;
  22. }
  23. template< class ImplTraits, class DataType >
  24. ANTLR_INLINE void TrieEntry<ImplTraits, DataType>::set_next( TrieEntry* next )
  25. {
  26. m_next = next;
  27. }
  28. template< class ImplTraits, class DataType >
  29. ANTLR_INLINE ANTLR_UINT32 IntTrieNode<ImplTraits, DataType>::get_bitNum() const
  30. {
  31. return m_bitNum;
  32. }
  33. template< class ImplTraits, class DataType >
  34. ANTLR_INLINE ANTLR_INTKEY IntTrieNode<ImplTraits, DataType>::get_key() const
  35. {
  36. return m_key;
  37. }
  38. template< class ImplTraits, class DataType >
  39. ANTLR_INLINE typename IntTrieNode<ImplTraits, DataType>::BucketsType* IntTrieNode<ImplTraits, DataType>::get_buckets() const
  40. {
  41. return m_buckets;
  42. }
  43. template< class ImplTraits, class DataType >
  44. ANTLR_INLINE IntTrieNode<ImplTraits, DataType>* IntTrieNode<ImplTraits, DataType>::get_leftN() const
  45. {
  46. return m_leftN;
  47. }
  48. template< class ImplTraits, class DataType >
  49. ANTLR_INLINE IntTrieNode<ImplTraits, DataType>* IntTrieNode<ImplTraits, DataType>::get_rightN() const
  50. {
  51. return m_rightN;
  52. }
  53. template< class ImplTraits, class DataType >
  54. ANTLR_INLINE void IntTrieNode<ImplTraits, DataType>::set_bitNum( ANTLR_UINT32 bitNum )
  55. {
  56. m_bitNum = bitNum;
  57. }
  58. template< class ImplTraits, class DataType >
  59. ANTLR_INLINE void IntTrieNode<ImplTraits, DataType>::set_key( ANTLR_INTKEY key )
  60. {
  61. m_key = key;
  62. }
  63. template< class ImplTraits, class DataType >
  64. ANTLR_INLINE void IntTrieNode<ImplTraits, DataType>::set_buckets( BucketsType* buckets )
  65. {
  66. m_buckets = buckets;
  67. }
  68. template< class ImplTraits, class DataType >
  69. ANTLR_INLINE void IntTrieNode<ImplTraits, DataType>::set_leftN( IntTrieNode* leftN )
  70. {
  71. m_leftN = leftN;
  72. }
  73. template< class ImplTraits, class DataType >
  74. ANTLR_INLINE void IntTrieNode<ImplTraits, DataType>::set_rightN( IntTrieNode* rightN )
  75. {
  76. m_rightN = rightN;
  77. }
  78. ANTLR_INLINE const ANTLR_UINT8* IntTrieBase::get_bitIndex()
  79. {
  80. static ANTLR_UINT8 bitIndex[256] =
  81. {
  82. 0, // 0 - Just for padding
  83. 0, // 1
  84. 1, 1, // 2..3
  85. 2, 2, 2, 2, // 4..7
  86. 3, 3, 3, 3, 3, 3, 3, 3, // 8+
  87. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, // 16+
  88. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, // 32+
  89. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  90. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, // 64+
  91. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  92. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  93. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  94. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 128+
  95. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  96. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  97. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  98. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  99. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  100. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  101. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7
  102. };
  103. return bitIndex;
  104. }
  105. ANTLR_INLINE const ANTLR_UINT64* IntTrieBase::get_bitMask()
  106. {
  107. static ANTLR_UINT64 bitMask[64] =
  108. {
  109. 0x0000000000000001ULL, 0x0000000000000002ULL, 0x0000000000000004ULL, 0x0000000000000008ULL,
  110. 0x0000000000000010ULL, 0x0000000000000020ULL, 0x0000000000000040ULL, 0x0000000000000080ULL,
  111. 0x0000000000000100ULL, 0x0000000000000200ULL, 0x0000000000000400ULL, 0x0000000000000800ULL,
  112. 0x0000000000001000ULL, 0x0000000000002000ULL, 0x0000000000004000ULL, 0x0000000000008000ULL,
  113. 0x0000000000010000ULL, 0x0000000000020000ULL, 0x0000000000040000ULL, 0x0000000000080000ULL,
  114. 0x0000000000100000ULL, 0x0000000000200000ULL, 0x0000000000400000ULL, 0x0000000000800000ULL,
  115. 0x0000000001000000ULL, 0x0000000002000000ULL, 0x0000000004000000ULL, 0x0000000008000000ULL,
  116. 0x0000000010000000ULL, 0x0000000020000000ULL, 0x0000000040000000ULL, 0x0000000080000000ULL,
  117. 0x0000000100000000ULL, 0x0000000200000000ULL, 0x0000000400000000ULL, 0x0000000800000000ULL,
  118. 0x0000001000000000ULL, 0x0000002000000000ULL, 0x0000004000000000ULL, 0x0000008000000000ULL,
  119. 0x0000010000000000ULL, 0x0000020000000000ULL, 0x0000040000000000ULL, 0x0000080000000000ULL,
  120. 0x0000100000000000ULL, 0x0000200000000000ULL, 0x0000400000000000ULL, 0x0000800000000000ULL,
  121. 0x0001000000000000ULL, 0x0002000000000000ULL, 0x0004000000000000ULL, 0x0008000000000000ULL,
  122. 0x0010000000000000ULL, 0x0020000000000000ULL, 0x0040000000000000ULL, 0x0080000000000000ULL,
  123. 0x0100000000000000ULL, 0x0200000000000000ULL, 0x0400000000000000ULL, 0x0800000000000000ULL,
  124. 0x1000000000000000ULL, 0x2000000000000000ULL, 0x4000000000000000ULL, 0x8000000000000000ULL
  125. };
  126. return bitMask;
  127. }
  128. template< class ImplTraits, class DataType >
  129. IntTrie<ImplTraits, DataType>::IntTrie( ANTLR_UINT32 depth )
  130. {
  131. /* Now we need to allocate the root node. This makes it easier
  132. * to use the tree as we don't have to do anything special
  133. * for the root node.
  134. */
  135. m_root = new IntTrieNodeType;
  136. /* Now we seed the root node with the index being the
  137. * highest left most bit we want to test, which limits the
  138. * keys in the trie. This is the trie 'depth'. The limit for
  139. * this implementation is 63 (bits 0..63).
  140. */
  141. m_root->set_bitNum( depth );
  142. /* And as we have nothing in here yet, we set both child pointers
  143. * of the root node to point back to itself.
  144. */
  145. m_root->set_leftN( m_root );
  146. m_root->set_rightN( m_root );
  147. m_count = 0;
  148. /* Finally, note that the key for this root node is 0 because
  149. * we use calloc() to initialise it.
  150. */
  151. m_allowDups = false;
  152. m_current = NULL;
  153. }
  154. template< class ImplTraits, class DataType >
  155. IntTrie<ImplTraits, DataType>::~IntTrie()
  156. {
  157. /* Descend from the root and free all the nodes
  158. */
  159. delete m_root;
  160. /* the nodes are all gone now, so we need only free the memory
  161. * for the structure itself
  162. */
  163. }
  164. template< class ImplTraits, class DataType >
  165. typename IntTrie<ImplTraits, DataType>::TrieEntryType* IntTrie<ImplTraits, DataType>::get( ANTLR_INTKEY key)
  166. {
  167. IntTrieNodeType* thisNode;
  168. IntTrieNodeType* nextNode;
  169. if (m_count == 0)
  170. return NULL; /* Nothing in this trie yet */
  171. /* Starting at the root node in the trie, compare the bit index
  172. * of the current node with its next child node (starts left from root).
  173. * When the bit index of the child node is greater than the bit index of the current node
  174. * then by definition (as the bit index decreases as we descent the trie)
  175. * we have reached a 'backward' pointer. A backward pointer means we
  176. * have reached the only node that can be reached by the bits given us so far
  177. * and it must either be the key we are looking for, or if not then it
  178. * means the entry was not in the trie, and we return NULL. A backward pointer
  179. * points back in to the tree structure rather than down (deeper) within the
  180. * tree branches.
  181. */
  182. thisNode = m_root; /* Start at the root node */
  183. nextNode = thisNode->get_leftN(); /* Examine the left node from the root */
  184. /* While we are descending the tree nodes...
  185. */
  186. const ANTLR_UINT64* bitMask = this->get_bitMask();
  187. while( thisNode->get_bitNum() > nextNode->get_bitNum() )
  188. {
  189. /* Next node now becomes the new 'current' node
  190. */
  191. thisNode = nextNode;
  192. /* We now test the bit indicated by the bitmap in the next node
  193. * in the key we are searching for. The new next node is the
  194. * right node if that bit is set and the left node it is not.
  195. */
  196. if (key & bitMask[nextNode->get_bitNum()])
  197. {
  198. nextNode = nextNode->get_rightN(); /* 1 is right */
  199. }
  200. else
  201. {
  202. nextNode = nextNode->get_leftN(); /* 0 is left */
  203. }
  204. }
  205. /* Here we have reached a node where the bitMap index is lower than
  206. * its parent. This means it is pointing backward in the tree and
  207. * must therefore be a terminal node, being the only point than can
  208. * be reached with the bits seen so far. It is either the actual key
  209. * we wanted, or if that key is not in the trie it is another key
  210. * that is currently the only one that can be reached by those bits.
  211. * That situation would obviously change if the key was to be added
  212. * to the trie.
  213. *
  214. * Hence it only remains to test whether this is actually the key or not.
  215. */
  216. if (nextNode->get_key() == key)
  217. {
  218. /* This was the key, so return the entry pointer
  219. */
  220. return nextNode->get_buckets();
  221. }
  222. else
  223. {
  224. return NULL; /* That key is not in the trie (note that we set the pointer to -1 if no payload) */
  225. }
  226. }
  227. template< class ImplTraits, class DataType >
  228. bool IntTrie<ImplTraits, DataType>::del( ANTLR_INTKEY /*key*/)
  229. {
  230. IntTrieNodeType* p;
  231. p = m_root;
  232. return false;
  233. }
  234. template< class ImplTraits, class DataType >
  235. bool IntTrie<ImplTraits, DataType>::add( ANTLR_INTKEY key, const DataType& data )
  236. {
  237. IntTrieNodeType* thisNode;
  238. IntTrieNodeType* nextNode;
  239. IntTrieNodeType* entNode;
  240. ANTLR_UINT32 depth;
  241. TrieEntryType* newEnt;
  242. TrieEntryType* nextEnt;
  243. ANTLR_INTKEY xorKey;
  244. /* Cache the bit depth of this trie, which is always the highest index,
  245. * which is in the root node
  246. */
  247. depth = m_root->get_bitNum();
  248. thisNode = m_root; /* Start with the root node */
  249. nextNode = m_root->get_leftN(); /* And assume we start to the left */
  250. /* Now find the only node that can be currently reached by the bits in the
  251. * key we are being asked to insert.
  252. */
  253. const ANTLR_UINT64* bitMask = this->get_bitMask();
  254. while (thisNode->get_bitNum() > nextNode->get_bitNum() )
  255. {
  256. /* Still descending the structure, next node becomes current.
  257. */
  258. thisNode = nextNode;
  259. if (key & bitMask[nextNode->get_bitNum()])
  260. {
  261. /* Bit at the required index was 1, so travers the right node from here
  262. */
  263. nextNode = nextNode->get_rightN();
  264. }
  265. else
  266. {
  267. /* Bit at the required index was 0, so we traverse to the left
  268. */
  269. nextNode = nextNode->get_leftN();
  270. }
  271. }
  272. /* Here we have located the only node that can be reached by the
  273. * bits in the requested key. It could in fact be that key or the node
  274. * we need to use to insert the new key.
  275. */
  276. if (nextNode->get_key() == key)
  277. {
  278. /* We have located an exact match, but we will only append to the bucket chain
  279. * if this trie accepts duplicate keys.
  280. */
  281. if (m_allowDups ==true)
  282. {
  283. /* Yes, we are accepting duplicates
  284. */
  285. newEnt = new TrieEntryType(data, NULL);
  286. /* We want to be able to traverse the stored elements in the order that they were
  287. * added as duplicate keys. We might need to revise this opinion if we end up having many duplicate keys
  288. * as perhaps reverse order is just as good, so long as it is ordered.
  289. */
  290. nextEnt = nextNode->get_buckets();
  291. while (nextEnt->get_next() != NULL)
  292. {
  293. nextEnt = nextEnt->get_next();
  294. }
  295. nextEnt->set_next(newEnt);
  296. m_count++;
  297. return true;
  298. }
  299. else
  300. {
  301. /* We found the key is already there and we are not allowed duplicates in this
  302. * trie.
  303. */
  304. return false;
  305. }
  306. }
  307. /* Here we have discovered the only node that can be reached by the bits in the key
  308. * but we have found that this node is not the key we need to insert. We must find the
  309. * the leftmost bit by which the current key for that node and the new key we are going
  310. * to insert, differ. While this nested series of ifs may look a bit strange, experimentation
  311. * showed that it allows a machine code path that works well with predicated execution
  312. */
  313. xorKey = (key ^ nextNode->get_key() ); /* Gives 1 bits only where they differ then we find the left most 1 bit*/
  314. /* Most common case is a 32 bit key really
  315. */
  316. const ANTLR_UINT8* bitIndex = this->get_bitIndex();
  317. #ifdef ANTLR_USE_64BIT
  318. if (xorKey & 0xFFFFFFFF00000000)
  319. {
  320. if (xorKey & 0xFFFF000000000000)
  321. {
  322. if (xorKey & 0xFF00000000000000)
  323. {
  324. depth = 56 + bitIndex[((xorKey & 0xFF00000000000000)>>56)];
  325. }
  326. else
  327. {
  328. depth = 48 + bitIndex[((xorKey & 0x00FF000000000000)>>48)];
  329. }
  330. }
  331. else
  332. {
  333. if (xorKey & 0x0000FF0000000000)
  334. {
  335. depth = 40 + bitIndex[((xorKey & 0x0000FF0000000000)>>40)];
  336. }
  337. else
  338. {
  339. depth = 32 + bitIndex[((xorKey & 0x000000FF00000000)>>32)];
  340. }
  341. }
  342. }
  343. else
  344. #endif
  345. {
  346. if (xorKey & 0x00000000FFFF0000)
  347. {
  348. if (xorKey & 0x00000000FF000000)
  349. {
  350. depth = 24 + bitIndex[((xorKey & 0x00000000FF000000)>>24)];
  351. }
  352. else
  353. {
  354. depth = 16 + bitIndex[((xorKey & 0x0000000000FF0000)>>16)];
  355. }
  356. }
  357. else
  358. {
  359. if (xorKey & 0x000000000000FF00)
  360. {
  361. depth = 8 + bitIndex[((xorKey & 0x0000000000000FF00)>>8)];
  362. }
  363. else
  364. {
  365. depth = bitIndex[xorKey & 0x00000000000000FF];
  366. }
  367. }
  368. }
  369. /* We have located the leftmost differing bit, indicated by the depth variable. So, we know what
  370. * bit index we are to insert the new entry at. There are two cases, being where the two keys
  371. * differ at a bit position that is not currently part of the bit testing, where they differ on a bit
  372. * that is currently being skipped in the indexed comparisons, and where they differ on a bit
  373. * that is merely lower down in the current bit search. If the bit index went bit 4, bit 2 and they differ
  374. * at bit 3, then we have the "skipped" bit case. But if that chain was Bit 4, Bit 2 and they differ at bit 1
  375. * then we have the easy bit <pun>.
  376. *
  377. * So, set up to descend the tree again, but this time looking for the insert point
  378. * according to whether we skip the bit that differs or not.
  379. */
  380. thisNode = m_root;
  381. entNode = m_root->get_leftN();
  382. /* Note the slight difference in the checks here to cover both cases
  383. */
  384. while (thisNode->get_bitNum() > entNode->get_bitNum() && entNode->get_bitNum() > depth)
  385. {
  386. /* Still descending the structure, next node becomes current.
  387. */
  388. thisNode = entNode;
  389. if (key & bitMask[entNode->get_bitNum()])
  390. {
  391. /* Bit at the required index was 1, so traverse the right node from here
  392. */
  393. entNode = entNode->get_rightN();
  394. }
  395. else
  396. {
  397. /* Bit at the required index was 0, so we traverse to the left
  398. */
  399. entNode = entNode->get_leftN();
  400. }
  401. }
  402. /* We have located the correct insert point for this new key, so we need
  403. * to allocate our entry and insert it etc.
  404. */
  405. nextNode = new IntTrieNodeType();
  406. /* Build a new entry block for the new node
  407. */
  408. newEnt = new TrieEntryType(data, NULL);
  409. /* Install it
  410. */
  411. nextNode->set_buckets(newEnt);
  412. nextNode->set_key(key);
  413. nextNode->set_bitNum( depth );
  414. /* Work out the right and left pointers for this new node, which involve
  415. * terminating with the current found node either right or left according
  416. * to whether the current index bit is 1 or 0
  417. */
  418. if (key & bitMask[depth])
  419. {
  420. nextNode->set_leftN(entNode); /* Terminates at previous position */
  421. nextNode->set_rightN(nextNode); /* Terminates with itself */
  422. }
  423. else
  424. {
  425. nextNode->set_rightN(entNode); /* Terminates at previous position */
  426. nextNode->set_leftN(nextNode); /* Terminates with itself */
  427. }
  428. /* Finally, we need to change the pointers at the node we located
  429. * for inserting. If the key bit at its index is set then the right
  430. * pointer for that node becomes the newly created node, otherwise the left
  431. * pointer does.
  432. */
  433. if (key & bitMask[thisNode->get_bitNum()] )
  434. {
  435. thisNode->set_rightN( nextNode );
  436. }
  437. else
  438. {
  439. thisNode->set_leftN(nextNode);
  440. }
  441. /* Et voila
  442. */
  443. m_count++;
  444. return true;
  445. }
  446. template< class ImplTraits, class DataType >
  447. IntTrieNode<ImplTraits, DataType>::IntTrieNode()
  448. {
  449. m_bitNum = 0;
  450. m_key = 0;
  451. m_buckets = NULL;
  452. m_leftN = NULL;
  453. m_rightN = NULL;
  454. }
  455. template< class ImplTraits, class DataType >
  456. IntTrieNode<ImplTraits, DataType>::~IntTrieNode()
  457. {
  458. TrieEntryType* thisEntry;
  459. TrieEntryType* nextEntry;
  460. /* If this node has a left pointer that is not a back pointer
  461. * then recursively call to free this
  462. */
  463. if ( m_bitNum > m_leftN->get_bitNum())
  464. {
  465. /* We have a left node that needs descending, so do it.
  466. */
  467. delete m_leftN;
  468. }
  469. /* The left nodes from here should now be dealt with, so
  470. * we need to descend any right nodes that are not back pointers
  471. */
  472. if ( m_bitNum > m_rightN->get_bitNum() )
  473. {
  474. /* There are some right nodes to descend and deal with.
  475. */
  476. delete m_rightN;
  477. }
  478. /* Now all the children are dealt with, we can destroy
  479. * this node too
  480. */
  481. thisEntry = m_buckets;
  482. while (thisEntry != NULL)
  483. {
  484. nextEntry = thisEntry->get_next();
  485. /* Now free the data for this bucket entry
  486. */
  487. delete thisEntry;
  488. thisEntry = nextEntry; /* See if there are any more to free */
  489. }
  490. /* The bucket entry is now gone, so we can free the memory for
  491. * the entry itself.
  492. */
  493. /* And that should be it for everything under this node and itself
  494. */
  495. }
  496. /**
  497. * Allocate and initialize a new ANTLR3 topological sorter, which can be
  498. * used to define edges that identify numerical node indexes that depend on other
  499. * numerical node indexes, which can then be sorted topologically such that
  500. * any node is sorted after all its dependent nodes.
  501. *
  502. * Use:
  503. *
  504. * /verbatim
  505. pANTLR3_TOPO topo;
  506. topo = antlr3NewTopo();
  507. if (topo == NULL) { out of memory }
  508. topo->addEdge(topo, 3, 0); // Node 3 depends on node 0
  509. topo->addEdge(topo, 0, 1); // Node - depends on node 1
  510. topo->sortVector(topo, myVector); // Sort the vector in place (node numbers are the vector entry numbers)
  511. * /verbatim
  512. */
  513. template<class ImplTraits>
  514. Topo<ImplTraits>::Topo()
  515. {
  516. // Initialize variables
  517. //
  518. m_visited = NULL; // Don't know how big it is yet
  519. m_limit = 1; // No edges added yet
  520. m_edges = NULL; // No edges added yet
  521. m_sorted = NULL; // Nothing sorted at the start
  522. m_cycle = NULL; // No cycles at the start
  523. m_cycleMark = 0; // No cycles at the start
  524. m_hasCycle = false; // No cycle at the start
  525. }
  526. // Topological sorter
  527. //
  528. template<class ImplTraits>
  529. void Topo<ImplTraits>::addEdge(ANTLR_UINT32 edge, ANTLR_UINT32 dependency)
  530. {
  531. ANTLR_UINT32 i;
  532. ANTLR_UINT32 maxEdge;
  533. BitsetType* edgeDeps;
  534. if (edge>dependency)
  535. {
  536. maxEdge = edge;
  537. }
  538. else
  539. {
  540. maxEdge = dependency;
  541. }
  542. // We need to add an edge to says that the node indexed by 'edge' is
  543. // dependent on the node indexed by 'dependency'
  544. //
  545. // First see if we have enough room in the edges array to add the edge?
  546. //
  547. if ( m_edges == NULL)
  548. {
  549. // We don't have any edges yet, so create an array to hold them
  550. //
  551. m_edges = AllocPolicyType::alloc0(sizeof(BitsetType*) * (maxEdge + 1));
  552. // Set the limit to what we have now
  553. //
  554. m_limit = maxEdge + 1;
  555. }
  556. else if (m_limit <= maxEdge)
  557. {
  558. // WE have some edges but not enough
  559. //
  560. m_edges = AllocPolicyType::realloc(m_edges, sizeof(BitsetType*) * (maxEdge + 1));
  561. // Initialize the new bitmaps to ;indicate we have no edges defined yet
  562. //
  563. for (i = m_limit; i <= maxEdge; i++)
  564. {
  565. *((m_edges) + i) = NULL;
  566. }
  567. // Set the limit to what we have now
  568. //
  569. m_limit = maxEdge + 1;
  570. }
  571. // If the edge was flagged as depending on itself, then we just
  572. // do nothing as it means this routine was just called to add it
  573. // in to the list of nodes.
  574. //
  575. if (edge == dependency)
  576. {
  577. return;
  578. }
  579. // Pick up the bit map for the requested edge
  580. //
  581. edgeDeps = *((m_edges) + edge);
  582. if (edgeDeps == NULL)
  583. {
  584. // No edges are defined yet for this node
  585. //
  586. edgeDeps = new BitsetType(0);
  587. *((m_edges) + edge) = edgeDeps;
  588. }
  589. // Set the bit in the bitmap that corresponds to the requested
  590. // dependency.
  591. //
  592. edgeDeps->add(dependency);
  593. // And we are all set
  594. //
  595. return;
  596. }
  597. /**
  598. * Given a starting node, descend its dependent nodes (ones that it has edges
  599. * to) until we find one without edges. Having found a node without edges, we have
  600. * discovered the bottom of a depth first search, which we can then ascend, adding
  601. * the nodes in order from the bottom, which gives us the dependency order.
  602. */
  603. template<class ImplTraits>
  604. void Topo<ImplTraits>::DFS(ANTLR_UINT32 node)
  605. {
  606. BitsetType* edges;
  607. // Guard against a revisit and check for cycles
  608. //
  609. if (m_hasCycle == true)
  610. {
  611. return; // We don't do anything else if we found a cycle
  612. }
  613. if ( m_visited->isMember(node))
  614. {
  615. // Check to see if we found a cycle. To do this we search the
  616. // current cycle stack and see if we find this node already in the stack.
  617. //
  618. ANTLR_UINT32 i;
  619. for (i=0; i< m_cycleMark; i++)
  620. {
  621. if ( m_cycle[i] == node)
  622. {
  623. // Stop! We found a cycle in the input, so rejig the cycle
  624. // stack so that it only contains the cycle and set the cycle flag
  625. // which will tell the caller what happened
  626. //
  627. ANTLR_UINT32 l;
  628. for (l = i; l < m_cycleMark; l++)
  629. {
  630. m_cycle[l - i] = m_cycle[l]; // Move to zero base in the cycle list
  631. }
  632. // Recalculate the limit
  633. //
  634. m_cycleMark -= i;
  635. // Signal disaster
  636. //
  637. m_hasCycle = true;
  638. }
  639. }
  640. return;
  641. }
  642. // So far, no cycles have been found and we have not visited this node yet,
  643. // so this node needs to go into the cycle stack before we continue
  644. // then we will take it out of the stack once we have descended all its
  645. // dependencies.
  646. //
  647. m_cycle[m_cycleMark++] = node;
  648. // First flag that we have visited this node
  649. //
  650. m_visited->add(node);
  651. // Now, if this node has edges, then we want to ensure we visit
  652. // them all before we drop through and add this node into the sorted
  653. // list.
  654. //
  655. edges = *((m_edges) + node);
  656. if (edges != NULL)
  657. {
  658. // We have some edges, so visit each of the edge nodes
  659. // that have not already been visited.
  660. //
  661. ANTLR_UINT32 numBits; // How many bits are in the set
  662. ANTLR_UINT32 i;
  663. ANTLR_UINT32 range;
  664. numBits = edges->numBits();
  665. range = edges->size(); // Number of set bits
  666. // Stop if we exahust the bit list or have checked the
  667. // number of edges that this node refers to (so we don't
  668. // check bits at the end that cannot possibly be set).
  669. //
  670. for (i=0; i<= numBits && range > 0; i++)
  671. {
  672. if (edges->isMember(i))
  673. {
  674. range--; // About to check another one
  675. // Found an edge, make sure we visit and descend it
  676. //
  677. this->DFS(i);
  678. }
  679. }
  680. }
  681. // At this point we will have visited all the dependencies
  682. // of this node and they will be ordered (even if there are cycles)
  683. // So we just add the node into the sorted list at the
  684. // current index position.
  685. //
  686. m_sorted[m_limit++] = node;
  687. // Remove this node from the cycle list if we have not detected a cycle
  688. //
  689. if (m_hasCycle == false)
  690. {
  691. m_cycleMark--;
  692. }
  693. return;
  694. }
  695. template<class ImplTraits>
  696. ANTLR_UINT32* Topo<ImplTraits>::sortToArray()
  697. {
  698. ANTLR_UINT32 v;
  699. ANTLR_UINT32 oldLimit;
  700. // Guard against being called with no edges defined
  701. //
  702. if (m_edges == NULL)
  703. {
  704. return 0;
  705. }
  706. // First we need a vector to populate with enough
  707. // entries to accomodate the sorted list and another to accomodate
  708. // the maximum cycle we could detect which is all nodes such as 0->1->2->3->0
  709. //
  710. m_sorted = AllocPolicyType::alloc( m_limit * sizeof(ANTLR_UINT32) );
  711. m_cycle = AllocPolicyType::alloc( m_limit * sizeof(ANTLR_UINT32));
  712. // Next we need an empty bitset to show whether we have visited a node
  713. // or not. This is the bit that gives us linear time of course as we are essentially
  714. // dropping through the nodes in depth first order and when we get to a node that
  715. // has no edges, we pop back up the stack adding the nodes we traversed in reverse
  716. // order.
  717. //
  718. m_visited = new BitsetType(0);
  719. // Now traverse the nodes as if we were just going left to right, but
  720. // then descend each node unless it has already been visited.
  721. //
  722. oldLimit = m_limit; // Number of nodes to traverse linearly
  723. m_limit = 0; // Next entry in the sorted table
  724. for (v = 0; v < oldLimit; v++)
  725. {
  726. // If we did not already visit this node, then descend it until we
  727. // get a node without edges or arrive at a node we have already visited.
  728. //
  729. if (m_visited->isMember(v) == false)
  730. {
  731. // We have not visited this one so descend it
  732. //
  733. this->DFS(v);
  734. }
  735. // Break the loop if we detect a cycle as we have no need to go any
  736. // further
  737. //
  738. if (m_hasCycle == true)
  739. {
  740. break;
  741. }
  742. }
  743. // Reset the limit to the number we recorded as if we hit a
  744. // cycle, then limit will have stopped at the node where we
  745. // discovered the cycle, but in order to free the edge bitmaps
  746. // we need to know how many we may have allocated and traverse them all.
  747. //
  748. m_limit = oldLimit;
  749. // Having traversed all the nodes we were given, we
  750. // are guaranteed to have ordered all the nodes or detected a
  751. // cycle.
  752. //
  753. return m_sorted;
  754. }
  755. template<class ImplTraits>
  756. template<typename DataType>
  757. void Topo<ImplTraits>::sortVector( typename ImplTraits::template VectorType<DataType>& v )
  758. {
  759. // To sort a vector, we first perform the
  760. // sort to an array, then use the results to reorder the vector
  761. // we are given. This is just a convenience routine that allows you to
  762. // sort the children of a tree node into topological order before or
  763. // during an AST walk. This can be useful for optimizations that require
  764. // dag reorders and also when the input stream defines thigns that are
  765. // interdependent and you want to walk the list of the generated trees
  766. // for those things in topological order so you can ignore the interdependencies
  767. // at that point.
  768. //
  769. ANTLR_UINT32 i;
  770. // Used as a lookup index to find the current location in the vector of
  771. // the vector entry that was originally at position [0], [1], [2] etc
  772. //
  773. ANTLR_UINT32* vIndex;
  774. // Sort into an array, then we can use the array that is
  775. // stored in the topo
  776. //
  777. if (this->sortToArray() == 0)
  778. {
  779. return; // There were no edges
  780. }
  781. if (m_hasCycle == true)
  782. {
  783. return; // Do nothing if we detected a cycle
  784. }
  785. // Ensure that the vector we are sorting is at least as big as the
  786. // the input sequence we were adsked to sort. It does not matter if it is
  787. // bigger as thaat probably just means that nodes numbered higher than the
  788. // limit had no dependencies and so can be left alone.
  789. //
  790. if (m_limit > v.size() )
  791. {
  792. // We can only sort the entries that we have dude! The caller is
  793. // responsible for ensuring the vector is the correct one and is the
  794. // correct size etc.
  795. //
  796. m_limit = v.size();
  797. }
  798. // We need to know the locations of each of the entries
  799. // in the vector as we don't want to duplicate them in a new vector. We
  800. // just use an indirection table to get the vector entry for a particular sequence
  801. // acording to where we moved it last. Then we can just swap vector entries until
  802. // we are done :-)
  803. //
  804. vIndex = AllocPolicyType::alloc(m_limit * sizeof(ANTLR_UINT32));
  805. // Start index, each vector entry is located where you think it is
  806. //
  807. for (i = 0; i < m_limit; i++)
  808. {
  809. vIndex[i] = i;
  810. }
  811. // Now we traverse the sorted array and moved the entries of
  812. // the vector around according to the sort order and the indirection
  813. // table we just created. The index telsl us where in the vector the
  814. // original element entry n is now located via vIndex[n].
  815. //
  816. for (i=0; i < m_limit; i++)
  817. {
  818. ANTLR_UINT32 ind;
  819. // If the vector entry at i is already the one that it
  820. // should be, then we skip moving it of course.
  821. //
  822. if (vIndex[m_sorted[i]] == i)
  823. {
  824. continue;
  825. }
  826. // The vector entry at i, should be replaced with the
  827. // vector entry indicated by topo->sorted[i]. The vector entry
  828. // at topo->sorted[i] may have already been swapped out though, so we
  829. // find where it is now and move it from there to i.
  830. //
  831. ind = vIndex[m_sorted[i]];
  832. std::swap( v[i], v[ind] );
  833. // Update our index. The element at i is now the one we wanted
  834. // to be sorted here and the element we swapped out is now the
  835. // element that was at i just before we swapped it. If you are lost now
  836. // don't worry about it, we are just reindexing on the fly is all.
  837. //
  838. vIndex[m_sorted[i]] = i;
  839. vIndex[i] = ind;
  840. }
  841. // Having traversed all the entries, we have sorted the vector in place.
  842. //
  843. AllocPolicyType::free(vIndex);
  844. return;
  845. }
  846. template<class ImplTraits>
  847. Topo<ImplTraits>::~Topo()
  848. {
  849. ANTLR_UINT32 i;
  850. // Free the result vector
  851. //
  852. if (m_sorted != NULL)
  853. {
  854. AllocPolicyType::free(m_sorted);
  855. }
  856. // Free the visited map
  857. //
  858. if (m_visited != NULL)
  859. {
  860. delete m_visited;
  861. }
  862. // Free any edgemaps
  863. //
  864. if (m_edges != NULL)
  865. {
  866. Bitset<AllocPolicyType>* edgeList;
  867. for (i=0; i<m_limit; i++)
  868. {
  869. edgeList = *((m_edges) + i);
  870. if (edgeList != NULL)
  871. {
  872. delete edgeList;
  873. }
  874. }
  875. AllocPolicyType::free( m_edges );
  876. }
  877. m_edges = NULL;
  878. // Free any cycle map
  879. //
  880. if (m_cycle != NULL)
  881. {
  882. AllocPolicyType::free(m_cycle);
  883. }
  884. }
  885. }