forked from LadybirdBrowser/ladybird
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEnumerate.h
65 lines (50 loc) · 1.29 KB
/
Enumerate.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* Copyright (c) 2024, Tim Flynn <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/StdLibExtras.h>
namespace AK {
namespace Detail {
template<typename Iterable>
class Enumerator {
using IteratorType = decltype(declval<Iterable>().begin());
using ValueType = decltype(*declval<IteratorType>());
struct Enumeration {
size_t index { 0 };
ValueType value;
};
public:
Enumerator(Iterable&& iterable)
: m_iterable(forward<Iterable>(iterable))
, m_iterator(m_iterable.begin())
, m_end(m_iterable.end())
{
}
Enumerator const& begin() const { return *this; }
Enumerator const& end() const { return *this; }
Enumeration operator*() { return { m_index, *m_iterator }; }
Enumeration operator*() const { return { m_index, *m_iterator }; }
bool operator!=(Enumerator const&) const { return m_iterator != m_end; }
void operator++()
{
++m_index;
++m_iterator;
}
private:
Iterable m_iterable;
size_t m_index { 0 };
IteratorType m_iterator;
IteratorType const m_end;
};
}
template<typename T>
auto enumerate(T&& range)
{
return Detail::Enumerator<T> { forward<T>(range) };
}
}
#ifdef USING_AK_GLOBALLY
using AK::enumerate;
#endif