Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
unordered_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 <unordered_map>
27
28#include <ext/ostream>
29
30#include <extensions/range.hpp>
31
32namespace ext {
33
46template < class T, class R, class Hash = std::hash < T >, class KeyEqual = std::equal_to < T >, class Alloc = std::allocator < std::pair < const T, R > > >
47class unordered_map : public std::unordered_map < T, R, Hash, KeyEqual, Alloc > {
48public:
52 using std::unordered_map < T, R, Hash, KeyEqual, Alloc >::unordered_map; // NOLINT(modernize-use-equals-default)
53
57 using std::unordered_map < T, R, Hash, KeyEqual, Alloc >::operator =;
58#ifndef __clang__
59
63 unordered_map ( ) = default;
64
68 unordered_map ( const unordered_map & other ) = default;
69
73 unordered_map ( unordered_map && other ) = default;
74
78 unordered_map & operator = ( unordered_map && other ) = default;
79
83 unordered_map & operator = ( const unordered_map & other ) = default;
84#endif
92 template < class Iterator >
94 }
95
100 using iterator = typename std::unordered_map<T, R, Hash, KeyEqual, Alloc>::iterator;
101
106 using std::unordered_map< T, R, Hash, KeyEqual, Alloc >::insert;
107
117 std::pair < iterator, bool > insert ( const T & key, const R & value ) {
118 return insert ( std::make_pair ( key, value ) );
119 }
120
130 std::pair < iterator, bool > insert ( const T & key, R && value ) {
131 return insert ( std::make_pair ( key, std::move ( value ) ) );
132 }
133
143 std::pair < iterator, bool > insert ( T && key, const R & value ) {
144 return insert ( std::make_pair ( std::move ( key ), value ) );
145 }
146
156 std::pair < iterator, bool > insert ( T && key, R && value ) {
157 return insert ( std::make_pair ( std::move ( key ), std::move ( value ) ) );
158 }
159
166 auto begin ( ) & {
168 }
169
176 auto begin ( ) const & {
178 }
179
186 auto begin ( ) && {
187 return make_map_move_iterator < T, R > ( this->begin ( ) );
188 }
189
196 auto end ( ) & {
198 }
199
206 auto end ( ) const & {
208 }
209
216 auto end ( ) && {
217 return make_map_move_iterator < T, R > ( this->end ( ) );
218 }
219
226 auto range ( ) & {
227 auto endIter = end ( );
228 auto beginIter = begin ( );
229 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
230 }
231
238 auto range ( ) const & {
239 auto endIter = end ( );
240 auto beginIter = begin ( );
241 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
242 }
243
250 auto range ( ) && {
251 auto endIter = std::move ( * this ).end ( );
252 auto beginIter = std::move ( * this ).begin ( );
253 return ext::iterator_range < decltype ( endIter ) > ( beginIter, endIter );
254 }
255};
256
270template< class T, class R, class ... Ts >
272 out << "{";
273
274 bool first = true;
275 for(const std::pair<const T, R>& item : unordered_map) {
276 if(!first) out << ", ";
277 first = false;
278 out << "(" << item.first << ", " << item.second << ")";
279 }
280
281 out << "}";
282 return out;
283}
284
285} /* namespace ext */
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
Definition: ostream.h:14
Class extending the unordered_map class from the standard library. Original reason is to allow printi...
Definition: unordered_map.hpp:47
unordered_map(const unordered_map &other)=default
unordered_map & operator=(unordered_map &&other)=default
unordered_map(const ext::iterator_range< Iterator > &range)
Definition: unordered_map.hpp:93
unordered_map()=default
std::pair< iterator, bool > insert(const T &key, const R &value)
Insert variant with explicit key and value parameters.
Definition: unordered_map.hpp:117
auto end() const &
Inherited behavior of end for const instance.
Definition: unordered_map.hpp:206
auto range() &&
Make range of move begin to end iterators.
Definition: unordered_map.hpp:250
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: unordered_map.hpp:166
std::pair< iterator, bool > insert(const T &key, R &&value)
Insert variant with explicit key and value parameters.
Definition: unordered_map.hpp:130
auto range() const &
Make range of non-const begin to end iterators.
Definition: unordered_map.hpp:238
auto end() &&
New variant of end for rvalues.
Definition: unordered_map.hpp:216
std::pair< iterator, bool > insert(T &&key, R &&value)
Insert variant with explicit key and value parameters.
Definition: unordered_map.hpp:156
auto range() &
Make range of non-const begin to end iterators.
Definition: unordered_map.hpp:226
auto begin() &&
New variant of begin for rvalues.
Definition: unordered_map.hpp:186
unordered_map(unordered_map &&other)=default
std::pair< iterator, bool > insert(T &&key, const R &value)
Insert variant with explicit key and value parameters.
Definition: unordered_map.hpp:143
auto begin() const &
Inherited behavior of begin for const instance.
Definition: unordered_map.hpp:176
auto end() &
Inherited behavior of end for non-const instance.
Definition: unordered_map.hpp:196
typename std::unordered_map< T, R, Hash, KeyEqual, Alloc >::iterator iterator
The iterator type is inheried.
Definition: unordered_map.hpp:100
Definition: sigHandler.cpp:20
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