Loading...
Searching...
No Matches
bitSetI.H
Go to the documentation of this file.
1/*---------------------------------------------------------------------------*\
2 ========= |
3 \\ / F ield | OpenFOAM: The Open Source CFD Toolbox
4 \\ / O peration |
5 \\ / A nd | www.openfoam.com
6 \\/ M anipulation |
7-------------------------------------------------------------------------------
8 Copyright (C) 2018-2025 OpenCFD Ltd.
9-------------------------------------------------------------------------------
10License
11 This file is part of OpenFOAM.
12
13 OpenFOAM is free software: you can redistribute it and/or modify it
14 under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
19 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
25
26\*---------------------------------------------------------------------------*/
27
28// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
30inline constexpr Foam::bitSet::bitSet() noexcept
31:
32 PackedList<1>()
33{}
34
36inline Foam::bitSet::bitSet(const label n)
37:
38 PackedList<1>(n)
39{}
40
41
42inline Foam::bitSet::bitSet(const label n, const bool val)
44 bitSet(n)
45{
46 if (val) fill(val);
47}
48
50inline Foam::bitSet::bitSet(const bitSet& bitset)
51:
52 PackedList<1>(bitset)
53{}
54
56inline Foam::bitSet::bitSet(bitSet&& bitset)
57:
58 PackedList<1>(std::move(bitset))
59{}
60
61
62inline Foam::bitSet::bitSet(const UList<bool>& bools)
64 bitSet()
65{
66 assign(bools);
67}
68
69
70inline Foam::bitSet::bitSet(const label n, const labelUList& locations)
71:
73{
74 setMany(locations.begin(), locations.end());
75}
76
77
78template<class Addr>
80(
81 const label n,
82 const IndirectListBase<label, Addr>& locations
83)
85 bitSet(n)
86{
87 setMany(locations.begin(), locations.end());
88}
89
90
92(
93 const label n,
94 std::initializer_list<label> locations
95)
97 bitSet(n)
98{
99 setMany(locations.begin(), locations.end());
100}
101
102
103inline Foam::bitSet::bitSet(const labelUList& locations)
104:
106{
107 setMany(locations.begin(), locations.end());
108}
109
110
111template<class Addr>
113(
114 const IndirectListBase<label, Addr>& locations
115)
117 bitSet()
118{
119 setMany(locations.begin(), locations.end());
120}
121
122
125 return autoPtr<bitSet>::New(*this);
126}
127
128
129// * * * * * * * * * * * * * * * * References * * * * * * * * * * * * * * * * //
130
132(
133 bitSet* parent,
134 const label index
135)
136:
137 PackedList<1>::reference(parent, index)
138{}
139
140
142{
143 const unsigned int mask = (max_value << shift_);
144 ref_ ^= mask;
145}
146
147
148inline void Foam::bitSet::reference::operator=
149(
150 const reference& other
152{
153 // Accepts self-assignment
154 set(other.get());
155}
156
157
158inline void Foam::bitSet::reference::operator=
159(
160 const unsigned int val
161)
162{
163 set(val);
164}
165
166
167inline Foam::bitSet::reference::operator unsigned int () const
168{
169 return get();
170}
171
172
173// * * * * * * * * * * * * * * * * Iterators * * * * * * * * * * * * * * * * //
174
175inline Foam::bitSet::const_iterator::const_iterator() noexcept
176:
177 set_(nullptr),
178 pos_(-1)
179{}
180
181
182inline Foam::bitSet::const_iterator::const_iterator(const bitSet* parent)
183:
184 set_(parent),
185 pos_(set_->find_first())
186{}
187
188
189inline Foam::bitSet::const_iterator::const_iterator
190(
191 const bitSet* parent,
192 label pos
194:
195 set_(parent),
196 pos_(set_->find_next(pos-1))
197{}
198
200inline Foam::label Foam::bitSet::const_iterator::operator*() const noexcept
201{
202 return pos_;
203}
204
205
207{
208 pos_ = set_->find_next(pos_);
209 return *this;
210}
211
212
213inline bool Foam::bitSet::const_iterator::operator==
214(
215 const const_iterator& iter
216) const noexcept
217{
218 return (iter.pos_ == pos_);
219}
220
221
222inline bool Foam::bitSet::const_iterator::operator!=
223(
224 const const_iterator& iter
225) const noexcept
226{
227 return (iter.pos_ != pos_);
228}
229
232{
233 return const_iterator(this);
234}
235
238{
239 return const_iterator(this);
240}
241
244{
245 return const_iterator(this, pos);
246}
247
250{
251 return const_iterator(this, pos);
252}
253
258}
259
262{
263 return const_iterator();
264}
265
266
267inline Foam::label Foam::bitSet::find_first() const
268{
269 const label blocki = first_block();
270
271 if (blocki >= 0)
272 {
273 label pos = (blocki * elem_per_block);
274
275 // Detect first '1' bit within the block
276
277 for
278 (
279 unsigned int blockval = blocks_[blocki];
280 blockval;
281 blockval >>= 1u
282 )
283 {
284 if (blockval & 1u)
285 {
286 return pos;
287 }
288 ++pos;
290 }
291
292 return -1;
293}
294
295
296inline Foam::label Foam::bitSet::find_first_not() const
297{
298 const label blocki = first_not_block();
299
300 if (blocki >= 0)
301 {
302 label pos = (blocki * elem_per_block);
303
304 // Detect first '0' bit within the block (check the complement)
305
306 // No special masking for the final block, that was already checked
307 // in the first_not_block() call.
308
309 for
310 (
311 unsigned int blockval = ~(blocks_[blocki]);
312 blockval;
313 blockval >>= 1u
314 )
315 {
316 if (blockval & 1u)
317 {
318 return pos;
319 }
320 ++pos;
322 }
323
324 return -1;
325}
326
327
328inline Foam::label Foam::bitSet::find_last() const
329{
330 // Process block-wise, detecting any '1' bits
331
332 for (label blocki = num_blocks(size())-1; blocki >= 0; --blocki)
333 {
334 unsigned int blockval = blocks_[blocki];
335
336 if (blockval)
337 {
338 label pos = (blocki * elem_per_block) - 1;
339
340 while (blockval)
341 {
342 blockval >>= 1u;
343 ++pos;
344 }
345
346 return pos;
348 }
349
350 return -1;
351}
352
353
354inline Foam::label Foam::bitSet::find_next(label pos) const
355{
356 ++pos;
357 if (pos < 0 || pos >= size())
358 {
359 return -1;
360 }
361
362 // The corresponding block/offset
363 label blocki = pos / elem_per_block;
364 unsigned int off = pos % elem_per_block;
365
366 for
367 (
368 unsigned int blockval = (blocks_[blocki] >> off);
369 blockval;
370 blockval >>= 1u
371 )
372 {
373 if (blockval & 1u)
374 {
375 return pos;
376 }
377 ++pos;
378 }
379
380 // Normal block-wise search. Starting at the next block
381
382 const label nblocks = num_blocks(size());
383 for (++blocki; blocki < nblocks; ++blocki)
384 {
385 label pos = (blocki * elem_per_block);
386
387 for
388 (
389 unsigned int blockval = blocks_[blocki];
390 blockval;
391 blockval >>= 1u
392 )
393 {
394 if (blockval & 1u)
395 {
396 return pos;
397 }
398 ++pos;
399 }
400 }
402 return -1;
403}
404
405
406// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
407
408inline bool Foam::bitSet::all() const
409{
410 if (empty()) return true; // SIC. boost convention
411 return (-1 == first_not_block());
412}
413
415inline bool Foam::bitSet::any() const
416{
417 return (-1 != first_block());
418}
419
421inline bool Foam::bitSet::none() const
422{
423 return (-1 == first_block());
424}
425
426
427inline unsigned int Foam::bitSet::count(const bool on) const
428{
429 unsigned int total = 0;
430
431 const label nblocks = num_blocks(size());
432
433 for (label blocki = 0; blocki < nblocks; ++blocki)
434 {
435 total += BitOps::bit_count(blocks_[blocki]);
436 }
437
438 if (!on)
439 {
440 // Return the number of bits that are OFF.
441 return (unsigned(size()) - total);
442 }
443
444 return total;
445}
446
449{
450 return toc();
451}
452
453
454inline void Foam::bitSet::resize_last()
455{
456 const label pos = find_last();
457
458 if (pos >= 0)
459 {
460 resize(pos+1);
461 }
462 else
463 {
464 clear();
465 }
466}
467
469inline void Foam::bitSet::swap(bitSet& bitset)
470{
471 PackedList<1>::swap(bitset);
472}
473
475inline void Foam::bitSet::transfer(bitSet& bitset)
476{
478}
479
480
481inline void Foam::bitSet::fill(const bool val)
482{
483 if (empty())
484 {
485 return;
486 }
487 else if (val)
488 {
489 std::fill_n(blocks_.data(), num_blocks(), ~0u);
491 }
492 else
493 {
494 // or: PackedList<1>::reset();
495 // or: blocks_ = 0u;
496 std::fill_n(blocks_.data(), num_blocks(), 0u);
497 }
498}
499
500
501inline bool Foam::bitSet::test_set(const label i, const bool val)
503 bool old = test(i);
504 PackedList<1>::set(i, val);
505 return old;
506}
507
509inline void Foam::bitSet::set(const bitSet& bitset)
510{
511 orEq(bitset);
512}
513
514
515inline Foam::label Foam::bitSet::set(const labelUList& locations)
516{
517 return setMany(locations.begin(), locations.end());
518}
519
520
521template<class Addr>
522inline Foam::label Foam::bitSet::set
523(
525)
526{
527 return setMany(locations.begin(), locations.end());
528}
529
530
531inline Foam::label Foam::bitSet::unset(const labelUList& locations)
532{
533 return unset(locations.begin(), locations.end());
534}
535
536
537template<class Addr>
538inline Foam::label Foam::bitSet::unset
539(
541)
542{
543 return unset(locations.begin(), locations.end());
544}
545
547inline Foam::bitSet& Foam::bitSet::unset(const bitSet& other)
548{
549 return minusEq(other);
550}
551
552
553inline void Foam::bitSet::flip()
554{
555 if (size())
556 {
557 const label nblocks = num_blocks(size());
558
559 for (label blocki=0; blocki < nblocks; ++blocki)
560 {
561 blocks_[blocki] = ~(blocks_[blocki]);
562 }
564 }
565}
566
567
568inline void Foam::bitSet::flip(const label i)
569{
570 if (i >= 0 && i < size())
571 {
572 reference(this, i).flip();
573 }
574}
575
576
577inline Foam::bitSet& Foam::bitSet::bound(const label maxSize)
578{
579 if (maxSize < size())
580 {
581 resize(maxSize);
582 }
583
584 return *this;
585}
586
588inline Foam::bitSet& Foam::bitSet::bound(const bitSet& other)
589{
590 return bound(other.size());
591}
592
593
594inline Foam::bitSet& Foam::bitSet::extend(const label minSize)
595{
596 if (size() < minSize)
597 {
598 resize(minSize);
599 }
600
601 return *this;
602}
603
604
605inline Foam::bitSet& Foam::bitSet::extend(const bitSet& other)
607 return extend(other.size());
608}
609
610
611// * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * //
613inline bool Foam::bitSet::operator()(const label pos) const
614{
615 return test(pos);
616}
617
619inline unsigned int Foam::bitSet::operator[](const label i) const
620{
621 return get(i);
622}
623
624
626{
627 #ifdef FULLDEBUG
628 checkIndex(i);
629 #endif
630 return reference(this, i);
631}
632
633
635{
637 return *this;
638}
639
640
642{
643 transfer(bitset);
644 return *this;
645}
646
647
649{
650 fill(val);
651 return *this;
652}
653
655inline Foam::bitSet& Foam::bitSet::operator&=(const bitSet& other)
656{
657 return andEq(other);
658}
659
661inline Foam::bitSet& Foam::bitSet::operator|=(const bitSet& other)
662{
663 return orEq(other);
664}
665
667inline Foam::bitSet& Foam::bitSet::operator^=(const bitSet& other)
668{
669 return xorEq(other);
670}
671
672
673inline Foam::bitSet& Foam::bitSet::operator-=(const bitSet& other)
675 return minusEq(other);
676}
677
678
679// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
680
681inline Foam::bitSet Foam::operator~(const bitSet& bitset)
683 bitSet result(bitset);
684 result.flip();
685 return result;
686}
687
688
689inline Foam::bitSet Foam::operator&(const bitSet& a, const bitSet& b)
690{
691 bitSet result(a);
692 result &= b;
693
694 result.resize_last();
695 return result;
696}
697
698
699inline Foam::bitSet Foam::operator|(const bitSet& a, const bitSet& b)
700{
701 bitSet result(a);
702 result |= b;
703
704 result.resize_last();
705 return result;
706}
707
708
709inline Foam::bitSet Foam::operator^(const bitSet& a, const bitSet& b)
710{
711 bitSet result(a);
712 result ^= b;
713
714 result.resize_last();
715 return result;
716}
717
718
719inline Foam::bitSet Foam::operator-(const bitSet& a, const bitSet& b)
720{
721 bitSet result(a);
722 result |= b;
723
724 result.resize_last();
725 return result;
726}
727
728
729// ************************************************************************* //
label n
Base for lists with indirect addressing, templated on the list contents type and the addressing type....
iterator end()
Return an iterator at end of list.
iterator begin()
Return an iterator at begin of list.
unsigned shift_
The bit shift to access the given sub-portion.
Definition PackedList.H:708
block_type & ref_
Reference to the block.
Definition PackedList.H:703
label first_block() const
void swap(PackedList< Width > &rhs)
Swap contents with argument.
void checkIndex(const label i) const
block_container blocks_
Definition PackedList.H:224
bool empty() const noexcept
Definition PackedList.H:387
void resize(const label numElem, const unsigned int val=0u)
static constexpr block_type max_value
Definition PackedList.H:182
void operator=(const PackedList< Width > &list)
Copy assignment.
bool set(const label i, unsigned int val=~0u)
Set value at index i, default value set is the max_value.
unsigned int get(const label i) const
constexpr PackedList() noexcept
static constexpr unsigned elem_per_block
Definition PackedList.H:174
void transfer(PackedList< Width > &rhs)
Transfer the contents of the argument list into this list and annul the argument list.
static constexpr label num_blocks(label numElem) noexcept
Definition PackedList.H:196
label size() const noexcept
Definition PackedList.H:392
label first_not_block() const
A 1D vector of objects of type <T>, where the size of the vector is known and can be used for subscri...
Definition UList.H:89
iterator begin() noexcept
Return an iterator to begin traversing the UList.
Definition UListI.H:410
iterator end() noexcept
Return an iterator to end traversing the UList.
Definition UListI.H:454
T * data() noexcept
Return pointer to the underlying array serving as data storage.
Definition UListI.H:274
Pointer management similar to std::unique_ptr, with some additional methods and type checking.
Definition autoPtr.H:65
static autoPtr< T > New(Args &&... args)
Construct autoPtr with forwarding arguments.
Definition autoPtr.H:178
A const_iterator for iterating across on values.
Definition bitSet.H:695
label operator*() const noexcept
Return the current on position.
Definition bitSetI.H:193
const_iterator & operator++()
Move to the next on position.
Definition bitSetI.H:199
A reference supporting read/write access to an entry.
Definition bitSet.H:643
reference(bitSet *parent, const label index)
Construct by taking reference of block from within the list and the specified index.
Definition bitSetI.H:125
void flip()
Flip the bit at the position, no range-checking.
Definition bitSetI.H:134
friend class bitSet
Definition bitSet.H:646
A bitSet stores bits (elements with only two states) in packed internal format and supports a variety...
Definition bitSet.H:61
unsigned int count(const bool on=true) const
Count number of bits set.
Definition bitSetI.H:420
void fill(const bool val)
Assign all entries to the given value.
Definition bitSetI.H:474
const_iterator cbegin() const
Iterator set to the position of the first on bit.
Definition bitSetI.H:230
void flip()
Invert all bits in the addressable region.
Definition bitSetI.H:546
label find_last() const
Locate the last bit set.
Definition bitSetI.H:321
labelList sortedToc() const
The indices of the on bits as a sorted labelList.
Definition bitSetI.H:441
const_iterator end() const noexcept
Iterator beyond the end of the bitSet.
Definition bitSetI.H:248
void set(const bitSet &bitset)
Set specified bits from another bitset.
Definition bitSetI.H:502
bitSet & operator-=(const bitSet &other)
Remove entries from this list - identical to the unset() method.
Definition bitSetI.H:666
bitSet & operator|=(const bitSet &other)
Bitwise-OR operator - similar to the set() method.
Definition bitSetI.H:654
void transfer(bitSet &bitset)
Transfer the contents of the argument list into this list and annul the argument list.
Definition bitSetI.H:468
bool none() const
True if no bits in this bitset are set.
Definition bitSetI.H:414
autoPtr< bitSet > clone() const
Clone.
Definition bitSetI.H:116
constexpr bitSet() noexcept
Default construct an empty, zero-sized bitSet.
Definition bitSetI.H:23
bool test(const label pos) const
Test for True value at specified position, never auto-vivify entries.
Definition bitSet.H:334
bitSet & bound(const label maxSize)
Ensure the addressable range does not exceed maxSize.
Definition bitSetI.H:570
label find_first_not() const
Locate the first bit that is unset.
Definition bitSetI.H:289
bitSet & xorEq(const bitSet &other)
The set logical XOR.
Definition bitSet.C:171
bool test_set(const label i, const bool val=true)
Test for True value at specified position and change the value at that position.
Definition bitSetI.H:494
bool all() const
True if all bits in this bitset are set or if the set is empty.
Definition bitSetI.H:401
labelList toc() const
The indices of the on bits as a sorted labelList.
Definition bitSet.C:476
const_iterator begin() const
Iterator set to the position of the first on bit.
Definition bitSetI.H:224
void assign(const UList< bool > &bools)
Copy assign all entries from a list of bools.
Definition bitSet.C:271
bitSet & operator&=(const bitSet &other)
Bitwise-AND all the bits in other with the bits in this bitset.
Definition bitSetI.H:648
bool operator()(const label pos) const
Test value at specified position, same as test().
Definition bitSetI.H:606
void resize_last()
Resize to include the last on bit only.
Definition bitSetI.H:447
bitSet & andEq(const bitSet &other)
The set logical AND.
Definition bitSet.C:74
bitSet & orEq(const bitSet &other)
The set logical OR.
Definition bitSet.C:126
bitSet & operator^=(const bitSet &other)
Bitwise-XOR operator - retains unique entries.
Definition bitSetI.H:660
bitSet & unset(const bitSet &other)
Unset (subtract) the bits specified in the other bitset, which is a set difference corresponds to the...
Definition bitSetI.H:540
void swap(bitSet &bitset)
Swap contents.
Definition bitSetI.H:462
label setMany(InputIter first, InputIter last)
Set the locations listed by the iterator range, auto-vivify entries if needed.
const_iterator cend() const noexcept
Iterator beyond the end of the bitSet.
Definition bitSetI.H:254
label find_next(label pos) const
Locate the next bit set, starting one beyond the specified position.
Definition bitSetI.H:347
bitSet & minusEq(const bitSet &other)
The set difference.
Definition bitSet.C:39
bool any() const
True if any bits in this bitset are set.
Definition bitSetI.H:408
bitSet & extend(const label minSize)
Ensure that minSize is covered by the bitSet.
Definition bitSetI.H:587
unsigned int operator[](const label i) const
Identical to get() - get value at index.
Definition bitSetI.H:612
label find_first() const
Locate the first bit that is set.
Definition bitSetI.H:260
bitSet & operator=(const bitSet &bitset)
Copy assignment.
Definition bitSetI.H:627
unsigned int bit_count(UIntType x)
Count arbitrary number of bits (of an integral type).
Definition BitOps.H:242
tmp< faMatrix< Type > > operator-(const faMatrix< Type > &)
Unary negation.
bitSet operator~(const bitSet &bitset)
Bitset complement, returns a copy of the bitset with all its bits flipped.
Definition bitSetI.H:674
dimensionedScalar pos(const dimensionedScalar &ds)
bitSet operator|(const bitSet &a, const bitSet &b)
Bitwise-OR of two bitsets.
Definition bitSetI.H:692
List< label > labelList
A List of labels.
Definition List.H:62
bitSet operator^(const bitSet &a, const bitSet &b)
Bitwise-XOR of two bitsets to form a unique bit-set.
Definition bitSetI.H:702
tmp< GeometricField< Type, faPatchField, areaMesh > > operator&(const faMatrix< Type > &, const DimensionedField< Type, areaMesh > &)
const direction noexcept
Definition scalarImpl.H:265
UList< label > labelUList
A UList of labels.
Definition UList.H:75
volScalarField & b