enum_indexed_array.h 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #pragma once
  2. #include <library/cpp/yt/misc/enum.h>
  3. namespace NYT {
  4. ////////////////////////////////////////////////////////////////////////////////
  5. //! A statically sized vector with elements of type |T| indexed by
  6. //! the items of enumeration type |E|.
  7. /*!
  8. * By default, valid indexes are in range from the minimum declared value of |E|
  9. * to the maximum declared value of |E|.
  10. *
  11. * Items are value-initialized on construction.
  12. */
  13. template <
  14. class E,
  15. class T,
  16. E Min = TEnumTraits<E>::GetMinValue(),
  17. E Max = TEnumTraits<E>::GetMaxValue()
  18. >
  19. class TEnumIndexedArray
  20. {
  21. public:
  22. static_assert(Min <= Max);
  23. using TIndex = E;
  24. using TValue = T;
  25. constexpr TEnumIndexedArray() = default;
  26. TEnumIndexedArray(std::initializer_list<std::pair<E, T>> elements);
  27. constexpr TEnumIndexedArray(const TEnumIndexedArray&) = default;
  28. constexpr TEnumIndexedArray(TEnumIndexedArray&&) = default;
  29. constexpr TEnumIndexedArray& operator=(const TEnumIndexedArray&) = default;
  30. constexpr TEnumIndexedArray& operator=(TEnumIndexedArray&&) = default;
  31. T& operator[] (E index);
  32. const T& operator[] (E index) const;
  33. // STL interop.
  34. T* begin();
  35. const T* begin() const;
  36. T* end();
  37. const T* end() const;
  38. constexpr size_t size() const;
  39. static bool IsValidIndex(E index);
  40. private:
  41. static constexpr size_t Size = static_cast<size_t>(Max) - static_cast<size_t>(Min) + 1;
  42. std::array<T, Size> Items_{};
  43. };
  44. ////////////////////////////////////////////////////////////////////////////////
  45. } // namespace NYT
  46. #define ENUM_INDEXED_ARRAY_INL_H_
  47. #include "enum_indexed_array-inl.h"
  48. #undef ENUM_INDEXED_ARRAY_INL_H_