diff options
author | George L. Albany <Megacake1234@gmail.com> | 2023-10-12 16:21:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-12 16:21:38 +0200 |
commit | bb22324da1225a0ac458c1d69893bb3bd28bd6b7 (patch) | |
tree | efeebd80a707a07eda296883772140e9ce32a9ee /src/openvic-simulation/utility | |
parent | 1d113b46161f27551bc3a6a857b8727cfb657b81 (diff) | |
parent | 5c7af98e3a8a3e7f1462e389c273566d7cdaa5d4 (diff) |
Merge pull request #43 from OpenVicProject/add/vdf-search
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r-- | src/openvic-simulation/utility/ConstexprIntToStr.hpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/openvic-simulation/utility/ConstexprIntToStr.hpp b/src/openvic-simulation/utility/ConstexprIntToStr.hpp new file mode 100644 index 0000000..e383365 --- /dev/null +++ b/src/openvic-simulation/utility/ConstexprIntToStr.hpp @@ -0,0 +1,56 @@ +#pragma once + +#include <array> +#include <cstddef> +#include <string_view> +#include <utility> + +namespace OpenVic::ConstexprIntToStr { + template<class T, T... values1, T... values2> + constexpr decltype(auto) append_sequence(std::integer_sequence<T, values1...>, std::integer_sequence<T, values2...>) { + return std::integer_sequence<T, values1..., values2...> {}; + } + + template<class sequence_t> + struct string_sequence_to_view; + + template<char... chars> + struct string_sequence_to_view<std::integer_sequence<char, chars...>> { + static constexpr decltype(auto) get() { + return std::string_view { c_str }; + } + + static constexpr const char c_str[] { chars..., char {} }; + }; + + template<std::size_t value> + constexpr decltype(auto) integer_to_string_sequence() { + constexpr auto digits = []() { + return "0123456789abcdefghijklmnopqrstuvwxyz"; + }; + + constexpr std::size_t remainder = value % 10; + constexpr std::size_t next_value = value / 10; + + if constexpr (next_value != 0) { + return append_sequence(integer_to_string_sequence<next_value>(), std::integer_sequence<char, digits()[remainder]> {}); + } else { + return std::integer_sequence<char, digits()[remainder]> {}; + } + } + template<std::size_t i> + constexpr std::string_view make_string() { + return string_sequence_to_view<decltype(integer_to_string_sequence<i>())> {}.c_str; + } + + template<std::size_t... ManyIntegers> + constexpr auto generate_itosv_array(std::integer_sequence<std::size_t, ManyIntegers...>) { + return std::array<std::string_view, sizeof...(ManyIntegers)> { make_string<ManyIntegers>()... }; + } + + // Make array of N string views, countings up from 0 to N - 1 + template<std::size_t N> + constexpr auto make_itosv_array() { + return generate_itosv_array(std::make_integer_sequence<std::size_t, N>()); + } +}
\ No newline at end of file |