aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/misc/enum_indexed_array-inl.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-inl.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-inl.h')
-rw-r--r--library/cpp/yt/misc/enum_indexed_array-inl.h73
1 files changed, 73 insertions, 0 deletions
diff --git a/library/cpp/yt/misc/enum_indexed_array-inl.h b/library/cpp/yt/misc/enum_indexed_array-inl.h
new file mode 100644
index 0000000000..edda891683
--- /dev/null
+++ b/library/cpp/yt/misc/enum_indexed_array-inl.h
@@ -0,0 +1,73 @@
+#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