HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
ranges.hpp
1// Copyright Take Vos 2021-2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "utility/module.hpp"
8#include <ranges>
9#include <algorithm>
10#include <concepts>
11#include <type_traits>
12#include <vector>
13#include <exception>
14
15namespace hi::inline v1 {
16
17template<typename Value, typename Range>
18[[nodiscard]] constexpr Value get_first(Range &&range)
19{
20 auto it = std::ranges::begin(range);
21 auto last = std::ranges::end(range);
22
23 if (it == last) {
24 throw std::out_of_range{"Range is empty"};
25 }
26
27 auto value = *it++;
28 return Value{value};
29}
30
33template<typename Range>
34[[nodiscard]] constexpr Range::value_type get_first(Range&& range)
35{
36 return get_first<typename Range::value_type>(std::forward<Range>(range));
37}
38
42template<typename Value, typename Range>
43[[nodiscard]] constexpr std::vector<Value> make_vector(Range&& range)
44{
45 hilet first = std::ranges::begin(range);
46 hilet last = std::ranges::end(range);
47
48 if constexpr (requires(std::vector<Value> & x) { std::ranges::copy(first, last, std::back_inserter(x)); }) {
49 // This should handle almost everything.
50 auto r = std::vector<Value>{};
51 if constexpr (requires { std::distance(first, last); }) {
52 r.reserve(std::distance(first, last));
53 }
54 std::ranges::copy(first, last, std::back_inserter(r));
55 return r;
56
57 } else if constexpr (requires { Value{std::string_view{(*first).begin(), (*first).end()}}; }) {
58 // std::views::split returns a range of ranges, handle the string_view cases.
59 auto r = std::vector<Value>{};
60 if constexpr (requires { std::distance(first, last); }) {
61 r.reserve(std::distance(first, last));
62 }
63 for (auto it = first; it != last; ++it) {
64 r.emplace_back(std::string_view{(*it).begin(), (*it).end()});
65 }
66 return r;
67
68 } else {
70 }
71}
72
75template<typename Range>
76[[nodiscard]] constexpr std::vector<typename Range::value_type> make_vector(Range&& range)
77{
78 return make_vector<typename Range::value_type>(std::forward<Range>(range));
79}
80
81} // namespace hi::inline v1
#define hi_static_not_implemented(...)
This part of the code has not been implemented yet.
Definition assert.hpp:327
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr std::vector< Value > make_vector(Range &&range)
Make a vector from a view.
Definition ranges.hpp:43
T back_inserter(T... args)
T distance(T... args)
T reserve(T... args)