CeresEngine 0.2.0
A game development framework
Loading...
Searching...
No Matches
Enumerate.hpp
Go to the documentation of this file.
1//
2// CeresEngine - A game development framework
3//
4// Created by Rogiel Sulzbach.
5// Copyright (c) 2018-2022 Rogiel Sulzbach. All rights reserved.
6//
7
8#pragma once
9
11
12#include <functional>
13#include <iterator>
14
15namespace CeresEngine {
16
23 template<typename Iterator, typename Index = std::size_t> class EnumerateIterator {
24 private:
27
30
31 public:
32 using iterator_category = typename std::iterator_traits<Iterator>::iterator_category;
33 using value_type = typename std::iterator_traits<Iterator>::value_type;
34 using difference_type = typename std::iterator_traits<Iterator>::difference_type;
35 using pointer = typename std::iterator_traits<Iterator>::pointer;
36 using reference = typename std::iterator_traits<Iterator>::reference;
37
40 EnumerateIterator(Index index, Iterator&& iterator) : mIndex(index), mIterator(std::forward<Iterator>(iterator)) {}
41
44 ++mIndex;
45 ++mIterator;
46 return *this;
47 }
48
51 EnumerateIterator copy = *this;
52 mIndex++;
53 mIterator++;
54 return copy;
55 }
56
59 mIndex += offset;
60 mIterator += offset;
61 return *this;
62 }
63
66 --mIndex;
67 --mIterator;
68 return *this;
69 }
70
73 EnumerateIterator copy = *this;
74 mIndex++;
75 mIterator++;
76 return copy;
77 }
78
81 mIndex -= offset;
82 mIterator -= offset;
83 return *this;
84 }
85
88 if constexpr(std::is_lvalue_reference_v<decltype(*mIterator)> || std::is_rvalue_reference_v<decltype(*mIterator)>) {
89 return std::make_pair(mIndex, std::ref(*mIterator));
90 } else {
91 return std::make_pair(mIndex, *mIterator);
92 }
93 }
94
96 auto* operator->() const noexcept { return &**this; }
97
98 bool operator==(const EnumerateIterator& other) const { return mIterator == other.mIterator; }
99 bool operator!=(const EnumerateIterator& other) const { return mIterator != other.mIterator; }
100 bool operator>(const EnumerateIterator& other) const { return mIterator > other.mIterator; }
101 bool operator>=(const EnumerateIterator& other) const { return mIterator >= other.mIterator; }
102 bool operator<(const EnumerateIterator& other) const { return mIterator < other.mIterator; }
103 bool operator<=(const EnumerateIterator& other) const { return mIterator <= other.mIterator; }
104 };
105
113 template<typename Index = std::size_t, typename Range> inline auto enumerate(Range& range, Index startingAt = 0) {
114 return wrap(EnumerateIterator(startingAt, std::begin(range)), EnumerateIterator(~Index(0), std::end(range)));
115 }
116
124 template<typename Index = std::size_t, typename Range> inline auto enumerate(Range&& range, Index startingAt = 0) {
125 using Iterator = std::decay_t<decltype(std::begin(range))>;
127 std::forward<Range>(range));
128 }
129
130} // namespace CeresEngine
An iterator type that keeps an index count of the current item.
Definition Enumerate.hpp:23
typename std::iterator_traits< Iterator >::iterator_category iterator_category
Definition Enumerate.hpp:32
EnumerateIterator & operator+(difference_type offset)
Advances the iterator by the given offset.
Definition Enumerate.hpp:58
EnumerateIterator & operator--()
Decrements the iterator. Will also decrement the index position.
Definition Enumerate.hpp:65
typename std::iterator_traits< Iterator >::value_type value_type
Definition Enumerate.hpp:33
bool operator!=(const EnumerateIterator &other) const
Definition Enumerate.hpp:99
bool operator==(const EnumerateIterator &other) const
Definition Enumerate.hpp:98
EnumerateIterator operator++(int)
Increments the iterator. Will also increment the index position.
Definition Enumerate.hpp:50
bool operator<(const EnumerateIterator &other) const
Definition Enumerate.hpp:102
auto operator*() const noexcept
Dereferences the iterator and obtain it's value.
Definition Enumerate.hpp:87
bool operator>=(const EnumerateIterator &other) const
Definition Enumerate.hpp:101
EnumerateIterator operator--(int)
Decrements the iterator. Will also decrement the index position.
Definition Enumerate.hpp:72
bool operator>(const EnumerateIterator &other) const
Definition Enumerate.hpp:100
typename std::iterator_traits< Iterator >::reference reference
Definition Enumerate.hpp:36
auto * operator->() const noexcept
Dereferences the iterator and obtain it's value.
Definition Enumerate.hpp:96
EnumerateIterator & operator-(difference_type offset)
Recedes the iterator by the given offset.
Definition Enumerate.hpp:80
Iterator mIterator
The current iterator position.
Definition Enumerate.hpp:29
typename std::iterator_traits< Iterator >::pointer pointer
Definition Enumerate.hpp:35
bool operator<=(const EnumerateIterator &other) const
Definition Enumerate.hpp:103
EnumerateIterator(Index index, Iterator &&iterator)
Creates a new enumerate iterator at the given index positioned at the given it iterator.
Definition Enumerate.hpp:40
typename std::iterator_traits< Iterator >::difference_type difference_type
Definition Enumerate.hpp:34
EnumerateIterator & operator++()
Increments the iterator. Will also increment the index position.
Definition Enumerate.hpp:43
Index mIndex
The current iteration index.
Definition Enumerate.hpp:26
IteratorPrototype where advance is defined by the functional held by F.
Definition Iterator.hpp:212
Definition Application.hpp:19
auto range()
Returns an iterator that increases it's value from 0 to end by 1 for each step.
Definition Iterator.hpp:350
auto enumerate(Range &range, Index startingAt=0)
Return a range object.
Definition Enumerate.hpp:113
@ Index
Index buffer type.
auto wrap(IB &&a, IE &&b, Context &&context)
Wraps two iterators into a single-use container with begin/end methods to match the C++ iterator conv...
Definition Iterator.hpp:296
void copy(const A &a, B &b, T &&t=T())
Copies values from one container to another.
Definition Iterator.hpp:564
constexpr size_t hash(const T &v)
Generates a hash for the provided type.
Definition Hash.hpp:25
Definition Span.hpp:668