Skip to content

Instantly share code, notes, and snippets.

@HookedBehemoth
Last active October 13, 2020 22:14
Show Gist options
  • Save HookedBehemoth/59e202ea81c35af31e60eb3068ad04f4 to your computer and use it in GitHub Desktop.
Save HookedBehemoth/59e202ea81c35af31e60eb3068ad04f4 to your computer and use it in GitHub Desktop.
#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