20#ifndef LIB_ENUMERATOR_H_
21#define LIB_ENUMERATOR_H_
25#include <initializer_list>
32#include "iterator_range.h"
35enum class EnumeratorState { NotStarted, Valid, PastEnd };
48class EnumeratorHandle {
51 explicit EnumeratorHandle(
Enumerator<T> *enumerator) : enumerator(enumerator) {}
55 using iterator_category = std::input_iterator_tag;
56 using difference_type = std::ptrdiff_t;
61 reference operator*()
const;
62 const EnumeratorHandle<T> &operator++();
63 bool operator==(
const EnumeratorHandle<T> &other)
const;
64 bool operator!=(
const EnumeratorHandle<T> &other)
const;
71 EnumeratorState state = EnumeratorState::NotStarted;
75 friend class Enumerator;
76 static std::vector<T> emptyVector;
78 friend class EnumeratorHandle;
83 Enumerator() { this->
reset(); }
85 virtual ~Enumerator() =
default;
93 virtual void reset() { this->state = EnumeratorState::NotStarted; }
99 EnumeratorHandle<T> end() {
return EnumeratorHandle<T>(
nullptr); }
101 const char *stateName()
const {
102 switch (this->state) {
103 case EnumeratorState::NotStarted:
105 case EnumeratorState::Valid:
107 case EnumeratorState::PastEnd:
110 throw std::logic_error(
"Unexpected state " + std::to_string(
static_cast<int>(this->state)));
114 template <
typename Container>
116 "Use Util::enumerate() instead")]]
static Enumerator<typename Container::value_type> *
117 createEnumerator(
const Container &data);
118 static Enumerator<T> *emptyEnumerator();
119 template <
typename Iter>
120 [[deprecated(
"Use Util::enumerate() instead")]]
static Enumerator<typename Iter::value_type> *
121 createEnumerator(Iter begin, Iter end);
122 template <
typename Iter>
123 [[deprecated(
"Use Util::enumerate() instead")]]
static Enumerator<typename Iter::value_type> *
124 createEnumerator(iterator_range<Iter> range);
127 template <
typename Filter>
128 Enumerator<T> *
where(Filter filter);
130 template <
typename Mapper>
131 Enumerator<std::invoke_result_t<Mapper, T>> *
map(Mapper
map);
133 template <
typename S>
136 virtual Enumerator<T> *
concat(Enumerator<T> *other);
138 static Enumerator<T> *
concatAll(Enumerator<Enumerator<T> *> *inputs);
140 std::vector<T> toVector() {
141 std::vector<T> result;
159 if (!
next)
throw std::logic_error(
"There is no element for `single()'");
162 if (
next)
throw std::logic_error(
"There are multiple elements when calling `single()'");
170 if (!
next)
return T{};
173 if (
next)
throw std::logic_error(
"There are multiple elements when calling `single()'");
180 if (!
next)
return T{};
187 if (!
next)
throw std::logic_error(
"There is no element for `next()'");
199template <
typename Iter>
200class IteratorEnumerator :
public Enumerator<typename std::iterator_traits<Iter>::value_type> {
206 friend class Enumerator<typename
std::iterator_traits<Iter>::value_type>;
209 IteratorEnumerator(Iter begin, Iter end, const char *name)
210 : Enumerator<typename std::iterator_traits<Iter>::value_type>(),
216 [[nodiscard]] std::string toString()
const {
217 return std::string(this->name) +
":" + this->stateName();
221 switch (this->state) {
222 case EnumeratorState::NotStarted:
223 this->current = this->begin;
224 if (this->current == this->end) {
225 this->state = EnumeratorState::PastEnd;
228 this->state = EnumeratorState::Valid;
231 case EnumeratorState::PastEnd:
233 case EnumeratorState::Valid:
235 if (this->current == this->end) {
236 this->state = EnumeratorState::PastEnd;
242 throw std::runtime_error(
"Unexpected enumerator state");
245 typename std::iterator_traits<Iter>::value_type
getCurrent()
const {
246 switch (this->state) {
247 case EnumeratorState::NotStarted:
248 throw std::logic_error(
"You cannot call 'getCurrent' before 'moveNext'");
249 case EnumeratorState::PastEnd:
250 throw std::logic_error(
"You cannot call 'getCurrent' past the collection end");
251 case EnumeratorState::Valid:
252 return *this->current;
254 throw std::runtime_error(
"Unexpected enumerator state");
261class SingleEnumerator :
public Enumerator<T> {
265 explicit SingleEnumerator(T v) : Enumerator<T>(), value(v) {}
267 switch (this->state) {
268 case EnumeratorState::NotStarted:
269 this->state = EnumeratorState::Valid;
271 case EnumeratorState::PastEnd:
273 case EnumeratorState::Valid:
274 this->state = EnumeratorState::PastEnd;
277 throw std::runtime_error(
"Unexpected enumerator state");
280 switch (this->state) {
281 case EnumeratorState::NotStarted:
282 throw std::logic_error(
"You cannot call 'getCurrent' before 'moveNext'");
283 case EnumeratorState::PastEnd:
284 throw std::logic_error(
"You cannot call 'getCurrent' past the collection end");
285 case EnumeratorState::Valid:
288 throw std::runtime_error(
"Unexpected enumerator state");
298 [[nodiscard]] std::string toString()
const {
return "EmptyEnumerator"; }
302 throw std::logic_error(
"You cannot call 'getCurrent' on an EmptyEnumerator");
313template <
typename T,
typename Filter>
314class FilterEnumerator final :
public Enumerator<T> {
315 Enumerator<T> *input;
320 FilterEnumerator(Enumerator<T> *input, Filter filter)
321 : input(input), filter(std::move(filter)) {}
325 this->state = EnumeratorState::Valid;
326 while (this->input->moveNext()) {
327 this->current = this->input->getCurrent();
328 bool match = this->filter(this->current);
329 if (match)
return true;
331 this->state = EnumeratorState::PastEnd;
336 [[nodiscard]] std::string toString()
const {
337 return "FilterEnumerator(" + this->input->toString() +
"):" + this->stateName();
341 this->input->reset();
346 switch (this->state) {
347 case EnumeratorState::NotStarted:
348 case EnumeratorState::Valid:
349 return this->advance();
350 case EnumeratorState::PastEnd:
353 throw std::runtime_error(
"Unexpected enumerator state");
357 switch (this->state) {
358 case EnumeratorState::NotStarted:
359 throw std::logic_error(
"You cannot call 'getCurrent' before 'moveNext'");
360 case EnumeratorState::PastEnd:
361 throw std::logic_error(
"You cannot call 'getCurrent' past the collection end");
362 case EnumeratorState::Valid:
363 return this->current;
365 throw std::runtime_error(
"Unexpected enumerator state");
377template <
typename From,
typename To,
typename =
void>
378static constexpr bool can_be_casted =
false;
380template <
typename From,
typename To>
382 can_be_casted<From *, To *, std::void_t<decltype(std::declval<From *>()->template to<To>())>> =
387template <
typename T,
typename S>
388class AsEnumerator final :
public Enumerator<S> {
389 template <
typename U = S>
390 typename std::enable_if_t<!Detail::can_be_casted<T, S>, U> getCurrentImpl()
const {
391 T current = input->getCurrent();
392 return dynamic_cast<S
>(current);
395 template <
typename U = S>
396 typename std::enable_if_t<Detail::can_be_casted<T, S>, U> getCurrentImpl()
const {
397 T current = input->getCurrent();
398 return current->template to<std::remove_pointer_t<S>>();
402 Enumerator<T> *input;
405 explicit AsEnumerator(Enumerator<T> *input) : input(input) {}
407 std::string toString()
const {
408 return "AsEnumerator(" + this->input->toString() +
"):" + this->stateName();
413 this->input->
reset();
417 bool result = this->input->
moveNext();
419 this->state = EnumeratorState::Valid;
421 this->state = EnumeratorState::PastEnd;
431template <
typename T,
typename S,
typename Mapper>
432class MapEnumerator final :
public Enumerator<S> {
434 Enumerator<T> *input;
439 MapEnumerator(Enumerator<T> *input, Mapper map) : input(input), map(std::move(map)) {}
442 this->input->
reset();
446 [[nodiscard]] std::string toString()
const {
447 return "MapEnumerator(" + this->input->toString() +
"):" + this->stateName();
451 switch (this->state) {
452 case EnumeratorState::NotStarted:
453 case EnumeratorState::Valid: {
454 bool success = input->moveNext();
457 this->current = this->map(currentInput);
458 this->state = EnumeratorState::Valid;
461 this->state = EnumeratorState::PastEnd;
465 case EnumeratorState::PastEnd:
468 throw std::runtime_error(
"Unexpected enumerator state");
472 switch (this->state) {
473 case EnumeratorState::NotStarted:
474 throw std::logic_error(
"You cannot call 'getCurrent' before 'moveNext'");
475 case EnumeratorState::PastEnd:
476 throw std::logic_error(
"You cannot call 'getCurrent' past the collection end");
477 case EnumeratorState::Valid:
478 return this->current;
480 throw std::runtime_error(
"Unexpected enumerator state");
484template <
typename T,
typename Mapper>
485MapEnumerator(Enumerator<T> *,
486 Mapper) -> MapEnumerator<T, typename std::invoke_result_t<Mapper, T>, Mapper>;
492class ConcatEnumerator final :
public Enumerator<T> {
493 std::vector<Enumerator<T> *> inputs;
497 ConcatEnumerator() =
default;
499 explicit ConcatEnumerator(std::vector<Enumerator<T> *> &&inputs) : inputs(std::move(inputs)) {
500 for (
auto *currentInput : inputs)
501 if (currentInput ==
nullptr)
throw std::logic_error(
"Null iterator in concatenation");
504 ConcatEnumerator(std::initializer_list<Enumerator<T> *> inputs) : inputs(inputs) {
505 for (
auto *currentInput : inputs)
506 if (currentInput ==
nullptr)
throw std::logic_error(
"Null iterator in concatenation");
508 explicit ConcatEnumerator(Enumerator<Enumerator<T> *> *inputs)
509 : ConcatEnumerator(inputs->toVector()) {}
511 [[nodiscard]] std::string toString()
const {
return "ConcatEnumerator:" + this->stateName(); }
515 this->state = EnumeratorState::Valid;
516 for (
auto *currentInput : inputs) {
517 if (currentInput->moveNext()) {
518 this->currentResult = currentInput->getCurrent();
523 this->state = EnumeratorState::PastEnd;
528 Enumerator<T> *
concat(Enumerator<T> *other)
override {
530 if (this->state == EnumeratorState::PastEnd)
531 throw std::runtime_error(
"Invalid enumerator state to concatenate");
533 inputs.push_back(other);
539 for (
auto *currentInput : inputs) currentInput->reset();
544 switch (this->state) {
545 case EnumeratorState::NotStarted:
546 case EnumeratorState::Valid:
547 return this->advance();
548 case EnumeratorState::PastEnd:
551 throw std::runtime_error(
"Unexpected enumerator state");
555 switch (this->state) {
556 case EnumeratorState::NotStarted:
557 throw std::logic_error(
"You cannot call 'getCurrent' before 'moveNext'");
558 case EnumeratorState::PastEnd:
559 throw std::logic_error(
"You cannot call 'getCurrent' past the collection end");
560 case EnumeratorState::Valid:
561 return this->currentResult;
563 throw std::runtime_error(
"Unexpected enumerator state");
570template <
typename Mapper>
582template <
typename Filter>
588template <
typename Container>
594Enumerator<T> *Enumerator<T>::emptyEnumerator() {
595 return new EmptyEnumerator<T>();
599template <
typename Iter>
600Enumerator<typename Iter::value_type> *Enumerator<T>::createEnumerator(Iter begin, Iter end) {
601 return new IteratorEnumerator(begin, end,
"iterator");
605template <
typename Iter>
623T EnumeratorHandle<T>::operator*()
const {
624 if (enumerator ==
nullptr)
throw std::logic_error(
"Dereferencing end() iterator");
625 return enumerator->getCurrent();
629const EnumeratorHandle<T> &EnumeratorHandle<T>::operator++() {
630 enumerator->moveNext();
635bool EnumeratorHandle<T>::operator==(
const EnumeratorHandle<T> &other)
const {
636 return !(*
this != other);
641 if (this->enumerator == other.enumerator)
return true;
642 if (other.enumerator !=
nullptr)
throw std::logic_error(
"Comparison with different iterator");
643 return this->enumerator->state == EnumeratorState::Valid;
646template <
typename Iter>
651template <
typename Iter>
656template <
typename Container>
669template <
typename... Args>
670auto concat(Args &&...inputs) {
671 using FirstEnumeratorTy =
672 std::remove_pointer_t<std::decay_t<std::tuple_element_t<0, std::tuple<Args...>>>>;
673 std::initializer_list<Enumerator<typename FirstEnumeratorTy::value_type> *> init{
674 std::forward<Args>(inputs)...};
Casts each element.
Definition enumerator.h:388
S getCurrent() const override
Get current element in the collection.
Definition enumerator.h:425
void reset() override
Move back to the beginning of the collection.
Definition enumerator.h:411
bool moveNext() override
Definition enumerator.h:416
Concatenation.
Definition enumerator.h:492
void reset() override
Move back to the beginning of the collection.
Definition enumerator.h:538
bool moveNext() override
Definition enumerator.h:543
T getCurrent() const override
Get current element in the collection.
Definition enumerator.h:554
Enumerator< T > * concat(Enumerator< T > *other) override
Append all elements of other after all elements of this.
Definition enumerator.h:528
Always empty iterator (equivalent to end())
Definition enumerator.h:296
T getCurrent() const
Get current element in the collection.
Definition enumerator.h:301
bool moveNext()
Always returns false.
Definition enumerator.h:300
Definition enumerator.h:48
Type-erased Enumerator interface.
Definition enumerator.h:69
Enumerator< S > * as()
Cast to an enumerator of S objects.
Definition enumerator.h:577
Enumerator< std::invoke_result_t< Mapper, T > > * map(Mapper map)
Apply specified function to all elements of this enumerator.
Definition enumerator.h:571
virtual bool moveNext()=0
virtual Enumerator< T > * concat(Enumerator< T > *other)
Append all elements of other after all elements of this.
Definition enumerator.h:616
T nextOrDefault()
Next element, or the default value if none exists.
Definition enumerator.h:178
T single()
The only next element; throws if the enumerator does not have exactly 1 element.
Definition enumerator.h:157
virtual void reset()
Move back to the beginning of the collection.
Definition enumerator.h:93
bool any()
True if the enumerator has at least one element.
Definition enumerator.h:154
virtual T getCurrent() const =0
Get current element in the collection.
T next()
Next element; throws if there are no elements.
Definition enumerator.h:185
uint64_t count()
Enumerate all elements and return the count.
Definition enumerator.h:147
T singleOrDefault()
Definition enumerator.h:168
Enumerator< T > * where(Filter filter)
Return an enumerator returning all elements that pass the filter.
Definition enumerator.h:583
static Enumerator< T > * concatAll(Enumerator< Enumerator< T > * > *inputs)
Concatenate all these collections into a single one.
Definition enumerator.h:611
Definition enumerator.h:314
T getCurrent() const
Get current element in the collection.
Definition enumerator.h:356
void reset()
Move back to the beginning of the collection.
Definition enumerator.h:340
bool moveNext()
Definition enumerator.h:345
A generic iterator returning elements of type T.
Definition enumerator.h:200
bool moveNext()
Definition enumerator.h:220
std::iterator_traits< Iter >::value_type getCurrent() const
Get current element in the collection.
Definition enumerator.h:245
Transforms all elements from type T to type S.
Definition enumerator.h:432
void reset()
Move back to the beginning of the collection.
Definition enumerator.h:441
bool moveNext()
Definition enumerator.h:450
S getCurrent() const
Get current element in the collection.
Definition enumerator.h:471
bool moveNext()
Definition enumerator.h:266
T getCurrent() const
Get current element in the collection.
Definition enumerator.h:279
Definition iterator_range.h:44