-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcbset.h
2115 lines (1975 loc) · 52.9 KB
/
cbset.h
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
/*
* Copyright (C) 2017 Felix Salfelder
* Authors: Felix Salfelder
*
* This file is part of "freetdi", the free tree decomposition intiative
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
*
* bitvector based set implementation.
*/
#ifndef GALA_CBSET_H
#define GALA_CBSET_H
#include "platform.h"
#include "trace.h"
#include "assert.h"
#include <string.h> // memcpy
#include <bit>
/*--------------------------------------------------------------------------*/
// TODO: size/storage control
// - first-set-bit?
// - fixed-K
// - more stl compliance
// - rename
#define BSDarg unsigned NCHNK, \
typename CHUNK_T, \
typename HMT, \
typename OST, \
typename SCT
#define BSDt template<BSDarg>
#define BSDto template<BSDarg ## 2>
#define BSDa NCHNK, CHUNK_T, HMT, OST, SCT
#define BSDa1 NCHNK, CHUNK_T, HMT, OST, SCT1
#define BSDa2 NCHNK, CHUNK_T, HMT, OST, SCT2
#undef lassert
#define lassert(x) assert(x)
#ifdef NDEBUG
//#undef unreachable
//#define unreachable() __builtin_unreachable()
#endif
namespace cbset{
namespace bits{
// fixme: upper end missing?
template<class CT>
static inline void shiftBy(CT* s, unsigned dist, unsigned howmany=-1u)
{
assert(dist>0);
assert(dist);
if(howmany==-1u){ untested();
#ifndef NDEBUG
incomplete();
#endif
}else{
howmany += dist;
}
assert(howmany>0);
for(unsigned x=howmany-1; x+1 != dist; --x) {
unsigned tgt=x;
if(tgt>howmany) break;
s[x] = s[x-dist];
}
// lower pad;
for(unsigned x=0; x<dist; ++x) {
s[x] = 0;
}
}
} // bits
/*--------------------------------------------------------------------------*/
//template<class S>
//static inline bool contains(S const& s, typename S::value_type i);
template<class S>
static inline bool contains(S const& s, typename S::value_type i)
{
return s.contains(i);
}
/*--------------------------------------------------------------------------*/
struct nosize_t{
nosize_t(int){
}
bool operator!=(const nosize_t&)const{ untested();
return false;
}
};
/*--------------------------------------------------------------------------*/
struct nooffset_t{
nooffset_t(int){
}
operator unsigned() const { return 0; }
};
/*--------------------------------------------------------------------------*/
struct nohowmany_t{
nohowmany_t(){}
nohowmany_t(int){
}
bool operator==(unsigned)const{ untested();
return false;
}
};
/*--------------------------------------------------------------------------*/
template<unsigned NCHNK,
typename CHUNK_T=uint8_t,
typename OST=uint8_t,
typename HMT=uint8_t,
typename SCT=unsigned>
class BSET_DYNAMIC;
/*--------------------------------------------------------------------------*/
namespace detail{
/*--------------------------------------------------------------------------*/
BSDt
struct cbshelp{
static unsigned get_size(BSET_DYNAMIC<BSDa> const&, unsigned size) {
return size;
}
static bool get_empty(BSET_DYNAMIC<BSDa> const&, unsigned size) {
return !size;
}
static void set_size(SCT& x, unsigned size) {
x = size;
}
};
/*--------------------------------------------------------------------------*/
} // detail
/*--------------------------------------------------------------------------*/
namespace detail{
/*--------------------------------------------------------------------------*/
template<class OST>
struct ofhelp{
typedef OST type;
template<class H>
static const H& get(H const& h, unsigned){ untested();
return h;
}
template<class H>
static void set(type& h, H x){ untested();
h = x;
}
static constexpr bool active=true;
};
/*--------------------------------------------------------------------------*/
template<>
struct ofhelp<nooffset_t>{
typedef unsigned type;
template<class NCHNK>
static NCHNK get(nooffset_t, NCHNK w){ untested();
return w;
}
template<class H>
static void set(nooffset_t&, H){ itested();
}
static constexpr bool active=false;
};
/*--------------------------------------------------------------------------*/
template<class HMT>
struct hmhelp{
typedef HMT type;
typedef HMT const& ref;
template<class H>
static const H& get(H const& h, unsigned){
return h;
}
template<class H>
static type& set(type& h, H x){
h = x;
return h;
}
template<class D>
static void trim(HMT& h, D const* d){ untested();
for(;h && !d[h-1]; --h);
}
template<class D>
static void trim_(HMT& h, D const* d){ untested();
for(;!d[h-1]; --h);
}
template<class X, class D>
static void pad(type& h, X const& x, D* d){
for(; h<=x; ++h){
d[h] = 0;
}
}
template<class D>
static void clear(D*, unsigned){
}
static constexpr bool active=true;
};
/*--------------------------------------------------------------------------*/
template<>
struct hmhelp<nohowmany_t>{
typedef unsigned type;
typedef unsigned ref;
template<class NCHNK>
static NCHNK get(nohowmany_t, NCHNK w){
return w;
}
template<class H>
static void set(nohowmany_t&, H){itested();
}
template<class D>
static void trim(nohowmany_t&, D*){itested();
}
template<class D>
static void trim_(nohowmany_t&, D*){
}
template<class X, class D>
static void pad(nohowmany_t&, X, D*){
}
template<class D>
static void clear(D* d, unsigned n){
for(unsigned i=0; i<n; ++i){
d[i]=0;
}
}
static constexpr bool active=false;
};
/*--------------------------------------------------------------------------*/
} // detail
/*--------------------------------------------------------------------------*/
template<class S1, class S2>
class lazy_intersection;
/*--------------------------------------------------------------------------*/
BSDt
class BSET_DYNAMIC {
public:
constexpr static unsigned CHUNKBITS=8*sizeof(CHUNK_T);
constexpr static unsigned max_element=8*sizeof(CHUNK_T)*NCHNK-1;
typedef unsigned value_type;
typedef OST offset_type;
typedef enum{setminus} _setminus_t;
typedef enum{sqcup} _sqcup_t;
typedef enum{cup} _cup_t;
typedef enum{cap} _cap_t;
public:
static bool use_offset(){ return detail::ofhelp<OST>::active; }
static bool use_howmany(){ return detail::hmhelp<HMT>::active; }
public:
explicit BSET_DYNAMIC()
: _howmany(0),
_offset(0),
_size(0)
{
detail::hmhelp<HMT>::clear(_d, NCHNK);
}
BSET_DYNAMIC(BSET_DYNAMIC&& o) noexcept
: _howmany(o._howmany), _offset(o._offset), _size(o._size) {
memcpy(_d, o._d, howmany()*sizeof(CHUNK_T));
assert(howmany()<=NCHNK); // for now.
// for (unsigned i=0; i<howmany(); ++i) { itested();
// _d[i] = o._d[i];
// }
assert(size()==o.size()); // for now.
}
BSET_DYNAMIC(BSET_DYNAMIC const& o) :
_howmany(o._howmany), _offset(o._offset), _size(o._size) {
memcpy(_d, o._d, howmany()*sizeof(CHUNK_T));
assert(howmany()<=NCHNK); // for now.
assert(size()==o.size()); // for now.
}
BSET_DYNAMIC& operator=(BSET_DYNAMIC const&o) noexcept{ itested();
_howmany = o._howmany;
_offset = o._offset;
_size = o._size;
memcpy(_d, o._d, howmany()*sizeof(CHUNK_T));
return *this;
}
BSET_DYNAMIC& operator=(BSET_DYNAMIC&& o) noexcept{
_howmany = o._howmany;
_offset = o._offset;
_size = o._size;
memcpy(_d, o._d, howmany()*sizeof(CHUNK_T));
// for (unsigned i=0; i<howmany(); ++i) {
// _d[i] = o._d[i];
// }
assert(size()==o.size()); // for now.
return *this;
}
public:
template<class SCT2>
BSET_DYNAMIC(const BSET_DYNAMIC& s, const BSET_DYNAMIC<BSDa2>& t, _setminus_t);
template<class SCT2>
BSET_DYNAMIC(const BSET_DYNAMIC& s, const BSET_DYNAMIC<BSDa2>& t, _cup_t);
template<class SCT2>
BSET_DYNAMIC(const BSET_DYNAMIC& s, const BSET_DYNAMIC<BSDa2>& t, _sqcup_t);
template<class SCT2>
BSET_DYNAMIC(const BSET_DYNAMIC& s, const BSET_DYNAMIC<BSDa2>& t, _cap_t);
public:
class const_iterator{
public:
const_iterator() : _k(0), _s(NULL) {
}
const_iterator(const const_iterator& o)
: _k(o._k), _c(o._c), _s(o._s)
{
}
const_iterator(unsigned x, BSET_DYNAMIC const& s) :
_k(0), _s(&s)
{
assert(x>=CHUNKBITS*s.offset());
_k = x/CHUNKBITS;
if(_k==NCHNK){
_c = 0;
}else if(s.howmany()==0){
// skip to end...
_k = 1;
_c = 0;
}else{
// assert(_i<CHUNKBITS*(s._offset+s._howmany));
_c = _s->_d[_k-_s->offset()] >> (x%CHUNKBITS);
skip();
}
}
bool operator==(const_iterator const& o) const{
assert(_s);
if(_k!=o._k){
return false;
}else if(_c!=o._c){
return false;
}else{
return true;
}
}
bool operator!=(const_iterator const& o) const{
return !operator==(o);
}
#if __cplusplus >= 201709
unsigned operator*() const{
return _k * CHUNKBITS + std::countr_zero(_c);
}
#else
unsigned operator*() const{
if(CHUNKBITS>32) {
return _k * CHUNKBITS + __builtin_ctzl(_c);
}else{
return _k * CHUNKBITS + __builtin_ctz(_c);
}
}
#endif
const_iterator& operator++(){
_c ^= _c & -_c;
skip();
return *this;
}
const_iterator& operator=(const_iterator const& o){ itested();
_k = o._k;
_c = o._c;
_s = o._s;
return *this;
}
private:
void /*const_iterator::*/skip(){
while(_k<(_s->howmany()+_s->offset())){
if (_c){
break;
}else{
++_k;
// trace1("skip", _k);
if(_k==NCHNK){
_c = 0;
}else{
_c = _s->_d[_k-_s->offset()];
}
}
}
}
private:
unsigned _k;
CHUNK_T _c;
BSET_DYNAMIC const* _s;
}; // const_iterator
typedef const_iterator iterator;
class const_reverse_iterator{
public:
const_reverse_iterator() : _i(-1u), _s(NULL) {
}
const_reverse_iterator(const const_reverse_iterator& o)
: _i(o._i), _c(o._c), _s(o._s)
{
}
const_reverse_iterator(unsigned x, BSET_DYNAMIC const& s) :
_i(x), _s(&s)
{
assert(x>=CHUNKBITS*s.offset());
if(_i==-1u){
// }else if(_i>=CHUNKBITS*(s._offset+s._howmany)){ untested();
// unreachable();
// _i = -1u; // CHUNKBITS*(s._offset+s._howmany);
// _c = 0;
}else if(s.howmany()==0){
// skip to end...
_i=-1u;
}else{
// assert(_i<CHUNKBITS*(s._offset+s._howmany));
_c = _s->_d[(x/CHUNKBITS)-_s->offset()] >> (x%CHUNKBITS);
if(_c){
// newonb is not trimmed...
// need to iterate anyway.
unsigned clz;
if(CHUNKBITS>32){
clz = __builtin_clzl(_c);
}else{
clz = __builtin_clz(_c);
}
_i = CHUNKBITS - 1 - clz;
// _c = _c << clz;
}else{
}
// skip();
}
}
unsigned operator*() const{return _i;}
private:
unsigned _i;
CHUNK_T _c;
BSET_DYNAMIC const* _s;
};
class intersection_iterator{ //
public:
intersection_iterator(BSET_DYNAMIC const& s, BSET_DYNAMIC const& t, unsigned)
: _i(CHUNKBITS*std::min(s.howmany()+s.offset(), t.howmany()+t.offset()))
{ untested();
}
intersection_iterator(BSET_DYNAMIC const& s, BSET_DYNAMIC const& t)
: _s((s.offset()<=t.offset())?&t:&s),
_t((s.offset()<=t.offset())?&s:&t),
_delta(_s->offset()-_t->offset())
{ untested();
_i = _s->offset();
unsigned M=std::min(_s->howmany()+_s->offset(), _t->howmany()+_t->offset());
if(_i>M){ untested();
_i=M;
}
_i *= CHUNKBITS;
#ifndef NDEBUG
if(!_s->howmany()){ untested();
incomplete();
}else if(!t.howmany()){ untested();
incomplete();
}
#endif
_c = _s->_d[0];
if(_delta<_t->howmany()){ untested();
_c &= _t->_d[_delta];
}else{ untested();
}
skip();
}
bool operator==(intersection_iterator const& o) const{ untested();
assert(_s);
return _i==o._i;
}
bool operator!=(intersection_iterator const& o) const{ untested();
return !operator==(o);
}
unsigned operator*() const{ untested();
assert(_s);
return _i;
}
intersection_iterator& operator++(){ untested();
assert(_s);
assert(_t);
unsigned M=std::min(_s->howmany()+_s->offset(), _t->howmany()+_t->offset());
assert(_i<CHUNKBITS*M);
_c = _c+0;
inc();
skip();
return *this;
}
private:
void inc(){ untested();
++_i;
assert(_i <= CHUNKBITS*(_s->howmany()+_s->offset())
||_i <= CHUNKBITS*(_t->howmany()+_t->offset()));
if(_i%CHUNKBITS){ untested();
_c = _c >> 1;
}else{ untested();
if(_i/CHUNKBITS-_s->offset() < _s->howmany()){ untested();
_c = _s->_d[_i/CHUNKBITS-_s->offset()];
}else{ untested();
_c = 0;
}
if(_i/CHUNKBITS-_t->offset() < _t->howmany()){ untested();
_c &= _t->_d[_i/CHUNKBITS-_t->offset()];
}else{ untested();
}
}
}
void skip(){ untested();
unsigned M=std::min(_s->howmany()+_s->offset(), _t->howmany()+_t->offset());
while(_i<CHUNKBITS*M){ untested();
if (_c & 1){ untested();
break;
}else{ untested();
}
inc();
}
}
unsigned _i;
CHUNK_T _c;
BSET_DYNAMIC const* _s;
BSET_DYNAMIC const* _t;
unsigned _delta;
}; // intersection_iterator
class union_iterator{
public:
union_iterator(BSET_DYNAMIC const& s, BSET_DYNAMIC const& t, unsigned)
: _i(CHUNKBITS*std::max(s.offset()+s.howmany(), t.offset()+t.howmany()))
{ untested();
}
union_iterator(BSET_DYNAMIC const& s, BSET_DYNAMIC const& t)
: _s((s.offset()<=t.offset())?&s:&t),
_t((s.offset()<=t.offset())?&t:&s),
_delta(_t->offset()-_s->offset())
{ untested();
_i=(_s->offset()*CHUNKBITS);
if(!s.howmany()){ untested();
_c = 0;
}else if(!t.howmany()){ untested();
_c = 0;
}else{ untested();
_c = _s->_d[0] | _t->_d[_delta];
}
skip();
}
bool operator==(union_iterator const& o) const{return _i==o._i;}
bool operator!=(union_iterator const& o) const{return _i!=o._i;}
unsigned operator*() const{return _i;}
union_iterator& operator++(){ untested();
assert(_s);
assert(_t);
unsigned M=std::max(_s->howmany()+_s->offset(), _t->howmany()+_t->offset());
assert(_i<CHUNKBITS*M);
inc();
skip();
return *this;
}
private:
void inc(){ untested();
++_i;
assert(_i <= CHUNKBITS*(_s->howmany()+_s->offset())
||_i <= CHUNKBITS*(_t->howmany()+_t->offset()));
assert(_i!=-1u);
if(_i%CHUNKBITS){ untested();
_c = _c >> 1u;
}else{ untested();
if(_i/CHUNKBITS-_s->offset() < _s->howmany()){ untested();
_c = _s->_d[_i/CHUNKBITS-_s->offset()];
}else{ untested();
_c = 0;
}
if(_i/CHUNKBITS-_t->offset() < _t->howmany()){ untested();
_c |= _t->_d[_i/CHUNKBITS-_t->offset()];
}else{ untested();
}
}
}
void skip(){ untested();
unsigned M=std::max(_s->howmany()+_s->offset(), _t->howmany()+_t->offset());
while(_i<CHUNKBITS*M){ itested();
if (_c & 1){ itested();
break;
}else{ itested();
}
inc();
}
}
unsigned _i;
CHUNK_T _c;
BSET_DYNAMIC const* _s;
BSET_DYNAMIC const* _t;
unsigned _delta;
};
bool contains(value_type i) const;
bool operator==(BSET_DYNAMIC const& t) const;
bool operator!=(BSET_DYNAMIC const& t) const{ untested();
return !(*this == t);
}
bool is_subset_of(BSET_DYNAMIC const& t) const;
int compare_int(BSET_DYNAMIC const& t) const;
unsigned size() const {
return detail::cbshelp<BSDa>::get_size(*this, _size);
}
bool empty() const {
return detail::cbshelp<BSDa>::get_empty(*this, _size);
}
unsigned recount() const;
unsigned long hash() const;
template<class SCT2>
bool intersects(BSET_DYNAMIC<BSDa2> const& t) const;
public: // modify
std::pair<unsigned /*SCT?*/, bool> insert(value_type i);
void add(value_type i);
template<class S>
void add_sorted_sequence(S const& s);
void erase(value_type i);
template<class S>
void remove_sorted_sequence(S const& s);
void intersect(BSET_DYNAMIC const& t);
void carve(const BSET_DYNAMIC& t);
void subtract(BSET_DYNAMIC const& t);
template<class SCT2>
void merge(BSET_DYNAMIC<BSDa2> const& t);
value_type front() const;
value_type back() const;
void clear(){
_howmany = 0;
_offset = 0;
_size = 0;
detail::hmhelp<HMT>::clear(_d, NCHNK);
}
public: // dangerous
void erase_(value_type i);
void trim_above();
void trim_below();
public: // protect and friends?
void set_offset(offset_type x){
_offset=x;
}
void set_size(unsigned s){
detail::cbshelp<BSDa>::set_size(_size, s);
}
public: // const xs
const_iterator begin() const{
assert(offset()==offset());
return const_iterator(CHUNKBITS*offset(), *this);
}
const_reverse_iterator rbegin() const{
assert(offset()==offset());
return const_reverse_iterator(CHUNKBITS*offset(), *this);
}
const_iterator end() const{
return const_iterator(CHUNKBITS*(offset()+howmany()), *this);
}
const_iterator find(unsigned x) const{
if(contains(x)){
return const_iterator(x, *this);
}else{
return end();
}
}
OST offset()const {
if(_offset<NCHNK){
// ok
}else{ untested();
}
return _offset;
}
CHUNK_T const& chunk(int idx) const{ itested();
return _d[idx];
}
public: // howmany stuff
typename detail::hmhelp<HMT>::ref const howmany() const {
return detail::hmhelp<HMT>::get(_howmany, NCHNK);
}
private:
void trim_tail(){itested();
detail::hmhelp<HMT>::trim(_howmany, _d);
}
void set_howmany(unsigned h){
detail::hmhelp<HMT>::set(_howmany, h);
}
public:
void* next()const { untested();
return (void*)(uintptr_t(this)
+ sizeof(HMT) + sizeof(OST) + sizeof(SCT)
+ howmany()*sizeof(CHUNK_T));
}
void check() const{itested();
#ifndef NDEBUG
if(howmany()){itested();
assert(_d[0] == _d[0]);
assert(_d[howmany()-1] == _d[howmany()-1]);
}
assert(_offset<=NCHNK);
assert(size() == recount());
for(auto const& v: *this){itested();
assert(contains(v));
}
#endif
}
private:
template<class SCT1, class SCT2>
void merge(BSET_DYNAMIC<BSDa1> const&,
BSET_DYNAMIC<BSDa2> const&, unsigned disjoint=0);
private:
HMT _howmany;
OST _offset;
SCT _size;
CHUNK_T _d[NCHNK];
public:
template<unsigned W_, typename CHUNK_T_, typename H_, typename O_, class SCT_>
friend class BSET_DYNAMIC;
template<class A, class B>
friend class lazy_intersection; // too many?!
}; // BSET_DYNAMIC
/*--------------------------------------------------------------------------*/
namespace detail{
/*--------------------------------------------------------------------------*/
template<unsigned NCHNK, typename CHUNK_T, typename HMT, typename OST>
struct cbshelp<NCHNK, CHUNK_T, HMT, OST, nosize_t>{
static unsigned get_size(
BSET_DYNAMIC<NCHNK, CHUNK_T, HMT, OST, nosize_t> const& n, nosize_t)
{
return n.recount();
}
static unsigned get_empty(
BSET_DYNAMIC<NCHNK, CHUNK_T, HMT, OST, nosize_t> const& n, nosize_t)
{
return n.begin() == n.end();
}
static void set_size(nosize_t, unsigned)
{
}
};
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
template<class window_t, class chunk_t>
struct cnt{
static constexpr unsigned size=sizeof(window_t);
static constexpr unsigned chunksize=sizeof(chunk_t);
template<class D, class NCHNK, class H>
static unsigned count_bits(D const* dd, NCHNK& w, H h){
unsigned k=0;
if(sizeof(chunk_t)<=size){
for (; w+(size/chunksize)<=h;){
window_t d;
memcpy(&d, dd+w, size);
k += popcount(d);
w += size/chunksize;
}
}
return k;
}
/*--------------------------------------------------------------------------*/
}; // cnt
/*--------------------------------------------------------------------------*/
} // detail
/*--------------------------------------------------------------------------*/
BSDt
inline unsigned BSET_DYNAMIC<BSDa>::recount() const
{
// no, carve does not trim...
// if(_howmany){ untested();
// assert(_d[0]);
// assert(_d[_howmany-1]);
// }
assert(sizeof(long)==8);
assert(sizeof(unsigned)==4);
assert(sizeof(short)==2);
unsigned k = 0;
unsigned w = 0;
k += detail::cnt<unsigned long, CHUNK_T>::count_bits(_d, w, howmany());
k += detail::cnt<unsigned, CHUNK_T>::count_bits(_d, w, howmany());
k += detail::cnt<unsigned short, CHUNK_T>::count_bits(_d, w, howmany());
k += detail::cnt<unsigned char, CHUNK_T>::count_bits(_d, w, howmany());
assert(k<=max_element+1);
return k;
}
/*--------------------------------------------------------------------------*/
BSDt
static inline void subtract(BSET_DYNAMIC<BSDa>& s, BSET_DYNAMIC<BSDa> const& t)
{ untested();
return s.subtract(t);
}
/*--------------------------------------------------------------------------*/
BSDt
inline void BSET_DYNAMIC<BSDa>::subtract(BSET_DYNAMIC const& t)
{ itested();
// counter??
unsigned i=std::max(offset(), t.offset());
for (; i<offset()+howmany() && i<t.offset()+t.howmany(); i++) { itested();
_d[i-offset()] &= ~t._d[i-t.offset()];
}
}
/*--------------------------------------------------------------------------*/
template<BSDarg, typename SCT2>
static inline BSET_DYNAMIC<BSDa> diff(BSET_DYNAMIC<BSDa> const& s,
BSET_DYNAMIC<BSDa2> const& t)
{itested();
s.check();
t.check();
return BSET_DYNAMIC<BSDa>(s, t, BSET_DYNAMIC<BSDa>::setminus);
}
/*--------------------------------------------------------------------------*/
// construct s\t
BSDt
template<class SCT2>
BSET_DYNAMIC<BSDa>::BSET_DYNAMIC(const BSET_DYNAMIC& s,
const BSET_DYNAMIC<BSDa2>& t, BSET_DYNAMIC::_setminus_t)
: _offset(s._offset), _size(s._size)
{itested();
#ifndef NDEBUG
set_howmany(0);
for(unsigned i=0; i<NCHNK; ++i){itested();
_d[i] = 0;
}
#endif
assert(s.size()==s.recount());
assert(t.size()==t.recount());
int delta=s._offset-t._offset;
if(delta>=0){itested();
}else{ untested();
}
int i=0;
// not reached from t.
for (; int(i)<-delta && unsigned(i)<s.howmany(); i++) { untested();
_d[i] = s._d[i];
}
for (; unsigned(i)<s.howmany() && (i+delta)<int(t.howmany()); i++) {itested();
assert(i+delta>=0);
set_size(size() - popcount(s._d[i] & t._d[i+delta]));
_d[i] = s._d[i] & ~t._d[i+delta];
}
assert(i>=0);
for (; unsigned(i)<s.howmany(); i++) { untested();
assert(i>=0);
assert(i+delta>=0);
_d[i] = s._d[i];
}
set_howmany(i);
trim_tail();
assert(howmany()<=NCHNK); // for now.
trim_below(); // inefficient... (but works)
assert(size()==recount());
check();
}
/*--------------------------------------------------------------------------*/
BSDt
void BSET_DYNAMIC<BSDa>::carve(const BSET_DYNAMIC& t)
{itested();
assert(size() == recount());
assert(t.size() == t.recount());
int delta=_offset-t._offset;
unsigned i;
// not reached from t.
#if 1
i = 0;
for (; int(i)<-delta && i<howmany(); i++);
#else
if(delta>=0){ untested();
i = 0;
}else if(-delta<int(howmany())){ untested();
i = -delta;
}else{ untested();
i = howmany();
}
#endif
for (; i<howmany() && i+delta<t.howmany() ; i++) {itested();
set_size(size() - popcount(_d[i] & t._d[i+delta]));
_d[i] &= ~t._d[i+delta];
}
assert(size()==recount());
#if 0
for(; _howmany && !_d[_howmany-1];){ untested();
--_howmany;
}
#else
trim_tail();
#endif
}
/*--------------------------------------------------------------------------*/
BSDt
static inline void unionWith(BSET_DYNAMIC<BSDa>& s, BSET_DYNAMIC<BSDa> const& t)
{
assert(s.size()==s.recount());
assert(t.size()==t.recount());
s.merge(t);
assert(s.size()==s.recount());
}
/*--------------------------------------------------------------------------*/
BSDt
std::ostream& operator<<(std::ostream& o, BSET_DYNAMIC<BSDa> const& s);
// union.
BSDt
template<class SCT2>
BSET_DYNAMIC<BSDa>::BSET_DYNAMIC(const BSET_DYNAMIC& s,
const BSET_DYNAMIC<BSDa2>& t, BSET_DYNAMIC::_cup_t)
: _offset(t._offset), _size(0)
{
#ifndef NDEBUG
set_howmany(0);
for(unsigned i=0; i<NCHNK; ++i){
_d[i] = 0;
}
#endif
merge(s, t);
assert(size()==recount());
}
/*--------------------------------------------------------------------------*/
BSDt
std::ostream& operator<<(std::ostream& o, BSET_DYNAMIC<BSDa> const& s);
/*--------------------------------------------------------------------------*/
// union.
BSDt
template<class SCT2>
BSET_DYNAMIC<BSDa>::BSET_DYNAMIC(const BSET_DYNAMIC& s,
const BSET_DYNAMIC<BSDa2>& t, BSET_DYNAMIC::_sqcup_t)
: _offset(t._offset), _size(0)
{ untested();
#ifndef NDEBUG
set_howmany(0);
for(unsigned i=0; i<NCHNK; ++i){ untested();
_d[i] = 0;
}
#endif
merge(s, t, 1);
assert(_size==recount());
}
/*--------------------------------------------------------------------------*/
BSDt
template<class SCT2>
inline void BSET_DYNAMIC<BSDa>::merge(BSET_DYNAMIC<BSDa2> const& t)
{itested();
assert(size()==recount());
assert(t.size()==t.recount());
#ifndef NDEBUG
for(auto const& v: *this){itested();
assert(contains(v));
}
#endif
BSET_DYNAMIC<BSDa> bak(*this);
int delta=_offset-t._offset;
unsigned char newhowmany=0;
if(t.size()==0)
#if 0
if(!t._howmany)
#endif
{ untested();
return;
}else if(_offset>t._offset){ itested();
unsigned delta=_offset-t._offset;
newhowmany = t.howmany();
unsigned x=t.howmany()-1;
for(; x>howmany()+unsigned(delta-1); --x) { untested();
set_size(size() + popcount(t._d[x]));
_d[x] = t._d[x];
}
// pure shift
x = howmany()+unsigned(delta-1);
if(howmany()+delta>newhowmany){ untested();
newhowmany = howmany()+delta;
}else{ untested();
}
CHUNK_T* dd=(CHUNK_T*)(_d-delta);
for(; x>=unsigned(delta) && x>=t.howmany(); --x) { untested();