-
Notifications
You must be signed in to change notification settings - Fork 7
/
bitvector.h
1472 lines (1121 loc) · 44.6 KB
/
bitvector.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.
/////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Implements a bit vector, which is essentially a vector of bool but which
// uses bits instead of bytes. It is thus similar to the original std::vector<bool>.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Note: This code is not yet complete: it isn't tested and doesn't yet
// support containers other than vector.
///////////////////////////////////////////////////////////////////////////////
#ifndef EASTL_BITVECTOR_H
#define EASTL_BITVECTOR_H
#include <eastl/internal/config.h>
#include <eastl/vector.h>
#include <eastl/algorithm.h>
#include <eastl/bitset.h>
#if EASTL_EXCEPTIONS_ENABLED
#include <stdexcept>
#endif
EA_DISABLE_VC_WARNING(4480); // nonstandard extension used: specifying underlying type for enum
#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
{
/// EASTL_BITVECTOR_DEFAULT_NAME
///
/// Defines a default container name in the absence of a user-provided name.
///
#ifndef EASTL_BITVECTOR_DEFAULT_NAME
#define EASTL_BITVECTOR_DEFAULT_NAME EASTL_DEFAULT_NAME_PREFIX " bitvector" // Unless the user overrides something, this is "EASTL bitvector".
#endif
/// EASTL_BITVECTOR_DEFAULT_ALLOCATOR
///
#ifndef EASTL_BITVECTOR_DEFAULT_ALLOCATOR
#define EASTL_BITVECTOR_DEFAULT_ALLOCATOR allocator_type(EASTL_BITVECTOR_DEFAULT_NAME)
#endif
/// BitvectorWordType
/// Defines the integral data type used by bitvector.
typedef EASTL_BITSET_WORD_TYPE_DEFAULT BitvectorWordType;
template <typename Element>
class bitvector_const_iterator;
template <typename Element>
class bitvector_reference
{
public:
typedef eastl_size_t size_type;
bitvector_reference(Element* ptr, eastl_size_t i);
bitvector_reference(const bitvector_reference& other);
bitvector_reference& operator=(bool value);
bitvector_reference& operator=(const bitvector_reference& rhs);
operator bool() const // Defined here because some compilers fail otherwise.
{ return (*mpBitWord & (Element(1) << mnBitIndex)) != 0; }
protected:
friend class bitvector_const_iterator<Element>;
Element* mpBitWord;
size_type mnBitIndex;
bitvector_reference() {}
void CopyFrom(const bitvector_reference& rhs);
};
template <typename Element>
class bitvector_const_iterator
{
public:
typedef EASTL_ITC_NS::random_access_iterator_tag iterator_category;
typedef bitvector_const_iterator<Element> this_type;
typedef bool value_type;
typedef bitvector_reference<Element> reference_type;
typedef ptrdiff_t difference_type;
typedef Element element_type;
typedef element_type* pointer; // This is wrong. It needs to be someting that acts as a pointer to a bit.
typedef element_type& reference; // This is not right. It needs to be someting that acts as a pointer to a bit.
typedef eastl_size_t size_type;
protected:
reference_type mReference;
enum
{
kBitCount = (8 * sizeof(Element))
};
public:
bool operator*() const;
bool operator[](difference_type n) const;
bitvector_const_iterator();
bitvector_const_iterator(const element_type* p, eastl_size_t i);
bitvector_const_iterator(const reference_type& referenceType);
bitvector_const_iterator(const bitvector_const_iterator& other);
bitvector_const_iterator& operator++();
bitvector_const_iterator operator++(int);
bitvector_const_iterator& operator--();
bitvector_const_iterator operator--(int);
bitvector_const_iterator& operator+=(difference_type dist);
bitvector_const_iterator& operator-=(difference_type dist);
bitvector_const_iterator operator+ (difference_type dist) const;
bitvector_const_iterator operator- (difference_type dist) const;
difference_type operator-(const this_type& rhs) const;
bitvector_const_iterator& operator= (const this_type& rhs);
bool operator==(const this_type& rhs) const;
bool operator!=(const this_type& rhs) const;
bool operator< (const this_type& rhs) const;
bool operator<=(const this_type& rhs) const;
bool operator> (const this_type& rhs) const;
bool operator>=(const this_type& rhs) const;
int validate(const element_type* pStart, const element_type* pEnd, eastl_size_t nExtraBits) const;
protected:
template <typename, typename, typename>
friend class bitvector;
reference_type& get_reference_type() { return mReference; }
};
template <typename Element>
class bitvector_iterator : public bitvector_const_iterator<Element>
{
public:
typedef EASTL_ITC_NS::random_access_iterator_tag iterator_category;
typedef bitvector_iterator this_type;
typedef bitvector_const_iterator<Element> base_type;
typedef bool value_type;
typedef bitvector_reference<Element> reference_type;
typedef ptrdiff_t difference_type;
typedef Element element_type;
typedef element_type* pointer; // This is wrong. It needs to be someting that acts as a pointer to a bit.
typedef element_type& reference; // This is not right. It needs to be someting that acts as a pointer to a bit.
public:
reference_type operator*() const;
reference_type operator[](difference_type n) const;
bitvector_iterator();
bitvector_iterator(element_type* p, eastl_size_t i);
bitvector_iterator(reference_type& referenceType);
bitvector_iterator& operator++() { base_type::operator++(); return *this; }
bitvector_iterator& operator--() { base_type::operator--(); return *this; }
bitvector_iterator operator++(int);
bitvector_iterator operator--(int);
bitvector_iterator& operator+=(difference_type dist) { base_type::operator+=(dist); return *this; }
bitvector_iterator& operator-=(difference_type dist) { base_type::operator-=(dist); return *this; }
bitvector_iterator operator+ (difference_type dist) const;
bitvector_iterator operator- (difference_type dist) const;
// We need this here because we are overloading operator-, so for some reason the
// other overload of the function can't be found unless it's explicitly specified.
difference_type operator-(const base_type& rhs) const { return base_type::operator-(rhs); }
};
/// bitvector
///
/// Implements an array of bits treated as boolean values.
/// bitvector is similar to vector<bool> but uses bits instead of bytes and
/// allows the user to use other containers such as deque instead of vector.
/// bitvector is different from bitset in that bitset is less flexible but
/// uses less memory and has higher performance.
///
/// To consider: Rename the Element template parameter to WordType, for
/// consistency with bitset.
///
template <typename Allocator = EASTLAllocatorType,
typename Element = BitvectorWordType,
typename Container = eastl::vector<Element, Allocator> >
class bitvector
{
public:
typedef bitvector<Allocator, Element> this_type;
typedef bool value_type;
typedef bitvector_reference<Element> reference;
typedef bool const_reference;
typedef bitvector_iterator<Element> iterator;
typedef bitvector_const_iterator<Element> const_iterator;
typedef eastl::reverse_iterator<iterator> reverse_iterator;
typedef eastl::reverse_iterator<const_iterator> const_reverse_iterator;
typedef Allocator allocator_type;
typedef Element element_type;
typedef Container container_type;
typedef eastl_size_t size_type;
typedef ptrdiff_t difference_type;
#if defined(_MSC_VER) && (_MSC_VER >= 1400) && (_MSC_VER <= 1600) && !EASTL_STD_CPP_ONLY // _MSC_VER of 1400 means VS2005, 1600 means VS2010. VS2012 generates errors with usage of enum:size_type.
enum : size_type { // Use Microsoft enum language extension, allowing for smaller debug symbols than using a static const. Users have been affected by this.
npos = container_type::npos,
kMaxSize = container_type::kMaxSize
};
#else
static const size_type npos = container_type::npos; /// 'npos' means non-valid position or simply non-position.
static const size_type kMaxSize = container_type::kMaxSize; /// -1 is reserved for 'npos'. It also happens to be slightly beneficial that kMaxSize is a value less than -1, as it helps us deal with potential integer wraparound issues.
#endif
enum
{
kBitCount = 8 * sizeof(Element)
};
protected:
container_type mContainer;
size_type mFreeBitCount; // Unused bits in the last word of mContainer.
public:
bitvector();
explicit bitvector(const allocator_type& allocator);
explicit bitvector(size_type n, const allocator_type& allocator = EASTL_BITVECTOR_DEFAULT_ALLOCATOR);
bitvector(size_type n, value_type value, const allocator_type& allocator = EASTL_BITVECTOR_DEFAULT_ALLOCATOR);
template <typename InputIterator>
bitvector(InputIterator first, InputIterator last);
void swap(this_type& x);
template <typename InputIterator>
void assign(InputIterator first, InputIterator last);
iterator begin() EASTL_NOEXCEPT;
const_iterator begin() const EASTL_NOEXCEPT;
const_iterator cbegin() const EASTL_NOEXCEPT;
iterator end() EASTL_NOEXCEPT;
const_iterator end() const EASTL_NOEXCEPT;
const_iterator cend() const EASTL_NOEXCEPT;
reverse_iterator rbegin() EASTL_NOEXCEPT;
const_reverse_iterator rbegin() const EASTL_NOEXCEPT;
const_reverse_iterator crbegin() const EASTL_NOEXCEPT;
reverse_iterator rend() EASTL_NOEXCEPT;
const_reverse_iterator rend() const EASTL_NOEXCEPT;
const_reverse_iterator crend() const EASTL_NOEXCEPT;
bool empty() const EASTL_NOEXCEPT;
size_type size() const EASTL_NOEXCEPT;
size_type capacity() const EASTL_NOEXCEPT;
void resize(size_type n, value_type value);
void resize(size_type n);
void reserve(size_type n);
void setCapacity(size_type n = npos); // Revises the capacity to the user-specified value. Resizes the container to match the capacity if the requested capacity n is less than the current size. If n == npos then the capacity is reallocated (if necessary) such that capacity == size.
void pushBack();
void pushBack(value_type value);
void popBack();
reference front();
const_reference front() const;
reference back();
const_reference back() const;
bool test(size_type n, bool defaultValue) const; // Returns true if the bit index is < size() and set. Returns defaultValue if the bit is >= size().
void set(size_type n, bool value); // Resizes the container to accomodate n if necessary.
reference at(size_type n); // throws an out_of_range exception if n is invalid.
const_reference at(size_type n) const;
reference operator[](size_type n); // behavior is undefined if n is invalid.
const_reference operator[](size_type n) const;
/*
Work in progress:
template <bool value = true> iterator findFirst(); // Finds the lowest "on" bit.
template <bool value = true> iterator findNext(const_iterator it); // Finds the next lowest "on" bit after it.
template <bool value = true> iterator findLast(); // Finds the index of the last "on" bit, returns size if none are set.
template <bool value = true> iterator findPrev(const_iterator it); // Finds the index of the last "on" bit before last_find, returns size if none are set.
template <bool value = true> const_iterator findFirst() const; // Finds the lowest "on" bit.
template <bool value = true> const_iterator findNext(const_iterator it) const; // Finds the next lowest "on" bit after it.
template <bool value = true> const_iterator findLast() const; // Finds the index of the last "on" bit, returns size if none are set.
template <bool value = true> const_iterator findPrev(const_iterator it) const; // Finds the index of the last "on" bit before last_find, returns size if none are set.
*/
element_type* data() EASTL_NOEXCEPT;
const element_type* data() const EASTL_NOEXCEPT;
iterator insert(const_iterator position, value_type value);
void insert(const_iterator position, size_type n, value_type value);
// template <typename InputIterator> Not yet implemented. See below for disabled definition.
// void insert(const_iterator position, InputIterator first, InputIterator last);
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
reverse_iterator erase(const_reverse_iterator position);
reverse_iterator erase(const_reverse_iterator first, const_reverse_iterator last);
void clear();
void reset_lose_memory(); // This is a unilateral reset to an initially empty state. No destructors are called, no deallocation occurs.
container_type& get_container();
const container_type& get_container() const;
bool validate() const;
int validateIterator(const_iterator i) const;
};
///////////////////////////////////////////////////////////////////////
// bitvector_reference
///////////////////////////////////////////////////////////////////////
template <typename Element>
bitvector_reference<Element>::bitvector_reference(Element* p, eastl_size_t i)
: mpBitWord(p),
mnBitIndex(i)
{
}
template <typename Element>
bitvector_reference<Element>::bitvector_reference(const bitvector_reference& other)
: mpBitWord(other.mpBitWord),
mnBitIndex(other.mnBitIndex)
{
}
template <typename Element>
bitvector_reference<Element>&
bitvector_reference<Element>::operator=(bool value)
{
const Element mask = (Element)(Element(1) << mnBitIndex);
if(value)
*mpBitWord |= mask;
else
*mpBitWord &= ~mask;
return *this;
}
template <typename Element>
bitvector_reference<Element>&
bitvector_reference<Element>::operator=(const bitvector_reference& rhs)
{
return (*this = (bool)rhs);
}
template <typename Element>
void bitvector_reference<Element>::CopyFrom(const bitvector_reference& rhs)
{
mpBitWord = rhs.mpBitWord;
mnBitIndex = rhs.mnBitIndex;
}
///////////////////////////////////////////////////////////////////////
// bitvector_const_iterator
///////////////////////////////////////////////////////////////////////
template <typename Element>
bitvector_const_iterator<Element>::bitvector_const_iterator()
: mReference(0, 0)
{
}
template <typename Element>
bitvector_const_iterator<Element>::bitvector_const_iterator(const Element* p, eastl_size_t i)
: mReference(const_cast<Element*>(p), i) // const_cast is safe here because we never let mReference leak and we don't modify it.
{
}
template <typename Element>
bitvector_const_iterator<Element>::bitvector_const_iterator(const reference_type& reference)
: mReference(reference)
{
}
template <typename Element>
bitvector_const_iterator<Element>::bitvector_const_iterator(const bitvector_const_iterator& other)
: mReference(other.mReference)
{
}
template <typename Element>
bitvector_const_iterator<Element>&
bitvector_const_iterator<Element>::operator++()
{
++mReference.mnBitIndex;
if(mReference.mnBitIndex == kBitCount)
{
++mReference.mpBitWord;
mReference.mnBitIndex = 0;
}
return *this;
}
template <typename Element>
bitvector_const_iterator<Element>&
bitvector_const_iterator<Element>::operator--()
{
if(mReference.mnBitIndex == 0)
{
--mReference.mpBitWord;
mReference.mnBitIndex = kBitCount;
}
--mReference.mnBitIndex;
return *this;
}
template <typename Element>
bitvector_const_iterator<Element>
bitvector_const_iterator<Element>::operator++(int)
{
bitvector_const_iterator copy(*this);
++*this;
return copy;
}
template <typename Element>
bitvector_const_iterator<Element>
bitvector_const_iterator<Element>::operator--(int)
{
bitvector_const_iterator copy(*this);
--*this;
return copy;
}
template <typename Element>
bitvector_const_iterator<Element>&
bitvector_const_iterator<Element>::operator+=(difference_type n)
{
n += mReference.mnBitIndex;
if(n >= difference_type(0))
{
mReference.mpBitWord += n / kBitCount;
mReference.mnBitIndex = (size_type)(n % kBitCount);
}
else
{
// backwards is tricky
// figure out how many full words backwards we need to move
// n = [-1..-32] => 1
// n = [-33..-64] => 2
const size_type backwards = (size_type)(-n + kBitCount - 1);
mReference.mpBitWord -= backwards / kBitCount;
// -1 => 31; backwards = 32; 31 - (backwards % 32) = 31
// -2 => 30; backwards = 33; 31 - (backwards % 32) = 30
// -3 => 29; backwards = 34
// ..
// -32 => 0; backwards = 63; 31 - (backwards % 32) = 0
// -33 => 31; backwards = 64; 31 - (backwards % 32) = 31
mReference.mnBitIndex = (kBitCount - 1) - (backwards % kBitCount);
}
return *this;
}
template <typename Element>
bitvector_const_iterator<Element>&
bitvector_const_iterator<Element>::operator-=(difference_type n)
{
return (*this += -n);
}
template <typename Element>
bitvector_const_iterator<Element>
bitvector_const_iterator<Element>::operator+(difference_type n) const
{
bitvector_const_iterator copy(*this);
copy += n;
return copy;
}
template <typename Element>
bitvector_const_iterator<Element>
bitvector_const_iterator<Element>::operator-(difference_type n) const
{
bitvector_const_iterator copy(*this);
copy -= n;
return copy;
}
template <typename Element>
typename bitvector_const_iterator<Element>::difference_type
bitvector_const_iterator<Element>::operator-(const this_type& rhs) const
{
return ((mReference.mpBitWord - rhs.mReference.mpBitWord) * kBitCount) + mReference.mnBitIndex - rhs.mReference.mnBitIndex;
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator==(const this_type& rhs) const
{
return (mReference.mpBitWord == rhs.mReference.mpBitWord) && (mReference.mnBitIndex == rhs.mReference.mnBitIndex);
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator!=(const this_type& rhs) const
{
return !(*this == rhs);
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator<(const this_type& rhs) const
{
return (mReference.mpBitWord < rhs.mReference.mpBitWord) ||
((mReference.mpBitWord == rhs.mReference.mpBitWord) && (mReference.mnBitIndex < rhs.mReference.mnBitIndex));
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator<=(const this_type& rhs) const
{
return (mReference.mpBitWord < rhs.mReference.mpBitWord) ||
((mReference.mpBitWord == rhs.mReference.mpBitWord) && (mReference.mnBitIndex <= rhs.mReference.mnBitIndex));
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator>(const this_type& rhs) const
{
return !(*this <= rhs);
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator>=(const this_type& rhs) const
{
return !(*this < rhs);
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator*() const
{
return mReference;
}
template <typename Element>
bool bitvector_const_iterator<Element>::operator[](difference_type n) const
{
return *(*this + n);
}
template <typename Element>
bitvector_const_iterator<Element>& bitvector_const_iterator<Element>::operator= (const this_type& rhs)
{
mReference.CopyFrom(rhs.mReference);
return *this;
}
template <typename Element>
int bitvector_const_iterator<Element>::validate(const Element* pStart, const Element* pEnd, eastl_size_t nExtraBits) const
{
const Element* const pCurrent = mReference.mpBitWord;
if(pCurrent >= pStart)
{
if(nExtraBits == 0)
{
if(pCurrent == pEnd && mReference)
return eastl::isf_valid | eastl::isf_current;
else if(pCurrent < pEnd)
return eastl::isf_valid | eastl::isf_current | eastl::isf_can_dereference;
}
else if(pCurrent == (pEnd - 1))
{
const size_type bit = mReference.mnBitIndex;
const size_type lastbit = kBitCount - nExtraBits;
if(bit == lastbit)
return eastl::isf_valid | eastl::isf_current;
else if(bit < lastbit)
return eastl::isf_valid | eastl::isf_current | eastl::isf_can_dereference;
}
else if(pCurrent < pEnd)
{
return eastl::isf_valid | eastl::isf_current | eastl::isf_can_dereference;
}
}
return eastl::isf_none;
}
///////////////////////////////////////////////////////////////////////
// bitvector_iterator
///////////////////////////////////////////////////////////////////////
template <typename Element>
bitvector_iterator<Element>::bitvector_iterator()
: base_type()
{
}
template <typename Element>
bitvector_iterator<Element>::bitvector_iterator(Element* p, eastl_size_t i)
: base_type(p, i)
{
}
template <typename Element>
bitvector_iterator<Element>::bitvector_iterator(reference_type& reference)
: base_type(reference)
{
}
template <typename Element>
typename bitvector_iterator<Element>::reference_type
bitvector_iterator<Element>::operator*() const
{
return base_type::mReference;
}
template <typename Element>
typename bitvector_iterator<Element>::reference_type
bitvector_iterator<Element>::operator[](difference_type n) const
{
return *(*this + n);
}
template <typename Element>
void MoveBits(bitvector_iterator<Element> start,
bitvector_iterator<Element> end,
bitvector_iterator<Element> dest)
{
// Slow implemenation; could optimize by moving a word at a time.
if(dest <= start)
{
while(start != end)
{
*dest = *start;
++dest;
++start;
}
}
else
{
// Need to move backwards
dest += (end - start);
while(start != end)
{
--dest;
--end;
*dest = *end;
}
}
}
template <typename Element>
bitvector_iterator<Element>
bitvector_iterator<Element>::operator++(int)
{
bitvector_iterator copy(*this);
++*this;
return copy;
}
template <typename Element>
bitvector_iterator<Element>
bitvector_iterator<Element>::operator--(int)
{
bitvector_iterator copy(*this);
--*this;
return copy;
}
template <typename Element>
bitvector_iterator<Element>
bitvector_iterator<Element>::operator+(difference_type n) const
{
bitvector_iterator copy(*this);
copy += n;
return copy;
}
template <typename Element>
bitvector_iterator<Element>
bitvector_iterator<Element>::operator-(difference_type n) const
{
bitvector_iterator copy(*this);
copy -= n;
return copy;
}
///////////////////////////////////////////////////////////////////////
// bitvector
///////////////////////////////////////////////////////////////////////
template <typename Allocator, typename Element, typename Container>
template <typename InputIterator>
void bitvector<Allocator, Element, Container>::assign(InputIterator first, InputIterator last)
{
// To consider: We can maybe specialize this on bitvector_iterator to do a fast bitwise copy.
// We can also specialize for random access iterators to figure out the size & reserve first.
clear();
while(first != last)
{
pushBack(*first);
++first;
}
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::iterator
bitvector<Allocator, Element, Container>::begin() EASTL_NOEXCEPT
{
return iterator(mContainer.begin(), 0);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_iterator
bitvector<Allocator, Element, Container>::begin() const EASTL_NOEXCEPT
{
return const_iterator(mContainer.begin(), 0);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_iterator
bitvector<Allocator, Element, Container>::cbegin() const EASTL_NOEXCEPT
{
return const_iterator(mContainer.begin(), 0);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::iterator
bitvector<Allocator, Element, Container>::end() EASTL_NOEXCEPT
{
return iterator(mContainer.end(), 0) - mFreeBitCount;
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_iterator
bitvector<Allocator, Element, Container>::end() const EASTL_NOEXCEPT
{
return const_iterator(mContainer.end(), 0) - mFreeBitCount;
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_iterator
bitvector<Allocator, Element, Container>::cend() const EASTL_NOEXCEPT
{
return const_iterator(mContainer.end(), 0) - mFreeBitCount;
}
template <typename Allocator, typename Element, typename Container>
bool bitvector<Allocator, Element, Container>::empty() const EASTL_NOEXCEPT
{
return mContainer.empty();
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::size_type
bitvector<Allocator, Element, Container>::size() const EASTL_NOEXCEPT
{
return (mContainer.size() * kBitCount) - mFreeBitCount;
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::size_type
bitvector<Allocator, Element, Container>::capacity() const EASTL_NOEXCEPT
{
return mContainer.capacity() * kBitCount;
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::setCapacity(size_type n)
{
if(n == npos)
mContainer.setCapacity(npos);
else
mContainer.setCapacity((n + kBitCount - 1) / kBitCount);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::reverse_iterator
bitvector<Allocator, Element, Container>::rbegin() EASTL_NOEXCEPT
{
return reverse_iterator(end());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reverse_iterator
bitvector<Allocator, Element, Container>::rbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(end());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reverse_iterator
bitvector<Allocator, Element, Container>::crbegin() const EASTL_NOEXCEPT
{
return const_reverse_iterator(end());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::reverse_iterator
bitvector<Allocator, Element, Container>::rend() EASTL_NOEXCEPT
{
return reverse_iterator(begin());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reverse_iterator
bitvector<Allocator, Element, Container>::rend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(begin());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reverse_iterator
bitvector<Allocator, Element, Container>::crend() const EASTL_NOEXCEPT
{
return const_reverse_iterator(begin());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::reference
bitvector<Allocator, Element, Container>::front()
{
EASTL_ASSERT(!empty());
return reference(&mContainer[0], 0);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reference
bitvector<Allocator, Element, Container>::front() const
{
EASTL_ASSERT(!empty());
// To consider: make a better solution to this than const_cast.
return reference(const_cast<Element*>(&mContainer[0]), 0);
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::reference
bitvector<Allocator, Element, Container>::back()
{
EASTL_ASSERT(!empty());
return *(--end());
}
template <typename Allocator, typename Element, typename Container>
typename bitvector<Allocator, Element, Container>::const_reference
bitvector<Allocator, Element, Container>::back() const
{
EASTL_ASSERT(!empty());
return *(--end());
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::pushBack()
{
if(!mFreeBitCount)
{
mContainer.pushBack();
mFreeBitCount = kBitCount;
}
--mFreeBitCount;
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::pushBack(value_type value)
{
pushBack();
*--end() = value;
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::popBack()
{
EASTL_ASSERT(!empty());
if(++mFreeBitCount == kBitCount)
{
mContainer.popBack();
mFreeBitCount = 0;
}
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::reserve(size_type n)
{
const size_type wordCount = (n + kBitCount - 1) / kBitCount;
mContainer.reserve(wordCount);
}
template <typename Allocator, typename Element, typename Container>
void bitvector<Allocator, Element, Container>::resize(size_type n)
{
const size_type wordCount = (n + kBitCount - 1) / kBitCount;
const size_type extra = (wordCount * kBitCount) - n;
mContainer.resize(wordCount);
mFreeBitCount = extra;
}
template <typename Allocator, typename Element, typename Container>