From 6e1eaef2e6f381b42ce4a503a634ba3457d8eb5d Mon Sep 17 00:00:00 2001 From: ninemine <1371605831@qq.com> Date: Thu, 28 Aug 2025 16:49:48 +0800 Subject: [PATCH] =?UTF-8?q?=E5=87=86=E5=A4=87=E6=96=B0=E5=A2=9E=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E7=9A=84=E5=A2=9E=E5=BC=BA=E7=BB=93=E6=9E=84?= =?UTF-8?q?=EF=BC=8C=E5=8C=85=E6=8B=AC=E7=BC=96=E8=AF=91=E6=9C=9F=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CP.hpp | 1 + Core.hpp | 1 + detail/CP/CHash.hpp | 14 ++++ detail/CP/CPString.hpp | 7 ++ detail/Core/String.hpp | 174 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 197 insertions(+) create mode 100644 detail/CP/CPString.hpp create mode 100644 detail/Core/String.hpp diff --git a/CP.hpp b/CP.hpp index fba9505..b9aa4ed 100644 --- a/CP.hpp +++ b/CP.hpp @@ -12,5 +12,6 @@ #include "detail/CP/MaxMin.hpp" #include "detail/CP/Arithmetic.hpp" #include "detail/CP/Typen.hpp" +#include "detail\CP\CPString.hpp" #endif // __FILE_Operator_Hpp diff --git a/Core.hpp b/Core.hpp index cdf2c90..c8eb419 100644 --- a/Core.hpp +++ b/Core.hpp @@ -3,5 +3,6 @@ #define __FILE_Core_Hpp #include "detail/Core/Object.hpp" +#include "detail\Core\String.hpp" #endif // !__FILE_Core_Hpp diff --git a/detail/CP/CHash.hpp b/detail/CP/CHash.hpp index 346a4db..df1803c 100644 --- a/detail/CP/CHash.hpp +++ b/detail/CP/CHash.hpp @@ -46,6 +46,20 @@ inline uint32_t Hash(const int64_t A) return (uint32_t)A + ((uint32_t)(A >> 32) * 23); } +uint32_t Hash(const char* str, uint32_t hash = 5381) +{ + return *str == '\0' ? hash : Hash(str + 1, ((hash << 5) + hash) + *str); +} + +uint32_t Hash(std::string_view str, uint32_t hash = 5381) +{ + for (size_t i = 0; i < str.length(); ++i) + { + hash = ((hash << 5) + hash) + str[i]; + } + return hash; +} + constexpr uint32_t CHash(const uint8_t A) { return A; diff --git a/detail/CP/CPString.hpp b/detail/CP/CPString.hpp new file mode 100644 index 0000000..0201a68 --- /dev/null +++ b/detail/CP/CPString.hpp @@ -0,0 +1,7 @@ +#pragma once +#ifndef __FILE_Detail_CP_CPString_H +#define __FILE_Detail_CP_CPString_H + + + +#endif // !__FILE_Detail_CP_CPString_H diff --git a/detail/Core/String.hpp b/detail/Core/String.hpp new file mode 100644 index 0000000..ac6dd4a --- /dev/null +++ b/detail/Core/String.hpp @@ -0,0 +1,174 @@ +#pragma once +#ifndef __FILE_Detail_Core_String_H +#define __FILE_Detail_Core_String_H + +namespace Internal +{ + template + class PrefixStringTree + { + public: + PrefixStringTree* parent; + PrefixStringTree* child_head; + PrefixStringTree* child_end; + PrefixStringTree* next; + + using my_string_view = std::basic_string_view; + private: + Char mine; + Char* data; + my_string_view rdata; + public: + my_string_view ReadValue() const + { + return rdata; + } + + constexpr operator my_string_view() const + { + return rdata; + } + + PrefixStringTree& PopBack() + { + if (parent) + return *parent; + return *this; + } + + private: + void RegetDataFromChild(PrefixStringTree* target) + { + auto size = this->rdata.size(); + if (data) + { + delete this->data; + this->data = target->data; + } + this->rdata = target->rdata.substr(0, size); + if (parent) + parent->RegetDataFromChild(target); + } + PrefixStringTree* AddChild(Char ch) + { + auto next = child_head; + child_head = new PrefixStringTree(); + if (next == nullptr) + { + child_end = child_head; + } + child_head->mine = ch; + const auto size = rdata.size(); + child_head->data = new Char[size + 1]; + ::strcpy_s(child_head->data, size, data); + child_head->data[size] = ch; + child_head->rdata = child_head->data; + child_head->next = next; + RegetDataFromChild(child_head); + return child_head; + } + PrefixStringTree* FindChild(Char ch) + { + for (auto head = child_head; head; head = head->next) + { + if (head->mine) + return head; + } + return nullptr; + } + public: + PrefixStringTree& operator+(Char ch) + { + if (ch == 0) + return *this; + auto result = FindChild(ch); + if (result) + return *result; + return *AddChild(ch); + } + + PrefixStringTree& operator+(my_string_view view) + { + if (view.size() == 0 || view.front() == 0) + return *this; + return this->operator+(view.front()).operator+(view.substr(1)); + } + + bool operator==(my_string_view str) + { + return rdata == str; + } + bool operator!=(my_string_view str) + { + return rdata != str; + } + + static PrefixStringTree& GetRoot() + { + static PrefixStringTree root; + return root; + } + + decltype(auto) GetSize() + { + return rdata.size(); + } + + PrefixStringTree& GetPrefix(size_t size = -1) + { + if (size >= rdata.size()) + return *this; + return parent->GetPrefix(size); + } + + PrefixStringTree& GetSamePrefix(PrefixStringTree& other) + { + if (rdata.front() != other.rdata.front()) + return GetRoot(); + auto i = 0; + for (auto e = std::min(rdata.size(), other, rdata.size()); i <= e; i++) + { + if (rdata[i] != other.rdata[i]) + break; + } + return this->GetPrefix(i); + } + + void Clear() + { + for (auto head = child_head; head; head = head->next) + { + head->Clear(); + } + if (data) + { + delete data; + } + } + }; + +} + +template +class PrefixString +{ + using _Mychar = Char; + using my_string_view = std::basic_string_view; + Internal::PrefixStringTree& str; +public: + PrefixString() :str(Internal::PrefixStringTree::GetRoot()) {}; + PrefixString(my_string_view view) :str(Internal::PrefixStringTree::GetRoot() + view) {}; + PrefixString(const PrefixString& other) :str(other.str) {}; + + my_string_view ReadValue() const + { + return str; + } + + operator my_string_view() const + { + return str; + } +}; + +#endif // !__FILE_Detail_Core_String_H