Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_string_multi_map.h « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ccfdcdb0ad1ddc4ff33756d168b76b554045f6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef __BLI_STRING_MULTI_MAP_H__
#define __BLI_STRING_MULTI_MAP_H__

#include "BLI_string_map.h"
#include "BLI_string_ref.h"
#include "BLI_vector.h"

namespace BLI {

template<typename ValueT> class StringMultiMap {
 private:
  StringMap<Vector<ValueT>> m_map;

 public:
  StringMultiMap() = default;
  ~StringMultiMap() = default;

  uint key_amount() const
  {
    return m_map.size();
  }

  uint value_amount(StringRef key) const
  {
    return m_map.lookup(key).size();
  }

  bool add(StringRef key, const ValueT &value)
  {
    if (m_map.contains(key)) {
      m_map.lookup(key).append(value);
      return false;
    }
    else {
      m_map.add_new(key, Vector<ValueT>({value}));
      return true;
    }
  }

  void add_multiple(StringRef key, ArrayRef<ValueT> values)
  {
    if (m_map.contains(key)) {
      m_map.lookup(key).extend(values);
    }
    else {
      m_map.add_new(key, values);
    }
  }

  void add_multiple(const StringMultiMap<ValueT> &other)
  {
    other.foreach_item(
        [&](StringRefNull key, ArrayRef<ValueT> values) { this->add_multiple(key, values); });
  }

  ArrayRef<ValueT> lookup(StringRef key) const
  {
    return m_map.lookup(key);
  }

  ArrayRef<ValueT> lookup_default(StringRef key,
                                  ArrayRef<ValueT> default_array = ArrayRef<ValueT>()) const
  {
    const Vector<ValueT> *values = m_map.lookup_ptr(key);
    if (values == nullptr) {
      return default_array;
    }
    else {
      return *values;
    }
  }

  template<typename FuncT> void foreach_item(const FuncT &func) const
  {
    m_map.foreach_item([&](StringRefNull key, ArrayRef<ValueT> vector) { func(key, vector); });
  }
};

}  // namespace BLI

#endif /* __BLI_STRING_MULTI_MAP_H__ */