|
| 1 | +#ifndef _FPGEN_AGGREGATORS |
| 2 | +#define _FPGEN_AGGREGATORS |
| 3 | + |
| 4 | +#include "generator.hpp" |
| 5 | +#include <forward_list> |
| 6 | +#include <tuple> |
| 7 | +#include <type_traits> |
| 8 | + |
| 9 | +/** |
| 10 | + * \brief The namespace containing all of fpgen's code. |
| 11 | + */ |
| 12 | +namespace fpgen { |
| 13 | +/** |
| 14 | + * \brief Aggregates all data in the generator to a dataset. |
| 15 | + * |
| 16 | + * The supplied container should support `push_back` (like most of the `std::` |
| 17 | + * containers). Each element is extracted from the generator and inserted into |
| 18 | + * the container. The container is not cleared before inserting. Elements |
| 19 | + * already extracted (due to previous calls to the generator, ...) cannot be |
| 20 | + * reconstructed. |
| 21 | + * |
| 22 | + * \tparam T The type contained in the container and generator. |
| 23 | + * \tparam Args Other parameters to be passed to the container. |
| 24 | + * \tparam Container The container type to output to. |
| 25 | + * \param[in, out] gen The generator to extract from. |
| 26 | + * \param[out] out The container to output to. |
| 27 | + * \returns A reference to the modified container. |
| 28 | + */ |
| 29 | +template <typename T, typename... Args, |
| 30 | + template <typename...> typename Container> |
| 31 | +Container<T, Args...> &aggregate_to(generator<T> &gen, |
| 32 | + Container<T, Args...> &out) { |
| 33 | + while (gen) { |
| 34 | + out.push_back(gen()); |
| 35 | + } |
| 36 | + return out; |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * \brief Aggregates all data in the generator into an associative container. |
| 41 | + * |
| 42 | + * The supplied container should support `operator[]` (by reference, like |
| 43 | + * `std::map`). Duplicate key values will be be overwritten. The container is |
| 44 | + * not cleared before writing. Elements that have already been extracted from |
| 45 | + * the generator cannot be reconstructed. |
| 46 | + * |
| 47 | + * \tparam TKey The key type for the container. |
| 48 | + * \tparam TValue The value type for the container. |
| 49 | + * \tparam Args Other parameters to be passed to the container. |
| 50 | + * \tparam Container The container to be used. |
| 51 | + * \param[in, out] gen The generator to extract from. |
| 52 | + * \param[out] out The container to insert to. |
| 53 | + * \returns A reference to the modified container. |
| 54 | + */ |
| 55 | +template <typename TKey, typename TVal, typename... Args, |
| 56 | + template <typename...> typename Container> |
| 57 | +Container<TKey, TVal, Args...> & |
| 58 | +tup_aggregate_to(generator<std::tuple<TKey, TVal>> &gen, |
| 59 | + Container<TKey, TVal, Args...> &out) { |
| 60 | + while (gen) { |
| 61 | + std::tuple<TKey, TVal> tup = gen(); |
| 62 | + out[std::get<0>(tup)] = std::get<1>(tup); |
| 63 | + } |
| 64 | + return out; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * \brief Counts the amount of elements in the generator. |
| 69 | + * |
| 70 | + * Each element is extracted from the generator. These values are not |
| 71 | + * recoverable. Only values left in the generator are counted. Afterwards, the |
| 72 | + * generator will be empty. |
| 73 | + * |
| 74 | + * \tparam T The type of values contained in the generator. |
| 75 | + * \param[in,out] gen The generator to iterate over. |
| 76 | + * \returns The amount of elements in the generator. |
| 77 | + */ |
| 78 | +template <typename T> size_t count(generator<T> &gen) { |
| 79 | + size_t cnt = 0; |
| 80 | + while (gen) { |
| 81 | + gen(); |
| 82 | + cnt++; |
| 83 | + } |
| 84 | + return cnt; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * \brief Accumulates each value in the generator using the provided function. |
| 89 | + * |
| 90 | + * For each element in the generator, the provided function (which should have |
| 91 | + * (TOut, TIn) -> TOut) as signature is called. The result is stored in the |
| 92 | + * accumulator, which is passed down to the next value in the generator. Once |
| 93 | + * all values are extracted, the resulting accumulator is returned. The |
| 94 | + * accumulator is initialized using `TOut value = {};`. |
| 95 | + * |
| 96 | + * \tparam TOut The output type (accumulator type). |
| 97 | + * \tparam TIn The input type (type contained in the generator). |
| 98 | + * \tparam Fun The function type (should have the signature (TOut, TIn) -> |
| 99 | + * TOut). |
| 100 | + * \param[in,out] gen The generator to fold. |
| 101 | + * \param[in] folder The folding function. |
| 102 | + * \returns The final accumulator value. |
| 103 | + */ |
| 104 | +template <typename TOut, typename TIn, typename Fun> |
| 105 | +TOut fold(generator<TIn> &gen, Fun folder) { |
| 106 | + TOut value = {}; |
| 107 | + while (gen) { |
| 108 | + value = folder(value, gen()); |
| 109 | + } |
| 110 | + return value; |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * \brief Accumulates each value in the generator using the provided function. |
| 115 | + * |
| 116 | + * For each element in the generator, the provided function (which should have |
| 117 | + * (TOut, TIn) -> TOut) as signature is called. The result is stored in the |
| 118 | + * accumulator, which is passed down to the next value in the generator. Once |
| 119 | + * all values are extracted, the resulting accumulator is returned. The |
| 120 | + * accumulator is initialized using `TOut value(initial);`. |
| 121 | + * |
| 122 | + * \tparam TOut The output type (accumulator type). |
| 123 | + * \tparam TIn The input type (type contained in the generator). |
| 124 | + * \tparam Fun The function type (should have the signature (TOut, TIn) -> |
| 125 | + * TOut). |
| 126 | + * \param[in,out] gen The generator to fold. |
| 127 | + * \param[in] folder The folding function. |
| 128 | + * \param[in] initial The initial value for the accumulator (is copied). |
| 129 | + * \returns The final accumulator value. |
| 130 | + */ |
| 131 | +template <typename TOut, typename TIn, typename Fun> |
| 132 | +TOut fold(generator<TIn> &gen, Fun folder, TOut initial) { |
| 133 | + TOut value(initial); |
| 134 | + while (gen) { |
| 135 | + value = folder(value, gen()); |
| 136 | + } |
| 137 | + return value; |
| 138 | +} |
| 139 | + |
| 140 | +/** |
| 141 | + * \brief Accumulates each value in the generator using the provided function. |
| 142 | + * |
| 143 | + * For each element in the generator, the provided function (which should have |
| 144 | + * (TOut, TIn) -> TOut) as signature is called. The result is stored in the |
| 145 | + * provided accumulator, which is passed down to the next value in the |
| 146 | + * generator. Once all values are extracted, the resulting accumulator is |
| 147 | + * returned. Each step modifies the accumulator. |
| 148 | + * |
| 149 | + * \tparam TOut The output type (accumulator type). |
| 150 | + * \tparam TIn The input type (type contained in the generator). |
| 151 | + * \tparam Fun The function type (should have the signature (TOut, TIn) -> |
| 152 | + * TOut or (TOut &, TIn) -> TOut). |
| 153 | + * \param[in,out] gen The generator to fold. |
| 154 | + * \param[in] folder The folding function. |
| 155 | + * \param[out] initial The initial value for the accumulator (is overwritten). |
| 156 | + * \returns A reference to the value which was passed as initial value and is |
| 157 | + * now the output value. |
| 158 | + */ |
| 159 | +template <typename TOut, typename TIn, typename Fun> |
| 160 | +TOut &fold_ref(generator<TIn> &gen, Fun folder, TOut &initial) { |
| 161 | + while (gen) { |
| 162 | + initial = folder(initial, gen()); |
| 163 | + } |
| 164 | + return initial; |
| 165 | +} |
| 166 | + |
| 167 | +/** |
| 168 | + * \brief Sums each value in the generator. |
| 169 | + * |
| 170 | + * The type contained in the generator should support `operator+`. An initial |
| 171 | + * accumulator is constructed using `T accum = {}`. Each next value is added to |
| 172 | + * the accumulator, which is returned afterwards. |
| 173 | + * |
| 174 | + * \tparam T The type contained in the generator, should support `operator+`. |
| 175 | + * \param[in,out] gen The generator to sum over. |
| 176 | + * \returns The sum of all elements. |
| 177 | + */ |
| 178 | +template <typename T> T sum(generator<T> &gen) { |
| 179 | + T accum = {}; |
| 180 | + while (gen) { |
| 181 | + accum = accum + gen(); |
| 182 | + } |
| 183 | + return accum; |
| 184 | +} |
| 185 | + |
| 186 | +/** |
| 187 | + * \brief Loops over the generator, calling a function on each element in it. |
| 188 | + * |
| 189 | + * Each element is extracted from the generator and used only as function |
| 190 | + * argument. All other aggregators can be written as a combination of this |
| 191 | + * function and a lambda function. Afterwards, the generator will be empty. |
| 192 | + * |
| 193 | + * \tparam T The type of values contained in the generator. |
| 194 | + * \tparam Fun The function type of the callback. |
| 195 | + * \param[in,out] gen The generator to iterate over. |
| 196 | + * \param[in] func The function to use. |
| 197 | + */ |
| 198 | +template <typename T, typename Fun> void foreach (generator<T> &gen, Fun func) { |
| 199 | + while (gen) { |
| 200 | + func(gen()); |
| 201 | + } |
| 202 | +} |
| 203 | +} // namespace fpgen |
| 204 | + |
| 205 | +#endif |
0 commit comments