Blender V4.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"
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
131 typename MapType::ItemIterator items() const
132 {
133 return map_.items();
134 }
135
139 typename MapType::KeyIterator keys() const
140 {
141 return map_.keys();
142 }
143
147 typename MapType::ValueIterator values() const
148 {
149 return map_.values();
150 }
151
152 void clear()
153 {
154 map_.clear();
155 }
156
158 {
159 map_.clear_and_shrink();
160 }
161
163};
164
165} // namespace blender
#define BLI_STRUCT_EQUALITY_OPERATORS_1(Type, m)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Brightness Control the brightness and contrast of the input color Vector Map input vector components with curves Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert Invert a producing a negative Combine Generate a color from its and blue Hue Saturation Value
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
__int64 int64_t
Definition stdint.h:89