-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-compact
350 lines (296 loc) · 11.5 KB
/
brick-compact
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
* (c) 2019 Petr Ročkai <[email protected]>
* (c) 2017 Jan Mrázek <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
#include <vector>
#include <algorithm>
#include <functional>
#include <stdexcept>
#include "brick-assert"
#include "brick-order"
namespace brq
{
struct less_map
{
template< typename T >
auto operator()( const T &a, const T &b ) const -> decltype( a.first < b.first )
{
return a.first < b.first;
}
template< typename T, typename K >
auto operator()( const T &a, const K &b ) const -> decltype( a.first < b )
{
return a.first < b;
}
template< typename T, typename K >
auto operator()( const K &a, const T &b ) const -> decltype( a < b.first )
{
return a < b.first;
}
};
template< typename cmp_ = std::less<> >
struct std_sort
{
using cmp = cmp_;
template< typename it >
static void sort( it begin, it end )
{
std::sort( begin, end, cmp() );
}
template< typename It, typename K >
static It lower_bound( It begin, It end, const K &v )
{
return std::lower_bound( begin, end, v, cmp() );
}
};
template< typename cmp_ = std::less<> >
struct insert_sort
{
using cmp = cmp_;
template< typename It, typename K >
static It lower_bound( It begin, It end, const K &v )
{
if ( begin == end )
return begin;
auto pivot = begin + ( end - begin ) / 2;
if ( cmp()( *pivot, v ) )
return lower_bound( pivot + 1, end, v );
else
return lower_bound( begin, pivot, v );
}
template< typename It >
static void sort( It begin, It end )
{
int i = 1;
while ( i < end - begin )
{
int j = i;
while ( j > 0 && cmp()( begin[ j ], begin[ j - 1 ] ) )
{
std::swap( begin[ j ], begin[ j - 1 ] );
-- j;
}
++ i;
}
}
};
template< typename key_t, typename sort = std_sort<>, typename container = std::vector< key_t > >
struct array_set : derive_eq, derive_ord
{
using cmp = typename sort::cmp;
using key_type = key_t;
using value_type = key_t;
using size_type = typename container::size_type;
using iterator = typename container::iterator;
using const_iterator = typename container::const_iterator;
using reference = value_type&;
using const_reference = const value_type&;
container _container;
auto begin() { return _container.begin(); }
auto begin() const { return _container.cbegin(); }
auto cbegin() const { return _container.cbegin(); }
auto end() { return _container.end(); }
auto end() const { return _container.cend(); }
auto cend() const { return _container.cend(); }
auto empty() const { return _container.empty(); }
auto size() const { return _container.size(); }
void clear() noexcept { _container.clear(); }
array_set() = default;
array_set( std::initializer_list< value_type > il ) : _container( il )
{
sort::sort( begin(), end() );
}
std::pair< iterator, bool > insert( const_reference value )
{
auto it = find( value.first );
if ( it != cend() )
return { it, false };
_container.push_back( value );
sort::sort( begin(), end() );
return { find( value.first ), true };
}
template< class... Args >
std::pair< iterator, bool > emplace( Args&&... args )
{
_container.emplace_back( std::forward< Args >( args )... );
auto it = sort::lower_bound( begin(), end() - 1, _container.back() );
if ( it == end() - 1 || !eq( *it, _container.back() ) )
{
sort::sort( begin(), end() );
return { it, true };
}
else
{
_container.pop_back();
return { it, false };
}
}
void erase( iterator pos )
{
using std::swap;
swap( *pos, _container.back() );
_container.pop_back();
}
template< typename K >
auto erase( const K& key ) -> decltype( cmp()( key, *begin() ), size_type() )
{
auto it = find( key );
if ( it == end() )
return 0;
erase( it );
return 1;
}
void swap( array_set &other )
{
using std::swap;
swap( _container, other._container );
}
template< typename K >
static bool eq( const key_t &a, const K &b )
{
return !cmp()( a, b ) && !cmp()( b, a );
}
template< typename K >
size_type count( const K &key ) const
{
return find( key ) == cend() ? 0 : 1;
}
template< typename K >
iterator find( const K &key )
{
auto elem = sort::lower_bound( begin(), end(), key );
return elem != end() && eq( *elem, key ) ? elem : end();
}
template< typename K >
const_iterator find( const K& key ) const
{
auto elem = sort::lower_bound( cbegin(), cend(), key );
return elem != cend() && eq( *elem, key ) ? elem : cend();
}
bool operator<( const array_set &o ) const { return _container < o._container; }
bool operator==( const array_set &o ) const { return _container == o._container; }
};
template< typename key_t, typename val_t, typename sort = std_sort< less_map >,
typename container = std::vector< std::pair< key_t, val_t > > >
struct array_map : array_set< std::pair< key_t, val_t >, sort, container >
{
using key_type = key_t;
using mapped_type = val_t;
using value_type = std::pair< key_t, val_t >;
using reference = value_type &;
using const_reference = const value_type &;
val_t &operator[]( const key_t& key )
{
auto it = this->find( key );
if ( it != this->end() )
return it->second;
return this->emplace( key, val_t() ).first->second;
}
template< typename K >
val_t &at( const K& key )
{
auto it = this->find( key );
if ( it == this->end() )
throw std::out_of_range( "ArrayMap::at" );
return it->second;
}
template< typename K >
const val_t &at( const K& key ) const
{
auto it = this->find( key );
if ( it == this->cend() )
throw std::out_of_range( "ArrayMap::at" );
return it->second;
}
};
template< typename key_t, typename val_t, typename container = std::vector< val_t > >
struct dense_map : derive_eq, derive_ord
{
using key_type = key_t;
using mapped_type = val_t;
using value_type = std::pair< key_t, val_t >;
using size_type = typename container::size_type;
using reference = std::pair< key_t, val_t & >;
using const_reference = std::pair< key_t, const val_t & >;
using base_iterator = typename container::iterator;
using base_const_iterator = typename container::const_iterator;
container _container;
template< typename ref_t >
struct proxy
{
ref_t ref;
ref_t *operator->() { return &ref; }
proxy( key_t i, typename ref_t::second_type v ) : ref( i, v ) {}
};
template< typename base, typename ref >
struct iterator_t : derive_eq, derive_ord
{
base _iter;
key_t _index;
iterator_t( key_t idx, base it ) : _iter( it ), _index( idx ) {}
ref operator*() { return { _index, *_iter }; }
proxy< ref > operator->() { return { _index, *_iter }; }
bool operator<( iterator_t o ) const { return _iter < o._iter; }
bool operator==( iterator_t o ) const { return _iter == o._iter; }
iterator_t operator+( key_t k ) const { return iterator_t( _index + k, _iter + k ); }
iterator_t &operator++() { ++_iter; ++_index; return *this; }
iterator_t operator++(int) { auto rv = *this; ++*this; return rv; }
};
using const_iterator = iterator_t< typename container::const_iterator, const_reference >;
using iterator = iterator_t< typename container::iterator, reference >;
auto begin() { return iterator( 0, _container.begin() ); }
auto begin() const { return cbegin(); }
auto cbegin() const { return const_iterator( 0, _container.cbegin() ); }
auto end() { return iterator( size(), _container.end() ); }
auto end() const { return cend(); }
auto cend() const { return const_iterator( size(), _container.cend() ); }
auto empty() const { return _container.empty(); }
auto size() const { return _container.size(); }
void clear() noexcept { _container.clear(); }
bool _bump( key_t k )
{
if ( size_t( k ) < _container.size() )
return false;
else
return _container.resize( k + 1 ), true;
}
dense_map() = default;
val_t &operator[]( key_t k ) { _bump( k ); return _container[ k ]; }
std::pair< iterator, bool > insert( const_reference value )
{
key_t idx = value.first;
bool is_new = _bump( value.first );
if ( is_new || _container[ idx ] == val_t() )
_container[ idx ] = value.second;
return { iterator( idx, _container.begin() + idx ), is_new };
}
std::pair< iterator, bool > emplace( key_t idx, val_t val )
{
return insert( std::make_pair( idx, val ) );
}
void erase( iterator pos ) { pos->second = val_t(); }
void erase( key_t key ) { _container[ key ] = val_t(); }
size_type count( key_t key ) const
{
return size_t( key ) < size() && _container[ key ] != val_t();
}
iterator find( key_t k ) { return count( k ) ? begin() + k : end(); }
const_iterator find( key_t k ) const { return count( k ) ? cbegin() + k : end(); }
bool operator<( const dense_map &o ) const { return _container < o._container; }
bool operator==( const dense_map &o ) const { return _container == o._container; }
};
}