-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathClassKeyMap.hh
More file actions
775 lines (590 loc) · 14.2 KB
/
ClassKeyMap.hh
File metadata and controls
775 lines (590 loc) · 14.2 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
// -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
// vi: set ts=2 noet:
//
// (c) Copyright Rosetta Commons Member Institutions.
// (c) This file is part of the Rosetta software suite and is made available under license.
// (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
// (c) For more information, see http://www.rosettacommons.org. Questions about this can be
// (c) addressed to University of Washington CoMotion, email: license@uw.edu.
/// @file utility/keys/ClassKeyMap.hh
/// @brief Keyed-access map with key subset map for each client class
/// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
///
/// @note
/// @li Key can be any type that is convertible to the index map's index type
/// @li The Key type should not be the same as Index or you'll get an operator[] ambiguity
/// @li If a utility Key subtype is used it must declare the ClassKeyMap as a friend
/// @li Keys are added to map by assign(), operator(), add(), and activate()
/// @li Keys can be added to map out of order
/// @li Client (C) parameter => Distinct type per client
/// @li Static index map => Client vectors share key set
/// @li Can create elements and then assign keys or vice versa: At any time you
/// Can have a vector with more or fewer elements than there are active keys
#ifndef INCLUDED_utility_keys_ClassKeyMap_hh
#define INCLUDED_utility_keys_ClassKeyMap_hh
// Unit headers
#include <utility/keys/ClassKeyMap.fwd.hh>
// Project headers
#include <utility/vector1.hh>
// C++ headers
#include <utility/assert.hh>
#include <utility>
namespace utility {
namespace keys {
/// @brief Keyed-access map with key subset map for each client class
template< typename K, typename T, typename C >
class ClassKeyMap
{
private: // Types
typedef vector1< std::pair< K, T > > Vector;
typedef vector1< typename Vector::Index > IndexMap;
typedef typename IndexMap::Size IndexMapSize;
typedef typename IndexMap::Index IndexMapIndex;
public: // Types
// STL/boost style
typedef K key_type;
typedef T mapped_type;
typedef T & mapped_reference;
typedef T const & mapped_const_reference;
typedef T * mapped_pointer;
typedef T const * mapped_const_pointer;
typedef typename Vector::value_type value_type;
typedef typename Vector::reference reference;
typedef typename Vector::const_reference const_reference;
typedef typename Vector::pointer pointer;
typedef typename Vector::const_pointer const_pointer;
typedef typename Vector::iterator iterator;
typedef typename Vector::const_iterator const_iterator;
typedef typename Vector::reverse_iterator reverse_iterator;
typedef typename Vector::const_reverse_iterator const_reverse_iterator;
typedef typename Vector::size_type size_type;
typedef typename Vector::index_type index_type;
typedef typename Vector::difference_type difference_type;
typedef typename Vector::allocator_type allocator_type;
typedef C client_type;
// Project style
typedef K Key;
typedef T Mapped;
typedef T & MappedReference;
typedef T const & MappedConstReference;
typedef T * MappedPointer;
typedef T const * MappedConstPointer;
typedef typename Vector::Value Value;
typedef typename Vector::Reference Reference;
typedef typename Vector::ConstReference ConstReference;
typedef typename Vector::Pointer Pointer;
typedef typename Vector::ConstPointer ConstPointer;
typedef typename Vector::Iterator Iterator;
typedef typename Vector::ConstIterator ConstIterator;
typedef typename Vector::ReverseIterator ReverseIterator;
typedef typename Vector::ConstReverseIterator ConstReverseIterator;
typedef typename Vector::Size Size;
typedef typename Vector::Index Index;
typedef typename Vector::Difference Difference;
typedef typename Vector::Allocator Allocator;
typedef C Client;
public: // Creation
/// @brief Default constructor
inline
ClassKeyMap()
= default;
/// @brief Copy constructor
inline
ClassKeyMap( ClassKeyMap const & a ) :
v_( a.v_ )
{}
/// @brief Iterator range constructor
template< typename InputIterator >
inline
ClassKeyMap(
InputIterator const beg,
InputIterator const end
) :
v_( beg, end )
{
for ( Index i = 1, e = v_.size(); i <= e; ++i ) {
add_key( v_[ i ].first );
}
}
/// @brief Destructor
inline
~ClassKeyMap() {}
public: // Assignment
/// @brief Copy assignment
inline
ClassKeyMap &
operator =( ClassKeyMap const & a )
{
if ( this != &a ) {
v_ = a.v_;
}
return *this;
}
/// @brief Uniform value assignment to current elements
inline
ClassKeyMap &
operator =( Mapped const & mapped )
{
for ( Index i = 1, e = v_.size(); i <= e; ++i ) {
v_[ i ].second = mapped;
}
return *this;
}
/// @brief Assign a value to an element with a key
/// @note Adds the key to the map if not present
/// @note Expands the vector if necessary
inline
ClassKeyMap &
assign(
Key const & key,
Mapped const & mapped
)
{
v_[ add_key( key ) ] = Value( key, mapped );
return *this;
}
/// @brief Assign a value to an element
/// @note Adds the key to the map if not present
/// @note Expands the vector if necessary
inline
ClassKeyMap &
assign( Value const & value )
{
v_[ add_key( value.first ) ] = value;
return *this;
}
/// @brief Iterator assignment
template< typename InputIterator >
inline
void
assign(
InputIterator const beg,
InputIterator const end
)
{
clear();
v_.assign( beg, end );
for ( Index i = 1, e = v_.size(); i <= e; ++i ) {
add_key( v_[ i ].first );
}
}
public: // Methods
/// @brief Add an element with a key if not present: Activate key if inactive
inline
ClassKeyMap &
add( Key const & key )
{
add_key( key );
return *this;
}
/// @brief Insert an element
/// @note Adds the key to the map if not present
/// @note Expands the vector if necessary
inline
ClassKeyMap &
insert( Value const & value )
{
v_[ add_key( value.first ) ] = value;
return *this;
}
/// @brief Insert elements from iterator range [beg,end)
template< typename InputIterator >
inline
void
insert(
InputIterator const beg,
InputIterator const end
)
{
for ( InputIterator i = beg; i != end; ++i ) {
insert( *i );
}
}
/// @brief Reserve space for a given number of elements
inline
void
reserve( Size const num )
{
v_.reserve( num );
}
/// @brief Shrink the vector to remove unused capacity
inline
void
shrink()
{
v_.shrink();
}
/// @brief swap( ClassKeyMap )
inline
void
swap( ClassKeyMap & a )
{
v_.swap( a.v_ );
}
/// @brief swap( ClassKeyMap, ClassKeyMap )
template< typename UK, typename UT, typename UC >
friend
void
swap( ClassKeyMap< UK, UT, UC > & a, ClassKeyMap< UK, UT, UC > & b );
/// @brief Clear the vector
inline
void
clear()
{
v_.clear();
}
public: // Properties
/// @brief Size
inline
Size
size() const
{
return v_.size();
}
/// @brief Empty?
inline
bool
empty() const
{
return v_.empty();
}
/// @brief Max size
inline
Size
max_size() const
{
return v_.max_size();
}
/// @brief Capacity
inline
Size
capacity() const
{
return v_.capacity();
}
/// @brief Is an element with a key present?
inline
bool
has( Key const & key ) const
{
return ( ( key > 0 ) && ( m().has( key ) ) && ( v_.has( m()[ key ] ) ) );
}
/// @brief Index of a key
inline
static
Index const &
index( Key const & key )
{
debug_assert( active( key ) );
return m()[ key ];
}
/// @brief Iterator to element with a key
inline
ConstIterator
find( Key const & key ) const
{
return ( active( key ) ? v_.begin() + m()[ key ] - 1 : v_.end() );
}
/// @brief Iterator to element with a key
inline
Iterator
find( Key const & key )
{
return ( active( key ) ? v_.begin() + m()[ key ] - 1 : v_.end() );
}
public: // Indexers
/// @brief ClassKeyMap( key )
/// @note Activates the key if inactive
/// @note Expands the vector if necessary
inline
MappedReference
operator ()( Key const & key )
{
return v_[ add_key( key ) ].second;
}
/// @brief ClassKeyMap[ key ] const
inline
MappedConstReference
operator []( Key const & key ) const
{
debug_assert( active( key ) );
return v_[ m()[ key ] ].second;
}
/// @brief ClassKeyMap[ key ]
inline
MappedReference
operator []( Key const & key )
{
debug_assert( active( key ) );
return v_[ m()[ key ] ].second;
}
/// @brief ClassKeyMap[ index ] const
inline
MappedConstReference
operator []( Index const & i ) const
{
return v_[ i ].second;
}
/// @brief ClassKeyMap[ index ]
inline
MappedReference
operator []( Index const & i )
{
return v_[ i ].second;
}
/// @brief ClassKeyMap( index ) const
inline
ConstReference
operator ()( Index const & i ) const
{
return v_[ i ];
}
/// @brief ClassKeyMap( index )
inline
Reference
operator ()( Index const & i )
{
return v_[ i ];
}
public: // Iterators
/// @brief Begin iterator
inline
ConstIterator
begin() const
{
return v_.begin();
}
/// @brief Begin iterator
inline
Iterator
begin()
{
return v_.begin();
}
/// @brief End iterator
inline
ConstIterator
end() const
{
return v_.end();
}
/// @brief End iterator
inline
Iterator
end()
{
return v_.end();
}
/// @brief Begin reverse iterator
inline
ConstReverseIterator
rbegin() const
{
return v_.rbegin();
}
/// @brief Begin reverse iterator
inline
ReverseIterator
rbegin()
{
return v_.rbegin();
}
/// @brief End reverse iterator
inline
ConstReverseIterator
rend() const
{
return v_.rend();
}
/// @brief End reverse iterator
inline
ReverseIterator
rend()
{
return v_.rend();
}
public: // Comparison
/// @brief ClassKeyMap == ClassKeyMap
friend
inline
bool
operator ==( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ == b.v_ );
}
/// @brief ClassKeyMap != ClassKeyMap
friend
inline
bool
operator !=( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ != b.v_ );
}
/// @brief ClassKeyMap < ClassKeyMap
friend
inline
bool
operator <( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ < b.v_ );
}
/// @brief ClassKeyMap > ClassKeyMap
friend
inline
bool
operator >( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ > b.v_ );
}
/// @brief ClassKeyMap <= ClassKeyMap
friend
inline
bool
operator <=( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ <= b.v_ );
}
/// @brief ClassKeyMap >= ClassKeyMap
friend
inline
bool
operator >=( ClassKeyMap const & a, ClassKeyMap const & b )
{
return ( a.v_ >= b.v_ );
}
public: // Static functions
/// @brief Is a key active?
inline
static
bool
active( Key const & key )
{
return ( ( m().has( key ) ) && ( m()[ key ] != 0 ) );
}
/// @brief Is a key inactive?
inline
static
bool
inactive( Key const & key )
{
return ( ( ! m().has( key ) ) || ( m()[ key ] == 0 ) );
}
/// @brief Activate a key if inactive
inline
static
void
activate( Key const & key )
{
debug_assert( key > 0 );
IndexMap & imap( m() );
if ( ! imap.has( key ) ) { // Extend index map and activate key
imap.resize( key, Index( 0 ) );
imap[ key ] = ++u();
} else if ( imap[ key ] == 0 ) { // Activate key
imap[ key ] = ++u();
}
}
private: // Methods
/// @brief Add an element with a key if not present and return its index: Activate key if inactive
inline
Index const &
add_key( Key const & key )
{
debug_assert( key > 0 );
Index const & i( activated_index( key ) );
if ( i > v_.size() ) v_.resize( i );
return i;
}
private: // Static functions
/// @brief Index map from keys into v_: Zero => inactive key
inline
static
IndexMap &
m()
{
static IndexMap m_; // Function local to support globals
return m_;
}
/// @brief Upper active index of active keys
inline
static
Index &
u()
{
static Index u_( 0 ); // Function local to support globals
return u_;
}
/// @brief Activate a key if inactive and return its index
inline
static
Index &
activated_index( Key const & key )
{
activate( key );
return m()[ key ];
}
/// @brief Shrink the index map to remove unused capacity
inline
static
void
map_shrink()
{
m().shrink();
}
private: // Fields
/// @brief Vector of values indexed by a subset of the possible keys
Vector v_;
}; // ClassKeyMap
// Friend function namespace declarations
/// @brief swap( ClassKeyMap, ClassKeyMap )
template< typename K, typename T, typename C >
void
swap( ClassKeyMap< K, T, C > & a, ClassKeyMap< K, T, C > & b )
{
a.v_.swap( b.v_ );
}
/// @brief ClassKeyMap == ClassKeyMap
template< typename K, typename T, typename C >
bool
operator ==( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
/// @brief ClassKeyMap != ClassKeyMap
template< typename K, typename T, typename C >
bool
operator !=( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
/// @brief ClassKeyMap < ClassKeyMap
template< typename K, typename T, typename C >
bool
operator <( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
/// @brief ClassKeyMap > ClassKeyMap
template< typename K, typename T, typename C >
bool
operator >( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
/// @brief ClassKeyMap <= ClassKeyMap
template< typename K, typename T, typename C >
bool
operator <=( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
/// @brief ClassKeyMap >= ClassKeyMap
template< typename K, typename T, typename C >
bool
operator >=( ClassKeyMap< K, T, C > const & a, ClassKeyMap< K, T, C > const & b );
} // namespace keys
} // namespace utility
#ifndef NO_STD_SWAP_OVERLOADS
// std::swap Overloads for Efficiency
//
// Technically you cannot add template functions overloads to namespace std
// but this works with most compilers and makes it much faster if someone uses
// std::swap instead of swap or utility::swap. The legal alternative would be
// to add specializations of swap for each anticipated ClassKeyMap instantiation.
namespace std {
/// @brief swap( ClassKeyMap, ClassKeyMap )
template< typename K, typename T, typename C >
inline
void
swap( utility::keys::ClassKeyMap< K, T, C > & a, utility::keys::ClassKeyMap< K, T, C > & b )
{
a.swap( b );
}
} // namespace std
#endif // NO_STD_SWAP_OVERLOADS
#endif // INCLUDED_utility_keys_ClassKeyMap_HH