smultimap.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <unordered_map>
5#include "core/common.hpp"
6
7namespace by {
24 template <typename K, typename V> class smultimap {
25 BY(ME(smultimap))
26
27 public:
28 class wrap;
29 class iterator;
30 friend class iterator;
31 typedef std::unordered_multimap<K, wrap> stlMap;
32
33 class wrap {
34 friend class smultimap<K, V>;
35
36 public:
37 wrap() = default;
38 wrap(V&& newValue);
39 wrap(const wrap& rhs);
40 wrap(wrap&& rhs);
41
42 public:
43 wrap& operator=(const wrap&);
44 wrap& operator=(wrap&&);
45
46 public:
47 const K* getKey() const;
48 V& getVal();
49 const V& getVal() const;
50
51 public:
52 void clear();
53
54 private:
55 const K* _key = nullptr;
56 V _value;
57 wrap* _prev = this;
58 wrap* _next = this;
59 };
60
61 class iterator {
62 BY(ME(iterator))
63 typedef smultimap<K, V> owner;
64
65 public:
66 iterator(const smultimap* owner, const wrap* pair, nbool isReversed);
67 iterator(const smultimap* owner, const wrap* pair, nbool isReversed, const K& key);
68 iterator(const smultimap* owner, const wrap* pair, nbool isReversed, const K* key);
69 friend owner;
70
71 public:
72 V& operator*();
73 V* operator->();
74
75 iterator& operator++();
76 iterator operator++(int);
77 iterator& operator--();
78 iterator operator--(int);
79 iterator operator+(ncnt step);
80
81 bool isEnd() const;
82
83 const K* getKey() const;
84 const V* getVal() const BY_CONST_FUNC(getVal())
85 V* getVal();
86
87 bool operator!=(const iterator& rhs) const;
88 bool operator==(const iterator& rhs) const;
89
90 private:
91 iterator& _step(ncnt step, nbool isReversed);
92 static const K& _getDummyKey();
93
94 private:
95 const owner* _owner;
96 const wrap* _wrap;
97 nbool _isReversed;
98 K _key;
99 };
100
101 public:
102 smultimap() = default;
103
104 public:
105 ncnt size() const;
106
107 iterator begin() const;
108 iterator end() const;
109 iterator begin(const K& key) const;
110 iterator begin(const K* key) const BY_SIDE_FUNC(key, begin(*key), begin());
111
112 iterator rbegin() const;
113 iterator rend() const;
114 iterator rbegin(const K& key) const;
115 iterator rbegin(const K* key) const BY_SIDE_FUNC(key, rbegin(*key), rbegin());
116
117 void insert(const K& key, V&& val);
118
120 void erase(const K& key);
121 void erase(const K* it) BY_SIDE_FUNC(erase);
122 void erase(const iterator& it);
123 void erase(const iterator* it) BY_SIDE_FUNC(erase);
124 void erase(const iterator& from, const iterator& to);
125 void erase(const iterator* from, const iterator& to) BY_SIDE_FUNC(from, erase(*from, to), void());
126 void erase(const iterator& from, const iterator* to) BY_SIDE_FUNC(to, erase(from, *to), void());
127 void erase(const iterator* from, const iterator* to) BY_SIDE_FUNC(from&& to, erase(*from, *to), void());
128
129 iterator find(const K& key) const;
130
131 void clear();
132
133 private:
134 typename stlMap::iterator _erase(const typename stlMap::iterator& e);
135 void _erase(const iterator& e);
136 void _link(wrap& newTail);
137 void _unlink(wrap& toDelete);
138 iterator _begin(const K& key, nbool isReversed) const;
139
140 private:
141 stlMap _map;
142 wrap _end;
143 };
144} // namespace by
Deferred execution utility like defer keyword in other languages.
Definition: end.hpp:14
Definition: smultimap.hpp:61
Definition: smultimap.hpp:33
Definition: smultimap.hpp:24
void erase(const K &key)
delete all elements matching given key.