-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathBitSet.hpp
1487 lines (1194 loc) · 48.6 KB
/
BitSet.hpp
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
/**
* @note This file is part of Empirical, https://github.com/devosoft/Empirical
* @copyright Copyright (C) Michigan State University, MIT Software license; see doc/LICENSE.md
* @date 2016-2018
*
* @file BitSet.hpp
* @brief A drop-in replacement for std::bitset, with additional bit magic features.
* @note Status: RELEASE
*
* @note Like std::bitset, bit zero is on the right side. Unlike std::bitset, emp::BitSet
* gives access to bit fields for easy access to different sized chucnk of bits and
* implementation new bit-magic tricks.
*/
#ifndef EMP_BIT_SET_HPP
#define EMP_BIT_SET_HPP
#include <iostream>
#include <initializer_list>
#include <cstring>
#include <bitset>
#include "../base/assert.hpp"
#include "../base/vector.hpp"
#include "../base/Ptr.hpp"
#include "../datastructs/hash_utils.hpp"
#include "../math/math.hpp"
#include "../math/Random.hpp"
#include "../math/random_utils.hpp"
#include "../polyfill/span.hpp"
#include "bitset_utils.hpp"
namespace emp {
/// SFINAE helper to determine field_t for BitSet
template <size_t NUM_BITS> struct FieldHelper {
#ifdef __EMSCRIPTEN__
///< Field sizes are 32 bits in Emscripten (max directly handled)
using field_t = uint32_t;
#else
///< Field sizes are 64 bits in native, unless NUM_BITS == 32
using field_t = uint64_t;
#endif
};
template <> struct FieldHelper<32> {
// if NUM_BITS == 32, use uint32_t
using field_t = uint32_t;
};
/// A fixed-sized (but arbitrarily large) array of bits, and optimizes operations on those bits
/// to be as fast as possible.
template <size_t NUM_BITS> class BitSet {
// make all templated instantiations friends with each other
template<size_t FRIEND_BITS> friend class BitSet;
private:
///< field size is 64 for native (except NUM_BITS == 32), 32 for emscripten
using field_t = typename FieldHelper<NUM_BITS>::field_t;
///< How many bytes are in a field?
static constexpr field_t FIELD_BYTES = sizeof(field_t);
///< How many bits are in a field?
static constexpr field_t FIELD_BITS = 8 * FIELD_BYTES;
static constexpr field_t FIELD_LOG2 = emp::Log2(FIELD_BITS);
/// Fields hold bits in groups of 32 or 64 (as uint32_t or uint64_t);
/// how many fields do we need?
static constexpr field_t NUM_FIELDS = (1 + ((NUM_BITS - 1) / FIELD_BITS));
/// End position of the stored bits in the last field; 0 if perfect fit.
static constexpr field_t LAST_BIT = NUM_BITS & (FIELD_BITS - 1);
/// How many total bytes are needed to represent these bits? (rounded up to full bytes)
static const field_t NUM_BYTES = 1 + ((NUM_BITS - 1) >> 3);
field_t bit_set[NUM_FIELDS]; ///< Fields to hold the actual bits for this BitSet.
/// BitProxy lets us use operator[] on with BitSet as an lvalue.
class BitProxy {
private:
BitSet<NUM_BITS> & bit_set; ///< BitSet object that this proxy refers to.
size_t index; ///< Position in BitSet the this proxy refers to.
public:
BitProxy(BitSet<NUM_BITS> & _set, size_t _idx) : bit_set(_set), index(_idx) {
emp_assert(_idx < bit_set.size());
}
/// Set the bit value that this proxy refers to.
BitProxy & operator=(bool b) { // lvalue handling...
bit_set.Set(index, b);
return *this;
}
/// Convert BitProxy to a regular boolean value.
operator bool() const { // rvalue handling...
return bit_set.Get(index);
}
/// Flip this bit.
BitProxy & Toggle() { bit_set.Toggle(index); return *this; }
};
friend class BitProxy;
inline static size_t FieldID(const size_t index) {
emp_assert((index >> FIELD_LOG2) < NUM_FIELDS);
return index >> FIELD_LOG2;
}
inline static size_t FieldPos(const size_t index) {
return index & (FIELD_BITS - 1);
}
inline static size_t Byte2Field(const size_t index) {
return index / FIELD_BYTES;
}
inline static size_t Byte2FieldPos(const size_t index) {
return FieldPos(index * 8);
}
inline void Copy(const field_t in_set[NUM_FIELDS]) {
std::memcpy(bit_set, in_set, sizeof(bit_set));
}
template<size_t shift>
void ShiftLeft() {
// profiled this templated, special case variant
// and did see a difference in runtime MAM
// TODO currently only implemented for NUM_FIELDS == 1
//static_assert( NUM_FIELDS == 1 );
//static_assert( LAST_BIT == 0 );
if constexpr (NUM_FIELDS != 1) {
ShiftLeft(shift);
}
else {
if constexpr (LAST_BIT != 0)
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
bit_set[0] <<= shift;
}
}
/// Helper: call SHIFT with positive number instead
void ShiftLeft(const size_t shift_size) {
if (shift_size > NUM_BITS) {
Clear();
return;
}
const int field_shift = shift_size / FIELD_BITS;
const int bit_shift = shift_size % FIELD_BITS;
const int bit_overflow = FIELD_BITS - bit_shift;
// Loop through each field, from L to R, and update it.
if (field_shift) {
for (int i = NUM_FIELDS - 1; i >= field_shift; --i) {
bit_set[i] = bit_set[i - field_shift];
}
for (int i = field_shift - 1; i >= 0; i--) bit_set[i] = 0;
}
// account for bit_shift
if (bit_shift) {
for (int i = NUM_FIELDS - 1; i > field_shift; --i) {
bit_set[i] <<= bit_shift;
bit_set[i] |= (bit_set[i-1] >> bit_overflow);
}
// Handle final field (field_shift position)
bit_set[field_shift] <<= bit_shift;
}
// Mask out any bits that have left-shifted away
if (LAST_BIT) { bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT); }
}
/// Helper for calling SHIFT with negative number
void ShiftRight(const size_t shift_size) {
if (!shift_size) return;
const field_t field_shift = shift_size / FIELD_BITS;
// only clear and return if we are field_shift-ing
// we want to be able to always shift by up to a byte
// so that Import and Export work
if (field_shift && shift_size > NUM_BITS) {
Clear();
return;
}
const field_t bit_shift = shift_size % FIELD_BITS;
const field_t bit_overflow = FIELD_BITS - bit_shift;
// account for field_shift
if (field_shift) {
for (size_t i = 0; i < (NUM_FIELDS - field_shift); ++i) {
bit_set[i] = bit_set[i + field_shift];
}
for (size_t i = NUM_FIELDS - field_shift; i < NUM_FIELDS; i++) bit_set[i] = 0;
}
// account for bit_shift
if (bit_shift) {
for (size_t i = 0; i < (NUM_FIELDS - 1 - field_shift); ++i) {
bit_set[i] >>= bit_shift;
bit_set[i] |= (bit_set[i+1] << bit_overflow);
}
bit_set[NUM_FIELDS - 1 - field_shift] >>= bit_shift;
}
}
/// Helper: call ROTATE with negative number instead
void RotateLeft(const size_t shift_size_raw) {
const field_t shift_size = shift_size_raw % NUM_BITS;
// use different approaches based on BitSet size
if constexpr (NUM_FIELDS == 1) {
// special case: for exactly one field_T, try to go low level
// adapted from https://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c
field_t & n = bit_set[0];
field_t c = shift_size;
// mask necessary to suprress shift count overflow warnings
constexpr field_t mask = MaskLow<field_t>(FIELD_LOG2);
c &= mask;
n = (n<<c) | (n>>( (-(c+FIELD_BITS-NUM_BITS))&mask ));
} else if (NUM_FIELDS < 32) {
// for small BitSets, shifting L/R and ORing is faster
emp::BitSet<NUM_BITS> dup(*this);
dup.ShiftLeft(shift_size);
ShiftRight(NUM_BITS - shift_size);
OR_SELF(dup);
} else {
// for big BitSets, manual rotating is fater
// note that we already modded shift_size by NUM_BITS
// so there's no need to mod by FIELD_SIZE here
const int field_shift = LAST_BIT ? (
(shift_size + FIELD_BITS - LAST_BIT) / FIELD_BITS
) : (
shift_size / FIELD_BITS
);
// if we field shift, we need to shift bits by (FIELD_BITS - LAST_BIT)
// more to account for the filler that gets pulled out of the middle
const int bit_shift = LAST_BIT && field_shift ? (
(shift_size + FIELD_BITS - LAST_BIT) % FIELD_BITS
) : (
shift_size % FIELD_BITS
);
const int bit_overflow = FIELD_BITS - bit_shift;
// if rotating more than field capacity, we need to rotate fields
std::rotate(
std::rbegin(bit_set),
std::rbegin(bit_set)+field_shift,
std::rend(bit_set)
);
// if necessary, shift filler bits out of the middle
if constexpr ((bool)LAST_BIT) {
const int filler_idx = (NUM_FIELDS - 1 + field_shift) % NUM_FIELDS;
for (int i = filler_idx + 1; i < (int)NUM_FIELDS; ++i) {
bit_set[i-1] |= bit_set[i] << LAST_BIT;
bit_set[i] >>= (FIELD_BITS - LAST_BIT);
}
}
// account for bit_shift
if (bit_shift) {
const field_t keystone = LAST_BIT ? (
(bit_set[NUM_FIELDS - 1] << (FIELD_BITS - LAST_BIT))
| (bit_set[NUM_FIELDS - 2] >> LAST_BIT)
) : (
bit_set[NUM_FIELDS - 1]
);
for (int i = NUM_FIELDS - 1; i > 0; --i) {
bit_set[i] <<= bit_shift;
bit_set[i] |= (bit_set[i-1] >> bit_overflow);
}
// Handle final field
bit_set[0] <<= bit_shift;
bit_set[0] |= keystone >> bit_overflow;
}
}
// Mask out filler bits
if constexpr ((bool)LAST_BIT) {
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
}
}
/// Helper for calling ROTATE with positive number
void RotateRight(const size_t shift_size_raw) {
const field_t shift_size = shift_size_raw % NUM_BITS;
// use different approaches based on BitSet size
if constexpr (NUM_FIELDS == 1) {
// special case: for exactly one field_t, try to go low level
// adapted from https://stackoverflow.com/questions/776508/best-practices-for-circular-shift-rotate-operations-in-c
field_t & n = bit_set[0];
field_t c = shift_size;
// mask necessary to suprress shift count overflow warnings
constexpr field_t mask = MaskLow<field_t>(FIELD_LOG2);
c &= mask;
n = (n>>c) | (n<<( (NUM_BITS-c)&mask ));
} else if (NUM_FIELDS < 32) {
// for small BitSets, shifting L/R and ORing is faster
emp::BitSet<NUM_BITS> dup(*this);
dup.ShiftRight(shift_size);
ShiftLeft(NUM_BITS - shift_size);
OR_SELF(dup);
} else {
// for big BitSets, manual rotating is fater
const field_t field_shift = (shift_size / FIELD_BITS) % NUM_FIELDS;
const int bit_shift = shift_size % FIELD_BITS;
const field_t bit_overflow = FIELD_BITS - bit_shift;
// if rotating more than field capacity, we need to rotate fields
std::rotate(
std::begin(bit_set),
std::begin(bit_set)+field_shift,
std::end(bit_set)
);
// if necessary, shift filler bits out of the middle
if constexpr ((bool)LAST_BIT) {
const int filler_idx = NUM_FIELDS - 1 - field_shift;
for (int i = filler_idx + 1; i < (int)NUM_FIELDS; ++i) {
bit_set[i-1] |= bit_set[i] << LAST_BIT;
bit_set[i] >>= (FIELD_BITS - LAST_BIT);
}
}
// account for bit_shift
if (bit_shift) {
const field_t keystone = LAST_BIT ? (
bit_set[0] >> (FIELD_BITS - LAST_BIT)
) : (
bit_set[0]
);
if constexpr ((bool)LAST_BIT) {
bit_set[NUM_FIELDS-1] |= bit_set[0] << LAST_BIT;
}
for (size_t i = 0; i < NUM_FIELDS - 1; ++i) {
bit_set[i] >>= bit_shift;
bit_set[i] |= (bit_set[i+1] << bit_overflow);
}
bit_set[NUM_FIELDS - 1] >>= bit_shift;
bit_set[NUM_FIELDS - 1] |= keystone << bit_overflow;
}
}
// Mask out filler bits
if constexpr ((bool)LAST_BIT) {
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
}
}
public:
/// Constructor: Assume all zeroes in set
BitSet() { Clear(); }
/// Copy constructor from another BitSet
BitSet(const BitSet & in_set) { Copy(in_set.bit_set); }
/// Constructor to generate a random BitSet (with equal prob of 0 or 1).
BitSet(Random & random) { Randomize(random); }
/// Constructor to generate a random BitSet with provided prob of 1's.
BitSet(Random & random, const double p1) { Clear(); Randomize(random, p1); }
/// Constructor to generate a BitSet from a std::bitset.
explicit BitSet(const std::bitset<NUM_BITS>& bitset) {
Clear(); // have to clear out field bits beyond NUM_BITS
for (size_t bit{}; bit < NUM_BITS; ++bit) Set( bit, bitset[bit] );
}
/// Constructor to generate a BitSet from a string.
explicit BitSet(const std::string& bitstring)
: BitSet( std::bitset<NUM_BITS>( bitstring ) )
{ emp_assert( bitstring.size() == NUM_BITS ); }
/// Constructor to fill in a bit set from a vector.
template <typename T>
BitSet(const std::initializer_list<T> l) {
// TODO: should we enforce the initializer list to be the same length as the bitset?
// emp_assert(l.size() == NUM_BITS);
// check that initializer list isn't longer than bitset
emp_assert(l.size() <= NUM_BITS);
Clear();
size_t idx = 0;
for (auto i = std::rbegin(l); i != std::rend(l); ++i) {
Set(idx, *i);
++idx;
}
}
/// Destructor.
~BitSet() = default;
/// Assignment operator.
BitSet & operator=(const BitSet<NUM_BITS> & in_set) {
Copy(in_set.bit_set);
return *this;
}
/// Set all bits randomly, with a 50% probability of being a 0 or 1.
void Randomize(Random & random) {
// Randomize all fields, then mask off bits in the last field if not complete.
random.RandFill(
reinterpret_cast<unsigned char*>(bit_set),
(NUM_BITS+7)/8
);
if constexpr (static_cast<bool>(LAST_BIT)) {
bit_set[NUM_FIELDS-1] &= MaskLow<field_t>(NUM_BITS%32);
}
}
/// Set all bits randomly, with a given probability of being a 1.
void Randomize(Random & random, const double p1) {
if (p1 == 0.5) return Randomize(random); // If 0.5 probability, generate by field!
for (size_t i = 0; i < NUM_BITS; i++) Set(i, random.P(p1));
}
/// Mutate bits, return how many mutations were performed
size_t Mutate(
Random & random,
const size_t num_muts, // @CAO: use tools/Binomial in Distribution.h with this part?
const size_t min_idx=0 // draw this from a distribution to make some
// bits more volatile than others
) {
emp_assert(min_idx <= NUM_BITS);
emp_assert(num_muts <= NUM_BITS - min_idx);
std::vector<size_t> res;
Choose(random, NUM_BITS - min_idx, num_muts, res);
for (size_t idx : res) Toggle(idx + min_idx);
return num_muts;
}
/// Assign from a BitSet of a different size.
template <size_t FROM_BITS>
BitSet & Import(
const BitSet<FROM_BITS> & from_set,
const size_t from_bit=0
) {
if constexpr (FROM_BITS == NUM_BITS) emp_assert(&from_set != this);
emp_assert(from_bit < FROM_BITS);
if (FROM_BITS - from_bit < NUM_BITS) Clear();
const size_t DEST_BYTES = (NUM_BITS + 7)/8;
const size_t FROM_BYTES = (FROM_BITS + 7)/8 - from_bit/8;
const size_t COPY_BYTES = std::min(DEST_BYTES, FROM_BYTES);
std::memcpy(
bit_set,
reinterpret_cast<const unsigned char*>(from_set.bit_set) + from_bit/8,
COPY_BYTES
);
if (from_bit%8) {
this->ShiftRight(from_bit%8);
if (FROM_BYTES > COPY_BYTES) {
reinterpret_cast<unsigned char*>(bit_set)[COPY_BYTES-1] |= (
reinterpret_cast<const unsigned char*>(
from_set.bit_set
)[from_bit/8 + COPY_BYTES]
<< (8 - from_bit%8)
);
}
}
// mask out filler bits
if constexpr (static_cast<bool>(LAST_BIT)) {
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
}
return *this;
}
/// Convert to a Bitset of a different size.
template <size_t FROM_BITS>
BitSet<FROM_BITS> Export(size_t start_bit=0) const {
BitSet<FROM_BITS> out_bits;
out_bits.Import(*this, start_bit);
return out_bits;
}
/// Test if two BitSet objects are identical.
bool operator==(const BitSet & in_set) const {
for (size_t i = 0; i < NUM_FIELDS; ++i) {
if (bit_set[i] != in_set.bit_set[i]) return false;
}
return true;
}
/// Compare two BitSet objects, based on the associated binary value.
bool operator<(const BitSet & in_set) const {
for (int i = NUM_FIELDS-1; i >= 0; --i) { // Start loop at the largest field.
if (bit_set[i] == in_set.bit_set[i]) continue; // If same, keep looking!
return (bit_set[i] < in_set.bit_set[i]); // Otherwise, do comparison
}
return false;
}
/// Compare two BitSet objects, based on the associated binary value.
bool operator<=(const BitSet & in_set) const {
for (int i = NUM_FIELDS-1; i >= 0; --i) { // Start loop at the largest field.
if (bit_set[i] == in_set.bit_set[i]) continue; // If same, keep looking!
return (bit_set[i] < in_set.bit_set[i]); // Otherwise, do comparison
}
return true;
}
/// Test if two BitSet objects are different.
bool operator!=(const BitSet & in_set) const { return !operator==(in_set); }
/// Compare two BitSet objects, based on the associated binary value.
bool operator>(const BitSet & in_set) const { return !operator<=(in_set); }
/// Compare two BitSet objects, based on the associated binary value.
bool operator>=(const BitSet & in_set) const { return !operator<(in_set); }
/// How many bits are in this BitSet?
constexpr static size_t GetSize() { return NUM_BITS; }
/// How many bytes are in this BitSet?
constexpr static size_t GetNumBytes() { return NUM_BYTES; }
/// Retrieve the bit as a specified index.
bool Get(size_t index) const {
emp_assert(index >= 0 && index < NUM_BITS);
const size_t field_id = FieldID(index);
const size_t pos_id = FieldPos(index);
return (bit_set[field_id] & (((field_t)1U) << pos_id)) != 0;
}
/// Set the bit at a specified index.
void Set(size_t index, bool value=true) {
emp_assert(index < NUM_BITS);
const size_t field_id = FieldID(index);
const size_t pos_id = FieldPos(index);
const field_t pos_mask = ((field_t)1U) << pos_id;
if (value) bit_set[field_id] |= pos_mask;
else bit_set[field_id] &= ~pos_mask;
}
/// Flip all bits in this BitSet
BitSet & Toggle() { return NOT_SELF(); }
/// Flip a single bit
BitSet & Toggle(size_t index) {
emp_assert(index >= 0 && index < NUM_BITS);
const size_t field_id = FieldID(index);
const size_t pos_id = FieldPos(index);
(bit_set[field_id] ^= (((field_t)1U) << pos_id));
return *this;
}
/// Flips all the bits in a range [start, end)
BitSet & Toggle(size_t start, size_t end) {
emp_assert(start <= end && end <= NUM_BITS);
for(size_t index = start; index < end; index++) {
Toggle(index);
}
return *this;
}
/// Get the full byte starting from the bit at a specified index.
uint8_t GetByte(size_t index) const {
emp_assert(index < NUM_BYTES);
const size_t field_id = Byte2Field(index);
const size_t pos_id = Byte2FieldPos(index);
return (bit_set[field_id] >> pos_id) & 255;
}
/// Get a read-only view into the internal array used by BitSet.
/// @return Read-only span of BitSet's bytes.
std::span<const std::byte> GetBytes() const {
return std::span<const std::byte>(
reinterpret_cast<const std::byte*>(bit_set),
NUM_BYTES
);
}
/// Set the full byte starting at the bit at the specified index.
void SetByte(size_t index, uint8_t value) {
emp_assert(index < NUM_BYTES);
const size_t field_id = Byte2Field(index);
const size_t pos_id = Byte2FieldPos(index);
const field_t val_uint = value;
bit_set[field_id] = (bit_set[field_id] & ~(((field_t)255U) << pos_id)) | (val_uint << pos_id);
}
/// Get the unsigned int; index in in 32-bit jumps
/// (i.e., this is a field ID not bit id)
uint32_t GetUInt(const size_t index) const { return GetUInt32(index); }
/// Set the unsigned int; index in in 32-bit jumps
/// (i.e., this is a field ID not bit id)
void SetUInt(const size_t index, const uint32_t value) {
SetUInt32(index, value);
}
/// Get the field_t unsigned int; index in in 32-bit jumps
/// (i.e., this is a field ID not bit id)
uint32_t GetUInt32(const size_t index) const {
emp_assert(index * 32 < NUM_BITS);
uint32_t res;
std::memcpy(
&res,
reinterpret_cast<const unsigned char*>(bit_set) + index * (32/8),
sizeof(res)
);
return res;
}
/// Set the field_t unsigned int; index in in 32-bit jumps
/// (i.e., this is a field ID not bit id)
void SetUInt32(const size_t index, const uint32_t value) {
emp_assert(index * 32 < NUM_BITS);
std::memcpy(
reinterpret_cast<unsigned char*>(bit_set) + index * (32/8),
&value,
sizeof(value)
);
// Mask out filler bits if necessary
if constexpr (static_cast<bool>(LAST_BIT)) {
// we only need to do this
// if (index * 32 == (NUM_FIELDS - 1) * FIELD_BITS)
// but just doing it always is probably faster
// check to make sure there are no leading ones in the unused bits
emp_assert((bit_set[NUM_FIELDS - 1] & ~MaskLow<field_t>(LAST_BIT)) == 0);
}
}
/// Get the field_t unsigned int; index in in 64-bit jumps
/// (i.e., this is a field ID not bit id)
uint64_t GetUInt64(const size_t index) const {
emp_assert(index * 64 < NUM_BITS);
uint64_t res = 0;
if constexpr (FIELD_BITS == 64) {
res = bit_set[index];
} else if constexpr (FIELD_BITS == 32 && (NUM_FIELDS % 2 == 0)) {
std::memcpy(
&res,
reinterpret_cast<const unsigned char*>(bit_set) + index * (64/8),
sizeof(res)
);
} else if constexpr (FIELD_BITS == 32 && NUM_FIELDS == 1) {
std::memcpy(
&res,
reinterpret_cast<const unsigned char*>(bit_set),
32/8
);
} else {
std::memcpy(
&res,
reinterpret_cast<const unsigned char*>(bit_set) + index * (64/8),
std::min(64, NUM_FIELDS * FIELD_BITS - 64 * index)/8
);
}
return res;
}
/// Set the field_t unsigned int; index in in 64-bit jumps
/// (i.e., this is a field ID not bit id)
void SetUInt64(const size_t index, const uint64_t value) {
emp_assert(index * 64 < NUM_BITS);
if constexpr (FIELD_BITS == 64) {
bit_set[index] = value;
} else if constexpr (FIELD_BITS == 32 && (NUM_FIELDS % 2 == 0)) {
std::memcpy(
reinterpret_cast<unsigned char*>(bit_set) + index * (64/8),
&value,
sizeof(value)
);
} else if constexpr (FIELD_BITS == 32 && NUM_FIELDS == 1) {
std::memcpy(
reinterpret_cast<unsigned char*>(bit_set),
&value,
32/8
);
} else {
std::memcpy(
reinterpret_cast<unsigned char*>(bit_set) + index * (64/8),
&value,
std::min(64, NUM_FIELDS * FIELD_BITS - 64 * index)/8
);
}
// Mask out filler bits if necessary
if constexpr (static_cast<bool>(LAST_BIT)) {
// we only need to do this
// if (index * 64 == (NUM_FIELDS - 1) * FIELD_BITS)
// but just doing it always is probably faster
// check to make sure there are no leading ones in the unused bits
emp_assert((bit_set[NUM_FIELDS - 1] & ~MaskLow<field_t>(LAST_BIT)) == 0);
}
}
/// Get the full uint32_t unsigned int starting from the bit at a specified index.
uint32_t GetUIntAtBit(const size_t index) { return GetUInt32AtBit(index); }
/// Get the full uint32_t unsigned int starting from the bit at a specified index.
uint32_t GetUInt32AtBit(const size_t index) {
emp_assert(index < NUM_BITS);
BitSet<32> res;
res.Import(*this, index);
return res.GetUInt32(0);
}
/// Get OUT_BITS bits starting from the bit at a specified index (max 32)
template <size_t OUT_BITS>
uint32_t GetValueAtBit(const size_t index) {
static_assert(OUT_BITS <= 32, "Requesting too many bits to fit in a UInt");
return GetUIntAtBit(index) & MaskLow<uint32_t>(OUT_BITS);
}
/// Get the unsigned numeric value represented by the BitSet as a double
double GetDouble() const {
if constexpr (NUM_BITS <= 64) {
uint64_t res{};
std::memcpy(&res, bit_set, NUM_BYTES);
return res;
} else {
double res = 0.0;
for (size_t i = 0; i < (NUM_BITS + 63) / 64; ++i) {
res += GetUInt64(i) * emp::Pow2(i * 64);
}
return res;
}
}
/// What is the maximum value this BitSet could contain, as a double?
static constexpr double MaxDouble() { return emp::Pow2(NUM_BITS) - 1.0; }
/// Return true if ANY bits in the BitSet are one, else return false.
bool Any() const {
// profiled the if constexpr else
// and did see a difference on perf reports and in runtime MAM
if constexpr (NUM_FIELDS == 1) return bit_set[0];
else {
for (auto i : bit_set) if (i) return true;
return false;
}
}
/// Return true if NO bits in the BitSet are one, else return false.
bool None() const { return !Any(); }
/// Return true if ALL bits in the BitSet are one, else return false.
bool All() const { return (~(*this)).None(); }
/// Index into a const BitSet (i.e., cannot be set this way.)
bool operator[](size_t index) const { return Get(index); }
/// Index into a BitSet, returning a proxy that will allow bit assignment to work.
BitProxy operator[](size_t index) { return BitProxy(*this, index); }
/// Set all bits to zero.
void Clear() { std::memset(bit_set, 0, sizeof(bit_set)); }
/// Set all bits to one.
void SetAll() {
std::memset(bit_set, 255, sizeof(bit_set));;
if constexpr (static_cast<bool>(LAST_BIT)) {
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
}
}
/// Overload ostream operator to return Print.
friend std::ostream& operator<<(std::ostream &out, const BitSet& bs){
bs.Print(out);
return out;
}
/// Print all bits to the provided output stream.
void Print(std::ostream & out=std::cout) const {
for (size_t i = NUM_BITS; i > 0; i--) { out << Get(i-1); }
}
/// Print all bits from smallest to largest, as if this were an array, not a bit representation.
void PrintArray(std::ostream & out=std::cout) const {
for (size_t i = 0; i < NUM_BITS; i++) out << Get(i);
}
/// Print the locations of all one bits, using the provided spacer (default is a single space)
void PrintOneIDs(std::ostream & out=std::cout, char spacer=' ') const {
for (size_t i = 0; i < NUM_BITS; i++) { if (Get(i)) out << i << spacer; }
}
/// Count 1's by looping through once for each bit equal to 1
size_t CountOnes_Sparse() const {
size_t bit_count = 0;
for (auto i : bit_set) {
while (i) {
i &= (i-1); // Peel off a single 1.
bit_count++; // And increment the counter
}
}
return bit_count;
}
/// Count 1's in semi-parallel; fastest for even 0's & 1's
size_t CountOnes_Mixed() const {
size_t bit_count = 0;
for (size_t f = 0; f < NUM_FIELDS; ++f) {
// when compiling with -O3 and -msse4.2, this is the fastest population count method.
// this is due to using a dedicated instuction that runs in 1 clock cycle.
std::bitset<FIELD_BITS> std_bs(bit_set[f]);
bit_count += std_bs.count();
}
return bit_count;
}
/// Count the number of ones in the BitSet using bit tricks for a speedup.
size_t CountOnes() const { return CountOnes_Mixed(); }
/// Return the index of the first one in the sequence; return -1 if no ones are available.
int FindBit() const {
size_t field_id = 0;
while (field_id < NUM_FIELDS && bit_set[field_id]==0) field_id++;
return (field_id < NUM_FIELDS) ? (int) (find_bit(bit_set[field_id]) + (field_id << FIELD_LOG2)) : -1;
}
/// Return index of first one in sequence (or -1 if no ones); change this position to zero.
int PopBit() {
size_t field_id = 0;
while (field_id < NUM_FIELDS && bit_set[field_id]==0) field_id++;
if (field_id == NUM_FIELDS) return -1; // Failed to find bit!
const int pos_found = (int) find_bit(bit_set[field_id]);
bit_set[field_id] &= ~(1U << pos_found);
return pos_found + (int)(field_id << FIELD_LOG2);
}
/// Return index of first one in sequence AFTER start_pos (or -1 if no ones)
int FindBit(const size_t start_pos) const {
// @CAO -- There are better ways to do this with bit tricks
// (but start_pos is tricky...)
for (size_t i = start_pos; i < NUM_BITS; i++) {
if (Get(i)) return (int) i;
}
return -1;
}
/// Return a vector indicating the posistions of all ones in the BitSet.
emp::vector<size_t> GetOnes() const {
// @CAO -- There are better ways to do this with bit tricks.
emp::vector<size_t> out_set(CountOnes());
size_t cur_pos = 0;
for (size_t i = 0; i < NUM_BITS; i++) {
if (Get(i)) out_set[cur_pos++] = i;
}
return out_set;
}
/// Finds the length of the longest segment of ones.
size_t LongestSegmentOnes() const {
size_t length = 0;
BitSet out_set(*this);
while(out_set.Any()){
BitSet temp( out_set );
// optimization currently only implemented for NUM_FIELDS == 1
if constexpr (NUM_FIELDS == 1) temp.template ShiftLeft<1>();
else temp <<= 1;
out_set.AND_SELF(temp);
++length;
}
return length;
}
/// Perform a Boolean NOT on this BitSet and return the result.
BitSet NOT() const {
BitSet out_set(*this);
out_set.NOT_SELF();
return out_set;
}
/// Perform a Boolean AND with a second BitSet and return the result.
BitSet AND(const BitSet & set2) const {
BitSet out_set(*this);
out_set.AND_SELF( set2 );
return out_set;
}
/// Perform a Boolean OR with a second BitSet and return the result.
BitSet OR(const BitSet & set2) const {
BitSet out_set(*this);
out_set.OR_SELF( set2 );
return out_set;
}
/// Perform a Boolean NAND with a second BitSet and return the result.
BitSet NAND(const BitSet & set2) const {
BitSet out_set(*this);
out_set.NAND_SELF( set2 );
return out_set;
}
/// Perform a Boolean NOR with a second BitSet and return the result.
BitSet NOR(const BitSet & set2) const {
BitSet out_set(*this);
out_set.NOR_SELF( set2 );
return out_set;
}
/// Perform a Boolean XOR with a second BitSet and return the result.
BitSet XOR(const BitSet & set2) const {
BitSet out_set(*this);
out_set.XOR_SELF( set2 );
return out_set;
}
/// Perform a Boolean EQU with a second BitSet and return the result.
BitSet EQU(const BitSet & set2) const {
BitSet out_set(*this);
out_set.EQU_SELF( set2 );
return out_set;
}
/// Perform a Boolean NOT on this BitSet, store result here, and return this object.
BitSet & NOT_SELF() {
if constexpr (NUM_FIELDS == 1) bit_set[0] = ~bit_set[0];
else for (size_t i = 0; i < NUM_FIELDS; i++) bit_set[i] = ~bit_set[i];
if constexpr (LAST_BIT > 0) {
bit_set[NUM_FIELDS - 1] &= MaskLow<field_t>(LAST_BIT);
}