Blender  V2.93
BLI_multi_value_map.hh
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  */
16 
17 #pragma once
18 
35 #include "BLI_map.hh"
36 #include "BLI_vector.hh"
37 
38 namespace blender {
39 
40 template<typename Key, typename Value> class MultiValueMap {
41  public:
42  using size_type = int64_t;
43 
44  private:
46  MapType map_;
47 
48  public:
53  void add(const Key &key, const Value &value)
54  {
55  this->add_as(key, value);
56  }
57  void add(const Key &key, Value &&value)
58  {
59  this->add_as(key, std::move(value));
60  }
61  void add(Key &&key, const Value &value)
62  {
63  this->add_as(std::move(key), value);
64  }
65  void add(Key &&key, Value &&value)
66  {
67  this->add_as(std::move(key), std::move(value));
68  }
69  template<typename ForwardKey, typename ForwardValue>
70  void add_as(ForwardKey &&key, ForwardValue &&value)
71  {
72  Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
73  vector.append(std::forward<ForwardValue>(value));
74  }
75 
76  void add_non_duplicates(const Key &key, const Value &value)
77  {
79  vector.append_non_duplicates(value);
80  }
81 
85  void add_multiple(const Key &key, Span<Value> values)
86  {
87  this->add_multiple_as(key, values);
88  }
90  {
91  this->add_multiple_as(std::move(key), values);
92  }
93  template<typename ForwardKey> void add_multiple_as(ForwardKey &&key, Span<Value> values)
94  {
95  Vector<Value> &vector = map_.lookup_or_add_default_as(std::forward<ForwardKey>(key));
96  vector.extend(values);
97  }
98 
102  Span<Value> lookup(const Key &key) const
103  {
104  return this->lookup_as(key);
105  }
106  template<typename ForwardKey> Span<Value> lookup_as(const ForwardKey &key) const
107  {
108  const Vector<Value> *vector = map_.lookup_ptr_as(key);
109  if (vector != nullptr) {
110  return vector->as_span();
111  }
112  return {};
113  }
114 
119  {
120  return this->lookup_as(key);
121  }
122  template<typename ForwardKey> MutableSpan<Value> lookup_as(const ForwardKey &key)
123  {
124  Vector<Value> *vector = map_.lookup_ptr_as(key);
125  if (vector != nullptr) {
126  return vector->as_mutable_span();
127  }
128  return {};
129  }
130 
134  typename MapType::ItemIterator items() const
135  {
136  return map_.items();
137  }
138 
142  typename MapType::KeyIterator keys() const
143  {
144  return map_.keys();
145  }
146 
150  typename MapType::ValueIterator values() const
151  {
152  return map_.values();
153  }
154 };
155 
156 } // namespace blender
Value & lookup_or_add_default_as(ForwardKey &&key)
Definition: BLI_map.hh:602
KeyIterator keys() const
Definition: BLI_map.hh:797
ValueIterator values() const
Definition: BLI_map.hh:806
const Value * lookup_ptr_as(const ForwardKey &key) const
Definition: BLI_map.hh:485
ItemIterator items() const
Definition: BLI_map.hh:825
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:92