Browse Source

初始化仓库

LinTianxiang 10 months ago
commit
8c7ca5e00b
5 changed files with 223 additions and 0 deletions
  1. 3 0
      .gitignore
  2. 0 0
      README.md
  3. 111 0
      list.cpp
  4. 51 0
      list.h
  5. 58 0
      tire.cpp

+ 3 - 0
.gitignore

@@ -0,0 +1,3 @@
+.vscode/
+bin/
+*.old

+ 0 - 0
README.md


+ 111 - 0
list.cpp

@@ -0,0 +1,111 @@
+#include "list.h"
+
+// #pragma region list<T>::iterator
+template<typename T>
+list<T>::iterator::iterator(list_node* ptr) {
+	_ptr = ptr;
+}
+
+template<typename T>
+typename::list<T>::iterator& list<T>::iterator::operator++() {
+	_ptr = _ptr->_next;
+	return *this;
+}
+
+template<typename T>
+typename::list<T>::iterator& list<T>::iterator::operator--() {
+	_ptr = _ptr->_prev;
+	return *this;
+}
+
+template<typename T>
+bool list<T>::iterator::operator== (const list<T>::iterator& it) {
+	// TBD 其它信息比较
+	return _ptr == it._ptr;
+}
+
+template<typename T>
+bool list<T>::iterator::operator!= (const list<T>::iterator& it) {
+	return !(*this == it);
+}
+// #pragma endregion
+
+// #pragma region list<T>
+template<typename T>
+list<T>::list() : dummy(&dummy, &dummy) {};
+
+template<typename T>
+typename::list<T>::iterator list<T>::begin() { return list<T>::iterator(head->_next); }
+
+template<typename T>
+typename::list<T>::iterator list<T>::end() { return list<T>::iterator(head); }
+
+template<typename T>
+typename::list<T>::iterator list<T>::rbegin() { return list<T>::iterator(tail->_prev); }
+
+template<typename T>
+typename::list<T>::iterator list<T>::rend() { return list<T>::iterator(tail); }
+
+template<typename T>
+typename::list<T>::iterator& list<T>::operator[] (int index) {
+	list_node* curr = head;
+	for (; index >= 0; --index) {
+		if (curr == nullptr) {
+			return end();
+		}
+		curr = curr->next;
+	}
+	return iterator(curr);
+}
+
+template<typename T>
+typename::list<T>& list<T>::operator+= (const T& value) {
+	list_node* node = new list_node(value, tail->_prev, tail);
+	tail->_prev->_next = node;
+	tail->_prev = node;
+	return *this;
+}
+// #pragma endregion
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+#ifdef __DEBUG_LIST__
+struct Test {
+	int num;
+	std::string str;
+	Test() {}
+	Test(const Test& t) {
+		*this = t;
+	}
+};
+
+int main() {
+	list<Test> l1;
+	for (int i = 0; i < 255; ++i) {
+		Test t;
+		t.str = std::to_string(i + 1);
+		t.num = i + 1;
+		l1 += t;
+	}
+
+	for (auto item : l1) {
+		std::cout << "str:" << item.str << ". num:" << item.num << ". " << std::endl;
+	}
+	return 0;
+}
+#endif

+ 51 - 0
list.h

@@ -0,0 +1,51 @@
+#ifndef __LIST_H__
+#define __LIST_H__
+
+#include <iostream>
+#include <string>
+#include <vector>
+#include <memory>
+
+template<typename T>
+class list {
+public:
+	struct list_node {
+		T* _value;
+		list_node* _prev, * _next;
+		list_node() : _prev(nullptr), _next(nullptr) { _value = nullptr; }
+		list_node(list_node* prev, list_node* next) : _prev(prev), _next(next) { _value = nullptr; }
+		list_node(const T& value, list_node* prev = nullptr, list_node* next = nullptr) : _prev(prev), _next(next) { _value = new T(value); }
+		T& operator*() { return *_value; }
+	};
+
+	class iterator {
+	private:
+		list_node* _ptr;
+		// TBD 其它信息
+	public:
+		iterator(list_node* ptr);
+		iterator& operator++();
+		iterator& operator--();
+		bool operator== (const iterator& it);
+		bool operator!= (const iterator& it);
+		T& operator*() { return **_ptr; }
+	};
+
+private:
+	list_node dummy;
+	list_node* head = &dummy;
+	list_node* tail = &dummy;
+public:
+	list();
+public:
+	iterator& operator[] (int index);
+	list& operator+= (const T& value);
+public:
+	iterator begin();
+	iterator end();
+	iterator rbegin();
+	iterator rend();
+};
+
+
+#endif

+ 58 - 0
tire.cpp

@@ -0,0 +1,58 @@
+#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; }
+	};
+
+	// #pragma region iterator
+public:
+	typedef list<T>::iterator iterator;
+
+	iterator begin() {
+		_list.begin();
+	}
+
+	iterator end() {
+		_list.end();
+	}
+	// #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();
+			}
+			curr = next;
+		}
+		return curr.is_word() ? *curr : end();
+	}
+
+	const T& operator[](const string& key) {
+		iterator it = find(key);
+		// TBD exception
+		return *it;
+	}
+};
+#endif