aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/enum_indexed_array.h
diff options
context:
space:
mode:
authorbabenko <babenko@yandex-team.com>2024-01-25 19:09:19 +0300
committerbabenko <babenko@yandex-team.com>2024-01-25 20:01:47 +0300
commit3fa3bc3d67f3e77fce7b45a7fc7608ef9a3131bb (patch)
tree8c5059e4aabe5be183a5a0b9fc851d12a086e568 /library/cpp/yt/misc/enum_indexed_array.h
parent4d0fd29627f04e0419f75981e41efb2015daf093 (diff)
downloadydb-3fa3bc3d67f3e77fce7b45a7fc7608ef9a3131bb.tar.gz
Introduce TEnumIndexedArray as a refurbished version of TEnumIndexedVector
Diffstat (limited to 'library/cpp/yt/misc/enum_indexed_array.h')
-rw-r--r--library/cpp/yt/misc/enum_indexed_array.h64
1 files changed, 64 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/enum_indexed_array.h b/library/cpp/yt/misc/enum_indexed_array.h
new file mode 100644
index 0000000000..8992456277
--- /dev/null
+++ b/library/cpp/yt/misc/enum_indexed_array.h
@@ -0,0 +1,64 @@
+#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_