Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
multimap.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 multimap : public std::multimap < T, R, Cmp, Alloc > {
49public:
53 using std::multimap< T, R, Cmp, Alloc >::multimap; // NOLINT(modernize-use-equals-default)
54
58 using std::multimap< T, R, Cmp, Alloc >::operator =;
59#ifndef __clang__
60
64 multimap ( ) = default;
65
69 multimap ( const multimap & other ) = default;
70
74 multimap ( multimap && other ) = default;
75
79 multimap & operator = ( multimap && other ) = default;
80
84 multimap & operator = ( const multimap & other ) = default;
85#endif
93 template < class Iterator >
95 }
96
101 using iterator = typename std::multimap<T, R, Cmp, Alloc>::iterator;
102
107 using std::multimap< T, R, Cmp, Alloc >::insert;
108
118 iterator insert ( const T & key, const R & value ) {
119 return insert ( std::make_pair ( key, value ) );
120 }
121
131 iterator insert ( const T & key, R && value ) {
132 return insert ( std::make_pair ( key, std::move ( value ) ) );
133 }
134
144 iterator insert ( T && key, const R & value ) {
145 return insert ( std::make_pair ( std::move ( key ), value ) );
146 }
147
157 iterator insert ( T && key, R && value ) {
158 return insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
159 }
160
167 auto begin ( ) & {
169 }
170
177 auto begin ( ) const & {
179 }
180
187 auto begin ( ) && {
188 return make_map_move_iterator < T, R > ( this->begin ( ) );
189 }
190
197 auto end ( ) & {
199 }
200
207 auto end ( ) const & {
209 }
210
217 auto end ( ) && {
218 return make_map_move_iterator < T, R > ( this->end ( ) );
219 }
220
227 auto range ( ) & {
228 auto endIter = end ( );
229 auto beginIter = begin ( );
230 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
231 }
232
239 auto range ( ) const & {
240 auto endIter = end ( );
241 auto beginIter = begin ( );
242 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
243 }
244
251 auto range ( ) && {
252 auto endIter = std::move ( * this ).end ( );
253 auto beginIter = std::move ( * this ).begin ( );
254 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
255 }
256
267 template < class K >
268 auto equal_range ( K && key ) const & {
269 auto range = std::multimap < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
270 return ext::iterator_range < decltype ( range.first ) > ( range.first, range.second );
271 }
272
283 template < class K >
284 auto equal_range ( K && key ) & {
285 auto range = std::multimap < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
286 return ext::iterator_range < decltype ( range.first ) > ( range.first, range.second );
287 }
288
299 template < class K >
300 auto equal_range ( K && key ) && {
301 auto range = std::multimap < T, R, Cmp, Alloc >::equal_range ( std::forward < K > ( key ) );
302 return ext::make_iterator_range ( make_map_move_iterator < T, R > ( range.first ), make_map_move_iterator < T, R > ( range.second ) );
303 }
304
305};
306
320template< class T, class R, class ... Ts >
322 out << "{";
323
324 bool first = true;
325 for(const std::pair<const T, R>& item : multimap) {
326 if(!first) out << ", ";
327 first = false;
328 out << "(" << item.first << ", " << item.second << ")";
329 }
330
331 out << "}";
332 return out;
333}
334
335} /* 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 multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
auto range() const &
Make range of non-const begin to end iterators.
Definition: multimap.hpp:239
auto begin() const &
Inherited behavior of begin for const instance.
Definition: multimap.hpp:177
auto end() &&
New variant of end for rvalues.
Definition: multimap.hpp:217
multimap & operator=(multimap &&other)=default
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: multimap.hpp:167
multimap(const multimap &other)=default
multimap()=default
auto range() &
Make range of non-const begin to end iterators.
Definition: multimap.hpp:227
auto end() const &
Inherited behavior of end for const instance.
Definition: multimap.hpp:207
typename std::multimap< T, R, Cmp, Alloc >::iterator iterator
The iterator type is inheried.
Definition: multimap.hpp:101
multimap(const ext::iterator_range< Iterator > &range)
Definition: multimap.hpp:94
iterator insert(T &&key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:144
iterator insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:118
auto range() &&
Make range of move begin to end iterators.
Definition: multimap.hpp:251
auto begin() &&
New variant of begin for rvalues.
Definition: multimap.hpp:187
multimap(multimap &&other)=default
auto equal_range(K &&key) &
Make range of elements with key equal to the key.
Definition: multimap.hpp:284
auto equal_range(K &&key) &&
Make range of elements with key equal to the key.
Definition: multimap.hpp:300
auto end() &
Inherited behavior of end for non-const instance.
Definition: multimap.hpp:197
iterator insert(T &&key, R &&value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:157
auto equal_range(K &&key) const &
Make range of elements with key equal to the key.
Definition: multimap.hpp:268
iterator insert(const T &key, R &&value)
Insert variant with explicit key and value parameters.
Definition: multimap.hpp:131
Definition: ostream.h:14
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