Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJacques Lucke <jacques@blender.org>2021-03-20 17:42:35 +0300
committerJacques Lucke <jacques@blender.org>2021-03-20 17:42:35 +0300
commit98721c85431d223c895a25d63dafb9e6637d34c4 (patch)
tree58a357fcef4d43f8d1e1e93835c9c2339eb66d18 /source/blender/blenlib/BLI_set.hh
parent59d3ec1eefd30a3f041a9b156b3e01607c2bcd71 (diff)
BLI: improve support for generic algorithms with c++ containers
Some generic algorithms from the standard library like `std::any_of` did not work with all container and iterator types. To improve the situation, this patch adds various type members to containers and iterators. Custom iterators for Set, Map and IndexRange now have an iterator category, which soe algorithms require. IndexRange could become a random access iterator, but adding all the missing methods can be done when it is necessary.
Diffstat (limited to 'source/blender/blenlib/BLI_set.hh')
-rw-r--r--source/blender/blenlib/BLI_set.hh34
1 files changed, 34 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_set.hh b/source/blender/blenlib/BLI_set.hh
index 06b56c3f8e5..fef657b2d8f 100644
--- a/source/blender/blenlib/BLI_set.hh
+++ b/source/blender/blenlib/BLI_set.hh
@@ -119,6 +119,16 @@ template<
*/
typename Allocator = GuardedAllocator>
class Set {
+ public:
+ class Iterator;
+ using value_type = Key;
+ using pointer = Key *;
+ using const_pointer = const Key *;
+ using reference = Key &;
+ using const_reference = const Key &;
+ using iterator = Iterator;
+ using size_type = int64_t;
+
private:
/**
* Slots are either empty, occupied or removed. The number of occupied slots can be computed by
@@ -401,6 +411,13 @@ class Set {
* also change their hash.
*/
class Iterator {
+ public:
+ using iterator_category = std::forward_iterator_tag;
+ using value_type = Key;
+ using pointer = const Key *;
+ using reference = const Key &;
+ using difference_type = std::ptrdiff_t;
+
private:
const Slot *slots_;
int64_t total_slots_;
@@ -422,17 +439,34 @@ class Set {
return *this;
}
+ Iterator operator++(int) const
+ {
+ Iterator copied_iterator = *this;
+ ++copied_iterator;
+ return copied_iterator;
+ }
+
const Key &operator*() const
{
return *slots_[current_slot_].key();
}
+ const Key *operator->() const
+ {
+ return slots_[current_slot_].key();
+ }
+
friend bool operator!=(const Iterator &a, const Iterator &b)
{
BLI_assert(a.slots_ == b.slots_);
BLI_assert(a.total_slots_ == b.total_slots_);
return a.current_slot_ != b.current_slot_;
}
+
+ friend bool operator==(const Iterator &a, const Iterator &b)
+ {
+ return !(a != b);
+ }
};
Iterator begin() const