Skip to content

Commit 4a17de8

Browse files
authored
Merge pull request #959 from PowerGridModel/feature/reduce-iterator-facade-complexity
reduce iterator facade complexity
2 parents 33c4176 + 85b24f1 commit 4a17de8

File tree

4 files changed

+13
-28
lines changed

4 files changed

+13
-28
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/auxiliary/dataset.hpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ template <typename T, dataset_type_tag dataset_type> class ColumnarAttributeRang
140140
constexpr auto dereference() const -> std::add_lvalue_reference_t<std::add_const_t<value_type>> {
141141
return current_;
142142
}
143-
constexpr auto equal(iterator const& other) const { return current_.idx_ == other.current_.idx_; }
143+
constexpr auto three_way_compare(iterator const& other) const { return current_.idx_ <=> other.current_.idx_; }
144144
constexpr auto distance_to(iterator const& other) const { return other.current_.idx_ - current_.idx_; }
145-
constexpr void increment() { ++current_.idx_; }
146-
constexpr void decrement() { --current_.idx_; }
147145
constexpr void advance(difference_type n) { current_.idx_ += n; }
148146

149147
Proxy current_;

power_grid_model_c/power_grid_model/include/power_grid_model/common/grouped_index_vector.hpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,6 @@ class SparseGroupedIdxVector {
124124
latest_value_ = value_type{(*indptr_)[group_], (*indptr_)[group_ + 1]};
125125
return latest_value_;
126126
}
127-
constexpr auto equal(GroupIterator const& other) const {
128-
assert(indptr_ == other.indptr_);
129-
return group_ == other.group_;
130-
}
131127
constexpr std::strong_ordering three_way_compare(GroupIterator const& other) const {
132128
assert(indptr_ == other.indptr_);
133129
return group_ <=> other.group_;
@@ -136,8 +132,6 @@ class SparseGroupedIdxVector {
136132
assert(indptr_ == other.indptr_);
137133
return other.group_ - group_;
138134
}
139-
constexpr void increment() { ++group_; }
140-
constexpr void decrement() { --group_; }
141135
constexpr void advance(Idx n) { group_ += n; }
142136
};
143137

@@ -210,7 +204,6 @@ class DenseGroupedIdxVector {
210204
narrow_cast<Idx>(std::distance(std::cbegin(*dense_vector_), group_range_.end()))};
211205
return latest_value_;
212206
}
213-
constexpr auto equal(GroupIterator const& other) const { return group_ == other.group_; }
214207
constexpr std::strong_ordering three_way_compare(GroupIterator const& other) const {
215208
return group_ <=> other.group_;
216209
}

power_grid_model_c/power_grid_model/include/power_grid_model/common/iterator_facade.hpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@ template <class Impl, typename ValueType, std::integral DifferenceType> class It
3232
constexpr auto operator->() -> decltype(auto) { return &(*(*this)); }
3333

3434
friend constexpr bool operator==(IteratorFacade const& first, IteratorFacade const& second) {
35-
return first.equal(second);
35+
return (first <=> second) == std::strong_ordering::equivalent;
3636
}
37-
friend constexpr std::strong_ordering operator<=>(IteratorFacade const& first, IteratorFacade const& second)
38-
requires requires(const_iterator it) {
39-
{ it.three_way_compare(it) } -> std::convertible_to<std::strong_ordering>;
40-
}
41-
{
37+
friend constexpr std::strong_ordering operator<=>(IteratorFacade const& first, IteratorFacade const& second) {
4238
return first.three_way_compare(second);
4339
}
4440

4541
constexpr auto operator++() -> iterator& {
46-
static_cast<iterator*>(this)->increment();
42+
if constexpr (requires(iterator it) { it.increment(); }) {
43+
static_cast<iterator*>(this)->increment();
44+
} else {
45+
static_cast<iterator*>(this)->advance(1);
46+
}
4747
return *static_cast<iterator*>(this);
4848
}
4949
constexpr auto operator--() -> iterator& {
50-
static_cast<iterator*>(this)->decrement();
50+
if constexpr (requires(iterator it) { it.decrement(); }) {
51+
static_cast<iterator*>(this)->decrement();
52+
} else {
53+
static_cast<iterator*>(this)->advance(-1);
54+
}
5155
return *static_cast<iterator*>(this);
5256
}
5357
constexpr auto operator++(std::integral auto /*idx*/) -> iterator {
@@ -81,10 +85,6 @@ template <class Impl, typename ValueType, std::integral DifferenceType> class It
8185

8286
private:
8387
// overloads for public bidirectional exposure (difference between MSVC and ClangCL)
84-
constexpr bool equal(IteratorFacade const& other) const {
85-
return static_cast<std::add_lvalue_reference_t<const_iterator>>(*this).equal(
86-
static_cast<std::add_lvalue_reference_t<const_iterator>>(other));
87-
}
8888
constexpr std::strong_ordering three_way_compare(IteratorFacade const& other) const {
8989
return static_cast<std::add_lvalue_reference_t<const_iterator>>(*this).three_way_compare(
9090
static_cast<std::add_lvalue_reference_t<const_iterator>>(other));

power_grid_model_c/power_grid_model/include/power_grid_model/container.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -309,16 +309,10 @@ class Container<RetrievableTypes<GettableTypes...>, StorageableTypes...> {
309309
return container_ptr_->template get_item_by_seq<base_type>(idx_);
310310
}
311311
constexpr Gettable& dereference() { return container_ptr_->template get_item_by_seq<base_type>(idx_); }
312-
constexpr bool equal(Iterator const& other) const {
313-
assert(container_ptr_ == other.container_ptr_);
314-
return idx_ == other.idx_;
315-
}
316312
constexpr auto three_way_compare(Iterator const& other) const {
317313
assert(container_ptr_ == other.container_ptr_);
318314
return idx_ <=> other.idx_;
319315
}
320-
constexpr void increment() { ++idx_; }
321-
constexpr void decrement() { --idx_; }
322316
constexpr void advance(Idx n) { idx_ += n; }
323317
constexpr Idx distance_to(Iterator const& other) const {
324318
assert(container_ptr_ == other.container_ptr_);

0 commit comments

Comments
 (0)