From fb0e2f172a35fad4df0d3b8ee25918cf2ad85730 Mon Sep 17 00:00:00 2001 From: Aleksandr Orefkov Date: Wed, 12 Nov 2025 15:08:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20=D0=BE=20=D1=82=D0=B8=D0=BF=D0=B5=20=D1=85?= =?UTF-8?q?=D0=B5=D1=88=D0=B0=20=D0=B4=D0=BB=D1=8F=20=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B5=D0=B9=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B0=20=D0=B2=20=D1=82=D0=B8=D0=BF=20=D0=BA=D0=BB=D1=8E?= =?UTF-8?q?=D1=87=D0=B0.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/simstr/sstring.h | 88 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/include/simstr/sstring.h b/include/simstr/sstring.h index 20f0ade..5ad41e2 100644 --- a/include/simstr/sstring.h +++ b/include/simstr/sstring.h @@ -5389,7 +5389,7 @@ auto e_repl_const_symbols(simple_str src, Repl&& ... other) { return expr_replace_const_symbols(src, std::forward(other)...); } -template +template struct StoreType { simple_str str; size_t hash; @@ -5403,10 +5403,6 @@ struct StoreType { } }; -using HashKeyA = StoreType; -using HashKeyW = StoreType; -using HashKeyU = StoreType; - template struct fnv_const { static inline constexpr size_t basis = static_cast(14695981039346656037ULL); @@ -5469,7 +5465,7 @@ inline consteval size_t fnv_hash_ia_compile(const K* ptr, size_t l) { return fnv_hash_ia(ptr, l); }; -static_assert(std::is_trivially_copyable_v>, "Store type must be trivially copyable"); +static_assert(std::is_trivially_copyable_v>, "Store type must be trivially copyable"); template struct streql; @@ -5525,9 +5521,9 @@ struct strhash; * но и не смертельный, если его нет. */ template, typename E = streql> -class hashStrMap : public std::unordered_map, T, H, E> { +class hashStrMap : public std::unordered_map, T, H, E> { protected: - using InStore = StoreType; + using InStore = StoreType; public: using my_type = hashStrMap; @@ -5562,7 +5558,7 @@ public: }; my_type& operator=(my_type&&) = default; - hashStrMap(std::initializer_list, T>>&& init) { + hashStrMap(std::initializer_list>&& init) { for (const auto& e: init) emplace(e.first, e.second); } @@ -5576,7 +5572,7 @@ public: // При входе хэш должен быть уже посчитан template - auto try_emplace(const StoreType& key, ValArgs&&... args) { + auto try_emplace(const InStore& key, ValArgs&&... args) { auto it = hash_t::try_emplace(key, std::forward(args)...); if (it.second) { InStore& stored = const_cast(it.first->first); @@ -5586,7 +5582,7 @@ public: return it; } - static StoreType toStoreType(simple_str key) { + static InStore toStoreType(simple_str key) { return {key, H{}(key)}; } @@ -5603,7 +5599,7 @@ public: } template - auto emplace(const StoreType& key, ValArgs&&... args) { + auto emplace(const InStore& key, ValArgs&&... args) { auto it = try_emplace(key, std::forward(args)...); if (!it.second) { it.first->second = T(std::forward(args)...); @@ -5621,7 +5617,7 @@ public: return it; } - auto& operator[](const StoreType& key) { + auto& operator[](const InStore& key) { return try_emplace(key).first->second; } @@ -5631,10 +5627,10 @@ public: return try_emplace(std::forward(key)).first->second; } - decltype(auto) at(const StoreType& key) { + decltype(auto) at(const InStore& key) { return hash_t::at(key); } - decltype(auto) at(const StoreType& key) const { + decltype(auto) at(const InStore& key) const { return hash_t::at(key); } @@ -5645,7 +5641,7 @@ public: return hash_t::at(toStoreType(key)); } - auto find(const StoreType& key) const { + auto find(const InStore& key) const { return hash_t::find(key); } @@ -5653,7 +5649,7 @@ public: return find(toStoreType(key)); } - auto find(const StoreType& key) { + auto find(const InStore& key) { return hash_t::find(key); } @@ -5668,7 +5664,7 @@ public: return hash_t::erase(it); } - auto erase(const StoreType& key) { + auto erase(const InStore& key) { auto it = hash_t::find(key); if (it != hash_t::end()) { ((sstring*)it->first.node)->~sstring(); @@ -5705,7 +5701,7 @@ public: ((sstring*)k.first.node)->~sstring(); hash_t::clear(); } - bool contains(const StoreType& key) const { + bool contains(const InStore& key) const { return hash_t::find(key) != this->end(); } @@ -5716,7 +5712,8 @@ public: template struct streql { - bool operator()(const StoreType& _Left, const StoreType& _Right) const { + template + bool operator()(const StoreType& _Left, const StoreType& _Right) const { return _Left.hash == _Right.hash && _Left.str == _Right.str; } }; @@ -5726,14 +5723,16 @@ struct strhash { // hash functor for basic_string size_t operator()(simple_str _Keyval) const { return fnv_hash(_Keyval.symbols(), _Keyval.length()); } - size_t operator()(const StoreType& _Keyval) const { + template + size_t operator()(const StoreType& _Keyval) const { return _Keyval.hash; } }; template struct streqlia { - bool operator()(const StoreType& _Left, const StoreType& _Right) const { + template + bool operator()(const StoreType& _Left, const StoreType& _Right) const { return _Left.hash == _Right.hash && _Left.str.equal_ia(_Right.str); } }; @@ -5743,14 +5742,16 @@ struct strhashia { size_t operator()(simple_str _Keyval) const { return fnv_hash_ia(_Keyval.symbols(), _Keyval.length()); } - size_t operator()(const StoreType& _Keyval) const { + template + size_t operator()(const StoreType& _Keyval) const { return _Keyval.hash; } }; template struct streqliu { - bool operator()(const StoreType& _Left, const StoreType& _Right) const { + template + bool operator()(const StoreType& _Left, const StoreType& _Right) const { return _Left.hash == _Right.hash && _Left.str.equal_iu(_Right.str); } }; @@ -5760,7 +5761,8 @@ struct strhashiu { size_t operator()(simple_str _Keyval) const { return unicode_traits::hashiu(_Keyval.symbols(), _Keyval.length()); } - size_t operator()(const StoreType& _Keyval) const { + template + size_t operator()(const StoreType& _Keyval) const { return _Keyval.hash; } }; @@ -6116,8 +6118,8 @@ SS_CONSTEVAL simple_str_nt operator""_ss(const u32s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_h(const u8s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_compile(ptr, l)}; +consteval StoreType> operator""_h(const u8s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_compile(ptr, l)}; } /*! @@ -6126,8 +6128,8 @@ consteval StoreType operator""_h(const u8s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_ia(const u8s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; +consteval StoreType> operator""_ia(const u8s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; } /*! @@ -6136,8 +6138,8 @@ consteval StoreType operator""_ia(const u8s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -inline StoreType operator""_iu(const u8s* ptr, size_t l) { - return StoreType{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; +inline StoreType> operator""_iu(const u8s* ptr, size_t l) { + return StoreType>{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; } /*! @@ -6146,8 +6148,8 @@ inline StoreType operator""_iu(const u8s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_h(const u16s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_compile(ptr, l)}; +consteval StoreType> operator""_h(const u16s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_compile(ptr, l)}; } /*! @@ -6156,8 +6158,8 @@ consteval StoreType operator""_h(const u16s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_ia(const u16s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; +consteval StoreType> operator""_ia(const u16s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; } /*! @@ -6166,8 +6168,8 @@ consteval StoreType operator""_ia(const u16s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -inline StoreType operator""_iu(const u16s* ptr, size_t l) { - return StoreType{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; +inline StoreType> operator""_iu(const u16s* ptr, size_t l) { + return StoreType>{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; } /*! @@ -6176,8 +6178,8 @@ inline StoreType operator""_iu(const u16s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_h(const u32s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_compile(ptr, l)}; +consteval StoreType> operator""_h(const u32s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_compile(ptr, l)}; } /*! @@ -6186,8 +6188,8 @@ consteval StoreType operator""_h(const u32s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -consteval StoreType operator""_ia(const u32s* ptr, size_t l) { - return StoreType{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; +consteval StoreType> operator""_ia(const u32s* ptr, size_t l) { + return StoreType>{{ptr, l}, fnv_hash_ia_compile(ptr, l)}; } /*! @@ -6196,8 +6198,8 @@ consteval StoreType operator""_ia(const u32s* ptr, size_t l) { * @param l - длина строки * @return StoreType */ -inline StoreType operator""_iu(const u32s* ptr, size_t l) { - return StoreType{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; +inline StoreType> operator""_iu(const u32s* ptr, size_t l) { + return StoreType>{{ptr, l}, strhashiu{}(simple_str{ptr, l})}; } } // namespace literals