Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Playing around with flattening dynamic memory for static storage.
- Requires executing twice: once for size and once for values.
- Therefor it also requires that the operation be deterministic
- and consistent between executions. (No way to avoid this atm?...)
- */
- #include <ranges> // range_value_t
- #include <type_traits> // invoke_result_t
- #include <array>
- // omitting `require` clauses and concepts for brevity...
- template <typename T>
- using typer = std::ranges::range_value_t<std::invoke_result_t<T>>;
- template <typename T>
- constexpr std::size_t sizer{ std::ranges::size(T{}()) };
- template <typename T>
- consteval auto crystallizer()
- {
- std::array<typer<T>, sizer<T>> out{};
- auto&& temp{ T{}() };
- std::copy(temp.begin(), temp.end(), out.begin());
- return out;
- }
- #define CRYSTALLIZE(...) crystallizer<decltype([]{ return __VA_ARGS__; })>()
- // arbitrary constexpr work ( this is enterprise :P gotta over-complicate it )
- #include <functional> // plus
- #include <algorithm> // fold_left
- #include <ranges> // reverse_view, take
- #include <vector>
- constexpr std::vector<int> fibby(std::size_t n)
- {
- using namespace std::ranges;
- using namespace std::ranges::views;
- if (n == 0) return std::vector<int>{};
- if (n == 1) return std::vector<int>({0});
- std::vector<int> seq({0,1});
- for (std::size_t i{ 2 }; i < n; ++i)
- seq.push_back(fold_left(reverse_view(seq) | take(2), 0, std::plus<int>()));
- return seq;
- }
- // demo
- #include <iostream>
- int main()
- {
- for (auto& e : CRYSTALLIZE(fibby(20)))
- std::cout << e << ", ";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement