Trie.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #pragma once
  2. #include "CharMap.h"
  3. namespace Framework
  4. {
  5. template<class T> class Trie;
  6. template<class T> struct TrieIteratorData
  7. {
  8. CharMap<Trie<T>, T>* map;
  9. TrieIteratorData<T>* parent;
  10. TrieIteratorData()
  11. {
  12. map = 0;
  13. parent = 0;
  14. }
  15. TrieIteratorData(CharMap<Trie<T>, T>* zMap, TrieIteratorData<T> parent)
  16. {
  17. map = zMap;
  18. this->parent = new TrieIteratorData<T>(parent);
  19. }
  20. TrieIteratorData(const TrieIteratorData& data)
  21. {
  22. map = data.map;
  23. if (data.parent)
  24. parent = new TrieIteratorData(*data.parent);
  25. else
  26. parent = 0;
  27. }
  28. ~TrieIteratorData()
  29. {
  30. delete parent;
  31. }
  32. TrieIteratorData& operator=(const TrieIteratorData& data)
  33. {
  34. map = data.map;
  35. TrieIteratorData* tmp = parent;
  36. if (data.parent)
  37. parent = new TrieIteratorData(*data.parent);
  38. else
  39. parent = 0;
  40. delete tmp;
  41. return *this;
  42. }
  43. void set(CharMap<Trie<T>, T>* zMap, TrieIteratorData parent)
  44. {
  45. map = zMap;
  46. parent = new TrieIteratorData(parent);
  47. }
  48. };
  49. template<class T> class TrieIterator
  50. {
  51. private:
  52. TrieIteratorData<T> data;
  53. public:
  54. TrieIterator(TrieIteratorData<T> data)
  55. {
  56. this->data = data;
  57. }
  58. TrieIterator next()
  59. {
  60. for (unsigned char i = 0; true; i++)
  61. {
  62. if (data.map->z(i))
  63. return TrieIterator(
  64. TrieIteratorData<T>(data.map->z(i)->map, data));
  65. if (i == 255) break;
  66. }
  67. if (!data.parent || !data.parent->map)
  68. return TrieIterator(
  69. TrieIteratorData<T>(0, TrieIteratorData<T>()));
  70. TrieIteratorData<T> d = data;
  71. do
  72. {
  73. for (unsigned char i = 0; true; i++)
  74. {
  75. if (d.parent->map->z(i)
  76. && d.parent->map->z(i)->map == d.map)
  77. {
  78. if (i < 256)
  79. {
  80. for (unsigned char j = (unsigned char)(i + 1); true;
  81. j++)
  82. {
  83. if (d.parent->map->z(j))
  84. return TrieIterator(TrieIteratorData<T>(
  85. d.parent->map->z(j)->map, *d.parent));
  86. if (j == 255) break;
  87. }
  88. }
  89. d = *d.parent;
  90. break;
  91. }
  92. if (i == 255) break;
  93. }
  94. } while (d.parent && d.parent->map);
  95. return TrieIterator(TrieIteratorData<T>(0, TrieIteratorData<T>()));
  96. }
  97. TrieIterator<T>& operator++() //! prefix
  98. {
  99. data = next().data;
  100. return *this;
  101. }
  102. TrieIterator<T> operator++(int) //! postfix
  103. {
  104. TrieIterator<T> temp(*this);
  105. data = next().data;
  106. return temp;
  107. }
  108. operator bool()
  109. {
  110. return data.map != 0;
  111. }
  112. operator T*()
  113. {
  114. return data.map->zValue();
  115. }
  116. T* operator->()
  117. {
  118. return data.map->zValue();
  119. }
  120. T* val()
  121. {
  122. return data.map->zValue();
  123. }
  124. };
  125. template<class T> class Trie : public virtual ReferenceCounter
  126. {
  127. private:
  128. CharMap<Trie<T>, T>* map;
  129. public:
  130. Trie()
  131. : ReferenceCounter()
  132. {
  133. map = new CharMap<Trie<T>, T>();
  134. }
  135. ~Trie()
  136. {
  137. map->release();
  138. }
  139. void set(const char* addr, int addrLen, T* value)
  140. {
  141. if (!addrLen)
  142. map->setValue(value);
  143. else
  144. {
  145. if (!map->z(*addr)) map->set(*addr, new Trie<T>());
  146. map->z(*addr)->set(addr + 1, addrLen - 1, value);
  147. }
  148. }
  149. void remove(const char* addr, int addrLen)
  150. {
  151. if (!addrLen)
  152. map->setValue(0);
  153. else
  154. {
  155. if (!map->z(*addr)) return;
  156. map->z(*addr)->remove(addr + 1, addrLen - 1);
  157. }
  158. }
  159. T* get(const char* addr, int addrLen)
  160. {
  161. if (!addrLen)
  162. return map->getValue();
  163. else
  164. {
  165. if (!map->z(*addr)) return 0;
  166. return map->z(*addr)->get(addr + 1, addrLen - 1);
  167. }
  168. }
  169. T* z(const char* addr, int addrLen)
  170. {
  171. if (!addrLen)
  172. return map->zValue();
  173. else
  174. {
  175. if (!map->z(*addr)) return 0;
  176. return map->z(*addr)->z(addr + 1, addrLen - 1);
  177. }
  178. }
  179. void leeren()
  180. {
  181. map->release();
  182. map = new CharMap<Trie<T>, T>();
  183. }
  184. TrieIterator<T> getIterator()
  185. {
  186. return TrieIterator<T>(
  187. TrieIteratorData<T>(map, TrieIteratorData<T>()));
  188. }
  189. friend TrieIterator<T>;
  190. };
  191. } // namespace Framework