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 <ranges>
8#include <algorithm>
9#include <concepts>
10#include <string>
11#include <string_view>
12#include <type_traits>
13#include <vector>
14
15namespace hi::inline v1 {
16
20template<typename Value, typename Range>
21[[nodiscard]] constexpr std::vector<Value> make_vector(Range&& range)
22{
23 auto first = std::ranges::begin(range);
24 auto last = std::ranges::end(range);
25
26 if constexpr (requires(std::vector<Value> & x) { std::ranges::copy(first, last, std::back_inserter(x)); }) {
27 // This should handle almost everything.
28 auto r = std::vector<Value>{};
29 if constexpr (requires { std::distance(first, last); }) {
30 r.reserve(std::distance(first, last));
31 }
32 std::ranges::copy(first, last, std::back_inserter(r));
33 return r;
34
35 } else if constexpr (requires { Value{std::string_view{(*first).begin(), (*first).end()}}; }) {
36 // std::views::split returns a range of ranges, handle the string_view cases.
37 auto r = std::vector<Value>{};
38 if constexpr (requires { std::distance(first, last); }) {
39 r.reserve(std::distance(first, last));
40 }
41 for (auto it = first; it != last; ++it) {
42 r.emplace_back(std::string_view{(*it).begin(), (*it).end()});
43 }
44 return r;
45
46 } else {
47 hi_static_not_implemented();
48 }
49}
50
53template<typename Range>
54[[nodiscard]] constexpr std::vector<typename Range::value_type> make_vector(Range&& range)
55{
56 return make_vector<typename Range::value_type>(std::forward<Range>(range));
57}
58
59} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:15
constexpr std::vector< Value > make_vector(Range &&range)
Make a vector from a view.
Definition ranges.hpp:21
T back_inserter(T... args)
T distance(T... args)
T reserve(T... args)