Last active
October 13, 2020 22:14
-
-
Save HookedBehemoth/59e202ea81c35af31e60eb3068ad04f4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iterator> | |
#include <utility> | |
template <typename Base> | |
class Enumerate { | |
private: | |
Base &base; | |
class Iterator { | |
public: | |
using BaseIterator = decltype(std::begin(base)); | |
using DataType = decltype(*BaseIterator()); | |
using ReturnType = std::pair<size_t, DataType &>; | |
static_assert(std::is_same_v<decltype(std::begin(base)), decltype(std::end(base))>); | |
private: | |
BaseIterator it; | |
size_t idx; | |
public: | |
Iterator(BaseIterator &&_it) | |
: it(_it) | |
, idx() { } | |
const ReturnType operator*() { | |
return { idx, *it }; | |
} | |
bool operator!=(const Iterator &rhs) const { | |
return it != rhs.it; | |
} | |
void operator++() { | |
++it; | |
++idx; | |
} | |
}; | |
public: | |
Enumerate(Base &_base) | |
: base(_base) { } | |
Iterator begin() { | |
return { std::begin(base) }; | |
} | |
Iterator end() { | |
return { std::end(base) }; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment