Blender V5.3
BLI_multi_value_map.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
22
23#include "BLI_map.hh"
24#include "BLI_vector.hh"
25
26namespace blender {
27
28template<typename Key, typename Value> class MultiValueMap {
29 public:
31
32 private:
33 using MapType = Map<Key, Vector<Value>>;
34 MapType map_;
35
36 public:
41 void add(const Key &key, const Value &value)
42 {
43 this->add_as(key, value);
44 }
45 void add(const Key &key, Value &&value)
46 {
47 this->add_as(key, std::move(value));
48 }
49 void add(Key &&key, const Value &value)
50 {
51 this->add_as(std::move(key), value);
52 }
53 void add(Key &&key, Value &&value)
54 {
55 this->add_as(std::move(key), std::move(value));
56 }
57 template<typename ForwardKey, typename ForwardValue>
58 void add_as(ForwardKey &&key, ForwardValue &&value)
59 {
60 Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
61 vector.append(std::forward<ForwardValue>(value));
62 }
63
64 void add_non_duplicates(const Key &key, const Value &value)
65 {
66 Vector<Value> &vector = map_.lookup_or_add_default_as(key);
67 vector.append_non_duplicates(value);
68 }
69
74 {
75 this->add_multiple_as(key, values);
76 }
78 {
79 this->add_multiple_as(std::move(key), values);
80 }
81 template<typename ForwardKey> void add_multiple_as(ForwardKey &&key, Span<Value> values)
82 {
83 Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
84 vector.extend(values);
85 }
86
90 Span<Value> lookup(const Key &key) const
91 {
92 return this->lookup_as(key);
93 }
94 template<typename ForwardKey> Span<Value> lookup_as(const ForwardKey &key) const
95 {
96 const Vector<Value> *vector = map_.lookup_ptr_as(key);
97 if (vector != nullptr) {
98 return vector->as_span();
99 }
100 return {};
101 }
102
107 {
108 return this->lookup_as(key);
109 }
110 template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
111 {
112 Vector<Value> *vector = map_.lookup_ptr_as(key);
113 if (vector != nullptr) {
114 return vector->as_mutable_span();
115 }
116 return {};
117 }
118
122 int64_t size() const
123 {
124 return map_.size();
125 }
126
131 bool is_empty() const
132 {
133 return map_.is_empty();
134 }
135
140 {
141 return map_.items();
142 }
143
148 {
149 return map_.items();
150 }
151
155 typename MapType::KeyIterator keys() const
156 {
157 return map_.keys();
158 }
159
164 {
165 return map_.values();
166 }
167
172 {
173 return map_.values();
174 }
175
176 void clear()
177 {
178 map_.clear();
179 }
180
182 {
183 map_.clear_and_shrink();
184 }
185
186 friend bool operator==(const MultiValueMap &a, const MultiValueMap &b) = default;
187};
188
189} // namespace blender
MapType::KeyIterator keys() const
void add(const Key &key, Value &&value)
Span< bNodeLink * > lookup_as(const ForwardKey &key) const
void add(Key &&key, const Value &value)
friend bool operator==(const MultiValueMap &a, const MultiValueMap &b)=default
Span< Value > lookup(const Key &key) const
void add(Key &&key, Value &&value)
void add_non_duplicates(const Key &key, const Value &value)
void add_multiple(const Key &key, Span< Value > values)
MutableSpan< Value > lookup_as(const ForwardKey &key)
MutableSpan< Value > lookup(const Key &key)
void add_multiple(Key &&key, Span< Value > values)
MapType::ItemIterator items() const
MapType::MutableValueIterator values()
void add_multiple_as(ForwardKey &&key, Span< bNodeLink * > values)
MapType::MutableItemIterator items()
void add_as(ForwardKey &&key, ForwardValue &&value)
void add(const Key &key, const Value &value)
void append(const T &value)
void extend(Span< T > array)
void append_non_duplicates(const T &value)
BMesh Iterators.
__int64 int64_t
Definition BKE_text.h:90