1
0

tire.h 948 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef __L87_TIRE_H__
  2. #define __L87_TIRE_H__
  3. #include "list.h"
  4. #include <string>
  5. template<typename Type,
  6. typename Container = List<Type>>
  7. class Tire {
  8. public:
  9. struct Node {
  10. unsigned int bitmap = 0;
  11. typename Container::Node* value = nullptr;
  12. Node* next[16] { nullptr };
  13. Node();
  14. ~Node();
  15. Node* operator[](const int ch);
  16. };
  17. typedef typename Container::iterator iterator;
  18. private:
  19. Node* _root = new Node();
  20. Container _container;
  21. private:
  22. // 子树 (子树不支持迭代器, 且主树析构后子树不可用)
  23. bool _is_subtree;
  24. Tire(Node* root);
  25. public:
  26. Tire();
  27. ~Tire();
  28. Type& operator[](const std::string& key);
  29. iterator find(const std::string& key);
  30. iterator insert(const std::string& key, Type& value);
  31. size_t erase(const std::string& key);
  32. Tire& set_prefix(string& key);
  33. public:
  34. iterator begin();
  35. iterator end();
  36. iterator rbegin();
  37. iterator rend();
  38. };
  39. #include "tire.inl"
  40. #endif