-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_bitset.hpp
746 lines (545 loc) · 23.3 KB
/
dynamic_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
#pragma once
#include <vector>
#include <iostream>
#include <climits>
#include <algorithm>
#include <cctype>
namespace bit
{
template <typename Chunk_T = unsigned long long, typename Allocator_T = std::allocator <Chunk_T>>
class Bitset
{
private:
std::size_t size_; //size of the bitset
std::vector <Chunk_T> chunks_; //array containing the bitset
constexpr static std::size_t chunk_size_ = Bitset <Chunk_T, Allocator_T>::sizeof_chunk_(); //how many bits can a chunk hold
//so we dont compute these a billion times, at runtime
constexpr static Chunk_T set_chunk_ = ~Chunk_T(0); //1111 1111
constexpr static Chunk_T one_chunk_ = Chunk_T(1); //0000 0001
constexpr static Chunk_T zero_chunk_ = Chunk_T(0); //0000 0000
//single bit abstraction
class Reference
{
Chunk_T &chunk_reference_;
const Chunk_T chunk_mask_true_;
const Chunk_T chunk_mask_false_;
public:
constexpr explicit Reference(Bitset <Chunk_T, Allocator_T>&, const std::size_t);
~Reference() noexcept = default;
constexpr operator bool () const noexcept;
constexpr Reference& operator = (const bool) noexcept;
constexpr Reference& operator = (const Reference&) noexcept;
constexpr Reference& operator &= (const bool) noexcept;
constexpr Reference& operator |= (const bool) noexcept;
constexpr Reference& operator ^= (const bool) noexcept;
constexpr bool operator~() const noexcept;
};
public:
class Iterator
{
public:
constexpr explicit Iterator();
constexpr explicit Iterator(const Iterator&);
~Iterator() noexcept = default;
constexpr Iterator& operator ++ ();
constexpr Iterator operator ++ (std::size_t);
constexpr Iterator& operator -- ();
constexpr Iterator operator -- (std::size_t);
constexpr Iterator& operator += (std::size_t);
constexpr Iterator& operator -= (std::size_t);
constexpr Iterator operator + (std::size_t) const;
//constexpr friend Iterator operator + (std::size_t, const Iterator&);
constexpr Iterator operator - (std::size_t) const;
//constexpr friend Iterator operator - (std::size_t, const Iterator&);
};
public:
constexpr explicit Bitset(const std::size_t = 0, const bool value = false); //default constructor std::vector::vector() (3)
constexpr explicit Bitset(const std::initializer_list <bool>); //initializer list constructor std::vector::vector() (9)
~Bitset() noexcept = default; //default destructor std::vector::~vector()
//-------------------------ELEMENT ACCESS-------------------------
constexpr bool test(const std::size_t) const;
constexpr Reference at(const std::size_t);
constexpr bool none() const;//
//-------------------------CAPACITY-------------------------
constexpr std::size_t size() const noexcept;
//-------------------------MODIFIERS-------------------------
constexpr Bitset <Chunk_T, Allocator_T>& clear() noexcept;
constexpr Bitset <Chunk_T, Allocator_T>& resize(const std::size_t = 0, const bool = false);
constexpr Bitset <Chunk_T, Allocator_T>& set();
constexpr Bitset <Chunk_T, Allocator_T>& set(const std::size_t, const bool = true);
constexpr Bitset <Chunk_T, Allocator_T>& reset();
constexpr Bitset <Chunk_T, Allocator_T>& reset(const std::size_t);
constexpr Bitset <Chunk_T, Allocator_T>& flip();
constexpr Bitset <Chunk_T, Allocator_T>& flip(const std::size_t);
constexpr Bitset <Chunk_T, Allocator_T>& operator &= (Bitset <Chunk_T, Allocator_T>&);
constexpr Bitset <Chunk_T, Allocator_T>& operator |= (Bitset <Chunk_T, Allocator_T>&);
constexpr Bitset <Chunk_T, Allocator_T>& operator ^= (Bitset <Chunk_T, Allocator_T>&);
constexpr Bitset <Chunk_T, Allocator_T> operator ~ () const;
constexpr Bitset <Chunk_T, Allocator_T> operator << (const std::size_t) const;
constexpr Bitset <Chunk_T, Allocator_T>& operator <<= (const std::size_t);
constexpr Bitset <Chunk_T, Allocator_T> operator >> (const std::size_t); //cant be const since bit sanitization must be dealt with
constexpr Bitset <Chunk_T, Allocator_T>& operator >>= (const std::size_t);
//-------------------------CONVERSIONS-------------------------
template <typename Char_T = char, typename Traits_T = std::char_traits <Char_T>, typename Str_Alloc_T = std::allocator <Char_T>>
constexpr std::basic_string <Char_T, Traits_T, Str_Alloc_T> to_string(const Char_T = Char_T('0'), const Char_T = Char_T('1')) const;
//-------------------------NON MEMBER FUNCTIOS-------------------------
template <typename Chunk_FT, typename Allocator_FT, typename Char_T, typename Traits_T>
friend std::basic_ostream <Char_T, Traits_T>& operator << (std::basic_ostream <Char_T, Traits_T>&, const Bitset <Chunk_FT, Allocator_FT>&);
private:
//-------------------------helpers/getters-------------------------
constexpr std::size_t needed_chunks_(const std::size_t size) const noexcept; //just a ceil function
constexpr static std::size_t sizeof_chunk_() noexcept; //chunk_size_ compile time calculator
constexpr std::string oor_msg_builder_(const std::string&, const std::size_t) const; //out of range which/pos string formatter
constexpr std::size_t chunk_pos_(const std::size_t) const noexcept;
constexpr std::size_t bit_pos_(const std::size_t) const noexcept;
void set_unused_bits_(const bool = false); //sanitization tool
};
/*
============================================================================
-------------------------class Bitset methods BEGIN-------------------------
============================================================================
*/
//-------------------------constructors-------------------------
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>::Bitset(const std::size_t size, const bool value)
:
size_(size),
chunks_(needed_chunks_(size_), value ? set_chunk_ : zero_chunk_, Allocator_T())
{};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>::Bitset(const std::initializer_list <bool> init)
:
size_(init.size()),
chunks_(Allocator_T())
{
//optimized as to avoid checks and unneeded operations as much as possible (does anyone even use initializer list for big initializations lol?)
chunks_.reserve(needed_chunks_(size_));
Chunk_T temp_chunk = zero_chunk_;
auto curr_bit = init.begin();
const std::size_t full_chunks = size_ / chunk_size_; //amount of bits that will be initialized as full chunks
for(int itr = 0; itr < full_chunks; ++itr)
{
for(std::size_t bit_pos = 0; bit_pos < chunk_size_; ++bit_pos)
{
if(*(curr_bit++)) //check then increment
temp_chunk |= (one_chunk_ << bit_pos);
}
chunks_.push_back(temp_chunk);
temp_chunk = zero_chunk_;
}
const std::size_t remaining_bits = size_ % chunk_size_; //the remaining (less than chunk_size_) bits that will be initialized as a half chunk
for(std::size_t bit_pos = 0; bit_pos < remaining_bits; ++bit_pos)
{
if(*(curr_bit++))
temp_chunk |= (one_chunk_ << bit_pos);
}
chunks_.push_back(temp_chunk);
};
//-------------------------ELEMENT ACCESS-------------------------
template <typename Chunk_T, typename Allocator_T>
constexpr bool Bitset <Chunk_T, Allocator_T>::test(const std::size_t pos) const
{
if(pos >= size_)
throw std::out_of_range(oor_msg_builder_("test", pos));
return chunks_[chunk_pos_(pos)] & (one_chunk_ << bit_pos_(pos));
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference Bitset <Chunk_T, Allocator_T>::at(const std::size_t pos)
{
if(pos >= size_)
throw std::out_of_range(oor_msg_builder_("at", pos));
return Reference(*this, pos);
};
template <typename Chunk_T, typename Allocator_T>
constexpr bool Bitset <Chunk_T, Allocator_T>::none() const
{
std::cout << chunks_.size();
for(std::size_t itr = 0; itr < chunks_.size() - 1; ++itr)
{
if(chunks_[itr] != zero_chunk_)
{
return false;
}
}
const std::size_t remaining_bits = size_ % chunk_size_; //the remaining (less than chunk_size_) bits that will be initialized as a half chunk
if(remaining_bits)
{
if(chunks_.back() << (chunk_size_ - remaining_bits) != zero_chunk_)
{
return false;
}
}
else
{
if(chunks_.back() != zero_chunk_)
{
return false;
}
}
return true;
};
//-------------------------CAPACITY-------------------------
template <typename Chunk_T, typename Allocator_T> //pure
constexpr std::size_t Bitset <Chunk_T, Allocator_T>::size() const noexcept
{
return size_;
};
//-------------------------MODIFIERS-------------------------
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::clear() noexcept
{
size_ = 0;
chunks_.clear(); //memory will still be allocated, but objects will be destroyed
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::resize(const std::size_t size, const bool value)
{
set_unused_bits_(value); //set the newly added bits inside the last half chunk before doing any changes
size_ = size;
chunks_.resize(needed_chunks_(size), value ? set_chunk_ : zero_chunk_);
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::set()
{
std::fill(chunks_.begin(), chunks_.end(), set_chunk_);
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::set(const std::size_t pos, const bool value)
{
if(pos >= size_)
throw std::out_of_range(oor_msg_builder_("set", pos));
if(value)
chunks_[chunk_pos_(pos)] |= (one_chunk_ << bit_pos_(pos));
else
chunks_[chunk_pos_(pos)] &= ~(one_chunk_ << bit_pos_(pos));
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::reset()
{
std::fill(chunks_.begin(), chunks_.end(), zero_chunk_);
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::reset(const std::size_t pos)
{
set(pos, false);
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::flip()
{
for(auto &itr : chunks_)
itr = ~itr;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::flip(const std::size_t pos)
{
if(pos >= size_)
throw std::out_of_range(oor_msg_builder_("flip", pos));
chunks_[chunk_pos_(pos)] ^= (one_chunk_ << bit_pos_(pos));
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::operator &= (Bitset <Chunk_T, Allocator_T> &rhs)
{
std::size_t min_size = size_;
if(min_size > rhs.size_)
{
rhs.set_unused_bits_(true); //set the unused bits to 1 so that &op wont mess around with the result (only needed if rhs chunk overlaps with this and its got unused bits)
min_size = rhs.size_;
}
for(std::size_t itr = 0; itr < min_size; ++itr)
chunks_[itr] &= rhs.chunks_[itr];
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::operator |= (Bitset <Chunk_T, Allocator_T> &rhs)
{
std::size_t min_size = chunks_.size();
if(min_size > rhs.chunks_.size())
{
rhs.set_unused_bits_(false); //set the unused bits to 0 so that &op wont mess around with the result (only needed if rhs chunk overlaps with this and its got unused bits)
min_size = rhs.chunks_.size();
}
for(std::size_t itr = 0; itr < min_size; ++itr)
chunks_[itr] |= rhs.chunks_[itr];
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::operator ^= (Bitset <Chunk_T, Allocator_T> &rhs)
{
std::size_t min_size = size_;
if(min_size > rhs.size_)
{
rhs.set_unused_bits_(false); //set the unused bits to 0 so that &op wont mess around with the result (only needed if rhs chunk overlaps with this and its got unused bits)
min_size = rhs.size_;
}
for(std::size_t itr = 0; itr < min_size; ++itr)
chunks_[itr] ^= rhs.chunks_[itr];
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T> Bitset <Chunk_T, Allocator_T>::operator ~ () const
{
Bitset <Chunk_T, Allocator_T> flipped;
flipped.chunks_.reserve(chunks_.size());
flipped.size_ = size_;
for(auto &chunk : chunks_)
flipped.chunks_.push_back(~chunk);
return flipped;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T> Bitset <Chunk_T, Allocator_T>::operator << (const std::size_t pos) const
{
//shift chunks to right (increase value)
//unused bits should still be outside reach, therefore no need to mess with them
if(!pos)
return *this;
if(pos >= size_)
return Bitset <Chunk_T, Allocator_T>(size_);
Bitset <Chunk_T, Allocator_T> shifted(size_); //empty bitset
const std::size_t left_offset = pos % chunk_size_; //left part (smaller) of the chunk will become the right (larger) part of the new chunk
const std::size_t right_offset = chunk_size_ - left_offset; //right part (larger) of the chunk will become the left (smallest) part of the new chunk
const std::size_t chunk_offset = pos / chunk_size_; //first chunk to be set in the new bitset
if(pos % chunk_size_)
{
//we will set every chunk AFTER chunk_offset, we will deal with chunk_offset separately as it will only need part of it set
for(std::size_t itr = chunks_.size() - 1; itr > chunk_offset; --itr)
shifted.chunks_[itr] = (chunks_[itr - chunk_offset] << left_offset) | (chunks_[itr - chunk_offset - 1] >> right_offset);
shifted.chunks_[chunk_offset] = chunks_[0] << left_offset;
}
else
{
//full chunk shift
for(std::size_t itr = chunks_.size() - 1; itr >= chunk_offset; --itr)
shifted.chunks_[itr] = chunks_[itr - chunk_offset];
}
return shifted;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::operator <<= (const std::size_t pos)
{
if(!pos)
return *this;
if(pos >= size_)
{
std::fill(chunks_.begin(), chunks_.end(), zero_chunk_);
return *this;
}
const std::size_t left_offset = pos % chunk_size_; //left part (smaller) of the chunk will become the right (larger) part of the new chunk
const std::size_t right_offset = chunk_size_ - left_offset; //right part (larger) of the chunk will become the left (smallest) part of the new chunk
const std::size_t chunk_offset = pos / chunk_size_; //first chunk to be set in the new bitset
if(pos % chunk_size_)
{
//we will set every chunk AFTER chunk_offset, we will deal with chunk_offset separately as it will only need part of it set
for(std::size_t itr = chunks_.size() - 1; itr > chunk_offset; --itr)
chunks_[itr] = (chunks_[itr - chunk_offset] << left_offset) | (chunks_[itr - chunk_offset - 1] >> right_offset);
chunks_[chunk_offset] = chunks_[0] << left_offset;
}
else
{
//full chunk shift
for(std::size_t itr = chunks_.size() - 1; itr >= chunk_offset; --itr)
chunks_[itr] = chunks_[itr - chunk_offset];
}
std::fill(chunks_.begin(), chunks_.begin() + chunk_offset, zero_chunk_);
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T> Bitset <Chunk_T, Allocator_T>::operator >> (const std::size_t pos) //not const since might need to sanitize some bits...
{
//shift chunks to left (decrease value)
//unused bits are definetely a problem...
if(!pos)
return *this;
if(pos >= size_)
return Bitset <Chunk_T, Allocator_T>(size_);
set_unused_bits_(false);
Bitset <Chunk_T, Allocator_T> shifted(size_); //empty bitset
const std::size_t left_offset = pos % chunk_size_; //left part (smaller) of the chunk will become the right (larger) part of the new chunk
const std::size_t right_offset = chunk_size_ - left_offset; //right part (larger) of the chunk will become the left (smallest) part of the new chunk
const std::size_t chunk_offset = pos / chunk_size_; //last chunk to be set in the new bitset
if(pos % chunk_size_)
{
//we will set every chunk AFTER chunk_offset, we will deal with chunk_offset separately as it will only need part of it set
for(std::size_t itr = 0; itr < chunks_.size() - chunk_offset - 1; ++itr)
shifted.chunks_[itr] = (chunks_[itr + chunk_offset] >> left_offset) | (chunks_[itr + chunk_offset + 1] << right_offset);
shifted.chunks_[chunks_.size() - chunk_offset - 1] = chunks_[chunks_.size() - 1] >> left_offset;
}
else
{
//full chunk shift
for(std::size_t itr = 0; itr <= chunks_.size() - chunk_offset - 1; ++itr)
shifted.chunks_[itr] = chunks_[itr + chunk_offset];
}
return shifted;
};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>& Bitset <Chunk_T, Allocator_T>::operator >>= (const std::size_t pos)
{
//shift chunks to left (decrease value)
if(!pos)
return *this;
if(pos >= size_)
{
std::fill(chunks_.begin(), chunks_.end(), zero_chunk_);
return *this;
}
set_unused_bits_(false);
const std::size_t left_offset = pos % chunk_size_; //left part (smaller) of the chunk will become the right (larger) part of the new chunk
const std::size_t right_offset = chunk_size_ - left_offset; //right part (larger) of the chunk will become the left (smallest) part of the new chunk
const std::size_t chunk_offset = pos / chunk_size_; //last chunk to be set in the new bitset
if(pos % chunk_size_)
{
//we will set every chunk AFTER chunk_offset, we will deal with chunk_offset separately as it will only need part of it set
for(std::size_t itr = 0; itr < chunks_.size() - chunk_offset - 1; ++itr)
chunks_[itr] = (chunks_[itr + chunk_offset] >> left_offset) | (chunks_[itr + chunk_offset + 1] << right_offset);
chunks_[chunks_.size() - chunk_offset - 1] = chunks_[chunks_.size() - 1] >> left_offset;
}
else
{
//full chunk shift
for(std::size_t itr = 0; itr <= chunks_.size() - chunk_offset - 1; ++itr)
chunks_[itr] = chunks_[itr + chunk_offset];
}
std::fill(chunks_.begin() + chunks_.size() - chunk_offset, chunks_.end(), zero_chunk_);
return *this;
}
//-------------------------CONVERSIONS-------------------------
template <typename Chunk_T, typename Allocator_T>
template <typename Char_T, typename Traits_T, typename Str_Allocator_T>
constexpr std::basic_string <Char_T, Traits_T, Str_Allocator_T> Bitset<Chunk_T, Allocator_T>::to_string(const Char_T zero, const Char_T one) const
{
std::basic_string <Char_T, Traits_T, Str_Allocator_T> tmp(size_, zero);
std::size_t pos = size_;
for(auto chunk : chunks_)
{
for(std::size_t bit_pos = 0; bit_pos < chunk_size_ && pos-- > 0; ++ bit_pos)
{
if(chunk & (one_chunk_ << bit_pos))
Traits_T::assign(tmp[pos], one);
}
}
return tmp;
};
//-------------------------NON MEMBER FUNCTIOS-------------------------
template <typename Chunk_T, typename Allocator_T, typename Char_T, typename Traits_T>
std::basic_ostream <Char_T, Traits_T>& operator << (std::basic_ostream <Char_T, Traits_T>&out, const Bitset <Chunk_T, Allocator_T> &mask)
{
//i sure hope this is enough to make it portable, must investigate further
if(!mask.size_)
return out;
Char_T zero = std::use_facet <std::ctype <Char_T>>(out.getloc()).widen('0');
Char_T one = std::use_facet <std::ctype <Char_T>>(out.getloc()).widen('1');
return out << mask.template to_string <Char_T, Traits_T, std::allocator <Char_T>> (zero, one);
};
//-------------------------private helpers/getters-------------------------
template <typename Chunk_T, typename Allocator_T>
constexpr std::size_t Bitset <Chunk_T, Allocator_T>::needed_chunks_(const std::size_t size) const noexcept
{
return size / chunk_size_ + (size % chunk_size_ > 0);
};
template <typename Chunk_T, typename Allocator_T>
constexpr std::size_t Bitset <Chunk_T, Allocator_T>::sizeof_chunk_() noexcept
{
if constexpr (std::is_integral<Chunk_T>::value)
return sizeof(Chunk_T) * CHAR_BIT;
else
return Chunk_T::sizeof_ * CHAR_BIT;
};
template <typename Chunk_T, typename Allocator_T>
constexpr std::string Bitset <Chunk_T, Allocator_T>::oor_msg_builder_(const std::string &which, const std::size_t pos) const
{
return std::string(
"bit::Bitset::"
+ which
+ "(): pos (which is "
+ std::to_string(pos)
+ ") >= size() (which is "
+ std::to_string(size_)
+ ")"
);
};
template <typename Chunk_T, typename Allocator_T>
constexpr std::size_t Bitset <Chunk_T, Allocator_T>::chunk_pos_(const std::size_t pos) const noexcept
{
return pos / chunk_size_;
};
template <typename Chunk_T, typename Allocator_T>
constexpr std::size_t Bitset <Chunk_T, Allocator_T>::bit_pos_(const std::size_t pos) const noexcept
{
return pos % chunk_size_;
};
template <typename Chunk_T, typename Allocator_T>
void Bitset <Chunk_T, Allocator_T>::set_unused_bits_(const bool value) //noexcept if chunks isnt empty !!!!MAKE SURE THIS WORKS
{
std::size_t bit_pos = bit_pos_(size_);
if(bit_pos)
{
if(value)
chunks_.back() |= set_chunk_ << bit_pos; // 0000 0111
else
chunks_.back() &= ~(set_chunk_ << bit_pos); // 1111 1000
}
};
/*
============================================================================
-------------------------class Reference methods BEGIN-------------------------
============================================================================
*/
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>::Reference::Reference(Bitset <Chunk_T, Allocator_T> &target, const std::size_t pos)
:
chunk_reference_(target.chunks_[target.chunk_pos_(pos)]),
chunk_mask_true_(one_chunk_ << target.bit_pos_(pos)),
chunk_mask_false_(~(one_chunk_ << target.bit_pos_(pos)))
{};
template <typename Chunk_T, typename Allocator_T>
constexpr Bitset <Chunk_T, Allocator_T>::Reference::operator bool () const noexcept
{
return chunk_reference_ & chunk_mask_true_;
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference& Bitset <Chunk_T, Allocator_T>::Reference::operator = (const bool value) noexcept
{
value ? chunk_reference_ |= chunk_mask_true_ : chunk_reference_ &= chunk_mask_false_;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference& Bitset <Chunk_T, Allocator_T>::Reference::operator = (const Bitset <Chunk_T, Allocator_T>::Reference& rhs) noexcept
{
rhs ? chunk_reference_ |= chunk_mask_true_ : chunk_reference_ &= chunk_mask_false_;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference& Bitset <Chunk_T, Allocator_T>::Reference::operator &= (const bool value) noexcept
{
if(!value)
chunk_reference_ &= chunk_mask_false_;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference& Bitset <Chunk_T, Allocator_T>::Reference::operator |= (const bool value) noexcept
{
if(value)
chunk_reference_ |= chunk_mask_true_;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr typename Bitset <Chunk_T, Allocator_T>::Reference& Bitset <Chunk_T, Allocator_T>::Reference::operator ^= (const bool value) noexcept
{
if(value)
chunk_reference_ ^= chunk_mask_true_;
return *this;
};
template <typename Chunk_T, typename Allocator_T>
constexpr bool Bitset <Chunk_T, Allocator_T>::Reference::operator ~ () const noexcept
{
return !(chunk_reference_ & chunk_mask_true_);
};
} //namespace end