|
| 1 | +#ifndef _FPGEN_SOURCES |
| 2 | +#define _FPGEN_SOURCES |
| 3 | + |
| 4 | +#include "generator.hpp" |
| 5 | +#include <iostream> |
| 6 | +#include <iterator> |
| 7 | + |
| 8 | +/** |
| 9 | + * \brief The namespace containing all of fpgen's code. |
| 10 | + */ |
| 11 | +namespace fpgen { |
| 12 | +/** |
| 13 | + * \brief Creates a generator over a data source. |
| 14 | + * |
| 15 | + * The data source should have an iterator (using `std::begin` and `std::end`). |
| 16 | + * For most builtin containers (`std::vector`, ...) this is already satisfied. |
| 17 | + * For `std::map`, see fpgen::from_tup. |
| 18 | + * |
| 19 | + * \tparam T The type contained in the container. |
| 20 | + * \tparam TArgs Any other template parameters passed to the container. |
| 21 | + * \tparam Container The container type. |
| 22 | + * \param[in] cont The container to iterate over. |
| 23 | + * \returns A new generator which will iterate over the container. |
| 24 | + * \see fpgen::from_tup, fpgen::enumerate |
| 25 | + */ |
| 26 | +template <typename T, typename... TArgs, |
| 27 | + template <typename...> typename Container> |
| 28 | +generator<T> from(const Container<T, TArgs...> &cont) { |
| 29 | + for (auto it = std::begin(cont); it != std::end(cont); ++it) { |
| 30 | + co_yield *it; |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * \brief Creates a generator over a data source, with indexing. |
| 36 | + * |
| 37 | + * The data source does not have to allow indexing, but it is recommended for |
| 38 | + * repeatable behaviour. If the index is not needed, use fpgen::from. The data |
| 39 | + * source should support iterating (using `std::begin` and `std::end`). |
| 40 | + * |
| 41 | + * \tparam T The type contained in the container. |
| 42 | + * \tparam TArgs Any other template parameters passed to the container. |
| 43 | + * \tparam Container The container type. |
| 44 | + * \param[in] cont The container to iterate over. |
| 45 | + * \returns A new generator which will iterate over the container using index |
| 46 | + * and value. |
| 47 | + * \see fpgen::from_tup, fpgen::enumerate |
| 48 | + */ |
| 49 | +template <typename T, typename... TArgs, |
| 50 | + template <typename...> typename Container> |
| 51 | +generator<std::tuple<size_t, T>> enumerate(const Container<T, TArgs...> &cont) { |
| 52 | + size_t i = 0; |
| 53 | + for (auto it = std::begin(cont); it != std::end(cont); ++it) { |
| 54 | + co_yield {i, *it}; |
| 55 | + i++; |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +/** |
| 60 | + * \brief Creates a generator over an associative data source. |
| 61 | + * |
| 62 | + * The data source should have an iterator (using `std::begin` and `std::end`). |
| 63 | + * For most builtin containers (`std::map`, ...) this is already satisfied. |
| 64 | + * The container should have two type arguments. For single-type containers, see |
| 65 | + * fpgen::from. |
| 66 | + * |
| 67 | + * \tparam TKey The first type contained in the container (key type). |
| 68 | + * \tparam TValue The second type contained in the container (value type). |
| 69 | + * \tparam TArgs Any other template parameters passed to the container. |
| 70 | + * \tparam Container The container type. |
| 71 | + * \param[in] cont The container to iterate over. |
| 72 | + * \returns A new generator which will iterate over the container. |
| 73 | + * \see fpgen::from |
| 74 | + */ |
| 75 | +template <typename TKey, typename TVal, typename... TArgs, |
| 76 | + template <typename...> typename Container> |
| 77 | +generator<std::tuple<TKey, TVal>> |
| 78 | +from_tup(const Container<TKey, TVal, TArgs...> &cont) { |
| 79 | + for (auto it = cont.begin(); it != cont.end(); ++it) { |
| 80 | + co_yield *it; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/** |
| 85 | + * \brief Creates an infinitely incrementing generator. |
| 86 | + * |
| 87 | + * The generator is contstructed by continuously incrementing (a copy of) the |
| 88 | + * given value. While mainly meant for integral types, any type supporting |
| 89 | + * operator++() (the prefix increment operator) can be used. The first value |
| 90 | + * returned is the start value itself. |
| 91 | + * |
| 92 | + * \tparam T The type to increment. |
| 93 | + * \param[in] start The initial value. |
| 94 | + * \returns An infinite generator which increments a value. |
| 95 | + */ |
| 96 | +template <typename T> generator<T> inc(T start) { |
| 97 | + T value = start; |
| 98 | + while (true) { |
| 99 | + co_yield value; |
| 100 | + ++value; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace fpgen |
| 105 | + |
| 106 | +#endif |
0 commit comments