瀏覽代碼

完善tire

LinTianxiang 10 月之前
父節點
當前提交
0ce63dd0b2
共有 2 個文件被更改,包括 142 次插入47 次删除
  1. 104 47
      tire.cpp
  2. 38 0
      tire.h

+ 104 - 47
tire.cpp

@@ -1,58 +1,115 @@
-#ifndef __UNORDERED_TIRE_H__
-#define __UNORDERED_TIRE_H__
-
-#include "list.h"
-
-template<class T>
-class unordered_tire {
-private:
-	class tire_node {
-	private:
-		tire_node* _next[256] { nullptr };
-		list_node<T>* _value = nullptr;
-	public:
-		tire_node() {}
-		tire_node(T value) { _value = new list_node(value); }
-		tire_node* operator[](const unsigned char ch) { return _next[ch]; }
-		T& operator*() { return **_value; }
-		bool is_word() { retrun _value != nullptr; }
-	};
+#include "tire.h"
 
-	// #pragma region iterator
-public:
-	typedef list<T>::iterator iterator;
+#define HOLDER
+#define __TEMPLATE template<typename Type, typename Container>
+#define TEMPLATE(rtype) __TEMPLATE rtype
 
-	iterator begin() {
-		_list.begin();
-	}
+#define ADT Tire<Type, Container>
+#define NODE ADT::Node
+#define ITERATOR ADT::iterator
+
+// #pragma region Tire<Type>::Node
+TEMPLATE(HOLDER) NODE::Node() {
+
+}
+
+TEMPLATE(HOLDER) NODE::Node(Type& value) {
+	_value = typename Container::Node(value);
+}
+// #pragma endregion
+
+// #pragma region Tire<Type>
+TEMPLATE(HOLDER) ADT::Tire() {
+
+}
 
-	iterator end() {
-		_list.end();
+TEMPLATE(typename ITERATOR) ADT::find(const std::string& key) {
+	Node* curr = &_root;
+	Node* next = nullptr;
+	for (char ch : key) {
+		next = curr[ch];
+		if (next == nullptr) {
+
+		}
+		curr = next;
 	}
-	// #pragma endregion
-
-private:
-	// list_node npos;
-	tire_node _root;
-	list _list;
-public:
-	iterator find(const string& key) {
-		tire_node* curr = &_root;
-		tire_node* next = nullptr;
-		for (char ch : key) {
-			next = curr[ch];
-			if (next == nullptr) {
-				return end();
-			}
+	return curr->is_word() ? curr : nullptr;
+}
+
+TEMPLATE(Type&) ADT::operator[](const std::string& key) {
+	return *find(key);
+}
+
+TEMPLATE(typename ITERATOR) ADT::insert(const std::string& key, Type& value) {
+	Node* curr = &_root;
+	Node* next = nullptr;
+	for (const char& ch : key) {
+		next = curr->_next[(int)ch];
+		if (next != nullptr) {
 			curr = next;
+			continue;
 		}
-		return curr.is_word() ? *curr : end();
+		curr = curr->_next[(int)ch] = new Node();
 	}
+	iterator it = _container.append(value);
+	curr->_value = &it;
+	return it;
+}
+
+TEMPLATE(typename ITERATOR) ADT::erase(const std::string& key) {
+	return false; // TBD
+}
+
+TEMPLATE(typename ITERATOR) ADT::begin() { return _container.begin(); }
 
-	const T& operator[](const string& key) {
-		iterator it = find(key);
-		// TBD exception
-		return *it;
+TEMPLATE(typename ITERATOR) ADT::end() { return _container.end(); }
+
+TEMPLATE(typename ITERATOR) ADT::rbegin() { return _container.rbegin(); }
+
+TEMPLATE(typename ITERATOR) ADT::rend() { return _container.rend(); }
+
+// #pragma endregion
+
+#undef HOLDER
+#undef __TEMPLATE
+#undef TEMPLATE
+#undef ADT
+#undef NODE
+#undef ITERATOR
+
+#define __DEBUG_TIRE__
+#ifdef __DEBUG_TIRE__
+
+#include <iostream>
+#include <string>
+#include <vector>
+#include <memory>
+#include <unordered_map>
+
+struct Test {
+	int num;
+	std::string str;
+	Test() {}
+	Test(const Test& t) {
+		*this = t;
 	}
 };
+
+int main() {
+	Tire<Test> tire;
+	std::unordered_map<std::string, Test> umap;
+	for (int i = 0; i < 255; ++i) {
+		Test test;
+		test.num = i + 1;
+		test.str = std::to_string(i + 1);
+		tire.insert(test.str, test);
+		umap.insert({ test.str, test });
+	};
+
+	for (auto item : tire) {
+		std::cout << "str:" << item.str << ". num:" << item.num << ". " << std::endl;
+	}
+
+	return 0;
+}
 #endif

+ 38 - 0
tire.h

@@ -0,0 +1,38 @@
+#ifndef __L87_TIRE_H__
+#define __L87_TIRE_H__
+
+#include "list.h"
+#include "list.cpp"
+#include <string>
+
+template<typename Type,
+	typename Container = List<Type>>
+	class Tire {
+	public:
+		struct Node {
+			Node();
+			Node(Type& value);
+			Node* operator[](const int ch);
+			Node* _next[256] { nullptr };
+			typename Container::Node* _value = nullptr;
+		};
+
+		typedef typename Container::iterator iterator;
+
+	private:
+		Node _root;
+		Container _container;
+	public:
+		Tire();
+		Type& operator[](const std::string& key);
+		iterator find(const std::string& key);
+		iterator insert(const std::string& key, Type& value);
+		iterator erase(const std::string& key);
+	public:
+		iterator begin();
+		iterator end();
+		iterator rbegin();
+		iterator rend();
+};
+
+#endif