Blender  V3.3
BLI_multi_value_map.hh
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 #pragma once
4 
21 #include "BLI_map.hh"
22 #include "BLI_vector.hh"
23 
24 namespace blender {
25 
26 template<typename Key, typename Value> class MultiValueMap {
27  public:
28  using size_type = int64_t;
29 
30  private:
32  MapType map_;
33 
34  public:
39  void add(const Key &key, const Value &value)
40  {
41  this->add_as(key, value);
42  }
43  void add(const Key &key, Value &&value)
44  {
45  this->add_as(key, std::move(value));
46  }
47  void add(Key &&key, const Value &value)
48  {
49  this->add_as(std::move(key), value);
50  }
51  void add(Key &&key, Value &&value)
52  {
53  this->add_as(std::move(key), std::move(value));
54  }
55  template<typename ForwardKey, typename ForwardValue>
56  void add_as(ForwardKey &&key, ForwardValue &&value)
57  {
58  Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
59  vector.append(std::forward<ForwardValue>(value));
60  }
61 
62  void add_non_duplicates(const Key &key, const Value &value)
63  {
65  vector.append_non_duplicates(value);
66  }
67 
71  void add_multiple(const Key &key, Span<Value> values)
72  {
73  this->add_multiple_as(key, values);
74  }
76  {
77  this->add_multiple_as(std::move(key), values);
78  }
79  template<typename ForwardKey> void add_multiple_as(ForwardKey &&key, Span<Value> values)
80  {
81  Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
82  vector.extend(values);
83  }
84 
88  Span<Value> lookup(const Key &key) const
89  {
90  return this->lookup_as(key);
91  }
92  template<typename ForwardKey> Span<Value> lookup_as(const ForwardKey &key) const
93  {
94  const Vector<Value> *vector = map_.lookup_ptr_as(key);
95  if (vector != nullptr) {
96  return vector->as_span();
97  }
98  return {};
99  }
100 
105  {
106  return this->lookup_as(key);
107  }
108  template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
109  {
110  Vector<Value> *vector = map_.lookup_ptr_as(key);
111  if (vector != nullptr) {
112  return vector->as_mutable_span();
113  }
114  return {};
115  }
116 
120  typename MapType::ItemIterator items() const
121  {
122  return map_.items();
123  }
124 
128  typename MapType::KeyIterator keys() const
129  {
130  return map_.keys();
131  }
132 
136  typename MapType::ValueIterator values() const
137  {
138  return map_.values();
139  }
140 };
141 
142 } // namespace blender
Value & lookup_or_add_default_as(ForwardKey &&key)
Definition: BLI_map.hh:588
KeyIterator keys() const
Definition: BLI_map.hh:831
ValueIterator values() const
Definition: BLI_map.hh:840
const Value * lookup_ptr_as(const ForwardKey &key) const
Definition: BLI_map.hh:471
ItemIterator items() const
Definition: BLI_map.hh:859
MapType::KeyIterator keys() const
Span< Value > lookup_as(const ForwardKey &key) const
void add(const Key &key, Value &&value)
void add(Key &&key, const Value &value)
Span< Value > lookup(const Key &key) const
MutableSpan< Value > lookup_as(const ForwardKey &key)
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)
void add_multiple(Key &&key, Span< Value > values)
MapType::ItemIterator items() const
MutableSpan< Value > lookup(const Key &key)
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