CartesianIndices.cpp 9.25 KB
Newer Older
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#include "CartesianIndices.h"
#include "TypeCasts.hpp"
#include <iostream>
#include <iterator>
#include <numeric>

namespace elsa
{
    template <typename Fn>
    auto map(std::vector<IndexRange> v, Fn fn)
    {
        IndexVector_t result(v.size());
        std::transform(v.begin(), v.end(), result.begin(), std::move(fn));
        return result;
    }

    auto CartesianIndices::dims() const -> index_t { return as<index_t>(idxrange_.size()); }

    auto CartesianIndices::size() const -> index_t
    {
        // TODO: Once the coverage CI image is updated, change this to:
        // clang-format off
        // std::transform_reduce(idxrange_.begin(), idxrange_.end(), 1, std::multiplies<>{},
        //                      [](auto p) { return p.second - p.first; }));
        // clang-format on
        std::vector<index_t> tmp;
        tmp.reserve(idxrange_.size());

        std::transform(idxrange_.begin(), idxrange_.end(), std::back_inserter(tmp),
                       [](auto p) { return p.second - p.first; });

        return std::accumulate(tmp.begin(), tmp.end(), 1, std::multiplies<>{});
    }

    auto CartesianIndices::range(index_t i) const -> IndexRange { return idxrange_[asUnsigned(i)]; }

    auto CartesianIndices::first() -> IndexVector_t
    {
        return map(idxrange_, [](auto range) { return range.first; });
    }

    auto CartesianIndices::first() const -> IndexVector_t
    {
        return map(idxrange_, [](auto range) { return range.first; });
    }

    auto CartesianIndices::last() -> IndexVector_t
    {
        return map(idxrange_, [](auto range) { return range.second; });
    }

    auto CartesianIndices::last() const -> IndexVector_t
    {
        return map(idxrange_, [](auto range) { return range.second; });
    }

    auto CartesianIndices::begin() -> iterator { return iterator(first(), first(), last()); }

    auto CartesianIndices::begin() const -> iterator { return {first(), first(), last()}; }

    auto CartesianIndices::end() -> iterator { return {as_sentinel_t, first(), last()}; }

    auto CartesianIndices::end() const -> iterator { return {as_sentinel_t, first(), last()}; }

    /****** Iterator implementation ******/
    CartesianIndices::iterator::iterator(as_sentinel, const IndexVector_t& begin,
                                         const IndexVector_t& end)
        : cur_(end), begins_(end), ends_(end)
    {
        cur_.tail(cur_.size() - 1) = begin.tail(cur_.size() - 1);
    }

    CartesianIndices::iterator::iterator(IndexVector_t vec) : cur_(vec), begins_(vec), ends_(vec) {}

    CartesianIndices::iterator::iterator(IndexVector_t cur, IndexVector_t begin, IndexVector_t end)
        : cur_(cur), begins_(begin), ends_(end)
    {
    }

    auto CartesianIndices::iterator::operator*() const -> IndexVector_t { return cur_; }

    auto CartesianIndices::iterator::operator++() -> iterator&
    {
        inc();
        return *this;
    }

    auto CartesianIndices::iterator::operator++(int) -> iterator
    {
        auto copy = *this;
        ++(*this);
        return copy;
    }

    auto CartesianIndices::iterator::operator--() -> iterator&
    {
        prev();
        return *this;
    }

    auto CartesianIndices::iterator::operator--(int) -> iterator
    {
        auto copy = *this;
        --(*this);
        return copy;
    }

    auto CartesianIndices::iterator::operator+=(difference_type n) -> iterator&
    {
        advance(n);
        return *this;
    }

    auto CartesianIndices::iterator::operator-=(difference_type n) -> iterator&
    {
        advance(-n);
        return *this;
    }

    auto operator+(const CartesianIndices::iterator& iter,
                   CartesianIndices::iterator::difference_type n) -> CartesianIndices::iterator
    {
        auto copy = iter;
        copy += n;
        return copy;
    }

    auto operator+(CartesianIndices::iterator::difference_type n,
                   const CartesianIndices::iterator& iter) -> CartesianIndices::iterator
    {
        return iter + n;
    }

    auto operator-(const CartesianIndices::iterator& iter,
                   CartesianIndices::iterator::difference_type n) -> CartesianIndices::iterator
    {
        auto copy = iter;
        copy -= n;
        return copy;
    }

    auto operator-(const CartesianIndices::iterator& lhs, const CartesianIndices::iterator& rhs)
        -> CartesianIndices::iterator::difference_type
    {
        return rhs.distance_to(lhs);
    }

    auto CartesianIndices::iterator::operator[](difference_type n) const -> value_type
    {
        // TODO: Make this more efficient
        auto copy = *this;
        copy += n;
        return *copy;
    }

