Simple-Vector v1.3.0
Generators.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2021 - 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SIMPLE_VECTOR_GENERATORS_HPP
7#define SIMPLE_VECTOR_GENERATORS_HPP
8
9#pragma once
10
11#include "Concepts.hpp"
12
13#include <iterator>
14#include <ranges>
15#include <type_traits>
16
17namespace sl::vec::gen
18{
28 template <value_type T>
29 struct fill
30 {
31 using value_type = std::remove_cvref_t<T>;
32
34
39 constexpr value_type operator ()() const noexcept(std::is_nothrow_copy_constructible_v<value_type>)
40 {
41 return value;
42 }
43 };
44
45 template <value_type T>
47
52 template <value_type T>
54 requires std::weakly_incrementable<T>
56 struct iota
57 {
58 using value_type = std::remove_cvref_t<T>;
59
61
67 {
68 return value++;
69 }
70 };
71
72 template <value_type T>
74
83 template <std::ranges::forward_range TRange>
84 class range
85 {
86 public:
87 using range_type = std::conditional_t
88 <
89 std::ranges::borrowed_range<TRange>,
90 TRange,
91 std::remove_cvref_t<TRange>
92 >;
93 using iterator_type = std::ranges::iterator_t<range_type>;
94 using value_type = std::ranges::range_value_t<TRange>;
95
100 constexpr explicit range(TRange&& range)
101 : m_Range{ std::forward<TRange>(range) },
102 m_Iterator{ std::ranges::begin(m_Range) }
103 {
104 }
105
111 {
112 return *m_Iterator++;
113 }
114
115 private:
116 range_type m_Range;
117 iterator_type m_Iterator;
118 };
119
120 template <std::ranges::forward_range TRange>
121 range(TRange&&) -> range<TRange>;
122
124}
125
126#endif
Generator which retrieves its values out of the given source iterator.
Definition: Generators.hpp:85
std::conditional_t< std::ranges::borrowed_range< TRange >, TRange, std::remove_cvref_t< TRange > > range_type
Definition: Generators.hpp:92
std::ranges::iterator_t< range_type > iterator_type
Definition: Generators.hpp:93
constexpr value_type operator()()
post-increments the stored iterator and returns a copy of the previously pointed element.
Definition: Generators.hpp:110
constexpr range(TRange &&range)
Constructs the generator with a given range.
Definition: Generators.hpp:100
Checks if the given type is regular and can be used in common arithmetically operations.
Definition: Concepts.hpp:162
iota(T) -> iota< T >
fill(T) -> fill< T >
range(TRange &&) -> range< TRange >
Definition: Generators.hpp:18
Generator which repeatedly returns the same value on each invocation.
Definition: Generators.hpp:30
constexpr value_type operator()() const noexcept(std::is_nothrow_copy_constructible_v< value_type >)
Copies the stored value and returns it.
Definition: Generators.hpp:39
const value_type value
Definition: Generators.hpp:33
Generator which returns incrementing values on each invocation.
Definition: Generators.hpp:57
value_type value
Definition: Generators.hpp:60
constexpr value_type operator()()
post-increments the stored value and returns a copy
Definition: Generators.hpp:66