diff options
author | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-06-01 15:35:19 +0300 |
---|---|---|
committer | arcadia-devtools <arcadia-devtools@yandex-team.ru> | 2022-06-01 15:35:19 +0300 |
commit | 8e7094c87fa7ad049e6ea600a5f90f1c97d683bd (patch) | |
tree | d3e66d7ea5843080db0f65be0b7752538f69a84b /tools/enum_parser/enum_serialization_runtime/ordered_pairs.h | |
parent | e13a5fc9fa49c7a6508a49f29e21a15fd9dac9ec (diff) | |
download | ydb-8e7094c87fa7ad049e6ea600a5f90f1c97d683bd.tar.gz |
intermediate changes
ref:b1e863e3aa12b888b40dd5ec36411ca050a25bd8
Diffstat (limited to 'tools/enum_parser/enum_serialization_runtime/ordered_pairs.h')
-rw-r--r-- | tools/enum_parser/enum_serialization_runtime/ordered_pairs.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/tools/enum_parser/enum_serialization_runtime/ordered_pairs.h b/tools/enum_parser/enum_serialization_runtime/ordered_pairs.h new file mode 100644 index 0000000000..874c709418 --- /dev/null +++ b/tools/enum_parser/enum_serialization_runtime/ordered_pairs.h @@ -0,0 +1,71 @@ +#pragma once + +#include <util/generic/array_ref.h> +#include <util/generic/strbuf.h> + +namespace NEnumSerializationRuntime { + enum class ESortOrder: int { + Unordered = 0, + Ascending = 1, + StrictlyAscending = 2, + DirectMapping = 3, + }; + + template <typename TEnumRepresentationType> + struct TEnumStringPair { + const TEnumRepresentationType Key; + const TStringBuf Name; + }; + + template <typename TEnumRepresentationType> + constexpr ESortOrder GetKeyFieldSortOrder(const TArrayRef<const TEnumStringPair<TEnumRepresentationType>> initializer) { + if (initializer.empty()) { + return ESortOrder::DirectMapping; + } + bool direct = true; + bool strict = true; + bool sorted = true; + TEnumRepresentationType expected = initializer.data()[0].Key; + for (size_t i = 1; i < initializer.size(); ++i) { + const auto& prev = initializer.data()[i - 1].Key; + const auto& next = initializer.data()[i - 0].Key; + if (++expected != next) { + direct = false; + } + if (prev >= next) { + strict = false; + } + if (prev > next) { + sorted = false; + break; + } + } + return direct ? ESortOrder::DirectMapping + : strict ? ESortOrder::StrictlyAscending + : sorted ? ESortOrder::Ascending + : ESortOrder::Unordered; + } + + template <typename TEnumRepresentationType> + constexpr ESortOrder GetNameFieldSortOrder(const TArrayRef<const TEnumStringPair<TEnumRepresentationType>> initializer) { + if (initializer.empty()) { + return ESortOrder::DirectMapping; + } + bool strict = true; + bool sorted = true; + for (size_t i = 1; i < initializer.size(); ++i) { + const std::string_view prev = initializer.data()[i - 1].Name; + const std::string_view next = initializer.data()[i - 0].Name; + if (prev >= next) { + strict = false; + } + if (prev > next) { + sorted = false; + break; + } + } + return strict ? ESortOrder::StrictlyAscending + : sorted ? ESortOrder::Ascending + : ESortOrder::Unordered; + } +} |