forked from activeloopai/deeplake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflat_iterator.hpp
More file actions
195 lines (171 loc) · 5.6 KB
/
Copy pathflat_iterator.hpp
File metadata and controls
195 lines (171 loc) · 5.6 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#pragma once
#include <base/assert.hpp>
#include <cstdint>
#include <vector>
namespace icm {
/**
* @brief A random access iterator that iterates over a vector of containers.
* `flat_iterator` Gets a vector of containers and a vector of offsets, and iterates over the containers as if they were
* a single container. The offsets vector contains the offset of each container in the flattened container, it can be
* generated using `std::transform_exclusive_scan`.
* The vector of containers can be empty
* The vector of offsets should have the same size as the vector of containers.
* It's allowed to have empty containers in the vector of containers.
*/
template <typename C>
class flat_iterator
{
public:
using value_type = typename C::const_iterator::value_type;
using reference = typename C::const_iterator::reference;
using pointer = value_type*;
using difference_type = int64_t;
using iterator_category = std::random_access_iterator_tag;
static flat_iterator begin(const std::vector<C>& containers, const std::vector<value_type>& offsets)
{
if (containers.empty()) {
return flat_iterator();
}
return flat_iterator(containers, offsets, containers.begin(), containers.begin()->begin());
}
static flat_iterator end(const std::vector<C>& containers, const std::vector<value_type>& offsets)
{
if (containers.empty()) {
return flat_iterator();
}
return flat_iterator(containers, offsets, containers.end() - 1, containers.back().end());
}
reference operator*() const
{
return *child_iterator_;
}
flat_iterator& operator++()
{
++child_iterator_;
skip_empty_containers();
return *this;
}
flat_iterator operator++(int)
{
flat_iterator tmp = *this;
++*this;
return tmp;
}
flat_iterator& operator--()
{
while (child_iterator_ == parent_iterator_->begin() && parent_iterator_ != containers_->begin()) {
--parent_iterator_;
child_iterator_ = parent_iterator_->end();
}
--child_iterator_;
return *this;
}
flat_iterator operator--(int)
{
flat_iterator tmp = *this;
--*this;
return tmp;
}
flat_iterator& operator+=(difference_type n)
{
if (n > 0) {
while (n > 0) {
auto remaining = std::distance(child_iterator_, parent_iterator_->end());
if (n <= remaining) {
child_iterator_ += n;
break;
}
n -= remaining;
++parent_iterator_;
child_iterator_ = parent_iterator_->begin();
}
} else {
while (n < 0) {
auto remaining = std::distance(parent_iterator_->begin(), child_iterator_);
if (n >= -remaining) {
child_iterator_ += n;
break;
}
n += remaining;
--parent_iterator_;
child_iterator_ = parent_iterator_->end();
}
}
skip_empty_containers();
return *this;
}
flat_iterator operator+(difference_type n) const
{
flat_iterator tmp = *this;
tmp += n;
return tmp;
}
friend flat_iterator operator+(difference_type n, const flat_iterator& it)
{
return it + n;
}
flat_iterator& operator-=(difference_type n)
{
return *this += -n;
}
flat_iterator operator-(difference_type n) const
{
flat_iterator tmp = *this;
tmp -= n;
return tmp;
}
difference_type operator-(const flat_iterator& other) const
{
if (containers_ == nullptr) {
ASSERT(other.containers_ == nullptr);
return 0;
}
difference_type n = 0;
auto it = *this;
while (std::distance(it.parent_iterator_, other.parent_iterator_) > 0) {
n += std::distance(it.parent_iterator_->end(), it.child_iterator_);
++it.parent_iterator_;
it.child_iterator_ = it.parent_iterator_->begin();
}
while (std::distance(it.parent_iterator_, other.parent_iterator_) < 0) {
n += std::distance(it.parent_iterator_->begin(), it.child_iterator_);
--it.parent_iterator_;
it.child_iterator_ = it.parent_iterator_->end();
}
n += std::distance(other.child_iterator_, it.child_iterator_);
return n;
}
bool operator==(const flat_iterator& other) const
{
return child_iterator_ == other.child_iterator_;
}
bool operator!=(const flat_iterator& other) const
{
return !(*this == other);
}
private:
flat_iterator() = default;
flat_iterator(const std::vector<C>& containers,
const std::vector<value_type>& offsets,
std::vector<C>::const_iterator parent_iterator,
C::const_iterator child_iterator)
: containers_(&containers)
, offsets_(&offsets)
, parent_iterator_(parent_iterator)
, child_iterator_(child_iterator)
{
skip_empty_containers();
}
void skip_empty_containers()
{
while (parent_iterator_ != containers_->end() - 1 && child_iterator_ == (*parent_iterator_).end()) {
++parent_iterator_;
child_iterator_ = parent_iterator_->begin();
}
}
const std::vector<C>* containers_;
const std::vector<value_type>* offsets_;
std::vector<C>::const_iterator parent_iterator_;
C::const_iterator child_iterator_;
};
}