    auto operator<(const CartesianIndices::iterator& lhs, const CartesianIndices::iterator& rhs)
        -> bool
    {
        // Not sure why I have to instantiate them this way, else it doesn't work...
        using Array = Eigen::Array<index_t, Eigen::Dynamic, 1>;
        const Array a1 = lhs.cur_.array();
        const Array a2 = rhs.cur_.array();

        return (a1 < a2).any();
    }

    auto operator>(const CartesianIndices::iterator& lhs, const CartesianIndices::iterator& rhs)
        -> bool
    {
        return rhs < lhs;
    }

    auto operator<=(const CartesianIndices::iterator& lhs, const CartesianIndices::iterator& rhs)
        -> bool
    {
        return !(lhs > rhs);
    }

    auto operator>=(const CartesianIndices::iterator& lhs, const CartesianIndices::iterator& rhs)
        -> bool
    {
        return !(lhs < rhs);
    }

    auto CartesianIndices::iterator::at_end() -> bool { return cur_[0] == ends_[0]; }

    auto CartesianIndices::iterator::at_end() const -> bool { return cur_[0] == ends_[0]; }

    auto CartesianIndices::iterator::inc_recursive(index_t N) -> void
    {
        auto& iter = cur_[N];

        if (++iter == ends_[N] && N > 0) {
            iter = begins_[N];
            inc_recursive(N - 1);
        }
    }

    auto CartesianIndices::iterator::inc() -> void { inc_recursive(cur_.size() - 1); }

    auto CartesianIndices::iterator::prev_recursive(index_t N) -> void
    {
        auto& iter = cur_[N];

        if (iter == begins_[N] && N > 0) {
            iter = ends_[N];
            inc_recursive(N - 1);
        }
        --iter;
    }

    auto CartesianIndices::iterator::prev() -> void { prev_recursive(cur_.size() - 1); }

    auto CartesianIndices::iterator::distance_to_recusrive(const iterator& other,
                                                           difference_type N) const
        -> difference_type
    {
        if (N == 0) {
            return other.cur_[N] - cur_[N];
        } else {
            const auto d = distance_to_recusrive(other, N - 1);
            const auto scale = ends_[N] - begins_[N];
            const auto increment = other.cur_[N] - cur_[N];

            return difference_type{(d * scale) + increment};
        }
    }

    auto CartesianIndices::iterator::distance_to(const iterator& other) const -> difference_type
    {
        auto N = cur_.size() - 1;
        return distance_to_recusrive(other, N);
    }

    auto CartesianIndices::iterator::advance_recursive(difference_type n, index_t N) -> void
    {
        if (n == 0) {
            return;
        }

        auto& iter = cur_[N];
        const auto size = ends_[N] - begins_[N];
        const auto first = begins_[N];

        auto const idx = as<difference_type>(iter - first);
        n += idx;

        auto div = size ? n / size : 0;
        auto mod = size ? n % size : 0;

        if (N != 0) {
            if (mod < 0) {
                mod += size;
                div--;
            }
            advance_recursive(div, N - 1);
        } else {
            if (div > 0) {
                mod = size;
            }
        }

        iter = first + mod;
    }

    auto CartesianIndices::iterator::advance(difference_type n) -> void
    {
        advance_recursive(n, cur_.size() - 1);
    }

    CartesianIndices neighbours_in_slice(const IndexVector_t& pos, const IndexVector_t& dist)
    {
        Eigen::IOFormat format(4, 0, ", ", "", "", "", "[", "]");
        IndexVector_t from = pos;
        from.tail(pos.size() - 1).array() -= dist.array();

        IndexVector_t to = pos;
        to.tail(pos.size() - 1).array() += dist.array() + 1;
        to[0] += 1; // Be sure this is incremented, so we actually iterate over it

        return {from, to};
    }

    CartesianIndices neighbours_in_slice(const IndexVector_t& pos, index_t dist)
    {
        return neighbours_in_slice(pos, IndexVector_t::Constant(pos.size() - 1, dist));
    }

    CartesianIndices neighbours_in_slice(const IndexVector_t& pos, const IndexVector_t& dist,
                                         const IndexVector_t& lower, const IndexVector_t& upper)
    {

        IndexVector_t from = pos;
        from.tail(pos.size() - 1).array() -= dist.array();
        from = from.cwiseMax(lower);

        IndexVector_t to = pos;
        to.tail(pos.size() - 1).array() += dist.array() + 1;
        to[0] += 1;
        to = to.cwiseMin(upper);

        return {from, to};
    }

    CartesianIndices neighbours_in_slice(const IndexVector_t& pos, index_t dist,
                                         const IndexVector_t& lower, const IndexVector_t& upper)
    {
        return neighbours_in_slice(pos, IndexVector_t::Constant(pos.size() - 1, dist), lower,
                                   upper);
    }
} // namespace elsa