-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWithIndex.h
More file actions
42 lines (31 loc) · 1.15 KB
/
WithIndex.h
File metadata and controls
42 lines (31 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#pragma once
#include "adlRange.h"
#include <stddef.h> // size_t
namespace array19 {
/// Iterate a container with indices
/// usage:
/// for(auto [value, index] : WithIndex{container});
template<class C> struct WithIndex {
using It = decltype(adlBegin(*static_cast<C*>(nullptr)));
using Res = decltype(**static_cast<It*>(nullptr));
// not using std::pair to avoid reference hassles
struct result {
Res value;
size_t index;
};
// minimal iterator that is just enough to run ranged for loop
struct iterator {
It it;
size_t index{};
[[nodiscard]] constexpr auto operator*() const -> result { return {*it, index}; }
[[nodiscard]] constexpr bool operator!=(const It& o) const { return it != o; }
constexpr auto operator++() -> iterator& { return (++it, ++index, *this); }
};
constexpr WithIndex(C& c) noexcept : c(&c) {}
[[nodiscard]] constexpr auto begin() -> iterator { return iterator{adlBegin(*c), {}}; }
[[nodiscard]] constexpr auto end() -> It { return adlEnd(*c); }
private:
C* c;
};
template<class C> WithIndex(C&) -> WithIndex<C>;
} // namespace array19