Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
map.hpp
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <map>
27#include <utility>
28
29#include <ext/ostream>
30#include <ext/pair>
31
32#include <extensions/range.hpp>
33
34namespace ext {
35
47template < typename T, typename R, typename Cmp = std::less < >, typename Alloc = std::allocator < std::pair < const T, R > > >
48class map : public std::map < T, R, Cmp, Alloc > {
49public:
53 using std::map< T, R, Cmp, Alloc >::map; // NOLINT(modernize-use-equals-default)
54
58 using std::map< T, R, Cmp, Alloc >::operator =;
59#ifndef __clang__
60
64 map ( ) = default;
65
69 map ( const map & other ) = default;
70
74 map ( map && other ) = default;
75
79 map & operator = ( map && other ) = default;
80
84 map & operator = ( const map & other ) = default;
85#endif
93 template < class Iterator >
94 explicit map ( const ext::iterator_range < Iterator > & range ) : map ( range.begin ( ), range.end ( ) ) {
95 }
96
101 using iterator = typename std::map<T, R, Cmp, Alloc>::iterator;
102
107 using std::map< T, R, Cmp, Alloc >::insert;
108
118 std::pair < iterator, bool > insert ( const T & key, const R & value ) {
119 return insert ( std::make_pair ( key, value ) );
120 }
121
131 std::pair < iterator, bool > insert ( const T & key, R && value ) {
132 return insert ( std::make_pair ( key, std::move ( value ) ) );
133 }
134
144 std::pair < iterator, bool > insert ( T && key, const R & value ) {
145 return insert ( std::make_pair ( std::move ( key ), value ) );
146 }
147
157 std::pair < iterator, bool > insert ( T && key, R && value ) {
158 return insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
159 }
160
162
163 R & at ( const T & key, R & defaultValue ) {
164 auto value = this->find ( key );
165 if ( value == end ( ) )
166 return defaultValue;
167 else
168 return value->second;
169 }
170
171 const R & at ( const T & key, const R & defaultValue ) const {
172 auto value = this->find ( key );
173 if ( value == end ( ) )
174 return defaultValue;
175 else
176 return value->second;
177 }
178
185 auto begin ( ) & {
187 }
188
195 auto begin ( ) const & {
197 }
198
205 auto begin ( ) && {
206 return make_map_move_iterator < T, R > ( this->begin ( ) );
207 }
208
215 auto end ( ) & {
217 }
218
225 auto end ( ) const & {
227 }
228
235 auto end ( ) && {
236 return make_map_move_iterator < T, R > ( this->end ( ) );
237 }
238
245 auto range ( ) & {
246 auto endIter = end ( );
247 auto beginIter = begin ( );
248 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
249 }
250
257 auto range ( ) const & {
258 auto endIter = end ( );
259 auto beginIter = begin ( );
260 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
261 }
262
269 auto range ( ) && {
270 auto endIter = std::move ( * this ).end ( );
271 auto beginIter = std::move ( * this ).begin ( );
272 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
273 }
274
285 template < class K >
286 auto equal_range ( K && key ) const & {
287 auto res = std::map < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
288 return ext::iterator_range < decltype ( res.first ) > ( res.first, res.second );
289 }
290
301 template < class K >
302 auto equal_range ( K && key ) & {
303 auto res = std::map < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
304 return ext::iterator_range < decltype ( res.first ) > ( res.first, res.second );
305 }
306
317 template < class K >
318 auto equal_range ( K && key ) && {
319 auto res = std::map < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
320 return ext::make_iterator_range ( make_map_move_iterator < T, R > ( res.first ), make_map_move_iterator < T, R > ( res.second ) );
321 }
322
327 using std::map < T, R, Cmp, Alloc >::erase;
328
339 template < class K >
340 size_t erase ( const K & key ) {
341 auto iter = this->find ( key );
342 erase ( iter );
343 return 1;
344 }
345
350 using std::map < T, R, Cmp, Alloc >::operator [];
351
362 template < class K >
363 R & operator [ ] ( K && key ) {
364 auto iter = this->find ( key );
365 if ( iter == end ( ) ) {
366 return this->operator [ ] ( T ( std::forward < K > ( key ) ) );
367 } else {
368 return iter->second;
369 }
370 }
371};
372
386template< class T, class R, class ... Ts >
388 out << "{";
389
390 bool first = true;
391 for(const std::pair<const T, R>& item : map) {
392 if(!first) out << ", ";
393 first = false;
394 out << "(" << item.first << ", " << item.second << ")";
395 }
396
397 out << "}";
398 return out;
399}
400
401} /* namespace ext */
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
R & operator[](K &&key)
Retrieve value by key of arbitrary type.
Definition: map.hpp:363
const R & at(const T &key, const R &defaultValue) const
Definition: map.hpp:171
auto range() &
Make range of non-const begin to end iterators.
Definition: map.hpp:245
R & at(const T &key, R &defaultValue)
Definition: map.hpp:163
auto equal_range(K &&key) &
Make range of elements with key equal to the key.
Definition: map.hpp:302
map(const map &other)=default
std::pair< iterator, bool > insert(T &&key, R &&value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:157
auto equal_range(K &&key) const &
Make range of elements with key equal to the key.
Definition: map.hpp:286
std::pair< iterator, bool > insert(T &&key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:144
map(map &&other)=default
map & operator=(map &&other)=default
auto begin() const &
Inherited behavior of begin for const instance.
Definition: map.hpp:195
auto end() &&
New variant of end for rvalues.
Definition: map.hpp:235
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: map.hpp:185
auto end() const &
Inherited behavior of end for const instance.
Definition: map.hpp:225
map()=default
auto equal_range(K &&key) &&
Make range of elements with key equal to the key.
Definition: map.hpp:318
auto begin() &&
New variant of begin for rvalues.
Definition: map.hpp:205
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:118
std::pair< iterator, bool > insert(const T &key, R &&value)
Insert variant with explicit key and value parameters.
Definition: map.hpp:131
map(const ext::iterator_range< Iterator > &range)
Definition: map.hpp:94
auto end() &
Inherited behavior of end for non-const instance.
Definition: map.hpp:215
typename std::map< T, R, Cmp, Alloc >::iterator iterator
The iterator type is inheried.
Definition: map.hpp:101
size_t erase(const K &key)
Erase by key of arbitrary type.
Definition: map.hpp:340
auto range() &&
Make range of move begin to end iterators.
Definition: map.hpp:269
auto range() const &
Make range of non-const begin to end iterators.
Definition: map.hpp:257
Definition: ostream.h:14
return res
Definition: MinimizeByPartitioning.h:145
Definition: sigHandler.cpp:20
iterator_range< Iter > make_iterator_range(Iter begin, Iter end)
Helper to create iterator_range from two iterators.
Definition: range.hpp:235
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
std::ostream & operator<<(ext::reference_wrapper< std::ostream > &os, std::ostream &(*const func)(std::ostream &))
Overloaded function allowing same operations on wrapped output stream as on the actual output stream,...
Definition: GlobalData.cpp:33
auto begin(Container &&cont) -> decltype(std::forward(cont).begin())
Definition: iterator.hpp:900
void end()
Definition: measurements.cpp:19