Repository navigation
Expand file tree
/
Copy pathengineered.cpp
More file actions
2764 lines (2697 loc) · 98.8 KB
/
Copy pathengineered.cpp
File metadata and controls
2764 lines (2697 loc) · 98.8 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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------*/
/*-------------------------- File engineered.cpp ---------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* Tests of the engineered structures, beyond the book: the bucket queue and
* the d-ary heap of plain items, the hash table with control bytes, the
* hash table of pointers, the arena, the pool, the allocator on huge
* pages, the hashing with random tables, the queues between threads and
* the concurrent cuckoo table. Each one is checked against a reference of
* the standard library on random operations, at the level 3 of the
* contracts, and the structures between threads on real threads. The
* tables are those of ADS also where `ADS_WITH_BOOST` delegates them to
* Boost, since these tests look at their places one by one [see
* `engineered_boost.cpp` for the delegations].
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Università di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
// the code of ADS, not that of Boost
#define ADS_FLATHASH_BOOST 0
#define ADS_POINTERTABLE_BOOST 0
#include "ADSTest.h"
#include "ADS/concurrent/ConcurrentCuckooTable.h"
#include "ADS/concurrent/ConcurrentFlatTable.h"
#include "ADS/concurrent/MPSCInbox.h"
#include "ADS/concurrent/SPSCQueue.h"
#include "ADS/concurrent/SingleWriterFlatTable.h"
#include "ADS/ADT.h"
#include "ADS/dictionary/FlatHashTable.h"
#include "ADS/dictionary/OpenHashTable.h"
#include "ADS/dictionary/PointerTable.h"
#include "ADS/dictionary/RandomHashing.h"
#include "ADS/memory/Arena.h"
#include "ADS/memory/HugePageAllocator.h"
#include "ADS/memory/ObjectPool.h"
#include "ADS/priority/BucketQueue.h"
#include "ADS/priority/DaryHeap.h"
#include <algorithm>
#include <atomic>
#include <climits>
#include <cstdint>
#include <functional>
#include <limits>
#include <queue>
#include <random>
#include <set>
#include <stdexcept>
#include <string>
#include <thread>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>
static_assert( ADS_CONTRACTS == 3 , "the tests check all the contracts" );
/*--------------------------------------------------------------------------*/
/*------------------------------- USING ------------------------------------*/
/*--------------------------------------------------------------------------*/
using namespace ADS;
namespace
{
/*--------------------------------------------------------------------------*/
/*-------------------------------- TESTS -----------------------------------*/
/*--------------------------------------------------------------------------*/
// the bucket queue against a set ordered by least f, largest g and last
// pushed; negative keys and an empty queue violate the contracts; ties on
// both keys come out last pushed first, a layer emptied and freed takes a
// lower g, a key below the lazy least f is found, the keys 0 and an
// unsigned type of keys work
ADS_TEST( "bucket-queue" )
{
BucketQueue< int > Q;
CHECK_THROWS( ( void )Q.top() , contract_violation );
CHECK_THROWS( Q.pop() , contract_violation );
CHECK_THROWS( Q.popEntry() , contract_violation );
CHECK_THROWS( ( void )Q.topEntry() , contract_violation );
CHECK_THROWS( Q.push( 0 , -1 , 0 ) , contract_violation );
std::mt19937 g( 1 );
std::set< std::tuple< int , int , int , int > > R; // f , -g , -t , x
for( int t = 0 ; t < 5000 ; ++t )
if( R.empty() || ( g() % 3 ) ) {
const int f = int( g() % 40 ) , h = int( g() % 15 );
Q.push( t , f , h );
R.emplace( f , -h , -t , t );
}
else {
const auto [ f , mg , mt , x ] = *R.begin();
if( t % 2 ) { // by top() and topEntry(), and by popEntry()
CHECK( Q.topF() == f && Q.topG() == -mg && Q.top() == x );
const auto e = Q.topEntry();
CHECK( &e.item == &Q.top() && e.item == x && e.f == f && e.g == -mg );
CHECK( Q.pop() == x );
}
else {
const auto e = Q.popEntry();
CHECK( e.item == x && e.f == f && e.g == -mg );
}
R.erase( R.begin() );
}
CHECK( Q.size() == R.size() );
Q.clear();
CHECK( Q.empty() );
Q.push( 7 , 3 , 1 );
CHECK( Q.pop() == 7 && Q.empty() );
CHECK_THROWS( Q.push( 0 , 0 , -1 ) , contract_violation );
CHECK_THROWS( ( void )Q.topF() , contract_violation );
CHECK_THROWS( ( void )Q.topG() , contract_violation );
BucketQueue< int > Z; // all ties: LIFO
for( int x = 0 ; x < 5 ; ++x )
Z.push( x , 0 , 0 );
for( int x = 5 ; x-- > 0 ; )
CHECK( Z.pop() == x );
CHECK( Z.empty() && Z.begin() == Z.end() );
CHECK_THROWS( Z.pop() , contract_violation ); // emptied after use
Z.push( 1 , 3 , 4 );
CHECK( Z.pop() == 1 ); // the layer 3 is freed
Z.push( 2 , 3 , 1 ); // and taken again, lower g
CHECK( Z.topF() == 3 && Z.topG() == 1 && Z.pop() == 2 );
Z.push( 3 , 9 , 0 );
CHECK( Z.topF() == 9 ); // the least f moved to 9
Z.push( 4 , 2 , 0 ); // and back below it
CHECK( Z.topF() == 2 && Z.pop() == 4 && Z.topF() == 9 && Z.pop() == 3 );
BucketQueue< int , unsigned > U; // unsigned keys
U.push( 1 , 5u , 0u );
U.push( 2 , 0u , 0u );
U.push( 3 , 0u , 7u );
CHECK( U.pop() == 3 && U.pop() == 2 && U.pop() == 1 && U.empty() );
}
/// a bucket queue of the items 0 , 1 , ... in the order of their pushes,
/// with the set of ( f , -g , -t , t ) that orders them as it must, or of
/// ( f , g , -t , t ) if the least g comes first
template< class Tie >
struct BucketRef
{
BucketQueue< int , int , Tie > q;
std::set< std::tuple< int , int , int , int > > s;
int t = 0;
/// g as the set orders it, and back
static int sg( int g ) { return( Tie::least ? g : -g ); }
void push( int f , int g ) {
q.push( t , f , g );
s.emplace( f , sg( g ) , -t , t );
++t;
}
/// one pop, by popEntry() or by top(), topF(), topG() and pop()
void pop( bool entry = true ) {
const auto [ f , mg , mt , x ] = *s.begin();
if( entry ) {
const auto e = q.popEntry();
CHECK( e.item == x && e.f == f && e.g == sg( mg ) );
}
else {
CHECK( q.topF() == f && q.topG() == sg( mg ) && q.top() == x );
CHECK( q.pop() == x );
}
s.erase( s.begin() );
CHECK( q.size() == s.size() );
}
/// the view of the standard interface in the order of the set
void view( void ) const {
auto it = q.begin();
for( const auto & e : s ) {
CHECK( it != q.end() && *it == std::get< 3 >( e ) );
++it;
}
CHECK( it == q.end() );
}
void drain( void ) {
view();
while( ! s.empty() )
pop( s.size() % 2 );
CHECK( q.empty() && q.begin() == q.end() );
}
};
/// the reference of the default order, the largest g first
using BucketReference = BucketRef< LargestSecondKey >;
/// random operations on the bucket queue of the policy Tie against its
/// reference, from the seed: g small, around D and spread above D, so that
/// the layers keep runs, turn dense and are emptied, with the view of the
/// standard interface now and then
template< class Tie >
void bucketRandom( unsigned seed , int steps )
{
constexpr int D = int( BucketQueue< int >::D );
constexpr int R = int( BucketQueue< int >::R );
BucketRef< Tie > B;
std::mt19937 g( seed );
const int spread = seed % 2 ? 3000 : 2 * R;
for( int step = 0 ; step < steps ; ++step ) {
const unsigned c = g() % 10;
if( B.s.empty() || ( c < 6 ) ) {
const unsigned w = g() % 4;
const int x = w == 0 ? int( g() % 8 )
: w == 1 ? int( D - 3 + g() % 7 )
: int( D + g() % unsigned( spread ) );
B.push( int( g() % 10 ) , x );
}
else
B.pop( c % 2 );
if( step % 1000 == 0 )
B.view();
if( step % 7000 == 6999 )
B.drain();
}
B.drain();
}
// the lists of large g of the bucket queue, kept as up to R sparse runs
// in a layer and then dense: g just below, at and above D, ties of f and g
// last pushed first in the runs, in the dense lists and across the
// conversion, the conversion of a layer with items in its runs and in its
// dense lists (by a new g and by the g of a run), the least f back below a
// layer freed, a layer emptied and filled again many times, crossing the
// conversion now and then, g as large as int and as long long in a run,
// and random operations against a set by least f, largest g and last
// pushed, with the view of the standard interface; each step is checked
// at the level 3 of the contracts, whose invariant covers the runs
ADS_TEST( "bucket-queue-sparse" )
{
constexpr int D = int( BucketQueue< int >::D );
constexpr int R = int( BucketQueue< int >::R );
static_assert( ( D > 4 ) && ( R > 2 ) );
{
BucketReference B; // empty, then one item
CHECK_THROWS( ( void )B.q.topG() , contract_violation );
B.push( 0 , D + 1000 );
B.view();
B.pop( false );
CHECK_THROWS( B.q.pop() , contract_violation );
B.push( 3 , std::numeric_limits< int >::max() ); // no array of g
B.push( 3 , 0 );
B.push( 3 , std::numeric_limits< int >::max() );
B.drain();
BucketQueue< int , long long > L;
L.push( 1 , 2 , 1LL << 40 );
L.push( 2 , 2 , 7 );
CHECK( L.topG() == ( 1LL << 40 ) && L.topEntry().g == ( 1LL << 40 ) &&
L.pop() == 1 && L.topG() == 7 );
const auto e = L.topEntry(); // a sparse layer, of a run
CHECK( e.item == 2 && e.f == 2 && e.g == 7 && L.size() == 1 );
BucketQueue< int , int , LeastSecondKey > S;
S.push( 4 , 1 , 1 << 20 );
S.push( 5 , 1 , 3 );
const auto s = S.topEntry();
CHECK( s.item == 5 && s.f == 1 && s.g == 3 && S.size() == 2 );
}
{
BucketReference B; // around D, with ties
for( int g : { D - 1 , D , D + 1 , D - 1 , D , D + 1 , 0 , D } )
B.push( 1 , g );
B.view();
CHECK( B.q.topG() == D + 1 );
B.drain();
}
for( int how = 0 ; how < 2 ; ++how ) { // the conversion, items in
BucketReference B;
std::mt19937 g( 5 + how );
for( int x : { 0 , 3 , 4 , D - 1 , 3 , 0 } )
B.push( 2 , x );
std::vector< int > G;
for( int k = 0 ; k < R ; ++k )
G.push_back( D + 2 * k );
std::shuffle( G.begin() , G.end() , g );
for( int x : G ) {
B.push( 2 , x );
if( g() % 3 == 0 )
B.push( 2 , x ); // a tie in a run
}
B.push( 7 , D + 5 ); // another layer, untouched
B.view();
B.push( 2 , how ? D + 4 : D + 1 ); // a run of R + 1, or a tie
B.view();
CHECK( B.q.topF() == 2 && B.q.topG() == D + 2 * ( R - 1 ) );
for( int x : { D + 1 , 2 , D + 3 * R , D + 1 , D - 1 , D + 4 } )
B.push( 2 , x ); // into the dense layer
B.view();
for( int k = 0 ; k < R / 2 ; ++k )
B.pop( k % 2 );
B.push( 2 , D + 6 ); // still dense
B.push( 2 , D + 6 );
B.drain();
}
{
BucketReference B; // the least f back below
for( int k = 0 ; k < 5 ; ++k )
B.push( 5 , D + 100 * k );
B.push( 9 , D + 3 );
for( int k = 0 ; k < 5 ; ++k )
B.pop(); // the layer 5 freed
CHECK( B.q.topF() == 9 );
B.push( 5 , D + 50 ); // and filled again
B.push( 3 , D + 7 );
B.push( 5 , 1 );
B.view();
CHECK( B.q.topF() == 3 );
for( int k = 0 ; k <= R ; ++k )
B.push( 5 , D + 3 * k ); // dense
B.pop();
while( B.q.topF() == 5 )
B.pop( false ); // freed when dense
B.push( 5 , D + 8 ); // and sparse again
B.push( 5 , D + 9 );
B.drain();
}
{
BucketReference B; // emptied and filled again
std::mt19937 g( 9 );
B.push( 1 , D + 3 ); // stays below
for( int round = 0 ; round < 3000 ; ++round ) {
const int m = ( round % 100 == 99 ) ? R + 10 : 1 + round % 3;
for( int k = 0 ; k < m ; ++k )
B.push( 0 , int( D + g() % 8000 ) );
if( round % 500 == 0 )
B.view();
for( int k = 0 ; k < m ; ++k )
B.pop( k % 2 );
CHECK( B.q.topF() == 1 );
}
B.drain();
}
for( unsigned seed = 1 ; seed <= 4 ; ++seed ) // random
bucketRandom< LargestSecondKey >( seed , 20000 );
}
// the bucket queue in the lexicographic order of LPA*, least f, then least
// g, then last pushed (LeastSecondKey): ties on both keys, g below, at and
// above D, the dense lists given before the runs of a layer, a layer of
// runs only, a push below the lazy bound of g, the conversion of a layer of
// R runs to dense with items in its dense lists, the layer emptied, freed
// and filled again, the keys of LPA* from a pair ( k1 , k2 ), and random
// operations against the set ordered so, with the view of the standard
// interface, at the level 3 of the contracts; the default order is that of
// bucket-queue and bucket-queue-sparse
ADS_TEST( "bucket-queue-lexicographic" )
{
using Q = BucketQueue< int , int , LeastSecondKey >;
constexpr int D = int( Q::D );
constexpr int R = int( Q::R );
static_assert( std::is_same_v< Q::Ties , LeastSecondKey > &&
std::is_same_v< BucketQueue< int >::Ties ,
LargestSecondKey > );
{
Q q; // all ties: LIFO
for( int x = 0 ; x < 5 ; ++x )
q.push( x , 0 , 0 );
for( int x = 5 ; x-- > 0 ; )
CHECK( q.pop() == x );
CHECK_THROWS( q.pop() , contract_violation );
q.push( 1 , 4 , 9 ); // least g first
q.push( 2 , 4 , 3 );
q.push( 3 , 4 , 7 );
q.push( 4 , 5 , 0 );
CHECK( q.topF() == 4 && q.topG() == 3 && q.pop() == 2 );
q.push( 5 , 4 , 1 ); // below the lazy bound
CHECK( q.topG() == 1 && q.pop() == 5 && q.pop() == 3 && q.pop() == 1 );
CHECK( q.topF() == 5 && q.pop() == 4 && q.empty() );
}
{
BucketRef< LeastSecondKey > B; // around D, with ties
for( int g : { D + 1 , D , D - 1 , D + 1 , D , D - 1 , 0 , D } )
B.push( 1 , g );
B.view();
CHECK( B.q.topG() == 0 );
B.drain();
}
{
BucketRef< LeastSecondKey > B; // runs only, then dense too
for( int k : { 5 , 2 , 9 , 2 , 7 } )
B.push( 3 , D + k );
B.view();
CHECK( B.q.topG() == D + 2 );
B.pop();
B.push( 3 , 6 ); // before every run
B.push( 3 , D - 1 );
B.view();
CHECK( B.q.topG() == 6 );
B.pop();
B.pop( false ); // the dense lists emptied
CHECK( B.q.topG() == D + 2 ); // back to the runs
B.push( 3 , 1 );
B.drain();
}
for( int how = 0 ; how < 2 ; ++how ) { // the conversion, items in
BucketRef< LeastSecondKey > B;
std::mt19937 g( 15 + how );
for( int x : { 0 , 3 , 4 , D - 1 , 3 , 0 } )
B.push( 2 , x );
std::vector< int > G;
for( int k = 0 ; k < R ; ++k )
G.push_back( D + 2 * k );
std::shuffle( G.begin() , G.end() , g );
for( int x : G ) {
B.push( 2 , x );
if( g() % 3 == 0 )
B.push( 2 , x ); // a tie in a run
}
B.push( 7 , D + 5 ); // another layer, untouched
B.view();
B.push( 2 , how ? D + 4 : D + 1 ); // a run of R + 1, or a tie
B.view();
CHECK( B.q.topF() == 2 && B.q.topG() == 0 );
for( int k = 0 ; k < 6 ; ++k )
B.pop( k % 2 ); // the dense g < D
CHECK( B.q.topG() == D );
for( int x : { D + 1 , 2 , D + 3 * R , D + 1 , D - 1 , D + 4 } )
B.push( 2 , x ); // into the dense layer
B.view();
for( int k = 0 ; k < R / 2 ; ++k )
B.pop( k % 2 );
B.push( 2 , D + 6 );
B.push( 2 , 1 );
B.drain();
B.push( 2 , D + 8 ); // freed, sparse again
B.push( 2 , D + 9 );
B.push( 2 , D + 3 );
CHECK( B.q.topG() == D + 3 );
B.drain();
}
{
// the keys of LPA*, k1 = min( g , rhs ) + h and k2 = min( g , rhs ):
// least k1, then least k2, against a sort of the pairs
Q q;
std::mt19937 g( 21 );
std::vector< std::tuple< int , int , int > > v;
for( int x = 0 ; x < 3000 ; ++x ) {
const int k2 = int( g() % 400 ) , k1 = k2 + int( g() % 50 );
q.push( x , k1 , k2 );
v.emplace_back( k1 , k2 , -x );
}
std::sort( v.begin() , v.end() );
for( const auto & [ k1 , k2 , mx ] : v ) {
const auto e = q.popEntry();
CHECK( e.f == k1 && e.g == k2 && e.item == -mx );
}
CHECK( q.empty() );
}
for( unsigned seed = 1 ; seed <= 4 ; ++seed )
bucketRandom< LeastSecondKey >( seed , 20000 );
}
// the d-ary heap of plain items for D = 2 , 3 , 4 , 8 against
// std::priority_queue, the items carrying f and g, the least f and then the
// largest g out first; the edge cases: n = 0 , 1 , D , D + 1 items, one
// item (popped onto itself), all equal items, sorted and reverse sorted
// pushes, the extremes of int, an odd D, a pushed item that lives in the
// heap while the vector grows, and the empty heap after clear(); there is
// no decrease-key to test
struct Entry
{
int f , g , id;
};
struct EntryBetter
{
bool operator()( const Entry & a , const Entry & b ) const {
return( ( a.f < b.f ) || ( ( a.f == b.f ) && ( a.g > b.g ) ) ||
( ( a.f == b.f ) && ( a.g == b.g ) && ( a.id < b.id ) ) );
}
};
template< unsigned D >
void dary( unsigned seed )
{
DaryHeap< Entry , EntryBetter , D > H;
CHECK_THROWS( ( void )H.top() , contract_violation );
CHECK_THROWS( H.pop() , contract_violation );
auto worse = []( const Entry & a , const Entry & b ) {
return( EntryBetter()( b , a ) );
};
std::priority_queue< Entry , std::vector< Entry > , decltype( worse ) > R(
worse );
std::mt19937 g( seed );
H.reserve( 100 );
for( int t = 0 ; t < 3000 ; ++t )
if( R.empty() || ( g() % 3 ) ) {
const Entry e{ int( g() % 50 ) , int( g() % 10 ) , t };
H.push( e );
R.push( e );
}
else {
CHECK( H.top().id == R.top().id && H.pop().id == R.top().id );
R.pop();
}
CHECK( H.size() == R.size() );
}
/// the heap of arity D on n = 0 , 1 , D , D + 1 , D^2 + 2 items, distinct
/// and all equal, built by pushes and by assign() over items already there,
/// against std::priority_queue; the ties come out by better()
template< unsigned D >
void dary_edges( void )
{
for( std::size_t n : { std::size_t( 0 ) , std::size_t( 1 ) ,
std::size_t( D ) ,
std::size_t( D + 1 ) , std::size_t( D * D + 2 ) } )
for( int same = 0 ; same < 2 ; ++same )
for( int built = 0 ; built < 2 ; ++built ) {
DaryHeap< std::pair< int , int > , std::less<> , D > H;
std::priority_queue< std::pair< int , int > ,
std::vector< std::pair< int , int > > ,
std::greater<> >
R;
std::vector< std::pair< int , int > > X;
for( std::size_t i = 0 ; i < n ; ++i ) { // reversed: all move up
const std::pair< int , int > x( same ? 7 : int( n - i ) ,
same ? 0 : int( i ) );
X.push_back( x );
R.push( x );
}
if( built ) {
H.push( { -1 , -1 } ); // replaced by assign()
H.assign( X.begin() , X.end() );
}
else
for( const auto & x : X )
H.push( x );
CHECK( H.size() == n && std::size_t( H.end() - H.begin() ) == n );
while( ! R.empty() ) {
CHECK( H.top() == R.top() && H.pop() == R.top() );
R.pop();
}
CHECK( H.empty() );
}
}
ADS_TEST( "dary-heap" )
{
dary< 2 >( 2 );
dary< 3 >( 5 );
dary< 4 >( 3 );
dary< 8 >( 4 );
dary_edges< 2 >();
dary_edges< 3 >();
dary_edges< 4 >();
dary_edges< 8 >();
{ // the pushed item aliases
DaryHeap< std::string > A; // the heap, which grows
A.push( "b" );
for( int i = 0 ; i < 40 ; ++i )
A.push( A.top() );
A.push( "a" );
CHECK( A.size() == 42 && A.pop() == "a" && A.top() == "b" );
}
for( std::size_t n = 1 ; n <= 33 ; ++n ) // an item of the heap that
for( std::size_t k = 0 ; k < n ; ++k ) { // moves up, pushed when the
DaryHeap< std::string , std::less<> , 2 > A; // vector is full or not
std::multiset< std::string > R;
for( std::size_t i = 0 ; i < n ; ++i ) {
const std::string s( 24 , char( 'a' + ( i * 7 ) % 26 ) ); // no SSO
A.push( s );
R.insert( s );
}
const std::string & x = A.begin()[ k ];
R.insert( x );
A.push( x );
for( const auto & s : R )
CHECK( A.pop() == s );
CHECK( A.empty() );
}
DaryHeap< int > I; // the least first
for( int x : { 5 , 3 , 9 , 1 } )
I.push( x );
CHECK( I.pop() == 1 && I.pop() == 3 && I.size() == 2 );
I.clear();
CHECK( I.empty() && I.begin() == I.end() );
CHECK_THROWS( ( void )I.top() , contract_violation );
CHECK_THROWS( I.pop() , contract_violation );
DaryHeap< std::string > O; // the root is the last
O.push( "only" );
CHECK( O.top() == "only" && O.pop() == "only" && O.empty() );
DaryHeap< int , std::greater< int > , 3 > X; // the largest first
for( int x : { INT_MIN , 0 , INT_MAX , 0 , INT_MAX , INT_MIN } )
X.push( x );
for( int x : { INT_MAX , INT_MAX , 0 , 0 , INT_MIN , INT_MIN } )
CHECK( X.pop() == x );
CHECK( X.empty() );
for( int dir = 0 ; dir < 3 ; ++dir ) { // up, down, all equal
DaryHeap< int , std::less< int > , 2 > H;
H.reserve( 0 );
for( int k = 0 ; k < 100 ; ++k )
H.push( dir == 0 ? k : dir == 1 ? 99 - k : 42 );
for( int k = 0 ; k < 100 ; ++k )
CHECK( H.pop() == ( dir == 2 ? 42 : k ) );
CHECK( H.empty() );
}
}
// the table of pointers to the nodes of an arena, found by their states,
// against std::unordered_map, with the hashes of a mix and with hashes
// whose low bits are all 0, which the Fibonacci hashing spreads anyway;
// the edge cases: the capacity rounded up to a power of 2 at least 16, the
// growth exactly past 7/10 of the places, the empty table, and many keys
// with the same hash, whose first place is the last one, so that the
// probes wrap around the end
struct Node
{
std::uint64_t state;
int g;
};
ADS_TEST( "pointer-table" )
{
for( int kind = 0 ; kind < 2 ; ++kind ) {
Arena< Node > N( 64 );
PointerTable< Node > T;
std::unordered_map< std::uint64_t , Node * > R;
std::mt19937 gen( 5 + kind );
for( int op = 0 ; op < 20000 ; ++op ) {
const std::uint64_t s = gen() % 6000;
const std::uint64_t h = kind ? s << 20 : splitmix64( s );
T.prefetch( h );
const auto [ p , fresh ] = T.find_or_insert(
s , h , [ & ]( void ) { return( N.make( Node{ s , op } ) ); } );
const auto it = R.find( s );
CHECK( fresh == ( it == R.end() ) );
if( fresh )
R[ s ] = p;
else
CHECK( it->second == p );
CHECK( T.find( s , h ) == p && p->state == s );
}
CHECK( T.size() == R.size() && N.size() == R.size() );
CHECK( T.size() * 10 <= T.capacity() * 7 );
std::size_t n = 0;
T.forEach( [ & ]( Node * ) { ++n; } );
CHECK( n == R.size() );
CHECK( T.find( std::uint64_t( 999999 ) , splitmix64( 999999 ) ) == nullptr );
}
auto byG = []( const Node * p , int g ) { return( p->g == g ); };
Arena< Node > M;
PointerTable< Node , decltype( byG ) > G( 4 , byG ); // any equality
CHECK( G.capacity() == 16 );
auto make3 = [ & ]( void ) { return( M.make( Node{ 0 , 3 } ) ); };
const auto a = G.find_or_insert( 3 , 3 , make3 );
CHECK( a.second && ( G.find_or_insert( 3 , 3 , make3 ).first == a.first ) );
auto nil = []( void ) { return( static_cast< Node * >( nullptr ) ); };
CHECK_THROWS( G.find_or_insert( 4 , 4 , nil ) , contract_violation );
CHECK( G.size() == 1 && G.find( 4 , 4 ) == nullptr ); // not inserted
CHECK( PointerTable< Node >( 0 ).capacity() == 16 );
CHECK( PointerTable< Node >( 16 ).capacity() == 16 );
CHECK( PointerTable< Node >( 17 ).capacity() == 32 );
Arena< Node > N;
PointerTable< Node > E;
CHECK( E.size() == 0 && E.find( std::uint64_t( 0 ) , 0 ) == nullptr );
CHECK( E.begin() == E.end() );
for( std::uint64_t s = 0 ; s < 12 ; ++s ) {
E.find_or_insert( s , splitmix64( s ) ,
[ & ]( void ) { return( N.make( Node{ s , 0 } ) ); } );
CHECK( E.capacity() == ( s < 11 ? 16u : 32u ) ); // 12 > 0.7 * 16
}
std::uint64_t h = 0; // the last place of 1024
while( fibonacciPlace( h , 10 ) != 1023 )
++h;
PointerTable< Node > C( 1024 );
for( std::uint64_t s = 0 ; s < 50 ; ++s ) {
const auto r = C.find_or_insert(
s , h , [ & ]( void ) { return( N.make( Node{ s , 1 } ) ); } );
CHECK( r.second && r.first->state == s );
}
for( std::uint64_t s = 0 ; s < 50 ; ++s )
CHECK( C.find( s , h ) && C.find( s , h )->state == s );
CHECK( C.find( std::uint64_t( 50 ) , h ) == nullptr );
CHECK( C.size() == 50 && C.capacity() == 1024 );
// moved from: empty, found nothing, refilled; clear() keeps the places,
// reserve() leaves no growth up to its n, and no shrink below it
PointerTable< Node > P( std::move( C ) );
CHECK( P.size() == 50 && P.find( std::uint64_t( 7 ) , h )->state == 7 );
CHECK( C.size() == 0 && C.find( std::uint64_t( 7 ) , h ) == nullptr );
CHECK( C.begin() == C.end() );
C.prefetch( h );
const auto r = C.find_or_insert( std::uint64_t( 7 ) , h , [ & ]( void ) {
return( N.make( Node{ 7 , 2 } ) );
} );
CHECK( r.second && C.size() == 1 && C.capacity() == 16 );
PointerTable< Node > A;
A = std::move( P );
CHECK( A.size() == 50 && P.size() == 0 &&
P.find( std::uint64_t( 1 ) , h ) == nullptr );
PointerTable< Node > & self = A;
A = std::move( self );
CHECK( A.size() == 50 && A.find( std::uint64_t( 49 ) , h ) );
A.clear();
CHECK( A.size() == 0 && A.capacity() == 1024 &&
A.find( std::uint64_t( 3 ) , h ) == nullptr );
PointerTable< Node > R;
R.reserve( 700 );
CHECK( R.capacity() == 1024 ); // 700 <= 0.7 * 1024
for( std::uint64_t s = 0 ; s < 700 ; ++s )
R.find_or_insert( s , splitmix64( s ) ,
[ & ]( void ) { return( N.make( Node{ s , 3 } ) ); } );
CHECK( R.capacity() == 1024 && R.size() == 700 );
R.reserve( 10 );
CHECK( R.capacity() == 1024 );
R.reserve( 717 ); // 717 > 0.7 * 1024: 2048
CHECK( R.capacity() == 2048 && R.size() == 700 );
for( std::uint64_t s = 0 ; s < 700 ; ++s )
CHECK( R.find( s , splitmix64( s ) )->state == s );
P.reserve( 0 );
CHECK( P.capacity() == 16 && P.size() == 0 );
}
// the lookups of a const OpenHashTable from 4 threads at once, each against
// the keys inserted: the lookups write probes(), which must not race (a
// test for ThreadSanitizer), and the answers are those of one thread
ADS_TEST( "open-hash-readers" )
{
OpenHashTable< long , long > T( 1024 , Probing::linear() );
for( long k = 0 ; k < 500 ; ++k )
T.insert( 2 * k , k );
const auto & C = T;
std::vector< int > wrong( 4 , 0 );
std::vector< std::thread > W;
for( int t = 0 ; t < 4 ; ++t )
W.emplace_back( [ & , t ]( void ) {
for( int r = 0 ; r < 50 ; ++r )
for( long k = 0 ; k < 1000 ; ++k ) {
const auto v = C.lookup( k );
if( ( C.contains( k ) != ( k % 2 == 0 ) ) ||
( v.has_value() != ( k % 2 == 0 ) ) || ( v && ( *v != k / 2 ) ) )
++wrong[ t ];
}
} );
for( auto & w : W )
w.join();
CHECK( wrong == std::vector< int >( 4 , 0 ) );
CHECK( C.probes() >= 1 );
}
// the hash of FlatHashTable that declares itself avalanching, so that the
// table takes the key as its mixed hash and a test chooses the first group
// (the top bits), the fingerprint (the low 8 bits) and the bit of overflow
// (the bits 8 to 10) of each key; on 32 bits the table mixes it anyway
struct Raw
{
using is_avalanching = void;
std::size_t operator()( std::uint64_t x ) const {
return( std::size_t( x ) );
}
};
/// the key of Raw in the group g of a table of 2^lg groups, its low bits
/// low and j in the bits 11 and up
std::uint64_t raw( std::uint64_t g , int lg , std::uint64_t j ,
std::uint64_t low )
{
return( ( g << ( 64 - lg ) ) | ( j << 11 ) | low );
}
/// a hash that puts every key in one group with one fingerprint
struct Same
{
std::size_t operator()( std::uint64_t ) const { return( 42 ); }
};
/// a value that counts its living copies
struct Alive
{
static long count;
std::string s;
Alive( void ) { ++count; }
explicit Alive( std::string t ) : s( std::move( t ) ) { ++count; }
Alive( const Alive & o ) : s( o.s ) { ++count; }
Alive( Alive && o ) noexcept : s( std::move( o.s ) ) { ++count; }
Alive & operator=( const Alive & ) = default;
Alive & operator=( Alive && ) noexcept = default;
~Alive() { --count; }
};
long Alive::count = 0;
/// a value whose copies throw once a budget runs out, and whose moves may
/// throw, so that a rebuild copies the pairs
struct Fussy
{
static long count , budget;
std::string s;
explicit Fussy( std::string t ) : s( std::move( t ) ) { ++count; }
Fussy( const Fussy & o ) : s( o.s ) {
if( budget-- == 0 )
throw( std::runtime_error( "copy" ) );
++count;
}
Fussy( Fussy && o ) noexcept( false ) : s( o.s ) {
if( budget-- == 0 )
throw( std::runtime_error( "move" ) );
++count;
}
Fussy & operator=( const Fussy & ) = default;
~Fussy() { --count; }
};
long Fussy::count = 0 , Fussy::budget = -1;
/// t against r on random insertions, removals and lookups of keys in
/// [ 0 , range ), where the range keeps many deleted places
template< class T , class R , class G >
void against( T & t , R & r , std::uint64_t range , int ops , G && key ,
unsigned seed )
{
std::mt19937_64 g( seed );
for( int op = 0 ; op < ops ; ++op ) {
const auto k = key( g() % range );
switch( g() % 4 ) {
case( 0 ):
case( 1 ): {
const auto v = key( g() % 1000 );
CHECK( t.insert( k , v ) == ( r.find( k ) == r.end() ) );
r[ k ] = v;
break;
}
case( 2 ):
CHECK( t.remove( k ) == ( r.erase( k ) == 1 ) );
break;
default: {
const auto it = r.find( k );
const auto o = t.lookup( k );
CHECK( o.has_value() == ( it != r.end() ) );
if( o )
CHECK( *o == it->second );
}
}
}
CHECK( t.size() == r.size() );
std::size_t n = 0;
for( const auto & [ k , v ] : t ) {
++n;
CHECK( r.count( k ) == 1 && r.at( k ) == v );
}
CHECK( n == r.size() );
}
static_assert( Dictionary< FlatHashTable< int , int > > );
/// the keys of t by its iterators forward, checked against the walk
/// backward from the end, the same keys in the reverse order
template< class T >
std::vector< std::uint64_t > walk( const T & t )
{
std::vector< std::uint64_t > f , b;
for( const auto & [ k , v ] : t )
f.push_back( k );
for( auto e = t.end() ; e != t.begin() ; )
b.push_back( ( *--e ).first );
std::reverse( b.begin() , b.end() );
CHECK( f == b && f.size() == t.size() );
return( f );
}
// the product of 128 bits by 32 bits as by the machine, at the extremes
static_assert( detail::mulxPortable( 0 , 0x9e3779b97f4a7c15ULL ) == 0 );
static_assert( detail::mulxPortable( 1ULL << 63 , 2 ) == 1 );
static_assert( detail::mulxPortable( ~0ULL , ~0ULL ) == ~0ULL );
static_assert( detail::mulxPortable( 1ULL << 32 , 1ULL << 32 ) == 1 );
// the table with control bytes against std::unordered_map, on integers
// with the identity hash, random and clustered (multiples of the places,
// of the groups and of 2^32), on keys that are their own mixed hash, all
// with one fingerprint, and on strings; the edges: the product of the
// mixing on the machine and by 32 bits, the empty table with no places, a
// table for 1 pair, the growth exactly past 7 / 8 of the places, the
// removals down to empty and the reuse, many deleted places and then
// lookups of absent keys (which end), every key in one group with one
// fingerprint, the 15 places of a group with one fingerprint and the 16th
// key past them, the fingerprint 0 taken as 1, the bit of overflow set,
// the place emptied under it counted and reused, the bits cleared by a
// rebuild of the same size, a group that wraps around the end, the
// iterators across removals and erase( it ) while iterating, a value of
// the table inserted with a new key that grows it, a copy that throws in
// the rebuild and in the copy of the table, the copies and the moves, and
// no value leaked; the walk of the groups by the iterators, forward and
// backward: no places, after reserve(), after clear() and after a move, the
// first and the last group alone, two full groups, places removed under
// an iterator and skipped, the empty groups between them, erase() from the
// end back to the beginning, every rebuild of one overflowing group, and a
// self-move; run also with ADS_FLAT_SIMD=0, the broadword fallback
ADS_TEST( "flat-hash-table" )
{
for( std::uint64_t a :
{ 0ULL , 1ULL , 0xFFFFFFFFULL , 1ULL << 32 , 1ULL << 63 , ~0ULL ,
0x9e3779b97f4a7c15ULL , 0x123456789abcdef0ULL } )
for( std::uint64_t b : { 0ULL , 1ULL , 0xFFFFFFFFULL , 1ULL << 63 , ~0ULL ,
0x9e3779b97f4a7c15ULL } )
CHECK( detail::mulx( a , b ) == detail::mulxPortable( a , b ) );
{ // random, with tombstones
FlatHashTable< std::uint64_t , std::uint64_t > T;
std::unordered_map< std::uint64_t , std::uint64_t > R;
against( T , R , 3000 , 60000 , []( std::uint64_t x ) { return( x ); } , 1 );
against( T , R , 1 << 20 , 20000 , []( std::uint64_t x ) { return( x ); } ,
2 );
}
for( std::uint64_t c : { std::uint64_t( 30720 ) , std::uint64_t( 2048 ) ,
std::uint64_t( 1 ) << 32 } ) { // clustered
FlatHashTable< std::uint64_t , std::uint64_t > T;
std::unordered_map< std::uint64_t , std::uint64_t > R;
against(
T , R , 40000 , 60000 , [ c ]( std::uint64_t x ) { return( x * c ); } ,
5 );
}
FlatHashTable< std::uint64_t , int > E; // empty, no places
CHECK( E.empty() && E.capacity() == 0 && E.load() == 0.0 );
CHECK( ! E.lookup( 0 ) && E.find( 7 ) == nullptr && ! E.contains( 7 ) );
CHECK( ! E.remove( 7 ) && E.begin() == E.end() && E.size() == 0 );
E.clear();
E.reserve( 0 );
CHECK( E.capacity() == 0 && E.deleted() == 0 );
FlatHashTable< std::uint64_t , int > One( 1 ); // room for 1 pair
CHECK( One.capacity() == 30 && One.insert( 5 , 50 ) && One.size() == 1 );
CHECK( ! One.insert( 5 , 51 ) && One.lookup( 5 ) == 51 );
*One.find( 5 ) = 52;
CHECK( One.lookup( 5 ) == 52 && One.capacity() == 30 );
for( std::size_t m : { 30 , 60 , 240 } ) { // 7 / 8 of m, then one more
FlatHashTable< std::uint64_t , int > G;
for( std::uint64_t k = 0 ; k < m - m / 8 ; ++k )
G.insert( k , 0 );
CHECK( G.capacity() == m && G.size() == m - m / 8 );
G.insert( m , 0 );
CHECK( G.capacity() == 2 * m && G.size() == m - m / 8 + 1 );
for( std::uint64_t k = 0 ; k <= m ; ++k )
CHECK( G.contains( k ) == ( k < m - m / 8 || k == m ) );
using Int = FlatHashTable< int , int >;
CHECK( Int( m - m / 8 ).capacity() == m );
CHECK( Int( m - m / 8 + 1 ).capacity() == 2 * m );
}
FlatHashTable< std::uint64_t , int , Same > S; // one group, one tag
for( std::uint64_t k = 0 ; k < 200 ; ++k )
CHECK( S.insert( k , int( k ) ) );
for( std::uint64_t k = 0 ; k < 200 ; ++k )
CHECK( S.lookup( k ) == int( k ) );
CHECK( ! S.contains( 200 ) );
const std::size_t mS = S.capacity();
for( std::uint64_t k = 0 ; k < 200 ; ++k )
CHECK( S.remove( k ) );
CHECK( S.empty() && S.deleted() > 0 && S.capacity() == mS );
for( std::uint64_t k = 200 ; k < 300 ; ++k )
CHECK( ! S.contains( k ) );
for( std::uint64_t k = 0 ; k < 150 ; ++k )
S.insert( k , 1 ); // reused
CHECK( S.size() == 150 && S.capacity() == mS );
for( std::uint64_t k = 0 ; k < 150 ; ++k )
CHECK( S.lookup( k ) == 1 );
{ // churn at a steady size near the bound: the
// places counted recounted, few rebuilds
FlatHashTable< std::uint64_t , std::uint64_t > C;
std::unordered_map< std::uint64_t , std::uint64_t > R;
std::mt19937_64 g( 7 );
for( std::uint64_t k = 0 ; k < 1200 ; ++k ) { // 1200 of 1920 places
C.insert( k , k );
R[ k ] = k;
}
const std::size_t m = C.capacity();
CHECK( m == 1920 );
std::vector< std::uint64_t > in( 1200 );
for( std::uint64_t k = 0 ; k < 1200 ; ++k )
in[ k ] = k;
for( std::uint64_t t = 0 ; t < 100000 ; ++t ) { // one out, one in
const std::size_t i = g() % in.size();
CHECK( C.remove( in[ i ] ) && R.erase( in[ i ] ) == 1 );
in[ i ] = 1200 + t;
CHECK( C.insert( in[ i ] , t ) );
R[ in[ i ] ] = t;
const std::size_t c = C.capacity();
CHECK( C.size() + C.deleted() <= c - c / 8 );
}
CHECK( C.capacity() <= 2 * m && C.size() == R.size() );
for( const auto & [ k , v ] : R )
CHECK( C.lookup( k ) == v );
for( const auto & [ k , v ] : C )
CHECK( R.at( k ) == v );
}
{ // deleted places churned
FlatHashTable< std::uint64_t , int , Same > D;
for( int round = 0 ; round < 50 ; ++round ) {
for( std::uint64_t k = 0 ; k < 20 ; ++k )
D.insert( 100 * round + k , 0 );
for( std::uint64_t k = 0 ; k < 20 ; ++k )
D.remove( 100 * round + k );
CHECK( ! D.contains( 100 * round ) && D.capacity() <= 60 );
}
CHECK( D.empty() );
}
{ // one fingerprint, 2 groups