-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbitset.h
2109 lines (1643 loc) · 60.3 KB
/
bitset.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) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// This file implements a bitset much like the C++ std::bitset class.
// The primary distinctions between this bitset and std::bitset are:
// - bitset is more efficient than some other std::bitset implementations,
// notably the bitset that comes with Microsoft and other 1st party platforms.
// - bitset is savvy to an environment that doesn't have exception handling,
// as is sometimes the case with console or embedded environments.
// - bitset is savvy to environments in which 'unsigned long' is not the
// most efficient integral data type. std::bitset implementations use
// unsigned long, even if it is an inefficient integer type.
// - bitset removes as much function calls as practical, in order to allow
// debug builds to run closer in speed and code footprint to release builds.
// - bitset doesn't support string functionality. We can add this if
// it is deemed useful.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_BITSET_H
#define EASTL_BITSET_H
#include <eastl/internal/config.h>
#include <eastl/algorithm.h>
EA_DISABLE_ALL_VC_WARNINGS();
#include <stddef.h>
#include <string.h>
EA_RESTORE_ALL_VC_WARNINGS();
#if EASTL_EXCEPTIONS_ENABLED
EA_DISABLE_ALL_VC_WARNINGS();
#include <stdexcept> // std::out_of_range, std::length_error.
EA_RESTORE_ALL_VC_WARNINGS();
#endif
EA_DISABLE_VC_WARNING(4127); // Conditional expression is constant
#if defined(EASTL_PRAGMA_ONCE_SUPPORTED)
#pragma once // Some compilers (e.g. VC++) benefit significantly from using this. We've measured 3-4% build speed improvements in apps as a result.
#endif
namespace eastl
{
// To consider: Enable this for backwards compatibility with any user code that might be using BitsetWordType:
// #define BitsetWordType EASTL_BITSET_WORD_TYPE_DEFAULT
/// BITSET_WORD_COUNT
///
/// Defines the number of words we use, based on the number of bits.
/// nBitCount refers to the number of bits in a bitset.
/// WordType refers to the type of integer word which stores bitet data. By default it is BitsetWordType.
///
/// Note: for nBitCount == 0, returns 1!
#if !defined(__GNUC__) || (__GNUC__ >= 3) // GCC 2.x can't handle the simpler declaration below.
#define BITSET_WORD_COUNT(nBitCount, WordType) (nBitCount == 0 ? 1 : ((nBitCount - 1) / (8 * sizeof(WordType)) + 1))
#else
#define BITSET_WORD_COUNT(nBitCount, WordType) ((nBitCount - 1) / (8 * sizeof(WordType)) + 1)
#endif
/// EASTL_DISABLE_BITSET_ARRAYBOUNDS_WARNING
/// Before GCC 4.7 the '-Warray-bounds' buggy and was very likely to issue false positives for loops that are
/// difficult to evaluate.
/// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=45978
///
#if defined(__GNUC__) && (EA_COMPILER_VERSION > 4007) && defined(EA_PLATFORM_ANDROID) // Earlier than GCC 4.7
#define EASTL_DISABLE_BITSET_ARRAYBOUNDS_WARNING 1
#else
#define EASTL_DISABLE_BITSET_ARRAYBOUNDS_WARNING 0
#endif
template <size_t N, typename WordType = EASTL_BITSET_WORD_TYPE_DEFAULT>
class bitset;
namespace detail
{
template<typename T>
struct is_word_type : std::bool_constant<!is_const_v<T> && !is_volatile_v<T> && !is_same_v<T, bool> && is_integral_v<T> && is_unsigned_v<T>> {};
template<typename T>
constexpr bool is_word_type_v = is_word_type<T>::value;
// slices the min(N, UInt) lowest significant bits from value.
template<size_t N, typename WordType, typename UInt>
eastl::enable_if_t<is_word_type_v<UInt>> from_unsigned_integral(bitset<N, WordType>& bs, UInt value)
{
constexpr size_t numWords = (N > 0) ? ((N - 1) / (CHAR_BIT * sizeof(WordType)) + 1) : 0; // BITSET_WORD_COUNT(N, WordType) but 0 for N == 0
WordType* data = bs.data();
EA_CONSTEXPR_IF (numWords > 0)
{
// copy everything from value into our word array:
constexpr size_t bytes_to_copy = eastl::minAlt(numWords * sizeof(WordType), sizeof(UInt));
memcpy(data, &value, bytes_to_copy);
// zero any remaining elements in our array:
memset(reinterpret_cast<unsigned char*>(data) + bytes_to_copy, 0, numWords * sizeof(WordType) - bytes_to_copy);
// we may have copied bits into the final element that are unusable (ie. bit positions > N).
// zero these bits out, as this is an invariant for our implementation.
EA_CONSTEXPR_IF (N % (CHAR_BIT * sizeof(WordType)) != 0)
{
constexpr WordType lastElemUsedBitsMask = (WordType(1) << (N % (CHAR_BIT * sizeof(WordType)))) - 1;
data[numWords - 1] &= lastElemUsedBitsMask;
}
}
else
{
data[0] = 0; // our bitset implementation has a single element even when N == 0.
}
}
template<typename UInt, bool bAssertOnOverflow, size_t N, typename WordType>
eastl::enable_if_t<is_word_type_v<UInt>, UInt> to_unsigned_integral(const bitset<N, WordType>& bs)
{
constexpr size_t numWords = (N > 0) ? ((N - 1) / (CHAR_BIT * sizeof(WordType)) + 1) : 0; // BITSET_WORD_COUNT(N, WordType) but 0 for N == 0
EA_CONSTEXPR_IF (numWords > 0)
{
const WordType* data = bs.data();
UInt result = 0;
size_t numWordsCopied;
EA_CONSTEXPR_IF (sizeof(UInt) < sizeof(WordType))
{
constexpr size_t bytes_to_copy = sizeof(UInt);
memcpy(&result, data, bytes_to_copy);
// check remaining uncopied bits from the first word are zero:
constexpr WordType lastElemOverflowBitsMask = static_cast<WordType>(~((WordType(1) << (CHAR_BIT * sizeof(UInt))) - 1));
if ((data[0] & lastElemOverflowBitsMask) != 0)
{
#if EASTL_EXCEPTIONS_ENABLED
throw std::overflow_error("target type cannot represent the full bitset.");
#elif EASTL_ASSERT_ENABLED
EA_CONSTEXPR_IF(bAssertOnOverflow)
EASTL_FAIL_MSG("overflow_error");
#endif
}
numWordsCopied = 1;
}
else
{
constexpr size_t bytes_to_copy = eastl::minAlt(numWords * sizeof(WordType), sizeof(UInt));
memcpy(&result, data, bytes_to_copy);
numWordsCopied = bytes_to_copy / sizeof(WordType);
}
// check any remaining uncopied words are zero (don't contain any useful information).
for (size_t wordIndex = numWordsCopied; wordIndex < numWords; ++wordIndex)
{
if (data[wordIndex] != 0)
{
#if EASTL_EXCEPTIONS_ENABLED
throw std::overflow_error("target type cannot represent the full bitset.");
#elif EASTL_ASSERT_ENABLED
EA_CONSTEXPR_IF (bAssertOnOverflow)
EASTL_FAIL_MSG("overflow_error");
#endif
}
}
return result;
}
else
{
return 0;
}
}
} // namespace detail
/// BitsetBase
///
/// This is a default implementation that works for any number of words.
///
template <size_t NW, typename WordType> // Templated on the number of words used to hold the bitset and the word type.
struct BitsetBase
{
typedef WordType word_type;
typedef BitsetBase<NW, WordType> this_type;
#if EASTL_BITSET_SIZE_T
typedef size_t size_type;
#else
typedef eastl_size_t size_type;
#endif
enum {
kBitsPerWord = (8 * sizeof(word_type)),
kBitsPerWordMask = (kBitsPerWord - 1),
kBitsPerWordShift = ((kBitsPerWord == 8) ? 3 : ((kBitsPerWord == 16) ? 4 : ((kBitsPerWord == 32) ? 5 : (((kBitsPerWord == 64) ? 6 : 7)))))
};
public:
// invariant: we keep any high bits in the last word that are unneeded set to 0
// so that our toUlong() conversion can simply copy the words into the target type.
word_type mWord[NW];
public:
void operator&=(const this_type& x);
void operator|=(const this_type& x);
void operator^=(const this_type& x);
void operator<<=(size_type n);
void operator>>=(size_type n);
void flip();
void set();
void set(size_type i, bool value);
void reset();
bool operator==(const this_type& x) const;
bool any() const;
size_type count() const;
word_type& DoGetWord(size_type i);
word_type DoGetWord(size_type i) const;
size_type DoFindFirst() const;
size_type DoFindNext(size_type last_find) const;
size_type DoFindLast() const; // Returns NW * kBitsPerWord (the bit count) if no bits are set.
size_type DoFindPrev(size_type last_find) const; // Returns NW * kBitsPerWord (the bit count) if no bits are set.
}; // class BitsetBase
/// BitsetBase<1, WordType>
///
/// This is a specialization for a bitset that fits within one word.
///
template <typename WordType>
struct BitsetBase<1, WordType>
{
typedef WordType word_type;
typedef BitsetBase<1, WordType> this_type;
#if EASTL_BITSET_SIZE_T
typedef size_t size_type;
#else
typedef eastl_size_t size_type;
#endif
enum {
kBitsPerWord = (8 * sizeof(word_type)),
kBitsPerWordMask = (kBitsPerWord - 1),
kBitsPerWordShift = ((kBitsPerWord == 8) ? 3 : ((kBitsPerWord == 16) ? 4 : ((kBitsPerWord == 32) ? 5 : (((kBitsPerWord == 64) ? 6 : 7)))))
};
public:
word_type mWord[1]; // Defined as an array of 1 so that bitset can treat this BitsetBase like others.
public:
void operator&=(const this_type& x);
void operator|=(const this_type& x);
void operator^=(const this_type& x);
void operator<<=(size_type n);
void operator>>=(size_type n);
void flip();
void set();
void set(size_type i, bool value);
void reset();
bool operator==(const this_type& x) const;
bool any() const;
size_type count() const;
word_type& DoGetWord(size_type);
word_type DoGetWord(size_type) const;
size_type DoFindFirst() const;
size_type DoFindNext(size_type last_find) const;
size_type DoFindLast() const; // Returns 1 * kBitsPerWord (the bit count) if no bits are set.
size_type DoFindPrev(size_type last_find) const; // Returns 1 * kBitsPerWord (the bit count) if no bits are set.
}; // BitsetBase<1, WordType>
/// BitsetBase<2, WordType>
///
/// This is a specialization for a bitset that fits within two words.
/// The difference here is that we avoid branching (ifs and loops).
///
template <typename WordType>
struct BitsetBase<2, WordType>
{
typedef WordType word_type;
typedef BitsetBase<2, WordType> this_type;
#if EASTL_BITSET_SIZE_T
typedef size_t size_type;
#else
typedef eastl_size_t size_type;
#endif
enum {
kBitsPerWord = (8 * sizeof(word_type)),
kBitsPerWordMask = (kBitsPerWord - 1),
kBitsPerWordShift = ((kBitsPerWord == 8) ? 3 : ((kBitsPerWord == 16) ? 4 : ((kBitsPerWord == 32) ? 5 : (((kBitsPerWord == 64) ? 6 : 7)))))
};
public:
word_type mWord[2];
public:
void operator&=(const this_type& x);
void operator|=(const this_type& x);
void operator^=(const this_type& x);
void operator<<=(size_type n);
void operator>>=(size_type n);
void flip();
void set();
void set(size_type i, bool value);
void reset();
bool operator==(const this_type& x) const;
bool any() const;
size_type count() const;
word_type& DoGetWord(size_type);
word_type DoGetWord(size_type) const;
size_type DoFindFirst() const;
size_type DoFindNext(size_type last_find) const;
size_type DoFindLast() const; // Returns 2 * kBitsPerWord (the bit count) if no bits are set.
size_type DoFindPrev(size_type last_find) const; // Returns 2 * kBitsPerWord (the bit count) if no bits are set.
}; // BitsetBase<2, WordType>
/// bitset
///
/// Implements a bitset much like the C++ std::bitset.
///
/// As of this writing we don't implement a specialization of bitset<0>,
/// as it is deemed an academic exercise that nobody would actually
/// use and it would increase code space and provide little practical
/// benefit. Note that this doesn't mean bitset<0> isn't supported;
/// it means that our version of it isn't as efficient as it would be
/// if a specialization was made for it.
///
/// - N can be any unsigned (non-zero) value, though memory usage is
/// linear with respect to N, so large values of N use large amounts of memory.
/// - WordType must be a non-cv qualified unsigned integral other than bool.
/// By default the WordType is the largest native register type that the
/// target platform supports.
///
template <size_t N, typename WordType>
class bitset : private BitsetBase<BITSET_WORD_COUNT(N, WordType), WordType>
{
public:
static_assert(detail::is_word_type_v<WordType>, "Word type must be a non-cv qualified, unsigned integral other than bool.");
typedef BitsetBase<BITSET_WORD_COUNT(N, WordType), WordType> base_type;
typedef bitset<N, WordType> this_type;
typedef WordType word_type;
typedef typename base_type::size_type size_type;
enum
{
kBitsPerWord = (8 * sizeof(word_type)),
kBitsPerWordMask = (kBitsPerWord - 1),
kBitsPerWordShift = ((kBitsPerWord == 8) ? 3 : ((kBitsPerWord == 16) ? 4 : ((kBitsPerWord == 32) ? 5 : (((kBitsPerWord == 64) ? 6 : 7))))),
kSize = N, // The number of bits the bitset holds
kWordSize = sizeof(word_type), // The size of individual words the bitset uses to hold the bits.
kWordCount = BITSET_WORD_COUNT(N, WordType) // The number of words the bitset uses to hold the bits. sizeof(bitset<N, WordType>) == kWordSize * kWordCount.
};
// internal implementation details. do not use.
using base_type::mWord;
using base_type::DoGetWord;
using base_type::DoFindFirst;
using base_type::DoFindNext;
using base_type::DoFindLast;
using base_type::DoFindPrev;
using base_type::count;
using base_type::any;
public:
/// reference
///
/// A reference is a reference to a specific bit in the bitset.
/// The C++ standard specifies that this be a nested class,
/// though it is not clear if a non-nested reference implementation
/// would be non-conforming.
///
class reference
{
protected:
friend class bitset<N, WordType>;
word_type* mpBitWord;
size_type mnBitIndex;
reference(){} // The C++ standard specifies that this is private.
public:
reference(const bitset& x, size_type i);
reference& operator=(bool value);
reference& operator=(const reference& x);
bool operator~() const;
operator bool() const // Defined inline because CodeWarrior fails to be able to compile it outside.
{ return (*mpBitWord & (static_cast<word_type>(1) << (mnBitIndex & kBitsPerWordMask))) != 0; }
reference& flip();
};
public:
friend class reference;
bitset();
#if EA_IS_ENABLED(EASTL_DEPRECATIONS_FOR_2024_SEPT)
// note: this constructor will only copy the minimum of N or unsigned long long's size least significant bits.
bitset(unsigned long long value);
#else
bitset(uint32_t value);
#endif
// We don't define copy constructor and operator= because
// the compiler-generated versions will suffice.
this_type& operator&=(const this_type& x);
this_type& operator|=(const this_type& x);
this_type& operator^=(const this_type& x);
this_type& operator<<=(size_type n);
this_type& operator>>=(size_type n);
this_type& set();
this_type& set(size_type i, bool value = true);
this_type& reset();
this_type& reset(size_type i);
this_type& flip();
this_type& flip(size_type i);
this_type operator~() const;
reference operator[](size_type i);
bool operator[](size_type i) const;
const word_type* data() const;
word_type* data();
// Deprecated: use the bitset(unsigned long long) constructor instead.
// this was a workaround for when our constructor was defined as bitset(uint32_t) and could cause a narrowing conversion.
EASTL_REMOVE_AT_2024_SEPT void from_uint32(uint32_t value);
EASTL_REMOVE_AT_2024_SEPT void from_uint64(uint64_t value);
/// to_xxx()
///
/// Not recommended: Use one of
/// as_xxx() which is a compile time error if the target type cannot represent the entire bitset, or
/// to_xxx_assert_convertible() which is the standard conformant version of this function, or
/// to_xxx_no_assert_convertible() which has the same behaviour, explicit naming
///
/// Different from the standard:
/// Does *NOT* assert that the bitset can be represented as the target integer type (has bits set outside the target type).
/// However, if exceptions are enabled, it does throw an exception if the bitset cannot be represented as the target integer type.
unsigned long toUlong() const;
uint32_t to_uint32() const;
uint64_t to_uint64() const;
/// to_xxx_assert_convertible()
///
/// Equivalent to the standard library's toUlong() / to_ullong().
/// Asserts / throws an exception if the bitset cannot be represented as the target integer type.
uint32_t to_uint32_assert_convertible() const { return detail::to_unsigned_integral<uint32_t, true>(*this); }
uint64_t to_uint64_assert_convertible() const { return detail::to_unsigned_integral<uint64_t, true>(*this); }
unsigned long toUlong_assert_convertible() const { return detail::to_unsigned_integral<unsigned long, true>(*this); }
unsigned long long to_ullong_assert_convertible() const { return detail::to_unsigned_integral<unsigned long long, true>(*this); }
/// to_xxx_no_assert_convertible()
///
/// Prefer to_xxx_assert_convertible() instead of these functions.
///
/// Different from the standard:
/// Does *NOT* assert that the bitset can be represented as the target integer type (has bits set outside the target type).
/// However, if exceptions are enabled, it does throw an exception if the bitset cannot be represented as the target integer type.
uint32_t to_uint32_no_assert_convertible() const { return detail::to_unsigned_integral<uint32_t, false>(*this); }
uint64_t to_uint64_no_assert_convertible() const { return detail::to_unsigned_integral<uint64_t, false>(*this); }
unsigned long toUlong_no_assert_convertible() const { return detail::to_unsigned_integral<unsigned long, false>(*this); }
unsigned long long to_ullong_no_assert_convertible() const { return detail::to_unsigned_integral<unsigned long long, false>(*this); }
/// as_uint<UInt>() / as_xxx()
///
/// Extension to the standard: Cast to a unsigned integral that can represent the entire bitset.
/// If the target type cannot represent the entire bitset, then issue a compile error (overload does not exist).
/// Never throws / asserts.
template<typename UInt>
eastl::enable_if_t<detail::is_word_type_v<UInt> && N <= (CHAR_BIT * sizeof(UInt)), UInt> as_uint() const noexcept { return detail::to_unsigned_integral<UInt, true>(*this); }
template<size_t NumBits = N>
eastl::enable_if_t<NumBits <= (CHAR_BIT * sizeof(uint32_t)), uint32_t> as_uint32() const noexcept { return to_uint32_assert_convertible(); }
template<size_t NumBits = N>
eastl::enable_if_t<NumBits <= (CHAR_BIT * sizeof(uint64_t)), uint64_t> as_uint64() const noexcept { return to_uint64_assert_convertible(); }
template<size_t NumBits = N>
eastl::enable_if_t<NumBits <= (CHAR_BIT * sizeof(unsigned long)), unsigned long> as_ulong() const noexcept { return toUlong_assert_convertible(); }
template<size_t NumBits = N>
eastl::enable_if_t<NumBits <= (CHAR_BIT * sizeof(unsigned long long)), unsigned long long> as_ullong() const noexcept { return to_ullong_assert_convertible(); }
//size_type count() const; // We inherit this from the base class.
size_type size() const;
bool operator==(const this_type& x) const;
#if !defined(EA_COMPILER_HAS_THREE_WAY_COMPARISON)
bool operator!=(const this_type& x) const;
#endif
bool test(size_type i) const;
//bool any() const; // We inherit this from the base class.
bool all() const;
bool none() const;
this_type operator<<(size_type n) const;
this_type operator>>(size_type n) const;
// Finds the index of the first "on" bit, returns kSize if none are set.
size_type findFirst() const;
// Finds the index of the next "on" bit after last_find, returns kSize if none are set.
size_type findNext(size_type last_find) const;
// Finds the index of the last "on" bit, returns kSize if none are set.
size_type findLast() const;
// Finds the index of the last "on" bit before last_find, returns kSize if none are set.
size_type findPrev(size_type last_find) const;
}; // bitset
/// BitsetCountBits
///
/// This is a fast trick way to count bits without branches nor memory accesses.
///
template<typename UInt64>
eastl::enable_if_t<detail::is_word_type_v<UInt64> && sizeof(UInt64) == 8, uint32_t> BitsetCountBits(UInt64 x)
{
// GCC 3.x's implementation of UINT64_C is broken and fails to deal with
// the code below correctly. So we make a workaround for it. Earlier and
// later versions of GCC don't have this bug.
#if defined(__GNUC__) && (__GNUC__ == 3)
x = x - ((x >> 1) & 0x5555555555555555ULL);
x = (x & 0x3333333333333333ULL) + ((x >> 2) & 0x3333333333333333ULL);
x = (x + (x >> 4)) & 0x0F0F0F0F0F0F0F0FULL;
return (uint32_t)((x * 0x0101010101010101ULL) >> 56);
#else
x = x - ((x >> 1) & UINT64_C(0x5555555555555555));
x = (x & UINT64_C(0x3333333333333333)) + ((x >> 2) & UINT64_C(0x3333333333333333));
x = (x + (x >> 4)) & UINT64_C(0x0F0F0F0F0F0F0F0F);
return (uint32_t)((x * UINT64_C(0x0101010101010101)) >> 56);
#endif
}
template<typename UInt32>
eastl::enable_if_t<detail::is_word_type_v<UInt32> && sizeof(UInt32) == 4, uint32_t> BitsetCountBits(UInt32 x)
{
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x + (x >> 4)) & 0x0F0F0F0F;
return (uint32_t)((x * 0x01010101) >> 24);
}
template<typename SmallUInt>
eastl::enable_if_t< detail::is_word_type_v<SmallUInt> && sizeof(SmallUInt) < 4, uint32_t> BitsetCountBits(SmallUInt x)
{
return BitsetCountBits((uint32_t)x);
}
// const static char kBitsPerUint16[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4 };
#define EASTL_BITSET_COUNT_STRING "\0\1\1\2\1\2\2\3\1\2\2\3\2\3\3\4"
template<typename UInt8>
eastl::enable_if_t<detail::is_word_type_v<UInt8> && sizeof(UInt8) == 1, uint32_t> GetFirstBit(UInt8 x)
{
if(x)
{
uint32_t n = 1;
if((x & 0x0000000F) == 0) { n += 4; x >>= 4; }
if((x & 0x00000003) == 0) { n += 2; x >>= 2; }
return (uint32_t)(n - (x & 1));
}
return 8;
}
// To do: Update this to use VC++ _BitScanForward, _BitScanForward64;
// GCC __builtin_ctz, __builtin_ctzl.
// VC++ __lzcnt16, __lzcnt, __lzcnt64 requires recent CPUs (2013+) and probably can't be used.
// http://en.wikipedia.org/wiki/Haswell_%28microarchitecture%29#New_features
template<typename UInt16>
eastl::enable_if_t<detail::is_word_type_v<UInt16> && sizeof(UInt16) == 2, uint32_t> GetFirstBit(UInt16 x)
{
if(x)
{
uint32_t n = 1;
if((x & 0x000000FF) == 0) { n += 8; x >>= 8; }
if((x & 0x0000000F) == 0) { n += 4; x >>= 4; }
if((x & 0x00000003) == 0) { n += 2; x >>= 2; }
return (uint32_t)(n - (x & 1));
}
return 16;
}
template<typename UInt32>
eastl::enable_if_t<detail::is_word_type_v<UInt32> && sizeof(UInt32) == 4, uint32_t> GetFirstBit(UInt32 x)
{
#if defined(EA_COMPILER_MSVC) && (defined(EA_PROCESSOR_X86) || defined(EA_PROCESSOR_X86_64))
// This has been benchmarked as significantly faster than the generic code below.
unsigned char isNonZero;
unsigned long index;
isNonZero = _BitScanForward(&index, x);
return isNonZero ? (int)index : 32;
#elif (defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)) && !defined(EA_COMPILER_EDG)
if (x)
return __builtin_ctz(x);
return 32;
#else
if(x)
{
uint32_t n = 1;
if((x & 0x0000FFFF) == 0) { n += 16; x >>= 16; }
if((x & 0x000000FF) == 0) { n += 8; x >>= 8; }
if((x & 0x0000000F) == 0) { n += 4; x >>= 4; }
if((x & 0x00000003) == 0) { n += 2; x >>= 2; }
return (n - (x & 1));
}
return 32;
#endif
}
template<typename UInt64>
eastl::enable_if_t<detail::is_word_type_v<UInt64> && sizeof(UInt64) == 8, uint32_t> GetFirstBit(UInt64 x)
{
#if defined(EA_COMPILER_MSVC) && defined(EA_PROCESSOR_X86_64)
// This has been benchmarked as significantly faster than the generic code below.
unsigned char isNonZero;
unsigned long index;
isNonZero = _BitScanForward64(&index, x);
return isNonZero ? (int)index : 64;
#elif (defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)) && !defined(EA_COMPILER_EDG)
if (x)
return __builtin_ctzll(x);
return 64;
#else
if(x)
{
uint32_t n = 1;
if((x & 0xFFFFFFFF) == 0) { n += 32; x >>= 32; }
if((x & 0x0000FFFF) == 0) { n += 16; x >>= 16; }
if((x & 0x000000FF) == 0) { n += 8; x >>= 8; }
if((x & 0x0000000F) == 0) { n += 4; x >>= 4; }
if((x & 0x00000003) == 0) { n += 2; x >>= 2; }
return (n - ((uint32_t)x & 1));
}
return 64;
#endif
}
#if EASTL_INT128_SUPPORTED
inline uint32_t GetFirstBit(eastl_uint128_t x)
{
if(x)
{
uint32_t n = 1;
if((x & UINT64_C(0xFFFFFFFFFFFFFFFF)) == 0) { n += 64; x >>= 64; }
if((x & 0xFFFFFFFF) == 0) { n += 32; x >>= 32; }
if((x & 0x0000FFFF) == 0) { n += 16; x >>= 16; }
if((x & 0x000000FF) == 0) { n += 8; x >>= 8; }
if((x & 0x0000000F) == 0) { n += 4; x >>= 4; }
if((x & 0x00000003) == 0) { n += 2; x >>= 2; }
return (n - ((uint32_t)x & 1));
}
return 128;
}
#endif
template<typename UInt8>
eastl::enable_if_t<detail::is_word_type_v<UInt8> && sizeof(UInt8) == 1, uint32_t> GetLastBit(UInt8 x)
{
if(x)
{
uint32_t n = 0;
if(x & 0xFFF0) { n += 4; x >>= 4; }
if(x & 0xFFFC) { n += 2; x >>= 2; }
if(x & 0xFFFE) { n += 1; }
return n;
}
return 8;
}
template<typename UInt16>
eastl::enable_if_t<detail::is_word_type_v<UInt16> && sizeof(UInt16) == 2, uint32_t> GetLastBit(UInt16 x)
{
if(x)
{
uint32_t n = 0;
if(x & 0xFF00) { n += 8; x >>= 8; }
if(x & 0xFFF0) { n += 4; x >>= 4; }
if(x & 0xFFFC) { n += 2; x >>= 2; }
if(x & 0xFFFE) { n += 1; }
return n;
}
return 16;
}
template<typename UInt32>
eastl::enable_if_t<detail::is_word_type_v<UInt32> && sizeof(UInt32) == 4, uint32_t> GetLastBit(UInt32 x)
{
#if defined(EA_COMPILER_MSVC) && (defined(EA_PROCESSOR_X86) || defined(EA_PROCESSOR_X86_64))
// This has been benchmarked as significantly faster than the generic code below.
unsigned char isNonZero;
unsigned long index;
isNonZero = _BitScanReverse(&index, x);
return isNonZero ? (int)index : 32;
#elif (defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)) && !defined(EA_COMPILER_EDG)
if (x)
return 31 - __builtin_clz(x);
return 32;
#else
if(x)
{
uint32_t n = 0;
if(x & 0xFFFF0000) { n += 16; x >>= 16; }
if(x & 0xFFFFFF00) { n += 8; x >>= 8; }
if(x & 0xFFFFFFF0) { n += 4; x >>= 4; }
if(x & 0xFFFFFFFC) { n += 2; x >>= 2; }
if(x & 0xFFFFFFFE) { n += 1; }
return n;
}
return 32;
#endif
}
template<typename UInt64>
eastl::enable_if_t<detail::is_word_type_v<UInt64> && sizeof(UInt64) == 8, uint32_t> GetLastBit(UInt64 x)
{
#if defined(EA_COMPILER_MSVC) && defined(EA_PROCESSOR_X86_64)
// This has been benchmarked as significantly faster than the generic code below.
unsigned char isNonZero;
unsigned long index;
isNonZero = _BitScanReverse64(&index, x);
return isNonZero ? (int)index : 64;
#elif (defined(EA_COMPILER_GNUC) || defined(EA_COMPILER_CLANG)) && !defined(EA_COMPILER_EDG)
if (x)
return 63 - __builtin_clzll(x);
return 64;
#else
if(x)
{
uint32_t n = 0;
if(x & UINT64_C(0xFFFFFFFF00000000)) { n += 32; x >>= 32; }
if(x & 0xFFFF0000) { n += 16; x >>= 16; }
if(x & 0xFFFFFF00) { n += 8; x >>= 8; }
if(x & 0xFFFFFFF0) { n += 4; x >>= 4; }
if(x & 0xFFFFFFFC) { n += 2; x >>= 2; }
if(x & 0xFFFFFFFE) { n += 1; }
return n;
}
return 64;
#endif
}
#if EASTL_INT128_SUPPORTED
inline uint32_t GetLastBit(eastl_uint128_t x)
{
if(x)
{
uint32_t n = 0;
eastl_uint128_t mask(UINT64_C(0xFFFFFFFFFFFFFFFF)); // There doesn't seem to exist compiler support for INT128_C() by any compiler. EAStdC's int128_t supports it though.
mask <<= 64;
if(x & mask) { n += 64; x >>= 64; }
if(x & UINT64_C(0xFFFFFFFF00000000)) { n += 32; x >>= 32; }
if(x & UINT64_C(0x00000000FFFF0000)) { n += 16; x >>= 16; }
if(x & UINT64_C(0x00000000FFFFFF00)) { n += 8; x >>= 8; }
if(x & UINT64_C(0x00000000FFFFFFF0)) { n += 4; x >>= 4; }
if(x & UINT64_C(0x00000000FFFFFFFC)) { n += 2; x >>= 2; }
if(x & UINT64_C(0x00000000FFFFFFFE)) { n += 1; }
return n;
}
return 128;
}
#endif
///////////////////////////////////////////////////////////////////////////
// BitsetBase
//
// We tried two forms of array access here:
// for(word_type *pWord(mWord), *pWordEnd(mWord + NW); pWord < pWordEnd; ++pWord)
// *pWord = ...
// and
// for(size_t i = 0; i < NW; i++)
// mWord[i] = ...
//
// For our tests (~NW < 16), the latter (using []) access resulted in faster code.
///////////////////////////////////////////////////////////////////////////
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::operator&=(const this_type& x)
{
for(size_t i = 0; i < NW; i++)
mWord[i] &= x.mWord[i];
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::operator|=(const this_type& x)
{
for(size_t i = 0; i < NW; i++)
mWord[i] |= x.mWord[i];
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::operator^=(const this_type& x)
{
for(size_t i = 0; i < NW; i++)
mWord[i] ^= x.mWord[i];
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::operator<<=(size_type n)
{
const size_type nWordShift = (size_type)(n >> kBitsPerWordShift);
if(nWordShift)
{
for(int i = (int)(NW - 1); i >= 0; --i)
mWord[i] = (nWordShift <= (size_type)i) ? mWord[i - nWordShift] : (word_type)0;
}
if(n &= kBitsPerWordMask)
{
for(size_t i = (NW - 1); i > 0; --i)
mWord[i] = (word_type)((mWord[i] << n) | (mWord[i - 1] >> (kBitsPerWord - n)));
mWord[0] <<= n;
}
// We let the parent class turn off any upper bits.
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::operator>>=(size_type n)
{
const size_type nWordShift = (size_type)(n >> kBitsPerWordShift);
if(nWordShift)
{
for(size_t i = 0; i < NW; ++i)
mWord[i] = ((nWordShift < (NW - i)) ? mWord[i + nWordShift] : (word_type)0);
}
if(n &= kBitsPerWordMask)
{
for(size_t i = 0; i < (NW - 1); ++i)
mWord[i] = (word_type)((mWord[i] >> n) | (mWord[i + 1] << (kBitsPerWord - n)));
mWord[NW - 1] >>= n;
}
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::flip()
{
for(size_t i = 0; i < NW; i++)
mWord[i] = ~mWord[i];
// We let the parent class turn off any upper bits.
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::set()
{
for(size_t i = 0; i < NW; i++)
mWord[i] = static_cast<word_type>(~static_cast<word_type>(0));
// We let the parent class turn off any upper bits.
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::set(size_type i, bool value)
{
if(value)
mWord[i >> kBitsPerWordShift] |= (static_cast<word_type>(1) << (i & kBitsPerWordMask));
else
mWord[i >> kBitsPerWordShift] &= ~(static_cast<word_type>(1) << (i & kBitsPerWordMask));
}
template <size_t NW, typename WordType>
inline void BitsetBase<NW, WordType>::reset()
{
if(NW > 16) // This is a constant expression and should be optimized away.
{
// This will be fastest if compiler intrinsic function optimizations are enabled.
memset(mWord, 0, sizeof(mWord));
}
else
{
for(size_t i = 0; i < NW; i++)
mWord[i] = 0;
}
}
template <size_t NW, typename WordType>
inline bool BitsetBase<NW, WordType>::operator==(const this_type& x) const
{
for(size_t i = 0; i < NW; i++)
{
if(mWord[i] != x.mWord[i])
return false;
}
return true;
}
template <size_t NW, typename WordType>
inline bool BitsetBase<NW, WordType>::any() const
{
for(size_t i = 0; i < NW; i++)
{
if(mWord[i])
return true;