From 8ffc9037d78b4c94c4c946e145bdd8cf96166278 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Sat, 14 Dec 2024 16:07:23 +0100 Subject: [PATCH 01/11] add note and support for `std::forward_iterator` --- doc/collections_as_container.md | 14 ++++--- python/templates/macros/iterator.jinja2 | 4 +- tests/unittests/std_interoperability.cpp | 48 ++++++++++++++++++------ 3 files changed, 49 insertions(+), 17 deletions(-) diff --git a/doc/collections_as_container.md b/doc/collections_as_container.md index 860d21b8a..b936309a6 100644 --- a/doc/collections_as_container.md +++ b/doc/collections_as_container.md @@ -88,11 +88,15 @@ In the following tables a convention from `Collection` is used: `iterator` stand | `std::input_or_output_iterator` | ✔️ yes | ✔️ yes | | `std::input_iterator` | ✔️ yes | ✔️ yes | | `std::output_iterator` | ❌ no | ❌ no | -| `std::forward_iterator` | ❌ no | ❌ no | +| `std::forward_iterator` | ✔️ yes (see note below) | ✔️ yes (see note below) | | `std::bidirectional_iterator` | ❌ no | ❌ no | | `std::random_access_iterator` | ❌ no | ❌ no | | `std::contiguous_iterator` | ❌ no | ❌ no | +> [!NOTE] +>The collections' iterators fulfil the `std::forward_iterator` except that the pointers obtained with `->` remain valid only as long as the iterator is valid instead of as long as the range remain valid. In practice this means a `ptr` obtained with `auto* ptr = it.operator->();` is valid only as long as `it` is valid. +>The values obtained immediately through `->` (for instance `auto& e = it->energy();`) behaves as expected for `std::forward_iterator` as their validity is tied to the validity of a collection. + ### LegacyIterator | Requirement | Fulfilled by `iterator`/`const_iterator`? | Comment | @@ -180,7 +184,7 @@ In addition to the *LegacyForwardIterator* the C++ standard specifies also the * | `std::ranges::sized_range` | ✔️ yes | | `std::ranges::input_range` | ✔️ yes | | `std::ranges::output_range` | ❌ no | -| `std::ranges::forward_range` | ❌ no | +| `std::ranges::forward_range` | ✔️ yes | | `std::ranges::bidirectional_range` | ❌ no | | `std::ranges::random_access_range` | ❌ no | | `std::ranges::contiguous_range` | ❌ no | @@ -207,16 +211,16 @@ std::sort(std::begin(collection), std::end(collection)); // requires RandomAcces The arguments of standard range algorithms are checked at compile time and must fulfil certain iterator concepts, such as `std::input_iterator` or `std::ranges::input_range`. -The iterators of PODIO collections model the `std::input_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. +The iterators of PODIO collections model the `std::forward_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. In particular, the PODIO collections' iterators do not fulfil the `std::output_iterator` concept, and as a result, mutating algorithms relying on this iterator type will not compile. -Similarly the collections themselves model the `std::input_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. +Similarly the collections themselves model the `std::forward_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. For example: ```c++ std::ranges::find_if(collection, predicate ); // requires input_range -> OK -std::ranges::adjacent_find(collection, predicate ); // requires forward_range -> won't compile +std::ranges::adjacent_find(collection, predicate ); // requires forward_range -> OK std::ranges::fill(collection, value ); // requires output_range -> won't compile std::ranges::sort(collection); // requires random_access_range and sortable -> won't compile ``` diff --git a/python/templates/macros/iterator.jinja2 b/python/templates/macros/iterator.jinja2 index d3b512f94..114fbc9f2 100644 --- a/python/templates/macros/iterator.jinja2 +++ b/python/templates/macros/iterator.jinja2 @@ -8,7 +8,9 @@ public: using reference = {{ prefix }}{{ class.bare_type }}; using pointer = {{ prefix }}{{ class.bare_type }}*; using iterator_category = std::input_iterator_tag; - using iterator_concept = std::input_iterator_tag; + // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` + // remain valid as long as the iterator is valid, not as long as the range is valid. + using iterator_concept = std::forward_iterator_tag; {{ iterator_type }}(size_t index, const {{ class.bare_type }}ObjPointerContainer* collection) : m_index(index), m_object({{ ptr_init }}), m_collection(collection) {} {{ iterator_type }}() = default; diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 6f4acc890..44271aea6 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -495,6 +495,35 @@ TEST_CASE("Collection and iterator concepts", "[collection][container][iterator] // const_iterator STATIC_REQUIRE(std::input_iterator); } + + SECTION("forward_iterator") { + // iterator + STATIC_REQUIRE(std::forward_iterator); + { + REQUIRE(iterator{} == iterator{}); + auto coll = CollectionType(); + coll.create(); + auto i = coll.begin(); + auto j = coll.begin(); + REQUIRE(i == j); + REQUIRE(++i == ++j); + i = coll.begin(); + REQUIRE(((void)[](auto x) { ++x; }(i), *i) == *i); + } + // const_iterator + STATIC_REQUIRE(std::forward_iterator); + { + REQUIRE(const_iterator{} == const_iterator{}); + auto coll = CollectionType(); + coll.create(); + auto i = coll.cbegin(); + auto j = coll.cbegin(); + REQUIRE(i == j); + REQUIRE(++i == ++j); + i = coll.cbegin(); + REQUIRE(((void)[](auto x) { ++x; }(i), *i) == *i); + } + } } TEST_CASE("Collection and unsupported iterator concepts", "[collection][container][iterator][std]") { @@ -508,9 +537,6 @@ TEST_CASE("Collection and unsupported iterator concepts", "[collection][containe DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); - // std::forward_iterator - DOCUMENTED_STATIC_FAILURE(std::forward_iterator); - DOCUMENTED_STATIC_FAILURE(std::forward_iterator); // std::bidirectional_iterator DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); @@ -1085,7 +1111,7 @@ TEST_CASE("Collection as range", "[collection][ranges][std]") { DOCUMENTED_STATIC_FAILURE(std::ranges::output_range); DOCUMENTED_STATIC_FAILURE(std::ranges::output_range); // std::range::forward_range - DOCUMENTED_STATIC_FAILURE(std::ranges::forward_range); + STATIC_REQUIRE(std::ranges::forward_range); // std::range::bidirectional_range DOCUMENTED_STATIC_FAILURE(std::ranges::bidirectional_range); // std::range::random_access_range @@ -1154,13 +1180,14 @@ TEST_CASE("Collection and std ranges algorithms", "[collection][ranges][std]") { REQUIRE(subcoll.size() == 2); REQUIRE(subcoll[0].cellID() == 5); REQUIRE(subcoll[1].cellID() == 3); -} -// helper concept for unsupported algorithm compilation test -template -concept is_range_adjacent_findable = requires(T coll) { - std::ranges::adjacent_find(coll, [](const auto& a, const auto& b) { return a.cellID() == b.cellID(); }); -}; + auto adjacent_it = + std::ranges::adjacent_find(coll, [](const auto& a, const auto& b) { return a.cellID() == b.cellID(); }); + REQUIRE(adjacent_it != std::end(coll)); + auto target = std::begin(coll); + std::ranges::advance(target, 2); + REQUIRE(adjacent_it == target); +} // helper concept for unsupported algorithm compilation test template @@ -1173,7 +1200,6 @@ concept is_range_fillable = requires(T coll) { std::ranges::fill(coll, typename TEST_CASE("Collection and unsupported std ranges algorithms", "[collection][ranges][std]") { // check that algorithms requiring unsupported iterator concepts won't compile - DOCUMENTED_STATIC_FAILURE(is_range_adjacent_findable); DOCUMENTED_STATIC_FAILURE(is_range_sortable); DOCUMENTED_STATIC_FAILURE(is_range_fillable); } From bb84bcc1deca716d54cac4d245172f8bad2770c8 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Sat, 14 Dec 2024 17:56:41 +0100 Subject: [PATCH 02/11] add support for `std::bidirectional_iterator` --- doc/collections_as_container.md | 11 +-- python/templates/macros/iterator.jinja2 | 15 +++- tests/unittests/std_interoperability.cpp | 109 +++++++++++++++++++++-- 3 files changed, 121 insertions(+), 14 deletions(-) diff --git a/doc/collections_as_container.md b/doc/collections_as_container.md index b936309a6..0bb6a495c 100644 --- a/doc/collections_as_container.md +++ b/doc/collections_as_container.md @@ -89,7 +89,7 @@ In the following tables a convention from `Collection` is used: `iterator` stand | `std::input_iterator` | ✔️ yes | ✔️ yes | | `std::output_iterator` | ❌ no | ❌ no | | `std::forward_iterator` | ✔️ yes (see note below) | ✔️ yes (see note below) | -| `std::bidirectional_iterator` | ❌ no | ❌ no | +| `std::bidirectional_iterator` | ✔️ yes | ✔️ yes | | `std::random_access_iterator` | ❌ no | ❌ no | | `std::contiguous_iterator` | ❌ no | ❌ no | @@ -166,7 +166,7 @@ In addition to the *LegacyForwardIterator* the C++ standard specifies also the * | Adaptor | Compatible with Collection? | Comment | |---------|-----------------------------|---------| -| `std::reverse_iterator` | ❌ no | `iterator` and `const_iterator` not *LegacyBidirectionalIterator* or `std::bidirectional_iterator` | +| `std::reverse_iterator` | ❗ attention | `operator->` not defined as `iterator`'s and `const_iterator`'s `operator->` are non-`const` | | `std::back_insert_iterator` | ❗ attention | Compatible only with SubsetCollections, otherwise throws `std::invalid_argument` | | `std::front_insert_iterator` | ❌ no | `push_front` not defined | | `std::insert_iterator` | ❌ no | `insert` not defined | @@ -185,7 +185,7 @@ In addition to the *LegacyForwardIterator* the C++ standard specifies also the * | `std::ranges::input_range` | ✔️ yes | | `std::ranges::output_range` | ❌ no | | `std::ranges::forward_range` | ✔️ yes | -| `std::ranges::bidirectional_range` | ❌ no | +| `std::ranges::bidirectional_range` | ✔️ yes | | `std::ranges::random_access_range` | ❌ no | | `std::ranges::contiguous_range` | ❌ no | | `std::ranges::common_range` | ✔️ yes | @@ -211,16 +211,17 @@ std::sort(std::begin(collection), std::end(collection)); // requires RandomAcces The arguments of standard range algorithms are checked at compile time and must fulfil certain iterator concepts, such as `std::input_iterator` or `std::ranges::input_range`. -The iterators of PODIO collections model the `std::forward_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. +The iterators of PODIO collections model the `std::bidirectional_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. In particular, the PODIO collections' iterators do not fulfil the `std::output_iterator` concept, and as a result, mutating algorithms relying on this iterator type will not compile. -Similarly the collections themselves model the `std::forward_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. +Similarly the collections themselves model the `std::bidirectional_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. For example: ```c++ std::ranges::find_if(collection, predicate ); // requires input_range -> OK std::ranges::adjacent_find(collection, predicate ); // requires forward_range -> OK +std::ranges::views::reverse(collection); //requires bidirectional_range -> OK std::ranges::fill(collection, value ); // requires output_range -> won't compile std::ranges::sort(collection); // requires random_access_range and sortable -> won't compile ``` diff --git a/python/templates/macros/iterator.jinja2 b/python/templates/macros/iterator.jinja2 index 114fbc9f2..803c475fe 100644 --- a/python/templates/macros/iterator.jinja2 +++ b/python/templates/macros/iterator.jinja2 @@ -10,7 +10,7 @@ public: using iterator_category = std::input_iterator_tag; // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` // remain valid as long as the iterator is valid, not as long as the range is valid. - using iterator_concept = std::forward_iterator_tag; + using iterator_concept = std::bidirectional_iterator_tag; {{ iterator_type }}(size_t index, const {{ class.bare_type }}ObjPointerContainer* collection) : m_index(index), m_object({{ ptr_init }}), m_collection(collection) {} {{ iterator_type }}() = default; @@ -33,6 +33,8 @@ public: pointer operator->(); {{ iterator_type }}& operator++(); {{ iterator_type }} operator++(int); + {{ iterator_type }}& operator--(); + {{ iterator_type }} operator--(int); private: size_t m_index{0}; @@ -66,5 +68,16 @@ private: return copy; } +{{ iterator_type }}& {{ iterator_type }}::operator--() { + --m_index; + return *this; +} + +{{ iterator_type }} {{ iterator_type }}::operator--(int) { + auto copy = *this; + --m_index; + return copy; +} + {% endwith %} {% endmacro %} diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 44271aea6..53f4e65bc 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -524,6 +524,51 @@ TEST_CASE("Collection and iterator concepts", "[collection][container][iterator] REQUIRE(((void)[](auto x) { ++x; }(i), *i) == *i); } } + + SECTION("bidirectional_iterator") { + // iterator + STATIC_REQUIRE(std::bidirectional_iterator); + { + auto coll = CollectionType(); + coll.create(); + auto a = ++coll.begin(); + REQUIRE(std::addressof(--a) == std::addressof(a)); + a = ++coll.begin(); + auto b = ++coll.begin(); + REQUIRE(a == b); + REQUIRE(a-- == b); + a = ++coll.begin(); + REQUIRE(a == b); + a--; + --b; + REQUIRE(a == b); + a = ++coll.begin(); + b = ++coll.begin(); + REQUIRE(a == b); + REQUIRE(--(++a) == b); + REQUIRE(++(--a) == b); + } + // const_iterator + STATIC_REQUIRE(std::bidirectional_iterator); + auto coll = CollectionType(); + coll.create(); + auto a = ++coll.cbegin(); + REQUIRE(std::addressof(--a) == std::addressof(a)); + a = ++coll.cbegin(); + auto b = ++coll.cbegin(); + REQUIRE(a == b); + REQUIRE(a-- == b); + a = ++coll.cbegin(); + REQUIRE(a == b); + a--; + --b; + REQUIRE(a == b); + a = ++coll.cbegin(); + b = ++coll.cbegin(); + REQUIRE(a == b); + REQUIRE(--(++a) == b); + REQUIRE(++(--a) == b); + } } TEST_CASE("Collection and unsupported iterator concepts", "[collection][container][iterator][std]") { @@ -537,9 +582,6 @@ TEST_CASE("Collection and unsupported iterator concepts", "[collection][containe DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); - // std::bidirectional_iterator - DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); - DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); // std::random_access_iterator DOCUMENTED_STATIC_FAILURE(std::random_access_iterator); DOCUMENTED_STATIC_FAILURE(std::random_access_iterator); @@ -948,15 +990,63 @@ TEST_CASE("Collection and std iterator adaptors", "[collection][container][adapt STATIC_REQUIRE(traits::has_iterator_category_v>); DOCUMENTED_STATIC_FAILURE( std::is_base_of_v::iterator_category>); - DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); - // TODO add runtime checks here + STATIC_REQUIRE(std::bidirectional_iterator); + { + auto coll = CollectionType(); + coll.create().cellID(42); + coll.create().cellID(43); + auto rit = std::reverse_iterator(std::end(coll)); + DOCUMENTED_STATIC_FAILURE( + traits::has_member_of_pointer_v); // can't -> because + // std::reverse_iterator::operator->() + // is const + // REQUIRE(rit->cellID() == 43); + REQUIRE((*rit).cellID() == 43); + ++rit; + // REQUIRE(rit->cellID() == 42); + REQUIRE((*rit).cellID() == 42); + REQUIRE(rit.base()->cellID() == 43); + rit = std::reverse_iterator(std::begin(coll)); + REQUIRE(rit.base()->cellID() == 42); + --rit; + // REQUIRE(rit->cellID() == 42); + REQUIRE((*rit).cellID() == 42); + REQUIRE(rit.base()->cellID() == 43); + --rit; + // REQUIRE(rit->cellID() == 43); + REQUIRE((*rit).cellID() == 43); + } // const_iterator STATIC_REQUIRE(traits::has_const_iterator_v); STATIC_REQUIRE(traits::has_iterator_category_v>); DOCUMENTED_STATIC_FAILURE( std::is_base_of_v::iterator_category>); - DOCUMENTED_STATIC_FAILURE(std::bidirectional_iterator); - // TODO add runtime checks here + STATIC_REQUIRE(std::bidirectional_iterator); + { + auto coll = CollectionType(); + coll.create().cellID(42); + coll.create().cellID(43); + auto rit = std::reverse_iterator(std::cend(coll)); + DOCUMENTED_STATIC_FAILURE( + traits::has_member_of_pointer_v); // can't -> because + // std::reverse_iterator::operator->() + // is const + // REQUIRE(rit->cellID() == 43); + REQUIRE((*rit).cellID() == 43); + ++rit; + // REQUIRE(rit->cellID() == 42); + REQUIRE((*rit).cellID() == 42); + REQUIRE(rit.base()->cellID() == 43); + rit = std::reverse_iterator(std::cbegin(coll)); + REQUIRE(rit.base()->cellID() == 42); + --rit; + // REQUIRE(rit->cellID() == 42); + REQUIRE((*rit).cellID() == 42); + REQUIRE(rit.base()->cellID() == 43); + --rit; + // REQUIRE(rit->cellID() == 43); + REQUIRE((*rit).cellID() == 43); + } } SECTION("Back inserter") { DOCUMENTED_STATIC_FAILURE(traits::has_const_reference_v); @@ -1113,7 +1203,7 @@ TEST_CASE("Collection as range", "[collection][ranges][std]") { // std::range::forward_range STATIC_REQUIRE(std::ranges::forward_range); // std::range::bidirectional_range - DOCUMENTED_STATIC_FAILURE(std::ranges::bidirectional_range); + STATIC_REQUIRE(std::ranges::bidirectional_range); // std::range::random_access_range DOCUMENTED_STATIC_FAILURE(std::ranges::random_access_range); // std::range::contiguous_range @@ -1187,6 +1277,9 @@ TEST_CASE("Collection and std ranges algorithms", "[collection][ranges][std]") { auto target = std::begin(coll); std::ranges::advance(target, 2); REQUIRE(adjacent_it == target); + + auto rev_view = std::ranges::views::reverse(coll); + REQUIRE(rev_view.front().cellID() == 3); } // helper concept for unsupported algorithm compilation test From 80fbccc3829940026aca9fa27862e02da1003a01 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Sat, 14 Dec 2024 18:39:33 +0100 Subject: [PATCH 03/11] add reverse begin and end methods to collection --- python/templates/Collection.h.jinja2 | 22 ++++++++++++++++++++++ tests/unittests/unittest.cpp | 20 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/python/templates/Collection.h.jinja2 b/python/templates/Collection.h.jinja2 index 53be2ddb3..892341e11 100644 --- a/python/templates/Collection.h.jinja2 +++ b/python/templates/Collection.h.jinja2 @@ -50,6 +50,8 @@ public: using iterator = {{ class.bare_type }}MutableCollectionIterator; using difference_type = ptrdiff_t; using size_type = size_t; + using const_reverse_iterator = std::reverse_iterator; + using reverse_iterator = std::reverse_iterator; {{ class.bare_type }}Collection(); {{ class.bare_type }}Collection({{ class.bare_type }}CollectionData&& data, bool isSubsetColl); @@ -167,6 +169,26 @@ public: const_iterator cend() const { return end(); } + // reverse iterators + reverse_iterator rbegin() { + return reverse_iterator(end()); + } + const_reverse_iterator rbegin() const { + return const_reverse_iterator(end()); + } + const_reverse_iterator crbegin() const { + return rbegin(); + } + reverse_iterator rend() { + return reverse_iterator(begin()); + } + const_reverse_iterator rend() const { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crend() const { + return rend(); + } + {% for member in Members %} std::vector<{{ member.full_type }}> {{ member.name }}(const size_t nElem = 0) const; diff --git a/tests/unittests/unittest.cpp b/tests/unittests/unittest.cpp index 0e0210915..ca3f8bf54 100644 --- a/tests/unittests/unittest.cpp +++ b/tests/unittests/unittest.cpp @@ -206,6 +206,26 @@ TEST_CASE("Looping", "[basics]") { } } +TEST_CASE("Reverse iterators", "[basics]") { + auto coll = ExampleHitCollection(); + coll.create(); + coll.create(); + auto it = std::rbegin(coll); + (*it).energy(43); + (*++it).energy(42); + REQUIRE((*it).energy() == 42); + REQUIRE((*--it).energy() == 43); + it = std::rend(coll); + REQUIRE((*--it).energy() == 42); + REQUIRE((*--it).energy() == 43); + auto cit = std::crbegin(coll); + REQUIRE((*cit).energy() == 43); + REQUIRE((*++cit).energy() == 42); + cit = std::crend(coll); + REQUIRE((*--cit).energy() == 42); + REQUIRE((*--cit).energy() == 43); +} + TEST_CASE("Notebook", "[basics]") { auto hits = ExampleHitCollection(); for (unsigned i = 0; i < 12; ++i) { From ea3141abd628285cc15d9eed33c7d13f344b40e2 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Sun, 15 Dec 2024 00:05:02 +0100 Subject: [PATCH 04/11] add support for `std::random_access_iterator` --- doc/collections_as_container.md | 8 +- python/templates/macros/iterator.jinja2 | 47 ++++++- tests/unittests/std_interoperability.cpp | 164 ++++++++++++++++++++++- 3 files changed, 208 insertions(+), 11 deletions(-) diff --git a/doc/collections_as_container.md b/doc/collections_as_container.md index 0bb6a495c..f045d77cb 100644 --- a/doc/collections_as_container.md +++ b/doc/collections_as_container.md @@ -90,7 +90,7 @@ In the following tables a convention from `Collection` is used: `iterator` stand | `std::output_iterator` | ❌ no | ❌ no | | `std::forward_iterator` | ✔️ yes (see note below) | ✔️ yes (see note below) | | `std::bidirectional_iterator` | ✔️ yes | ✔️ yes | -| `std::random_access_iterator` | ❌ no | ❌ no | +| `std::random_access_iterator` | ✔️ yes | ✔️ yes | | `std::contiguous_iterator` | ❌ no | ❌ no | > [!NOTE] @@ -186,7 +186,7 @@ In addition to the *LegacyForwardIterator* the C++ standard specifies also the * | `std::ranges::output_range` | ❌ no | | `std::ranges::forward_range` | ✔️ yes | | `std::ranges::bidirectional_range` | ✔️ yes | -| `std::ranges::random_access_range` | ❌ no | +| `std::ranges::random_access_range` | ✔️ yes | | `std::ranges::contiguous_range` | ❌ no | | `std::ranges::common_range` | ✔️ yes | | `std::ranges::viewable_range` | ✔️ yes | @@ -211,11 +211,11 @@ std::sort(std::begin(collection), std::end(collection)); // requires RandomAcces The arguments of standard range algorithms are checked at compile time and must fulfil certain iterator concepts, such as `std::input_iterator` or `std::ranges::input_range`. -The iterators of PODIO collections model the `std::bidirectional_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. +The iterators of PODIO collections model the `std::random_access_iterator` concept, so range algorithms that require this iterator type will work correctly with PODIO iterators. If an algorithm compiles, it is guaranteed to work as expected. In particular, the PODIO collections' iterators do not fulfil the `std::output_iterator` concept, and as a result, mutating algorithms relying on this iterator type will not compile. -Similarly the collections themselves model the `std::bidirectional_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. +Similarly the collections themselves model the `std::random_access_range` concept and can be used in the range algorithms that require that concept. The algorithms requiring unsupported range concept, such as `std::output_range`, won't compile. For example: ```c++ diff --git a/python/templates/macros/iterator.jinja2 b/python/templates/macros/iterator.jinja2 index 803c475fe..f56b7287c 100644 --- a/python/templates/macros/iterator.jinja2 +++ b/python/templates/macros/iterator.jinja2 @@ -10,7 +10,7 @@ public: using iterator_category = std::input_iterator_tag; // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` // remain valid as long as the iterator is valid, not as long as the range is valid. - using iterator_concept = std::bidirectional_iterator_tag; + using iterator_concept = std::random_access_iterator_tag; {{ iterator_type }}(size_t index, const {{ class.bare_type }}ObjPointerContainer* collection) : m_index(index), m_object({{ ptr_init }}), m_collection(collection) {} {{ iterator_type }}() = default; @@ -21,8 +21,8 @@ public: {{ iterator_type }}& operator=({{iterator_type}}&&) = default; ~{{ iterator_type }}() = default; - bool operator!=(const {{ iterator_type}}& x) const { - return m_index != x.m_index; + auto operator<=>(const {{ iterator_type}}& other) const { + return m_index <=> other.m_index; } bool operator==(const {{ iterator_type }}& x) const { @@ -35,6 +35,13 @@ public: {{ iterator_type }} operator++(int); {{ iterator_type }}& operator--(); {{ iterator_type }} operator--(int); + {{ iterator_type }}& operator+=(difference_type n); + {{ iterator_type }} operator+(difference_type n) const; + friend {{ iterator_type }} operator+(difference_type n, const {{ iterator_type }}& it); + {{ iterator_type }}& operator-=(difference_type n); + {{ iterator_type }} operator-(difference_type n) const; + reference operator[](difference_type n) const; + difference_type operator-(const {{ iterator_type }}& other) const; private: size_t m_index{0}; @@ -79,5 +86,39 @@ private: return copy; } +{{ iterator_type }}& {{ iterator_type }}::operator+=(difference_type n) { + m_index += n; + return *this; +} + +{{ iterator_type }} {{ iterator_type }}::operator+(difference_type n) const { + auto copy = *this; + copy += n; + return copy; +} + +{{ iterator_type }} operator+({{ iterator_type }}::difference_type n, const {{ iterator_type }}& it) { + return it + n; +} + +{{ iterator_type }}& {{ iterator_type }}::operator-=(difference_type n) { + m_index -= n; + return *this; +} + +{{ iterator_type }} {{ iterator_type }}::operator-(difference_type n) const { + auto copy = *this; + copy -= n; + return copy; +} + +{{ iterator_type }}::reference {{ iterator_type }}::operator[](difference_type n) const { + return reference{ {{ ptr_type }}((*m_collection)[m_index + n]) }; +} + +{{ iterator_type }}::difference_type {{ iterator_type }}::operator-(const {{ iterator_type }}& other) const { + return m_index - other.m_index; +} + {% endwith %} {% endmacro %} diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 53f4e65bc..de330c3aa 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -569,6 +569,165 @@ TEST_CASE("Collection and iterator concepts", "[collection][container][iterator] REQUIRE(--(++a) == b); REQUIRE(++(--a) == b); } + + SECTION("random_access_iterator") { + // iterator + STATIC_REQUIRE(std::totally_ordered); + { + auto coll = CollectionType(); + coll.create(); + coll.create(); + coll.create(); + auto a = coll.begin(); + auto b = coll.begin(); + REQUIRE(!(a < b)); + REQUIRE(!(a > b)); + REQUIRE((a == b)); + b = ++coll.begin(); + REQUIRE((a < b)); + REQUIRE(!(a > b)); + REQUIRE(!(a == b)); + a = ++coll.begin(); + b = coll.begin(); + REQUIRE(!(a < b)); + REQUIRE(a > b); + REQUIRE(!(a == b)); + auto c = coll.begin(); + a = c++; + b = c++; + REQUIRE(a < b); + REQUIRE(b < c); + REQUIRE(a < c); + REQUIRE((a > b) == (b < a)); + REQUIRE((a >= b) == !(a < b)); + REQUIRE((a <= b) == !(a > b)); + } + STATIC_REQUIRE(std::sized_sentinel_for); + { + auto coll = CollectionType(); + coll.create(); + auto i = coll.begin(); + auto s = coll.end(); + auto n = 1; + REQUIRE(s - i == n); + REQUIRE(i - s == -n); + } + STATIC_REQUIRE(std::random_access_iterator); + { + auto coll = CollectionType(); + coll.create().cellID(42); + coll.create().cellID(43); + coll.create().cellID(44); + coll.create().cellID(45); + auto a = coll.begin(); + auto n = 2; + auto b = a + n; + REQUIRE((a += n) == b); + a = coll.begin(); + REQUIRE(std::addressof(a += n) == std::addressof(a)); + a = coll.begin(); + auto k = a + n; + REQUIRE(k == (a += n)); + a = coll.begin(); + REQUIRE((a + n) == (n + a)); + auto x = 1; + auto y = 2; + REQUIRE((a + (x + y)) == ((a + x) + y)); + REQUIRE((a + 0) == a); + b = a + n; + REQUIRE((--b) == (a + n - 1)); + b = a + n; + REQUIRE((b += -n) == a); + b = a + n; + REQUIRE((b -= +n) == a); + b = a + n; + REQUIRE(std::addressof(b -= n) == std::addressof(b)); + b = a + n; + k = b - n; + REQUIRE(k == (b -= n)); + b = a + n; + REQUIRE(a[n] == *b); + REQUIRE(a <= b); + } + // const_iterator + STATIC_REQUIRE(std::totally_ordered); + { + auto coll = CollectionType(); + coll.create(); + coll.create(); + coll.create(); + auto a = coll.cbegin(); + auto b = coll.cbegin(); + REQUIRE(!(a < b)); + REQUIRE(!(a > b)); + REQUIRE((a == b)); + b = ++coll.cbegin(); + REQUIRE((a < b)); + REQUIRE(!(a > b)); + REQUIRE(!(a == b)); + a = ++coll.cbegin(); + b = coll.cbegin(); + REQUIRE(!(a < b)); + REQUIRE(a > b); + REQUIRE(!(a == b)); + auto c = coll.cbegin(); + a = c++; + b = c++; + REQUIRE(a < b); + REQUIRE(b < c); + REQUIRE(a < c); + REQUIRE((a > b) == (b < a)); + REQUIRE((a >= b) == !(a < b)); + REQUIRE((a <= b) == !(a > b)); + } + STATIC_REQUIRE(std::sized_sentinel_for); + { + auto coll = CollectionType(); + coll.create(); + auto i = coll.cbegin(); + auto s = coll.cend(); + auto n = 1; + REQUIRE(s - i == n); + REQUIRE(i - s == -n); + } + STATIC_REQUIRE(std::random_access_iterator); + { + auto coll = CollectionType(); + coll.create().cellID(42); + coll.create().cellID(43); + coll.create().cellID(44); + coll.create().cellID(45); + auto a = coll.cbegin(); + auto n = 2; + auto b = a + n; + REQUIRE((a += n) == b); + a = coll.cbegin(); + REQUIRE(std::addressof(a += n) == std::addressof(a)); + a = coll.cbegin(); + auto k = a + n; + REQUIRE(k == (a += n)); + a = coll.cbegin(); + REQUIRE((a + n) == (n + a)); + auto x = 1; + auto y = 2; + REQUIRE((a + (x + y)) == ((a + x) + y)); + REQUIRE((a + 0) == a); + b = a + n; + REQUIRE((--b) == (a + n - 1)); + b = a + n; + REQUIRE((b += -n) == a); + b = a + n; + REQUIRE((b -= +n) == a); + b = a + n; + REQUIRE(std::addressof(b -= n) == std::addressof(b)); + b = a + n; + k = b - n; + REQUIRE(k == (b -= n)); + b = a + n; + REQUIRE(a[n] == *b); + REQUIRE(a <= b); + } + } } TEST_CASE("Collection and unsupported iterator concepts", "[collection][container][iterator][std]") { @@ -582,9 +741,6 @@ TEST_CASE("Collection and unsupported iterator concepts", "[collection][containe DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); - // std::random_access_iterator - DOCUMENTED_STATIC_FAILURE(std::random_access_iterator); - DOCUMENTED_STATIC_FAILURE(std::random_access_iterator); // std::contiguous_iterator DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); @@ -1205,7 +1361,7 @@ TEST_CASE("Collection as range", "[collection][ranges][std]") { // std::range::bidirectional_range STATIC_REQUIRE(std::ranges::bidirectional_range); // std::range::random_access_range - DOCUMENTED_STATIC_FAILURE(std::ranges::random_access_range); + STATIC_REQUIRE(std::ranges::random_access_range); // std::range::contiguous_range DOCUMENTED_STATIC_FAILURE(std::ranges::contiguous_range); // std::range::common_range From 6ccdde19efb510bcaafb8ad82c4036a4d5466e17 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 20 Dec 2024 00:26:13 +0100 Subject: [PATCH 05/11] add more detailed checks for `contiguous_iterator` --- tests/unittests/std_interoperability.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index de330c3aa..2cf2f4808 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -742,7 +742,11 @@ TEST_CASE("Collection and unsupported iterator concepts", "[collection][containe DOCUMENTED_STATIC_FAILURE(std::output_iterator); DOCUMENTED_STATIC_FAILURE(std::output_iterator); // std::contiguous_iterator + DOCUMENTED_STATIC_FAILURE(std::is_lvalue_reference_v); + DOCUMENTED_STATIC_FAILURE(std::same_as>); DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); + DOCUMENTED_STATIC_FAILURE(std::is_lvalue_reference_v); + STATIC_REQUIRE(std::same_as>); DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); } From 93e461299bfebde3d47969a6f23726ca442c2ac6 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Fri, 20 Dec 2024 10:33:11 +0100 Subject: [PATCH 06/11] fix format --- doc/collections_as_container.md | 4 ++-- python/templates/Collection.h.jinja2 | 2 +- tests/unittests/std_interoperability.cpp | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/doc/collections_as_container.md b/doc/collections_as_container.md index f045d77cb..7bf37c7bd 100644 --- a/doc/collections_as_container.md +++ b/doc/collections_as_container.md @@ -93,8 +93,8 @@ In the following tables a convention from `Collection` is used: `iterator` stand | `std::random_access_iterator` | ✔️ yes | ✔️ yes | | `std::contiguous_iterator` | ❌ no | ❌ no | -> [!NOTE] ->The collections' iterators fulfil the `std::forward_iterator` except that the pointers obtained with `->` remain valid only as long as the iterator is valid instead of as long as the range remain valid. In practice this means a `ptr` obtained with `auto* ptr = it.operator->();` is valid only as long as `it` is valid. +> [!NOTE] +>The collections' iterators fulfil the `std::forward_iterator` except that the pointers obtained with `->` remain valid only as long as the iterator is valid instead of as long as the range remain valid. In practice this means a `ptr` obtained with `auto* ptr = it.operator->();` is valid only as long as `it` is valid. >The values obtained immediately through `->` (for instance `auto& e = it->energy();`) behaves as expected for `std::forward_iterator` as their validity is tied to the validity of a collection. ### LegacyIterator diff --git a/python/templates/Collection.h.jinja2 b/python/templates/Collection.h.jinja2 index 892341e11..1ee2364c7 100644 --- a/python/templates/Collection.h.jinja2 +++ b/python/templates/Collection.h.jinja2 @@ -187,7 +187,7 @@ public: } const_reverse_iterator crend() const { return rend(); - } + } {% for member in Members %} diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 2cf2f4808..1202374a2 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -743,10 +743,10 @@ TEST_CASE("Collection and unsupported iterator concepts", "[collection][containe DOCUMENTED_STATIC_FAILURE(std::output_iterator); // std::contiguous_iterator DOCUMENTED_STATIC_FAILURE(std::is_lvalue_reference_v); - DOCUMENTED_STATIC_FAILURE(std::same_as>); + DOCUMENTED_STATIC_FAILURE(std::same_as>); DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); DOCUMENTED_STATIC_FAILURE(std::is_lvalue_reference_v); - STATIC_REQUIRE(std::same_as>); + STATIC_REQUIRE(std::same_as>); DOCUMENTED_STATIC_FAILURE(std::contiguous_iterator); } From dd4f98496bba5e4a32f3cb38651b88bb1427ad68 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 20 Jan 2025 14:34:41 +0100 Subject: [PATCH 07/11] make `LinkCollectionIterator` fulfill `std::forward_iterator` --- include/podio/detail/LinkCollectionIterator.h | 4 +++- tests/unittests/std_interoperability.cpp | 5 ++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/include/podio/detail/LinkCollectionIterator.h b/include/podio/detail/LinkCollectionIterator.h index b51c2fb40..953ef1837 100644 --- a/include/podio/detail/LinkCollectionIterator.h +++ b/include/podio/detail/LinkCollectionIterator.h @@ -17,7 +17,9 @@ class LinkCollectionIteratorT { using reference = LinkType; using pointer = LinkType*; using iterator_category = std::input_iterator_tag; - using iterator_concept = std::input_iterator_tag; + // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` + // remain valid as long as the iterator is valid, not as long as the range is valid. + using iterator_concept = std::forward_iterator_tag; LinkCollectionIteratorT(size_t index, const LinkObjPointerContainer* coll) : m_index(index), m_object(podio::utils::MaybeSharedPtr{nullptr}), m_collection(coll) { diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 1202374a2..0931e441a 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -1457,18 +1457,21 @@ TEST_CASE("Collection and unsupported std ranges algorithms", "[collection][rang DOCUMENTED_STATIC_FAILURE(is_range_fillable); } -TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][iterator][std]") { +TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][ranges][std]") { using link_iterator = podio::LinkCollectionIteratorT; using link_const_iterator = podio::LinkCollectionIteratorT; STATIC_REQUIRE(std::input_iterator); STATIC_REQUIRE(std::input_iterator); + STATIC_REQUIRE(std::forward_iterator); + STATIC_REQUIRE(std::forward_iterator); } TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { using link_collection = podio::LinkCollection; STATIC_REQUIRE(std::ranges::input_range); + STATIC_REQUIRE(std::ranges::forward_range); STATIC_REQUIRE(std::ranges::sized_range); STATIC_REQUIRE(std::ranges::common_range); STATIC_REQUIRE(std::ranges::viewable_range); From 6c6724a8d527bb12e5cf6f355149f6ef4484d1fb Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 20 Jan 2025 14:40:42 +0100 Subject: [PATCH 08/11] make `LinkCollectionIterator` fulfill `std::bidirectional_iterator` --- include/podio/detail/LinkCollectionIterator.h | 13 +++++- tests/unittests/std_interoperability.cpp | 46 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/include/podio/detail/LinkCollectionIterator.h b/include/podio/detail/LinkCollectionIterator.h index 953ef1837..d78a71665 100644 --- a/include/podio/detail/LinkCollectionIterator.h +++ b/include/podio/detail/LinkCollectionIterator.h @@ -19,7 +19,7 @@ class LinkCollectionIteratorT { using iterator_category = std::input_iterator_tag; // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` // remain valid as long as the iterator is valid, not as long as the range is valid. - using iterator_concept = std::forward_iterator_tag; + using iterator_concept = std::bidirectional_iterator_tag; LinkCollectionIteratorT(size_t index, const LinkObjPointerContainer* coll) : m_index(index), m_object(podio::utils::MaybeSharedPtr{nullptr}), m_collection(coll) { @@ -59,6 +59,17 @@ class LinkCollectionIteratorT { return copy; } + LinkCollectionIteratorT& operator--() { + --m_index; + return *this; + } + + LinkCollectionIteratorT operator--(int) { + auto copy = *this; + --m_index; + return copy; + } + private: size_t m_index{0}; LinkType m_object{podio::utils::MaybeSharedPtr{nullptr}}; diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 0931e441a..54f56b747 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -1460,11 +1460,56 @@ TEST_CASE("Collection and unsupported std ranges algorithms", "[collection][rang TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][ranges][std]") { using link_iterator = podio::LinkCollectionIteratorT; using link_const_iterator = podio::LinkCollectionIteratorT; + using link_collection = podio::LinkCollection; STATIC_REQUIRE(std::input_iterator); STATIC_REQUIRE(std::input_iterator); STATIC_REQUIRE(std::forward_iterator); STATIC_REQUIRE(std::forward_iterator); + SECTION("bidirectional_iterator") { + STATIC_REQUIRE(std::bidirectional_iterator); + { + auto coll = link_collection(); + coll.create(); + auto a = ++coll.begin(); + REQUIRE(std::addressof(--a) == std::addressof(a)); + a = ++coll.begin(); + auto b = ++coll.begin(); + REQUIRE(a == b); + REQUIRE(a-- == b); + a = ++coll.begin(); + REQUIRE(a == b); + a--; + --b; + REQUIRE(a == b); + a = ++coll.begin(); + b = ++coll.begin(); + REQUIRE(a == b); + REQUIRE(--(++a) == b); + REQUIRE(++(--a) == b); + } + STATIC_REQUIRE(std::bidirectional_iterator); + { + auto coll = link_collection(); + coll.create(); + auto a = ++coll.cbegin(); + REQUIRE(std::addressof(--a) == std::addressof(a)); + a = ++coll.cbegin(); + auto b = ++coll.cbegin(); + REQUIRE(a == b); + REQUIRE(a-- == b); + a = ++coll.cbegin(); + REQUIRE(a == b); + a--; + --b; + REQUIRE(a == b); + a = ++coll.cbegin(); + b = ++coll.cbegin(); + REQUIRE(a == b); + REQUIRE(--(++a) == b); + REQUIRE(++(--a) == b); + } + } } TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { @@ -1472,6 +1517,7 @@ TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { STATIC_REQUIRE(std::ranges::input_range); STATIC_REQUIRE(std::ranges::forward_range); + STATIC_REQUIRE(std::ranges::bidirectional_range); STATIC_REQUIRE(std::ranges::sized_range); STATIC_REQUIRE(std::ranges::common_range); STATIC_REQUIRE(std::ranges::viewable_range); From 93f195b8262041f8a7fede53fc574577ab3a0b56 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 20 Jan 2025 14:55:00 +0100 Subject: [PATCH 09/11] add reverse iterators methods to `LinkCollection` and `UserDataCollection` --- include/podio/UserDataCollection.h | 21 +++++++++++++++++++++ include/podio/detail/LinkCollectionImpl.h | 23 +++++++++++++++++++++++ tests/unittests/links.cpp | 13 +++++++++++++ tests/unittests/unittest.cpp | 19 +++++++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/include/podio/UserDataCollection.h b/include/podio/UserDataCollection.h index d49a07011..dae5c4c40 100644 --- a/include/podio/UserDataCollection.h +++ b/include/podio/UserDataCollection.h @@ -81,6 +81,8 @@ class UserDataCollection : public CollectionBase { using iterator = typename std::vector::iterator; using difference_type = typename std::vector::difference_type; using size_type = typename std::vector::size_type; + using const_reverse_iterator = typename std::vector::const_reverse_iterator; + using reverse_iterator = typename std::vector::reverse_iterator; UserDataCollection() = default; /// Constructor from an existing vector (which will be moved from!) @@ -222,6 +224,25 @@ class UserDataCollection : public CollectionBase { const_iterator cend() const { return _vec.cend(); } + // reverse iterators + reverse_iterator rbegin() { + return _vec.rbegin(); + } + const_reverse_iterator rbegin() const { + return _vec.rbegin(); + } + const_reverse_iterator crbegin() const { + return _vec.crbegin(); + } + reverse_iterator rend() { + return _vec.rend(); + } + const_reverse_iterator rend() const { + return _vec.rend(); + } + const_reverse_iterator crend() const { + return _vec.crend(); + } typename std::vector::reference operator[](size_t idx) { return _vec[idx]; diff --git a/include/podio/detail/LinkCollectionImpl.h b/include/podio/detail/LinkCollectionImpl.h index c4b9c20ce..947f7d68f 100644 --- a/include/podio/detail/LinkCollectionImpl.h +++ b/include/podio/detail/LinkCollectionImpl.h @@ -16,6 +16,8 @@ #include "podio/utilities/MaybeSharedPtr.h" #include "podio/utilities/TypeHelpers.h" +#include + #ifdef PODIO_JSON_OUTPUT #include "nlohmann/json.hpp" #endif @@ -48,6 +50,8 @@ class LinkCollection : public podio::CollectionBase { using iterator = LinkMutableCollectionIterator; using difference_type = ptrdiff_t; using size_type = size_t; + using const_reverse_iterator = std::reverse_iterator; + using reverse_iterator = std::reverse_iterator; LinkCollection() = default; @@ -172,6 +176,25 @@ class LinkCollection : public podio::CollectionBase { iterator end() { return iterator(m_storage.entries.size(), &m_storage.entries); } + // reverse iterators + reverse_iterator rbegin() { + return reverse_iterator(end()); + } + const_reverse_iterator rbegin() const { + return const_reverse_iterator(end()); + } + const_reverse_iterator crbegin() const { + return rbegin(); + } + reverse_iterator rend() { + return reverse_iterator(begin()); + } + const_reverse_iterator rend() const { + return const_reverse_iterator(begin()); + } + const_reverse_iterator crend() const { + return rend(); + } bool isValid() const override { return m_isValid; diff --git a/tests/unittests/links.cpp b/tests/unittests/links.cpp index eb80b076f..405141eb3 100644 --- a/tests/unittests/links.cpp +++ b/tests/unittests/links.cpp @@ -453,6 +453,19 @@ TEST_CASE("Links with interfaces", "[links][interface-types]") { REQUIRE(link.get() == cluster); } +TEST_CASE("Links reverse iterators", "[links][iterator]") { + const auto [linkColl, hitColl, clusterColl] = createLinkCollections(); + REQUIRE(linkColl.size() > 1); + auto it = --linkColl.rend(); + REQUIRE(*it == *linkColl.begin()); + it = linkColl.rbegin(); + REQUIRE(*it == *--linkColl.end()); + auto cit = --linkColl.rend(); + REQUIRE(*cit == *linkColl.cbegin()); + cit = linkColl.crbegin(); + REQUIRE(*cit == *--linkColl.cend()); +} + #ifdef PODIO_JSON_OUTPUT TEST_CASE("Link JSON conversion", "[links][json]") { const auto& [links, hits, clusters] = createLinkCollections(); diff --git a/tests/unittests/unittest.cpp b/tests/unittests/unittest.cpp index ca3f8bf54..851287bf5 100644 --- a/tests/unittests/unittest.cpp +++ b/tests/unittests/unittest.cpp @@ -226,6 +226,25 @@ TEST_CASE("Reverse iterators", "[basics]") { REQUIRE((*--cit).energy() == 43); } +TEST_CASE("UserDataCollection reverse iterators", "[basics]") { + auto coll = podio::UserDataCollection(); + coll.push_back(42); + coll.push_back(43); + auto it = std::rbegin(coll); + REQUIRE(*it == 43); + REQUIRE(*++it == 42); + REQUIRE(*--it == 43); + it = std::rend(coll); + REQUIRE(*--it == 42); + REQUIRE(*--it == 43); + auto cit = std::crbegin(coll); + REQUIRE(*cit == 43); + REQUIRE(*++cit == 42); + cit = std::crend(coll); + REQUIRE(*--cit == 42); + REQUIRE(*--cit == 43); +} + TEST_CASE("Notebook", "[basics]") { auto hits = ExampleHitCollection(); for (unsigned i = 0; i < 12; ++i) { From 372b32c36e59d3407b58f2853b2626a57160501b Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 20 Jan 2025 15:31:19 +0100 Subject: [PATCH 10/11] make `LinkCollectionIterator` fulfill `std::random_access_iterator` --- include/podio/detail/LinkCollectionIterator.h | 40 +++++++++- tests/unittests/std_interoperability.cpp | 77 +++++++++++++++++++ 2 files changed, 114 insertions(+), 3 deletions(-) diff --git a/include/podio/detail/LinkCollectionIterator.h b/include/podio/detail/LinkCollectionIterator.h index d78a71665..b8bb89c6e 100644 --- a/include/podio/detail/LinkCollectionIterator.h +++ b/include/podio/detail/LinkCollectionIterator.h @@ -19,7 +19,7 @@ class LinkCollectionIteratorT { using iterator_category = std::input_iterator_tag; // `std::forward_iterator` is supported except that the pointers obtained with `operator->()` // remain valid as long as the iterator is valid, not as long as the range is valid. - using iterator_concept = std::bidirectional_iterator_tag; + using iterator_concept = std::random_access_iterator_tag; LinkCollectionIteratorT(size_t index, const LinkObjPointerContainer* coll) : m_index(index), m_object(podio::utils::MaybeSharedPtr{nullptr}), m_collection(coll) { @@ -31,8 +31,8 @@ class LinkCollectionIteratorT { LinkCollectionIteratorT& operator=(LinkCollectionIteratorT&&) = default; ~LinkCollectionIteratorT() = default; - bool operator!=(const LinkCollectionIteratorT& other) const { - return m_index != other.m_index; + auto operator<=>(const LinkCollectionIteratorT& other) const { + return m_index <=> other.m_index; } bool operator==(const LinkCollectionIteratorT& other) const { @@ -70,6 +70,40 @@ class LinkCollectionIteratorT { return copy; } + LinkCollectionIteratorT& operator+=(difference_type n) { + m_index += n; + return *this; + } + + LinkCollectionIteratorT operator+(difference_type n) const { + auto copy = *this; + copy += n; + return copy; + } + + friend LinkCollectionIteratorT operator+(difference_type n, const LinkCollectionIteratorT& it) { + return it + n; + } + + LinkCollectionIteratorT& operator-=(difference_type n) { + m_index -= n; + return *this; + } + + LinkCollectionIteratorT operator-(difference_type n) const { + auto copy = *this; + copy -= n; + return copy; + } + + LinkType operator[](difference_type n) const { + return LinkType{podio::utils::MaybeSharedPtr((*m_collection)[m_index + n])}; + } + + difference_type operator-(const LinkCollectionIteratorT& other) const { + return m_index - other.m_index; + } + private: size_t m_index{0}; LinkType m_object{podio::utils::MaybeSharedPtr{nullptr}}; diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 54f56b747..6a27e4c42 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -1510,6 +1510,82 @@ TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][ranges][std]" REQUIRE(++(--a) == b); } } + SECTION("random_access_iterator") { + STATIC_REQUIRE(std::random_access_iterator); + { + auto coll = link_collection(); + coll.create(); + coll.create(); + coll.create(); + coll.create(); + auto a = coll.begin(); + auto n = 2; + auto b = a + n; + REQUIRE((a += n) == b); + a = coll.begin(); + REQUIRE(std::addressof(a += n) == std::addressof(a)); + a = coll.begin(); + auto k = a + n; + REQUIRE(k == (a += n)); + a = coll.begin(); + REQUIRE((a + n) == (n + a)); + auto x = 1; + auto y = 2; + REQUIRE((a + (x + y)) == ((a + x) + y)); + REQUIRE((a + 0) == a); + b = a + n; + REQUIRE((--b) == (a + n - 1)); + b = a + n; + REQUIRE((b += -n) == a); + b = a + n; + REQUIRE((b -= +n) == a); + b = a + n; + REQUIRE(std::addressof(b -= n) == std::addressof(b)); + b = a + n; + k = b - n; + REQUIRE(k == (b -= n)); + b = a + n; + REQUIRE(a[n] == *b); + REQUIRE(a <= b); + } + STATIC_REQUIRE(std::random_access_iterator); + { + auto coll = link_collection(); + coll.create(); + coll.create(); + coll.create(); + coll.create(); + auto a = coll.cbegin(); + auto n = 2; + auto b = a + n; + REQUIRE((a += n) == b); + a = coll.cbegin(); + REQUIRE(std::addressof(a += n) == std::addressof(a)); + a = coll.cbegin(); + auto k = a + n; + REQUIRE(k == (a += n)); + a = coll.cbegin(); + REQUIRE((a + n) == (n + a)); + auto x = 1; + auto y = 2; + REQUIRE((a + (x + y)) == ((a + x) + y)); + REQUIRE((a + 0) == a); + b = a + n; + REQUIRE((--b) == (a + n - 1)); + b = a + n; + REQUIRE((b += -n) == a); + b = a + n; + REQUIRE((b -= +n) == a); + b = a + n; + REQUIRE(std::addressof(b -= n) == std::addressof(b)); + b = a + n; + k = b - n; + REQUIRE(k == (b -= n)); + b = a + n; + REQUIRE(a[n] == *b); + REQUIRE(a <= b); + } + } } TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { @@ -1518,6 +1594,7 @@ TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { STATIC_REQUIRE(std::ranges::input_range); STATIC_REQUIRE(std::ranges::forward_range); STATIC_REQUIRE(std::ranges::bidirectional_range); + STATIC_REQUIRE(std::ranges::random_access_range); STATIC_REQUIRE(std::ranges::sized_range); STATIC_REQUIRE(std::ranges::common_range); STATIC_REQUIRE(std::ranges::viewable_range); From 80d82fc473c3ff3ff2c8934bae571a91b55aff80 Mon Sep 17 00:00:00 2001 From: Mateusz Jakub Fila Date: Mon, 20 Jan 2025 15:41:20 +0100 Subject: [PATCH 11/11] fix tags in LinkCollection tests for iterators and ranges --- tests/unittests/std_interoperability.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/unittests/std_interoperability.cpp b/tests/unittests/std_interoperability.cpp index 6a27e4c42..bb51cd8b3 100644 --- a/tests/unittests/std_interoperability.cpp +++ b/tests/unittests/std_interoperability.cpp @@ -1457,7 +1457,7 @@ TEST_CASE("Collection and unsupported std ranges algorithms", "[collection][rang DOCUMENTED_STATIC_FAILURE(is_range_fillable); } -TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][ranges][std]") { +TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][iterators][std]") { using link_iterator = podio::LinkCollectionIteratorT; using link_const_iterator = podio::LinkCollectionIteratorT; using link_collection = podio::LinkCollection; @@ -1588,7 +1588,7 @@ TEST_CASE("LinkCollectionIterator and iterator concepts", "[links][ranges][std]" } } -TEST_CASE("LinkCollection and range concepts", "[links][iterator][std]") { +TEST_CASE("LinkCollection and range concepts", "[links][ranges][std]") { using link_collection = podio::LinkCollection; STATIC_REQUIRE(std::ranges::input_range);