Trie.h 5.6 KB

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