diff options
author | Alexander Smirnov <alex@ydb.tech> | 2024-01-31 17:22:33 +0300 |
---|---|---|
committer | Alexander Smirnov <alex@ydb.tech> | 2024-01-31 17:22:33 +0300 |
commit | 52be5dbdd420165c68e7e90ba8f1d2f00da041f6 (patch) | |
tree | 5d47f5b2ff4e6a7c8e75d33931a1e683949b7229 /library/cpp/yt/misc | |
parent | ea57c8867ceca391357c3c5ffcc5ba6738b49adc (diff) | |
parent | 809f0cf2fdfddfbeacc2256ffdbaaf5808ce5ed4 (diff) | |
download | ydb-52be5dbdd420165c68e7e90ba8f1d2f00da041f6.tar.gz |
Merge branch 'mergelibs12' into main
Diffstat (limited to 'library/cpp/yt/misc')
-rw-r--r-- | library/cpp/yt/misc/enum-inl.h | 61 | ||||
-rw-r--r-- | library/cpp/yt/misc/enum.h | 46 | ||||
-rw-r--r-- | library/cpp/yt/misc/enum_indexed_array-inl.h | 73 | ||||
-rw-r--r-- | library/cpp/yt/misc/enum_indexed_array.h | 64 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/enum_indexed_array_ut.cpp | 45 | ||||
-rw-r--r-- | library/cpp/yt/misc/unittests/ya.make | 1 | ||||
-rw-r--r-- | library/cpp/yt/misc/ya.make | 1 |
7 files changed, 1 insertions, 290 deletions
diff --git a/library/cpp/yt/misc/enum-inl.h b/library/cpp/yt/misc/enum-inl.h index 02493d7ac5..5e1d04da1f 100644 --- a/library/cpp/yt/misc/enum-inl.h +++ b/library/cpp/yt/misc/enum-inl.h @@ -302,67 +302,6 @@ T TEnumTraits<T, true>::FromString(TStringBuf literal) //////////////////////////////////////////////////////////////////////////////// -template <class E, class T, E Min, E Max> -constexpr TEnumIndexedVector<E, T, Min, Max>::TEnumIndexedVector() - : Items_{} -{ } - -template <class E, class T, E Min, E Max> -constexpr TEnumIndexedVector<E, T, Min, Max>::TEnumIndexedVector(std::initializer_list<T> elements) - : Items_{} -{ - Y_ASSERT(std::distance(elements.begin(), elements.end()) <= N); - size_t index = 0; - for (const auto& element : elements) { - Items_[index++] = element; - } -} - -template <class E, class T, E Min, E Max> -T& TEnumIndexedVector<E, T, Min, Max>::operator[] (E index) -{ - Y_ASSERT(index >= Min && index <= Max); - return Items_[ToUnderlying(index) - ToUnderlying(Min)]; -} - -template <class E, class T, E Min, E Max> -const T& TEnumIndexedVector<E, T, Min, Max>::operator[] (E index) const -{ - return const_cast<TEnumIndexedVector&>(*this)[index]; -} - -template <class E, class T, E Min, E Max> -T* TEnumIndexedVector<E, T, Min, Max>::begin() -{ - return Items_.data(); -} - -template <class E, class T, E Min, E Max> -const T* TEnumIndexedVector<E, T, Min, Max>::begin() const -{ - return Items_.data(); -} - -template <class E, class T, E Min, E Max> -T* TEnumIndexedVector<E, T, Min, Max>::end() -{ - return begin() + N; -} - -template <class E, class T, E Min, E Max> -const T* TEnumIndexedVector<E, T, Min, Max>::end() const -{ - return begin() + N; -} - -template <class E, class T, E Min, E Max> -bool TEnumIndexedVector<E, T, Min, Max>::IsDomainValue(E value) -{ - return value >= Min && value <= Max; -} - -//////////////////////////////////////////////////////////////////////////////// - #define ENUM__BINARY_BITWISE_OPERATOR(T, assignOp, op) \ [[maybe_unused]] inline constexpr T operator op (T lhs, T rhs) \ { \ diff --git a/library/cpp/yt/misc/enum.h b/library/cpp/yt/misc/enum.h index 9da79b9fd3..954b63cbc0 100644 --- a/library/cpp/yt/misc/enum.h +++ b/library/cpp/yt/misc/enum.h @@ -185,52 +185,6 @@ struct TEnumTraits<T, true> //////////////////////////////////////////////////////////////////////////////// -// TODO(babenko): drop in favor of TEnumIndexedArray -//! A statically sized vector with elements of type |T| indexed by -//! the items of enumeration type |E|. -/*! - * Items are value-initialized on construction. - */ -template < - class E, - class T, - E Min = TEnumTraits<E>::GetMinValue(), - E Max = TEnumTraits<E>::GetMaxValue() -> -class TEnumIndexedVector -{ -public: - using TIndex = E; - using TValue = T; - - constexpr TEnumIndexedVector(); - constexpr TEnumIndexedVector(std::initializer_list<T> elements); - - constexpr TEnumIndexedVector(const TEnumIndexedVector&) = default; - constexpr TEnumIndexedVector(TEnumIndexedVector&&) noexcept = default; - - constexpr TEnumIndexedVector& operator=(const TEnumIndexedVector&) = default; - constexpr TEnumIndexedVector& operator=(TEnumIndexedVector&&) noexcept = default; - - T& operator[] (E index); - const T& operator[] (E index) const; - - // STL interop. - T* begin(); - const T* begin() const; - T* end(); - const T* end() const; - - static bool IsDomainValue(E value); - -private: - using TUnderlying = std::underlying_type_t<E>; - static constexpr int N = static_cast<TUnderlying>(Max) - static_cast<TUnderlying>(Min) + 1; - std::array<T, N> Items_; -}; - -//////////////////////////////////////////////////////////////////////////////// - //! Returns |true| iff the enumeration value is not bitwise zero. template <typename E> requires TEnumTraits<E>::IsBitEnum diff --git a/library/cpp/yt/misc/enum_indexed_array-inl.h b/library/cpp/yt/misc/enum_indexed_array-inl.h deleted file mode 100644 index edda891683..0000000000 --- a/library/cpp/yt/misc/enum_indexed_array-inl.h +++ /dev/null @@ -1,73 +0,0 @@ -#pragma once -#ifndef ENUM_INDEXED_ARRAY_INL_H_ -#error "Direct inclusion of this file is not allowed, include enum.h" -// For the sake of sane code completion. -#include "enum_indexed_array.h" -#endif - -#include <library/cpp/yt/assert/assert.h> - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -template <class E, class T, E Min, E Max> -TEnumIndexedArray<E, T, Min, Max>::TEnumIndexedArray(std::initializer_list<std::pair<E, T>> elements) -{ - for (const auto& [index, value] : elements) { - (*this)[index] = value; - } -} - -template <class E, class T, E Min, E Max> -T& TEnumIndexedArray<E, T, Min, Max>::operator[] (E index) -{ - YT_ASSERT(IsValidIndex(index)); - return Items_[ToUnderlying(index) - ToUnderlying(Min)]; -} - -template <class E, class T, E Min, E Max> -const T& TEnumIndexedArray<E, T, Min, Max>::operator[] (E index) const -{ - return const_cast<TEnumIndexedArray&>(*this)[index]; -} - -template <class E, class T, E Min, E Max> -T* TEnumIndexedArray<E, T, Min, Max>::begin() -{ - return Items_.data(); -} - -template <class E, class T, E Min, E Max> -const T* TEnumIndexedArray<E, T, Min, Max>::begin() const -{ - return Items_.data(); -} - -template <class E, class T, E Min, E Max> -T* TEnumIndexedArray<E, T, Min, Max>::end() -{ - return begin() + Size; -} - -template <class E, class T, E Min, E Max> -const T* TEnumIndexedArray<E, T, Min, Max>::end() const -{ - return begin() + Size; -} - -template <class E, class T, E Min, E Max> -constexpr size_t TEnumIndexedArray<E, T, Min, Max>::size() const -{ - return Size; -} - -template <class E, class T, E Min, E Max> -bool TEnumIndexedArray<E, T, Min, Max>::IsValidIndex(E index) -{ - return index >= Min && index <= Max; -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT diff --git a/library/cpp/yt/misc/enum_indexed_array.h b/library/cpp/yt/misc/enum_indexed_array.h deleted file mode 100644 index 8992456277..0000000000 --- a/library/cpp/yt/misc/enum_indexed_array.h +++ /dev/null @@ -1,64 +0,0 @@ -#pragma once - -#include "enum.h" - -namespace NYT { - -//////////////////////////////////////////////////////////////////////////////// - -//! A statically sized vector with elements of type |T| indexed by -//! the items of enumeration type |E|. -/*! - * By default, valid indexes are in range from the minimum declared value of |E| - * to the maximum declared value of |E|. - * - * Items are value-initialized on construction. - */ -template < - class E, - class T, - E Min = TEnumTraits<E>::GetMinValue(), - E Max = TEnumTraits<E>::GetMaxValue() -> -class TEnumIndexedArray -{ -public: - static_assert(Min <= Max); - - using TIndex = E; - using TValue = T; - - constexpr TEnumIndexedArray() = default; - TEnumIndexedArray(std::initializer_list<std::pair<E, T>> elements); - - constexpr TEnumIndexedArray(const TEnumIndexedArray&) = default; - constexpr TEnumIndexedArray(TEnumIndexedArray&&) = default; - - constexpr TEnumIndexedArray& operator=(const TEnumIndexedArray&) = default; - constexpr TEnumIndexedArray& operator=(TEnumIndexedArray&&) = default; - - T& operator[] (E index); - const T& operator[] (E index) const; - - // STL interop. - T* begin(); - const T* begin() const; - T* end(); - const T* end() const; - - constexpr size_t size() const; - - static bool IsValidIndex(E index); - -private: - static constexpr size_t Size = static_cast<size_t>(Max) - static_cast<size_t>(Min) + 1; - std::array<T, Size> Items_{}; -}; - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace NYT - -#define ENUM_INDEXED_ARRAY_INL_H_ -#include "enum_indexed_array-inl.h" -#undef ENUM_INDEXED_ARRAY_INL_H_ diff --git a/library/cpp/yt/misc/unittests/enum_indexed_array_ut.cpp b/library/cpp/yt/misc/unittests/enum_indexed_array_ut.cpp deleted file mode 100644 index 4111c6f4fe..0000000000 --- a/library/cpp/yt/misc/unittests/enum_indexed_array_ut.cpp +++ /dev/null @@ -1,45 +0,0 @@ -#include <library/cpp/testing/gtest/gtest.h> - -#include <library/cpp/yt/misc/enum_indexed_array.h> - -namespace NYT { -namespace { - -//////////////////////////////////////////////////////////////////////////////// - -DEFINE_ENUM(EColor, - ((Red) (10)) - ((Green)(20)) - ((Blue) (30)) -); - -TEST(TEnumIndexedArrayTest, Size) -{ - TEnumIndexedArray<EColor, int> arr; - EXPECT_EQ(std::ssize(arr), 21); -} - -TEST(TEnumIndexedArrayTest, IsValidIndex) -{ - TEnumIndexedArray<EColor, int> arr; - EXPECT_TRUE(arr.IsValidIndex(EColor::Red)); - EXPECT_TRUE(arr.IsValidIndex(EColor::Green)); - EXPECT_TRUE(arr.IsValidIndex(EColor::Blue)); - EXPECT_TRUE(arr.IsValidIndex(static_cast<EColor>(11))); - EXPECT_FALSE(arr.IsValidIndex(static_cast<EColor>(9))); -} - -TEST(TEnumIndexedArrayTest, Simple) -{ - TEnumIndexedArray<EColor, int> arr; - EXPECT_EQ(arr[EColor::Red], 0); - arr[EColor::Red] = 1; - EXPECT_EQ(arr[EColor::Red], 1); - EXPECT_EQ(arr[EColor::Blue], 0); -} - -//////////////////////////////////////////////////////////////////////////////// - -} // namespace -} // namespace NYT - diff --git a/library/cpp/yt/misc/unittests/ya.make b/library/cpp/yt/misc/unittests/ya.make index 43e9f9b9ff..611edd7217 100644 --- a/library/cpp/yt/misc/unittests/ya.make +++ b/library/cpp/yt/misc/unittests/ya.make @@ -4,7 +4,6 @@ INCLUDE(${ARCADIA_ROOT}/library/cpp/yt/ya_cpp.make.inc) SRCS( enum_ut.cpp - enum_indexed_array_ut.cpp guid_ut.cpp preprocessor_ut.cpp strong_typedef_ut.cpp diff --git a/library/cpp/yt/misc/ya.make b/library/cpp/yt/misc/ya.make index 586d14f6ee..841930aaa2 100644 --- a/library/cpp/yt/misc/ya.make +++ b/library/cpp/yt/misc/ya.make @@ -11,6 +11,7 @@ SRCS( PEERDIR( library/cpp/yt/exception + library/cpp/yt/assert ) CHECK_DEPENDENT_DIRS( |