tire.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef __UNORDERED_TIRE_H__
  2. #define __UNORDERED_TIRE_H__
  3. #include "list.h"
  4. template<class T>
  5. class unordered_tire {
  6. private:
  7. class tire_node {
  8. private:
  9. tire_node* _next[256] { nullptr };
  10. list_node<T>* _value = nullptr;
  11. public:
  12. tire_node() {}
  13. tire_node(T value) { _value = new list_node(value); }
  14. tire_node* operator[](const unsigned char ch) { return _next[ch]; }
  15. T& operator*() { return **_value; }
  16. bool is_word() { retrun _value != nullptr; }
  17. };
  18. // #pragma region iterator
  19. public:
  20. typedef list<T>::iterator iterator;
  21. iterator begin() {
  22. _list.begin();
  23. }
  24. iterator end() {
  25. _list.end();
  26. }
  27. // #pragma endregion
  28. private:
  29. // list_node npos;
  30. tire_node _root;
  31. list _list;
  32. public:
  33. iterator find(const string& key) {
  34. tire_node* curr = &_root;
  35. tire_node* next = nullptr;
  36. for (char ch : key) {
  37. next = curr[ch];
  38. if (next == nullptr) {
  39. return end();
  40. }
  41. curr = next;
  42. }
  43. return curr.is_word() ? *curr : end();
  44. }
  45. const T& operator[](const string& key) {
  46. iterator it = find(key);
  47. // TBD exception
  48. return *it;
  49. }
  50. };
  51. #endif