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/utility.hpp"
8#include "../macros.hpp"
9#include <ranges>
10#include <algorithm>
11#include <concepts>
12#include <type_traits>
13#include <vector>
14#include <exception>
15
16
17
18namespace hi::inline v1 {
19
20template<typename Value, typename Range>
21[[nodiscard]] constexpr Value get_first(Range &&range)
22{
23 auto it = std::ranges::begin(range);
24 auto last = std::ranges::end(range);
25
26 if (it == last) {
27 throw std::out_of_range{"Range is empty"};
28 }
29
30 auto value = *it++;
31 return Value{value};
32}
33
36template<typename Range>
37[[nodiscard]] constexpr Range::value_type get_first(Range&& range)
38{
39 return get_first<typename Range::value_type>(std::forward<Range>(range));
40}
41
45template<typename Value, typename Range>
47{
48 hilet first = std::ranges::begin(range);
49 hilet last = std::ranges::end(range);
50
51 if constexpr (requires(std::vector<Value> & x) { std::ranges::copy(first, last, std::back_inserter(x)); }) {
52 // This should handle almost everything.
53 auto r = std::vector<Value>{};
54 if constexpr (requires { std::distance(first, last); }) {
55 r.reserve(std::distance(first, last));
56 }
57 std::ranges::copy(first, last, std::back_inserter(r));
58 return r;
59
60 } else if constexpr (requires { Value{std::string_view{(*first).begin(), (*first).end()}}; }) {
61 // std::views::split returns a range of ranges, handle the string_view cases.
62 auto r = std::vector<Value>{};
63 if constexpr (requires { std::distance(first, last); }) {
64 r.reserve(std::distance(first, last));
65 }
66 for (auto it = first; it != last; ++it) {
67 r.emplace_back(std::string_view{(*it).begin(), (*it).end()});
68 }
69 return r;
70
71 } else {
72 hi_static_not_implemented();
73 }
74}
75
78template<typename Range>
83
84} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr std::vector< Value > make_vector(Range &&range)
Make a vector from a view.
Definition ranges.hpp:46
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
T back_inserter(T... args)
T distance(T... args)
T reserve(T... args)