25template <
typename Map,
typename Key>
26typename Map::mapped_type get(
const Map &m,
const Key &key) {
27 auto it = m.find(key);
28 return it != m.end() ? it->second :
typename Map::mapped_type{};
30template <
class Map,
typename Key,
typename Value>
31typename Map::mapped_type get(
const Map &m,
const Key &key, Value &&def) {
32 using M =
typename Map::mapped_type;
33 auto it = m.find(key);
34 return it != m.end() ? it->second :
static_cast<M
>(std::forward<Value>(def));
39template <
typename Map,
typename Key>
40const typename Map::mapped_type *getref(
const Map &m,
const Key &key) {
41 auto it = m.find(key);
42 return it != m.end() ? &it->second :
nullptr;
45template <
typename Map,
typename Key>
46typename Map::mapped_type *getref(Map &m,
const Key &key) {
47 auto it = m.find(key);
48 return it != m.end() ? &it->second :
nullptr;
53template <
class Map,
typename Key>
54std::optional<typename Map::mapped_type> get_optional(
const Map &m,
const Key &key) {
55 auto it = m.find(key);
56 if (it != m.end())
return std::optional<typename Map::mapped_type>(it->second);
61template <
typename Map,
typename Key>
62typename Map::mapped_type get(
const Map *m,
const Key &key) {
63 return m ? get(*m, key) : typename Map::mapped_type{};
66template <
class Map,
typename Key,
typename Value>
67typename Map::mapped_type get(
const Map *m,
const Key &key, Value &&def) {
68 return m ? get(*m, key, std::forward(def)) : typename Map::mapped_type{};
71template <
typename Map,
typename Key>
72const typename Map::mapped_type *getref(
const Map *m,
const Key &key) {
73 return m ? getref(*m, key) : nullptr;
76template <
typename Map,
typename Key>
77typename Map::mapped_type *getref(Map *m,
const Key &key) {
78 return m ? getref(*m, key) : nullptr;
82template <
class PairIter>
88 using iterator_category =
typename std::iterator_traits<PairIter>::iterator_category;
89 using value_type =
typename std::iterator_traits<PairIter>::value_type::first_type;
90 using difference_type =
typename std::iterator_traits<PairIter>::difference_type;
91 using pointer =
decltype(&it->first);
92 using reference =
decltype(*&it->first);
94 explicit iterator(PairIter i) : it(i) {}
95 iterator &operator++() {
99 iterator &operator--() {
103 iterator operator++(
int) {
108 iterator operator--(
int) {
113 bool operator==(
const iterator &i)
const {
return it == i.it; }
114 bool operator!=(
const iterator &i)
const {
return it != i.it; }
115 reference operator*()
const {
return it->first; }
116 pointer operator->()
const {
return &it->first; }
121 explicit IterKeys(U &map) : b(map.begin()), e(map.end()) {}
122 IterKeys(PairIter b, PairIter e) : b(b), e(e) {}
123 iterator begin()
const {
return b; }
124 iterator end()
const {
return e; }
137template <
class PairIter>
143template <
class PairIter>
149 using iterator_category =
typename std::iterator_traits<PairIter>::iterator_category;
150 using value_type =
typename std::iterator_traits<PairIter>::value_type::second_type;
151 using difference_type =
typename std::iterator_traits<PairIter>::difference_type;
152 using pointer =
decltype(&it->second);
153 using reference =
decltype(*&it->second);
155 explicit iterator(PairIter i) : it(i) {}
156 iterator &operator++() {
160 iterator &operator--() {
164 iterator operator++(
int) {
169 iterator operator--(
int) {
174 bool operator==(
const iterator &i)
const {
return it == i.it; }
175 bool operator!=(
const iterator &i)
const {
return it != i.it; }
176 reference operator*()
const {
return it->second; }
177 pointer operator->()
const {
return &it->second; }
181 using value_type =
typename std::iterator_traits<PairIter>::value_type::second_type;
184 explicit IterValues(U &map) : b(map.begin()), e(map.end()) {}
185 IterValues(PairIter b, PairIter e) : b(b), e(e) {}
186 iterator begin()
const {
return b; }
187 iterator end()
const {
return e; }
200template <
class PairIter>
209 typename M::key_type key;
211 using MapIt =
decltype(map.begin());
217 using iterator_category = std::forward_iterator_tag;
218 using value_type =
typename M::value_type;
219 using difference_type =
typename std::iterator_traits<MapIt>::difference_type;
220 using pointer =
decltype(&it->second);
221 using reference =
decltype(*&it->second);
223 iterator(
const MapForKey &s, MapIt i) : self(s), it(std::move(i)) {}
224 iterator &operator++() {
225 if (++it != self.map.end() && it->first != self.key) it = self.map.end();
228 iterator operator++(
int) {
233 bool operator==(
const iterator &i)
const {
return it == i.it; }
234 bool operator!=(
const iterator &i)
const {
return it != i.it; }
235 reference operator*()
const {
return it->second; }
236 pointer operator->()
const {
return &it->second; }
240 MapForKey(M &m,
typename M::key_type k) : map(m), key(k) {}
241 iterator begin()
const {
return iterator(*
this, map.find(key)); }
242 iterator end()
const {
return iterator(*
this, map.end()); }
246MapForKey<M> ValuesForKey(M &m,
typename M::key_type k) {