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
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:
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 {
68 vector.append_non_duplicates(value);
69 }
70
74 void add_multiple(const Key &key, Span<Value> values)
75 {
76 this->add_multiple_as(key, values);
77 }
78 void add_multiple(Key &&key, Span<Value> values)
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 {
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)
void clear()
Definition BLI_map.hh:989
KeyIterator keys() const
Definition BLI_map.hh:837
ValueIterator values() const
Definition BLI_map.hh:846
Value & lookup_or_add_default_as(ForwardKey &&key)
Definition BLI_map.hh:609
const Value * lookup_ptr_as(const ForwardKey &key) const
Definition BLI_map.hh:492
void clear_and_shrink()
Definition BLI_map.hh:1003
int64_t size() const
Definition BLI_map.hh:927
ItemIterator items() const
Definition BLI_map.hh:864
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