diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-20 17:42:35 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-20 17:42:35 +0300 |
commit | 98721c85431d223c895a25d63dafb9e6637d34c4 (patch) | |
tree | 58a357fcef4d43f8d1e1e93835c9c2339eb66d18 /source/blender/blenlib/BLI_map.hh | |
parent | 59d3ec1eefd30a3f041a9b156b3e01607c2bcd71 (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_map.hh')
-rw-r--r-- | source/blender/blenlib/BLI_map.hh | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_map.hh b/source/blender/blenlib/BLI_map.hh index 9480af89107..9fa69853e44 100644 --- a/source/blender/blenlib/BLI_map.hh +++ b/source/blender/blenlib/BLI_map.hh @@ -120,6 +120,9 @@ template< */ typename Allocator = GuardedAllocator> class Map { + public: + using size_type = int64_t; + private: /** * Slots are either empty, occupied or removed. The number of occupied slots can be computed by @@ -623,6 +626,9 @@ class Map { * This uses the "curiously recurring template pattern" (CRTP). */ template<typename SubIterator> struct BaseIterator { + using iterator_category = std::forward_iterator_tag; + using difference_type = std::ptrdiff_t; + Slot *slots_; int64_t total_slots_; int64_t current_slot_; @@ -642,6 +648,13 @@ class Map { return *this; } + BaseIterator operator++(int) const + { + BaseIterator copied_iterator = *this; + ++copied_iterator; + return copied_iterator; + } + friend bool operator!=(const BaseIterator &a, const BaseIterator &b) { BLI_assert(a.slots_ == b.slots_); @@ -649,6 +662,11 @@ class Map { return a.current_slot_ != b.current_slot_; } + friend bool operator==(const BaseIterator &a, const BaseIterator &b) + { + return !(a != b); + } + SubIterator begin() const { for (int64_t i = 0; i < total_slots_; i++) { @@ -672,6 +690,10 @@ class Map { class KeyIterator final : public BaseIterator<KeyIterator> { public: + using value_type = Key; + using pointer = const Key *; + using reference = const Key &; + KeyIterator(const Slot *slots, int64_t total_slots, int64_t current_slot) : BaseIterator<KeyIterator>(slots, total_slots, current_slot) { @@ -685,6 +707,10 @@ class Map { class ValueIterator final : public BaseIterator<ValueIterator> { public: + using value_type = Value; + using pointer = const Value *; + using reference = const Value &; + ValueIterator(const Slot *slots, int64_t total_slots, int64_t current_slot) : BaseIterator<ValueIterator>(slots, total_slots, current_slot) { @@ -698,6 +724,10 @@ class Map { class MutableValueIterator final : public BaseIterator<MutableValueIterator> { public: + using value_type = Value; + using pointer = Value *; + using reference = Value &; + MutableValueIterator(const Slot *slots, int64_t total_slots, int64_t current_slot) : BaseIterator<MutableValueIterator>(slots, total_slots, current_slot) { @@ -726,6 +756,10 @@ class Map { class ItemIterator final : public BaseIterator<ItemIterator> { public: + using value_type = Item; + using pointer = Item *; + using reference = Item &; + ItemIterator(const Slot *slots, int64_t total_slots, int64_t current_slot) : BaseIterator<ItemIterator>(slots, total_slots, current_slot) { @@ -740,6 +774,10 @@ class Map { class MutableItemIterator final : public BaseIterator<MutableItemIterator> { public: + using value_type = MutableItem; + using pointer = MutableItem *; + using reference = MutableItem &; + MutableItemIterator(const Slot *slots, int64_t total_slots, int64_t current_slot) : BaseIterator<MutableItemIterator>(slots, total_slots, current_slot) { |