Blender V5.0
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"
25#include "BLI_vector.hh"
26
27namespace blender {
28
29template<typename Key, typename Value> class MultiValueMap {
30 public:
32
33 private:
34 using MapType = Map<Key, Vector<Value>>;
35 MapType map_;
36
37 public:
42 void add(const Key &key, const Value &value)
43 {
44 this->add_as(key, value);
45 }
46 void add(const Key &key, Value &&value)
47 {
48 this->add_as(key, std::move(value));
49 }
50 void add(Key &&key, const Value &value)
51 {
52 this->add_as(std::move(key), value);
53 }
54 void add(Key &&key, Value &&value)
55 {
56 this->add_as(std::move(key), std::move(value));
57 }
58 template<typename ForwardKey, typename ForwardValue>
59 void add_as(ForwardKey &&key, ForwardValue &&value)
60 {
61 Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
62 vector.append(std::forward<ForwardValue>(value));
63 }
64
65 void add_non_duplicates(const Key &key, const Value &value)
66 {
67 Vector<Value> &vector = map_.lookup_or_add_default_as(key);
68 vector.append_non_duplicates(value);
69 }
70
75 {
76 this->add_multiple_as(key, values);
77 }
79 {
80 this->add_multiple_as(std::move(key), values);
81 }
82 template<typename ForwardKey> void add_multiple_as(ForwardKey &&key, Span<Value> values)
83 {
84 Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
85 vector.extend(values);
86 }
87
91 Span<Value> lookup(const Key &key) const
92 {
93 return this->lookup_as(key);
94 }
95 template<typename ForwardKey> Span<Value> lookup_as(const ForwardKey &key) const
96 {
97 const Vector<Value> *vector = map_.lookup_ptr_as(key);
98 if (vector != nullptr) {
99 return vector->as_span();
100 }
101 return {};
102 }
103
108 {
109 return this->lookup_as(key);
110 }
111 template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
112 {
113 Vector<Value> *vector = map_.lookup_ptr_as(key);
114 if (vector != nullptr) {
115 return vector->as_mutable_span();
116 }
117 return {};
118 }
119
123 int64_t size() const
124 {
125 return map_.size();
126 }
127
132 bool is_empty() const
133 {
134 return map_.is_empty();
135 }
136
140 typename MapType::ItemIterator items() const
141 {
142 return map_.items();
143 }
144
148 typename MapType::KeyIterator keys() const
149 {
150 return map_.keys();
151 }
152
156 typename MapType::ValueIterator values() const
157 {
158 return map_.values();
159 }
160
161 void clear()
162 {
163 map_.clear();
164 }
165
167 {
168 map_.clear_and_shrink();
169 }
170
172};
173
174} // namespace blender
#define BLI_STRUCT_EQUALITY_OPERATORS_1(Type, m)
long long int int64_t
MapType::KeyIterator keys() const
void add(const Key &key, Value &&value)
Span< Value > lookup_as(const ForwardKey &key) const
void add(Key &&key, const Value &value)
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
void add_multiple_as(ForwardKey &&key, Span< Value > values)
void add_as(ForwardKey &&key, ForwardValue &&value)
void add(const Key &key, const Value &value)
MapType::ValueIterator values() const