MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
iter.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_ITER_HPP
20#define MTCORE_ITER_HPP
21
22#include "../traits.hpp"
23#include "optional.hpp"
24
25namespace mtcore {
31 template<Addressable T>
32 struct PtrIter {
33 using IterElem = std::add_pointer_t<typename T::Elem>;
34 T &s;
35 size_t index = 0;
36
38 if (index >= s.size()) {
39 return {};
40 }
41 return &s[index++];
42 }
43 };
44
50 template<Addressable T>
51 struct ConstPtrIter {
52 using IterElem = std::add_pointer_t<std::add_const_t<typename T::Elem>>;
53 const T &s;
54 size_t index = 0;
55
57 if (index >= s.size()) {
58 return {};
59 }
60 return &s[index++];
61 }
62 };
63
69 template<Addressable T>
70 struct ValIter {
71 using IterElem = std::remove_const_t<typename T::Elem>;
72 const T &s;
73 size_t index = 0;
74
76 if (index >= s.size()) {
77 return {};
78 }
79 return s[index++];
80 }
81 };
82
91 namespace iter {
100 template<typename T>
102 return {r};
103 }
104
113 template<typename T>
114 ValIter<T> val(const T &r) {
115 return {r};
116 }
117
127 template<typename T>
129 return {r};
130 }
131 } // namespace iter
132} // namespace mtcore
133
134#endif // MTCORE_ITER_HPP
ValIter< T > val(const T &r)
Generic value iterator that uses the operator[] and incrementing indexes to iterate over a collection...
Definition iter.hpp:114
ConstPtrIter< T > const_ptr(const T &r)
Generic constant pointer iterator that uses the operator[] and incrementing indexes to iterate over a...
Definition iter.hpp:128
PtrIter< T > ptr(T &r)
Generic pointer iterator that uses the operator[] and incrementing indexes to iterate over a collecti...
Definition iter.hpp:101
Generic iterator defaults built on common contracts Does not guarantee performance of iterators Actua...
Definition iter.hpp:91
Core library for C++ with Zig-related functionality.
Iterator that gives const pointers to elements of a collection.
Definition iter.hpp:51
std::add_pointer_t< std::add_const_t< typename T::Elem > > IterElem
Definition iter.hpp:52
Optional< IterElem > next()
Definition iter.hpp:56
Represents a value that may or may not exist (an "Optional" value) Similar concept to std::optional,...
Definition optional.hpp:235
Iterator that gives pointers to elements of a collection.
Definition iter.hpp:32
Optional< IterElem > next()
Definition iter.hpp:37
std::add_pointer_t< typename T::Elem > IterElem
Definition iter.hpp:33
size_t index
Definition iter.hpp:35
Iterator that gives copies of elements of a collection.
Definition iter.hpp:70
Optional< IterElem > next()
Definition iter.hpp:75
std::remove_const_t< typename T::Elem > IterElem
Definition iter.hpp:71
const T & s
Definition iter.hpp:72
size_t index
Definition iter.hpp:73