diff options
author | dgolear <[email protected]> | 2025-07-10 22:33:21 +0300 |
---|---|---|
committer | dgolear <[email protected]> | 2025-07-10 22:56:45 +0300 |
commit | 5f7c8cead6f7a3f7bea3f01cb168eb1f7a926879 (patch) | |
tree | 96517df0eaef6d7553ad127257902f0ff0707e4a | |
parent | 612146bc655489da23249308c666ca9eb2ef6509 (diff) |
YT: Allow serializing TCompactSet
commit_hash:733610a293a0c3a2656dbef77630d359c16d3bdf
-rw-r--r-- | library/cpp/yt/compact_containers/compact_flat_map-inl.h | 18 | ||||
-rw-r--r-- | library/cpp/yt/compact_containers/compact_flat_map.h | 5 | ||||
-rw-r--r-- | library/cpp/yt/compact_containers/compact_set-inl.h | 28 | ||||
-rw-r--r-- | library/cpp/yt/compact_containers/compact_set.h | 17 | ||||
-rw-r--r-- | yt/yt/core/misc/mpl.h | 13 | ||||
-rw-r--r-- | yt/yt/core/yson/pull_parser_deserialize-inl.h | 6 | ||||
-rw-r--r-- | yt/yt/core/yson/pull_parser_deserialize.h | 6 | ||||
-rw-r--r-- | yt/yt/core/ytree/serialize-inl.h | 15 | ||||
-rw-r--r-- | yt/yt/core/ytree/serialize.h | 14 | ||||
-rw-r--r-- | yt/yt/core/ytree/unittests/serialize_ut.cpp | 13 | ||||
-rw-r--r-- | yt/yt/core/ytree/yson_schema-inl.h | 8 |
11 files changed, 87 insertions, 56 deletions
diff --git a/library/cpp/yt/compact_containers/compact_flat_map-inl.h b/library/cpp/yt/compact_containers/compact_flat_map-inl.h index e781569d9da..7bc52cbb1bb 100644 --- a/library/cpp/yt/compact_containers/compact_flat_map-inl.h +++ b/library/cpp/yt/compact_containers/compact_flat_map-inl.h @@ -21,18 +21,6 @@ TCompactFlatMap<TKey, TValue, N, TKeyCompare>::TCompactFlatMap(std::initializer_ { } template <class TKey, class TValue, size_t N, class TKeyCompare> -bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator==(const TCompactFlatMap& rhs) const -{ - return Storage_ == rhs.Storage_; -} - -template <class TKey, class TValue, size_t N, class TKeyCompare> -bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::operator!=(const TCompactFlatMap& rhs) const -{ - return !(*this == rhs); -} - -template <class TKey, class TValue, size_t N, class TKeyCompare> typename TCompactFlatMap<TKey, TValue, N, TKeyCompare>::iterator TCompactFlatMap<TKey, TValue, N, TKeyCompare>::begin() { return Storage_.begin(); @@ -130,18 +118,18 @@ bool TCompactFlatMap<TKey, TValue, N, TKeyCompare>::contains(const TOtherKey& k) template <class TKey, class TValue, size_t N, class TKeyCompare> auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(const value_type& value) -> std::pair<iterator, bool> { - return do_insert(value); + return DoInsert(value); } template <class TKey, class TValue, size_t N, class TKeyCompare> auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::insert(value_type&& value) -> std::pair<iterator, bool> { - return do_insert(std::move(value)); + return DoInsert(std::move(value)); } template <class TKey, class TValue, size_t N, class TKeyCompare> template <class TArg> -auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::do_insert(TArg&& value) -> std::pair<iterator, bool> +auto TCompactFlatMap<TKey, TValue, N, TKeyCompare>::DoInsert(TArg&& value) -> std::pair<iterator, bool> { auto [rangeBegin, rangeEnd] = equal_range(value.first); if (rangeBegin != rangeEnd) { diff --git a/library/cpp/yt/compact_containers/compact_flat_map.h b/library/cpp/yt/compact_containers/compact_flat_map.h index 49c8628c46c..65e3f2bff4b 100644 --- a/library/cpp/yt/compact_containers/compact_flat_map.h +++ b/library/cpp/yt/compact_containers/compact_flat_map.h @@ -61,8 +61,7 @@ public: TCompactFlatMap(std::initializer_list<value_type> values); - bool operator==(const TCompactFlatMap& rhs) const; - bool operator!=(const TCompactFlatMap& rhs) const; + bool operator==(const TCompactFlatMap& rhs) const = default; iterator begin(); const_iterator begin() const; @@ -122,7 +121,7 @@ private: TStorage Storage_; template <class TArg> - std::pair<iterator, bool> do_insert(TArg&& value); + std::pair<iterator, bool> DoInsert(TArg&& value); }; //////////////////////////////////////////////////////////////////////////////// diff --git a/library/cpp/yt/compact_containers/compact_set-inl.h b/library/cpp/yt/compact_containers/compact_set-inl.h index 20736f50833..9842d08fe03 100644 --- a/library/cpp/yt/compact_containers/compact_set-inl.h +++ b/library/cpp/yt/compact_containers/compact_set-inl.h @@ -208,6 +208,34 @@ TCompactSet<T, N, C, A>::TCompactSet(const A& allocator) { } template <typename T, size_t N, typename C, typename A> +template <typename TIterator> +TCompactSet<T, N, C, A>::TCompactSet(TIterator first, TIterator last) +{ + insert(first, last); +} + +template <typename T, size_t N, typename C, typename A> +TCompactSet<T, N, C, A>::TCompactSet(std::initializer_list<key_type> values) +{ + insert(values.begin(), values.end()); +} + +template <typename T, size_t N, typename C, typename A> +bool TCompactSet<T, N, C, A>::operator==(const TCompactSet& rhs) const +{ + if (size() != rhs.size()) { + return false; + } + + for (const auto& value : *this) { + if (!rhs.contains(value)) { + return false; + } + } + return true; +} + +template <typename T, size_t N, typename C, typename A> bool TCompactSet<T, N, C, A>::empty() const { return Vector_.empty() && Set_.empty(); diff --git a/library/cpp/yt/compact_containers/compact_set.h b/library/cpp/yt/compact_containers/compact_set.h index 354fad195cd..ce89bfb66ae 100644 --- a/library/cpp/yt/compact_containers/compact_set.h +++ b/library/cpp/yt/compact_containers/compact_set.h @@ -41,26 +41,33 @@ public: class const_iterator; using size_type = std::size_t; using key_type = T; + using value_type = T; TCompactSet() = default; TCompactSet(const A& allocator); + template <class TIterator> + TCompactSet(TIterator first, TIterator last); + TCompactSet(std::initializer_list<key_type> values); + + bool operator==(const TCompactSet& rhs) const; + [[nodiscard]] bool empty() const; size_type size() const; const T& front() const; - size_type count(const T& v) const; + size_type count(const T& value) const; - bool contains(const T& v) const; + bool contains(const T& value) const; - std::pair<const_iterator, bool> insert(const T& v); + std::pair<const_iterator, bool> insert(const T& value); template <typename TIter> - void insert(TIter i, TIter e); + void insert(TIter first, TIter last); - bool erase(const T& v); + bool erase(const T& value); void clear(); diff --git a/yt/yt/core/misc/mpl.h b/yt/yt/core/misc/mpl.h index fbff7202fb8..d75b788c2b6 100644 --- a/yt/yt/core/misc/mpl.h +++ b/yt/yt/core/misc/mpl.h @@ -92,4 +92,17 @@ concept CDistinct = NDetail::DistinctImpl<Ts...>; //////////////////////////////////////////////////////////////////////////////// +template <class T> +concept CAssociative = requires(T) { + typename T::key_type; +}; + +template <class T> +concept CMapping = requires(T) { + CAssociative<T>; + typename T::mapped_type; +}; + +//////////////////////////////////////////////////////////////////////////////// + } // namespace NYT::NMpl diff --git a/yt/yt/core/yson/pull_parser_deserialize-inl.h b/yt/yt/core/yson/pull_parser_deserialize-inl.h index b42a1b7858e..f4bb341b3a2 100644 --- a/yt/yt/core/yson/pull_parser_deserialize-inl.h +++ b/yt/yt/core/yson/pull_parser_deserialize-inl.h @@ -287,11 +287,11 @@ void Deserialize(std::tuple<T...>& value, TYsonPullParserCursor* cursor, std::en } // For any associative container. -template <template<typename...> class C, class... T, class K> +template <NMpl::CAssociative TContainer> void Deserialize( - C<T...>& value, + TContainer& value, TYsonPullParserCursor* cursor, - std::enable_if_t<ArePullParserDeserializable<typename NDetail::TRemoveConst<typename C<T...>::value_type>::Type>(), void*>) + std::enable_if_t<ArePullParserDeserializable<typename NDetail::TRemoveConst<typename TContainer::value_type>::Type>(), void*>) { NDetail::DeserializeAssociative(value, cursor); } diff --git a/yt/yt/core/yson/pull_parser_deserialize.h b/yt/yt/core/yson/pull_parser_deserialize.h index 669b976394b..2c0c22c35da 100644 --- a/yt/yt/core/yson/pull_parser_deserialize.h +++ b/yt/yt/core/yson/pull_parser_deserialize.h @@ -146,11 +146,11 @@ void Deserialize( std::enable_if_t<ArePullParserDeserializable<T...>(), void*> = nullptr); // For any associative container. -template <template<typename...> class C, class... T, class K = typename C<T...>::key_type> +template <NMpl::CAssociative TContainer> void Deserialize( - C<T...>& value, + TContainer& value, TYsonPullParserCursor* cursor, - std::enable_if_t<ArePullParserDeserializable<typename NDetail::TRemoveConst<typename C<T...>::value_type>::Type>(), void*> = nullptr); + std::enable_if_t<ArePullParserDeserializable<typename NDetail::TRemoveConst<typename TContainer::value_type>::Type>(), void*> = nullptr); template <class E, class T, E Min, E Max> void Deserialize( diff --git a/yt/yt/core/ytree/serialize-inl.h b/yt/yt/core/ytree/serialize-inl.h index 26639c20aa0..31de68160d0 100644 --- a/yt/yt/core/ytree/serialize-inl.h +++ b/yt/yt/core/ytree/serialize-inl.h @@ -450,15 +450,8 @@ void Serialize(const std::tuple<T...>& value, NYson::IYsonConsumer* consumer) // TODO(eshcherbin): Add a concept for associative containers. // Any associative container (except TCompactFlatMap). -template <template<typename...> class C, class... T, class K> -void Serialize(const C<T...>& value, NYson::IYsonConsumer* consumer) -{ - NDetail::SerializeAssociative(value, consumer); -} - -// TCompactFlatMap. -template <class K, class V, size_t N> -void Serialize(const TCompactFlatMap<K, V, N>& value, NYson::IYsonConsumer* consumer) +template <NMpl::CAssociative TContainer> +void Serialize(const TContainer& value, NYson::IYsonConsumer* consumer) { NDetail::SerializeAssociative(value, consumer); } @@ -679,8 +672,8 @@ void Deserialize(std::tuple<T...>& value, INodePtr node) } // For any associative container. -template <template<typename...> class C, class... T, class K> -void Deserialize(C<T...>& value, INodePtr node) +template <NMpl::CAssociative TContainer> +void Deserialize(TContainer& value, INodePtr node) { NDetail::DeserializeAssociative(value, node); } diff --git a/yt/yt/core/ytree/serialize.h b/yt/yt/core/ytree/serialize.h index 00aedcbd08a..294289c7695 100644 --- a/yt/yt/core/ytree/serialize.h +++ b/yt/yt/core/ytree/serialize.h @@ -158,13 +158,9 @@ void Serialize(const std::array<T, N>& value, NYson::IYsonConsumer* consumer); template <class... T> void Serialize(const std::tuple<T...>& value, NYson::IYsonConsumer* consumer); -// Any associative container (except TCompactFlatMap). -template <template<typename...> class C, class... T, class K = typename C<T...>::key_type> -void Serialize(const C<T...>& value, NYson::IYsonConsumer* consumer); - -// TCompactFlatMap. -template <class K, class V, size_t N> -void Serialize(const TCompactFlatMap<K, V, N>& value, NYson::IYsonConsumer* consumer); +// Any associative container (except TCompactFlatMap/TCompactSet). +template <NMpl::CAssociative TContainer> +void Serialize(const TContainer& value, NYson::IYsonConsumer* consumer); // TEnumIndexedArray template <class E, class T, E Min, E Max> @@ -284,8 +280,8 @@ template <class... T> void Deserialize(std::tuple<T...>& value, INodePtr node); // For any associative container. -template <template<typename...> class C, class... T, class K = typename C<T...>::key_type> -void Deserialize(C<T...>& value, INodePtr node); +template <NMpl::CAssociative TContainer> +void Deserialize(TContainer& mapping, INodePtr node); // TEnumIndexedArray template <class E, class T, E Min, E Max> diff --git a/yt/yt/core/ytree/unittests/serialize_ut.cpp b/yt/yt/core/ytree/unittests/serialize_ut.cpp index 241e7bf86aa..851e82af462 100644 --- a/yt/yt/core/ytree/unittests/serialize_ut.cpp +++ b/yt/yt/core/ytree/unittests/serialize_ut.cpp @@ -277,6 +277,12 @@ TEST(TSerializationTest, Map) TestSerializationDeserialization(original, EYsonType::MapFragment); } +TEST(TSerializationTest, CompactMap) +{ + TCompactFlatMap<TString, size_t, 4> original{{"First", 12U}, {"Second", 7883U}, {"Third", 7U}}; + TestSerializationDeserialization(original); +} + TEST(TSerializationTest, Set) { std::set<TString> original{"First", "Second", "Third"}; @@ -284,6 +290,13 @@ TEST(TSerializationTest, Set) TestSerializationDeserialization(original, EYsonType::ListFragment); } +TEST(TSerializationTest, CompactSet) +{ + TCompactSet<TString, 4> original{"First", "Second", "Third"}; + TestSerializationDeserialization(original); + TestSerializationDeserialization(original, EYsonType::ListFragment); +} + TEST(TSerializationTest, MultiSet) { std::multiset<TString> original{"First", "Second", "Third", "Second", "Third", "Third"}; diff --git a/yt/yt/core/ytree/yson_schema-inl.h b/yt/yt/core/ytree/yson_schema-inl.h index b549521304d..c5d881754f9 100644 --- a/yt/yt/core/ytree/yson_schema-inl.h +++ b/yt/yt/core/ytree/yson_schema-inl.h @@ -38,13 +38,7 @@ template <class T> concept CArray = std::ranges::range<T> && !CTuple<T>; template <class T> -concept CMapping = requires(T) { - typename T::key_type; - typename T::mapped_type; -}; - -template <class T> -concept CAssociativeArray = CArray<T> && CMapping<T>; +concept CAssociativeArray = CArray<T> && NMpl::CMapping<T>; template <class T> concept CHasWriteSchema = requires ( |