Advertisement
Guest User

Untitled

a guest
Jan 25th, 2014
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1.  
  2. #include <assert.h>
  3. #include <stdlib.h> /* srand, rand */
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <vector>
  7.  
  8. int Created = 0;
  9. int Deleted = 0;
  10.  
  11. template <typename T>
  12. T create_empty()
  13. {
  14. return T();
  15. }
  16.  
  17. template <>
  18. bool create_empty<bool>()
  19. {
  20. return false;
  21. }
  22.  
  23. template <typename T>
  24. bool check_empty(T& a)
  25. {
  26. return a.is_empty();
  27. }
  28.  
  29. template <>
  30. bool check_empty<bool>(bool& a)
  31. {
  32. return a == false;
  33. }
  34.  
  35. template <typename T,typename C,int Base>
  36. class TrieNode
  37. {
  38. TrieNode* childs[Base];
  39. T n;
  40.  
  41. unsigned int val;
  42.  
  43. void init(unsigned int val)
  44. {
  45. Created++;
  46.  
  47. this->val = val;
  48. memset(childs, 0 ,sizeof(TrieNode*) * Base);
  49. n = create_empty<T>();
  50. }
  51.  
  52. TrieNode(int mult)
  53. {
  54. init(mult);
  55. }
  56.  
  57. T& create(unsigned int number,int mult)
  58. {
  59. if(!number)
  60. {
  61. return n;
  62. }
  63. else
  64. {
  65. unsigned int digit = number % Base;
  66. number /= Base;
  67.  
  68. TrieNode*& child = childs[digit];
  69.  
  70. if(!child)
  71. child = new TrieNode(digit*mult);
  72.  
  73. return child->create(number,mult*Base);
  74. }
  75. }
  76.  
  77. C get_keys(int val)
  78. {
  79. C list;
  80.  
  81. if(!check_empty(n))
  82. {
  83. list.push_back(val);
  84. }
  85.  
  86. for(int i=0;i<Base;i++)
  87. {
  88. TrieNode<T,C,Base>*& child = childs[i];
  89.  
  90. if(child)
  91. {
  92. C list_new = child->get_keys(child->val + val);
  93.  
  94. for(int j=0;j<(int)list_new.size();j++)
  95. {
  96. list.push_back(list_new[j]);
  97. }
  98. }
  99. }
  100.  
  101. return list;
  102. }
  103.  
  104. TrieNode& move(TrieNode& other)
  105. {
  106. this->val = other.val;
  107. memcpy(this->childs,other.childs,sizeof(TrieNode*) * Base);
  108. this->n = other.n;
  109.  
  110. other.val = 1;
  111. memset(other.childs,0,sizeof(TrieNode*) * Base);
  112. other.n = create_empty<T>();
  113.  
  114. return *this;
  115. }
  116.  
  117.  
  118. public :
  119.  
  120. ~TrieNode()
  121. {
  122. Deleted++;
  123.  
  124. for(int i=0;i<Base;i++)
  125. delete childs[i];
  126. }
  127.  
  128.  
  129. TrieNode(){ init(1); }
  130.  
  131. TrieNode (const TrieNode& other)
  132. {
  133. move( const_cast<TrieNode&>(other) );
  134. }
  135.  
  136. TrieNode& operator= (TrieNode other)
  137. {
  138. return move(other);
  139. }
  140.  
  141. T& operator[](unsigned int number)
  142. {
  143. return this->create(number,1);
  144. }
  145.  
  146. bool is_empty()
  147. {
  148. if(!check_empty<T>(n))
  149. return false;
  150.  
  151. for(int i=0;i<Base;i++)
  152. {
  153. TrieNode*& child = childs[i];
  154.  
  155. if(child)
  156. {
  157. if(!check_empty< TrieNode<T,C,Base> >(*child))
  158. return false;
  159. }
  160. }
  161.  
  162. return true;
  163. }
  164.  
  165. C get_keys()
  166. {
  167. return this->get_keys(0);
  168. }
  169. };
  170.  
  171. template<typename T,typename C>
  172. struct TrieNodeTen
  173. {
  174. typedef TrieNode<T,C,10> type;
  175. };
  176.  
  177. template <typename T>
  178. struct TrieNodeTenVec
  179. {
  180. typedef typename TrieNodeTen< T,std::vector<int> >::type type;
  181. };
  182.  
  183. template <typename T,typename C>
  184. class TrieNodeTen1 : public TrieNode<T,C,10> {};
  185.  
  186. template <typename T>
  187. class TrieNodeTenVec1 : public TrieNodeTen1<T,std::vector<int> > {};
  188.  
  189. int main(int argc, char* argv[])
  190. {
  191. {
  192. TrieNodeTenVec1< TrieNodeTenVec1<bool> > trie;
  193.  
  194. for(int i=0;i<100;i++)
  195. trie[rand()][rand()] = true;
  196. }
  197.  
  198. printf("Created %d\n",Created);
  199. printf("Deleted %d\n",Deleted);
  200.  
  201. getchar();
  202.  
  203. return 0;
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement