barretenberg
Loading...
Searching...
No Matches
constexpr_utils.hpp
1#pragma once
2
3#include <cstddef>
4#include <tuple>
5#include <utility>
6
16namespace barretenberg {
17
65template <size_t Start, size_t End, size_t Inc, class F> constexpr void constexpr_for(F&& f)
66{
67 // Call function `f<Start>()` iff Start < End
68 if constexpr (Start < End) {
69 // F must be a template lambda with a single **typed** template parameter that represents the iterator
70 // (e.g. [&]<size_t i>(){ ... } is good)
71 // (and [&]<typename i>(){ ... } won't compile!)
72
92 f.template operator()<Start>();
93
94 // Once we have executed `f`, we recursively call the `constexpr_for` function, increasing the value of `Start`
95 // by `Inc`
96 constexpr_for<Start + Inc, End, Inc>(f);
97 }
98}
99
110template <const auto& container, auto key> constexpr bool constexpr_find()
111{
112 // using ElementType = typename std::remove_extent<ContainerType>::type;
113 bool found = false;
114 constexpr_for<0, container.size(), 1>([&]<size_t k>() {
115 if constexpr (std::get<k>(container) == key) {
116 found = true;
117 }
118 });
119 return found;
120}
121
140template <typename T, std::size_t... Is>
141constexpr std::array<T, sizeof...(Is)> create_array(T value, std::index_sequence<Is...> /*unused*/)
142{
143 // cast Is to void to remove the warning: unused value
144 std::array<T, sizeof...(Is)> result = { { (static_cast<void>(Is), value)... } };
145 return result;
146}
147
158template <typename T, size_t N> constexpr std::array<T, N> create_empty_array()
159{
160 return create_array(T(0), std::make_index_sequence<N>());
161}
162}; // namespace barretenberg
constexpr_utils defines some helper methods that perform some stl-equivalent operations but in a cons...
Definition: constexpr_utils.hpp:16
constexpr bool constexpr_find()
returns true/false depending on whether key is in container
Definition: constexpr_utils.hpp:110
constexpr std::array< T, sizeof...(Is)> create_array(T value, std::index_sequence< Is... >)
Create a constexpr array object whose elements contain a default value.
Definition: constexpr_utils.hpp:141
constexpr void constexpr_for(F &&f)
Implements a loop using a compile-time iterator. Requires c++20. Implementation (and description) fro...
Definition: constexpr_utils.hpp:65
constexpr std::array< T, N > create_empty_array()
Create a constexpr array object whose values all are 0.
Definition: constexpr_utils.hpp:158