MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
mtcore::Bitset2D Struct Reference

Represents a bitset with dynamically allocated memory (using an mtcore allocator) Allows operating on an arbitrary number of bits. More...

#include <bitset.hpp>

Collaboration diagram for mtcore::Bitset2D:

Classes

struct  BitsetAccess
 Helper for accessing a bitset position (including for setting bits) More...
 
struct  SetBitIter
 Simple bit set iterator that uses next() iteration to return the indices of all set bits. More...
 

Public Member Functions

Result< void, AllocationErrorinit (mtcore::Allocator &allocator, const size_t height, const size_t width)
 Initializes a bitset to have a total number of bits All bits will be set to zero.
 
Result< void, BitStrInitErrorinit (mtcore::Allocator &allocator, const Slice< const Slice< const char > > binaryStr)
 Initializes a bitset from a binary string of 1's and 0's.
 
Result< void, BitStrInitErrorinit (mtcore::Allocator &allocator, const Slice< Slice< const char > > binaryStr)
 Initializes a bitset from a binary string of 1's and 0's.
 
Result< void, BitStrInitErrorinit (mtcore::Allocator &allocator, const size_t height, const size_t width, const Slice< Slice< const char > > binaryStr)
 Initializes a bitset of a given size from a string Any bits not set by the string will be zero.
 
Result< void, BitStrInitErrorinit (mtcore::Allocator &allocator, const size_t height, const size_t width, const Slice< const Slice< const char > > binaryStr)
 Initializes a bitset of a given size from a string Any bits not set by the string will be zero.
 
void deinit (mtcore::Allocator &allocator)
 Deinitialies a bitset.
 
bool operator[] (std::tuple< size_t, size_t > index) const
 Access an individual bit at a specific position.
 
BitsetAccess operator[] (std::tuple< size_t, size_t > index)
 Access an individual bit at a specific position.
 
std::tuple< size_t, size_t > size () const
 Gets the size of the bitset.
 
bool at (size_t row, size_t col) const
 Reads a bit at a specific position.
 
Result< Bitset2D, AllocationErrorclone (mtcore::Allocator &allocator)
 Clones a bitset using a specific allocator.
 
Result< Bitset2D, AllocationErrorclone (mtcore::Allocator &allocator, const size_t newHeight, const size_t newWidth)
 Clones and resizes a bitset using a specific allocator.
 
Bitset2Dset (size_t row, size_t col, bool val)
 Sets a bit at a position to a specific value.
 
Bitset2Dtoggle (size_t row, size_t col)
 Toggles a bit at a specific position.
 
Optional< std::tuple< size_t, size_t > > first_set () const
 Returns the position of the first set bit (if one is set) Otherwise, returns a null Optional.
 
size_t pop_count () const
 Counts the number of set bits in the bitset.
 
SetBitIter set_bits () const
 Iterates over the indices of all set bits in the bitset.
 
Bitset2Doperator&= (const Bitset2D &other)
 
Bitset2Doperator|= (const Bitset2D &other)
 
Bitset2Doperator^= (const Bitset2D &other)
 
void set_all (bool value)
 Sets all bits to a specific value.
 

Public Attributes

Bitset underlying
 
size_t width
 
size_t height
 

Detailed Description

Represents a bitset with dynamically allocated memory (using an mtcore allocator) Allows operating on an arbitrary number of bits.

Definition at line 477 of file bitset.hpp.

Member Function Documentation

◆ at()

bool mtcore::Bitset2D::at ( size_t row,
size_t col ) const
inlinenodiscard

Reads a bit at a specific position.

Parameters
rowBit row index to read
colBit col index to read
Returns
whether it is set or not

Definition at line 647 of file bitset.hpp.

647 {
648 ensure(row < height, "OUT OF BOUNDS ACCESS, INVALID ROW");
649 ensure(col < width, "OUT OF BOUNDS ACCESS, INVALID COL");
650 const auto pos = row * width + col;
651 return underlying.at(pos);
652 }
#define ensure(check,...)
Ensures that a check holds true, aborts the program if not true Will print error if the condition is ...
Bitset underlying
Definition bitset.hpp:478
Here is the caller graph for this function:

◆ clone() [1/2]

Result< Bitset2D, AllocationError > mtcore::Bitset2D::clone ( mtcore::Allocator & allocator)
inline

Clones a bitset using a specific allocator.

Parameters
allocatorAllocator to use for cloning
Returns
Bitset on success, or error on failure

Definition at line 659 of file bitset.hpp.

659 {
660 auto copyRes = underlying.clone(allocator);
661 if (copyRes.is_success()) {
662 return copyRes.error();
663 }
664 auto cpy = *this;
665 cpy.underlying = *copyRes;
666 return cpy;
667 }

◆ clone() [2/2]

Result< Bitset2D, AllocationError > mtcore::Bitset2D::clone ( mtcore::Allocator & allocator,
const size_t newHeight,
const size_t newWidth )
inline

Clones and resizes a bitset using a specific allocator.

Parameters
allocatorAllocator to use for cloning
newHeightNew 2d array height
newWidthNew 2d array width
Returns
Bitset on success, or error on failure

Definition at line 676 of file bitset.hpp.

677 {
678 Bitset2D res;
679 auto initRes = res.init(allocator, newHeight, newWidth);
680 if (initRes.is_error()) {
681 return initRes.error();
682 }
683 for (size_t row = 0; row < newHeight && row < height; ++row) {
684 for (size_t col = 0; col < newWidth && col < width; ++col) {
685 res[{row, col}] = at(row, col);
686 }
687 }
688 return res;
689 }
bool at(size_t row, size_t col) const
Reads a bit at a specific position.
Definition bitset.hpp:647
Here is the call graph for this function:

◆ deinit()

void mtcore::Bitset2D::deinit ( mtcore::Allocator & allocator)
inline

Deinitialies a bitset.

Parameters
allocatorAllocator to use for deinitialization

Definition at line 589 of file bitset.hpp.

589 {
590 underlying.deinit(allocator);
591 width = 0;
592 height = 0;
593 }

◆ first_set()

Optional< std::tuple< size_t, size_t > > mtcore::Bitset2D::first_set ( ) const
inlinenodiscard

Returns the position of the first set bit (if one is set) Otherwise, returns a null Optional.

Returns
Optional with first set bit position

Definition at line 724 of file bitset.hpp.

724 {
725 auto index = underlying.first_set();
726 if (index.empty()) {
727 return nullopt;
728 }
729 size_t pos = *index;
730 size_t row = pos / width;
731 size_t col = pos % width;
732 return std::tuple{row, col};
733 }
constexpr auto nullopt
Placeholder value for an empty Optional.
Definition optional.hpp:409

◆ init() [1/5]

Result< void, AllocationError > mtcore::Bitset2D::init ( mtcore::Allocator & allocator,
const size_t height,
const size_t width )
inline

Initializes a bitset to have a total number of bits All bits will be set to zero.

Parameters
allocatorAllocator to use for memory allocation
height2d array height
width2d array width

Definition at line 489 of file bitset.hpp.

489 {
490 ensure(width, "INVALID WIDTH");
491 ensure(height, "INVALID HEIGHT");
492 auto baseRes = underlying.init(allocator, width * height);
493 if (baseRes.is_error()) {
494 return baseRes;
495 }
496
497 ensure(underlying.size() == width * height, "BAD UNDERLYING SIZE!");
498 this->width = width;
499 this->height = height;
500 return success();
501 }
Success< void > success()
Creates a successful void Result object.
Definition result.hpp:398
Here is the call graph for this function:
Here is the caller graph for this function:

◆ init() [2/5]

Result< void, BitStrInitError > mtcore::Bitset2D::init ( mtcore::Allocator & allocator,
const size_t height,
const size_t width,
const Slice< const Slice< const char > > binaryStr )
inline

Initializes a bitset of a given size from a string Any bits not set by the string will be zero.

Parameters
allocatorAllocator to use for memory allocation
height2D array height
width2D array width
binaryStrBinary string to use for initializing bits

Definition at line 547 of file bitset.hpp.

548 {
549 {
550 Slice<const char> curRow;
551 auto rowIter = binaryStr.iter();
552 while (rowIter.next().copy_if_present(curRow)) {
553 char curCell = '\0';
554 auto cellIter = curRow.iter();
555 while (cellIter.next().copy_if_present(curCell)) {
556 if (curCell != '1' && curCell != '0') {
558 }
559 }
560 }
561 }
562
563 if (const auto baseRes = init(allocator, height, width); baseRes.is_error()) {
565 }
566
567 Slice<const char> curRow;
568 auto rowIter = binaryStr.iter();
569 size_t row = 0;
570 while (rowIter.next().copy_if_present(curRow)) {
571 mtdefer { ++row; };
572 char curCell = '\0';
573 auto cellIter = curRow.iter();
574 size_t col = 0;
575 while (cellIter.next().copy_if_present(curCell)) {
576 mtdefer { ++col; };
577 if (curCell == '1') {
578 set(row, col, true);
579 }
580 }
581 }
582 return success();
583 }
#define mtdefer
Defer statement that will mtdefer execution until the scope is left, at which point the code will run...
Error< Underlying > error(Underlying err)
Creates an error.
Definition result.hpp:425
Result< void, AllocationError > init(mtcore::Allocator &allocator, const size_t height, const size_t width)
Initializes a bitset to have a total number of bits All bits will be set to zero.
Definition bitset.hpp:489
Bitset2D & set(size_t row, size_t col, bool val)
Sets a bit at a position to a specific value.
Definition bitset.hpp:697
Here is the call graph for this function:

◆ init() [3/5]

Result< void, BitStrInitError > mtcore::Bitset2D::init ( mtcore::Allocator & allocator,
const size_t height,
const size_t width,
const Slice< Slice< const char > > binaryStr )
inline

Initializes a bitset of a given size from a string Any bits not set by the string will be zero.

Parameters
allocatorAllocator to use for memory allocation
height2D array height
width2D array width
binaryStrBinary string to use for initializing bits

Definition at line 534 of file bitset.hpp.

535 {
536 return init(allocator, height, width, binaryStr.to_const());
537 }
Here is the call graph for this function:

◆ init() [4/5]

Result< void, BitStrInitError > mtcore::Bitset2D::init ( mtcore::Allocator & allocator,
const Slice< const Slice< const char > > binaryStr )
inline

Initializes a bitset from a binary string of 1's and 0's.

Parameters
allocatorAllocator to use for memory allocation
binaryStrBinary string to use for initializing bits

Definition at line 508 of file bitset.hpp.

508 {
509 ensure(binaryStr.head && binaryStr.len && binaryStr[0].head && binaryStr[0].len, "INVALID BINARY STRING");
510 const size_t max_width = binaryStr[0].len;
511 for (size_t i = 1; i < binaryStr.len; ++i) {
512 ensure(binaryStr[i].head && binaryStr[i].len, "INVALID BINARY STRING ROW");
513 }
514 return init(allocator, binaryStr.len, max_width, binaryStr);
515 }
Here is the call graph for this function:

◆ init() [5/5]

Result< void, BitStrInitError > mtcore::Bitset2D::init ( mtcore::Allocator & allocator,
const Slice< Slice< const char > > binaryStr )
inline

Initializes a bitset from a binary string of 1's and 0's.

Parameters
allocatorAllocator to use for memory allocation
binaryStrBinary string to use for initializing bits

Definition at line 522 of file bitset.hpp.

522 {
523 return init(allocator, binaryStr.to_const());
524 }
Here is the call graph for this function:

◆ operator&=()

Bitset2D & mtcore::Bitset2D::operator&= ( const Bitset2D & other)
inline

Definition at line 775 of file bitset.hpp.

775 {
776 size_t row = 0;
777 for (; row < other.height && row < height; ++row) {
778 size_t col = 0;
779 for (; col < other.width && col < width; ++col) {
780 this->operator[]({row, col}) &= other[{row, col}];
781 }
782 for (; col < width; ++col) {
783 set(row, col, false);
784 }
785 }
786 for (; row < height; ++row) {
787 for (size_t col = 0; col < width; ++col) {
788 set(row, col, false);
789 }
790 }
791 return *this;
792 }
bool operator[](std::tuple< size_t, size_t > index) const
Access an individual bit at a specific position.
Definition bitset.hpp:620
Here is the call graph for this function:

◆ operator[]() [1/2]

BitsetAccess mtcore::Bitset2D::operator[] ( std::tuple< size_t, size_t > index)
inlinenodiscard

Access an individual bit at a specific position.

Parameters
indexBit index to access
Returns
Wrapper for reading/setting bit

Definition at line 629 of file bitset.hpp.

629 {
630 ensure(std::get<0>(index) < height, "OUT OF BOUNDS ACCESS, INVALID ROW");
631 ensure(std::get<1>(index) < width, "OUT OF BOUNDS ACCESS, INVALID COL");
632 return {index, *this};
633 }

◆ operator[]() [2/2]

bool mtcore::Bitset2D::operator[] ( std::tuple< size_t, size_t > index) const
inlinenodiscard

Access an individual bit at a specific position.

Parameters
indexBit index to access
Returns
Wrapper for reading/setting bit

Definition at line 620 of file bitset.hpp.

620 {
621 return at(std::get<0>(index), std::get<1>(index));
622 }
Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator^=()

Bitset2D & mtcore::Bitset2D::operator^= ( const Bitset2D & other)
inline

Definition at line 803 of file bitset.hpp.

803 {
804 for (size_t row = 0; row < other.height && row < height; ++row) {
805 for (size_t col = 0; col < other.width && col < width; ++col) {
806 this->operator[]({row, col}) ^= other[{row, col}];
807 }
808 }
809 return *this;
810 }
Here is the call graph for this function:

◆ operator|=()

Bitset2D & mtcore::Bitset2D::operator|= ( const Bitset2D & other)
inline

Definition at line 794 of file bitset.hpp.

794 {
795 for (size_t row = 0; row < other.height && row < height; ++row) {
796 for (size_t col = 0; col < other.width && col < width; ++col) {
797 this->operator[]({row, col}) |= other[{row, col}];
798 }
799 }
800 return *this;
801 }
Here is the call graph for this function:

◆ pop_count()

size_t mtcore::Bitset2D::pop_count ( ) const
inlinenodiscard

Counts the number of set bits in the bitset.

Returns
count of set bits

Definition at line 739 of file bitset.hpp.

739{ return underlying.pop_count(); }

◆ set()

Bitset2D & mtcore::Bitset2D::set ( size_t row,
size_t col,
bool val )
inline

Sets a bit at a position to a specific value.

Parameters
rowBit row to set
colBit col to set
valBit value to set to

Definition at line 697 of file bitset.hpp.

697 {
698 ensure(row < height, "OUT OF BOUNDS ACCESS, INVALID ROW");
699 ensure(col < width, "OUT OF BOUNDS ACCESS, INVALID COL");
700 const auto pos = row * width + col;
701 underlying.set(pos, val);
702 return *this;
703 }
Here is the caller graph for this function:

◆ set_all()

void mtcore::Bitset2D::set_all ( bool value)
inline

Sets all bits to a specific value.

Parameters
valueto set to

Definition at line 816 of file bitset.hpp.

816{ underlying.set_all(value); }

◆ set_bits()

SetBitIter mtcore::Bitset2D::set_bits ( ) const
inlinenodiscard

Iterates over the indices of all set bits in the bitset.

Returns
an iterator over all set bits

Definition at line 773 of file bitset.hpp.

773{ return SetBitIter{*this, underlying.bits[0], 0}; }
Simple bit set iterator that uses next() iteration to return the indices of all set bits.
Definition bitset.hpp:745

◆ size()

std::tuple< size_t, size_t > mtcore::Bitset2D::size ( ) const
inlinenodiscard

Gets the size of the bitset.

Returns
number of bits in bitset (not number of bytes allocated)

Definition at line 639 of file bitset.hpp.

639{ return {height, width}; }

◆ toggle()

Bitset2D & mtcore::Bitset2D::toggle ( size_t row,
size_t col )
inline

Toggles a bit at a specific position.

Parameters
rowBit row to set
colBit col to set
Returns
a reference to the current bitset

Definition at line 711 of file bitset.hpp.

711 {
712 ensure(row < height, "OUT OF BOUNDS ACCESS, INVALID ROW");
713 ensure(col < width, "OUT OF BOUNDS ACCESS, INVALID COL");
714 const auto pos = row * width + col;
715 underlying.toggle(pos);
716 return *this;
717 }

Member Data Documentation

◆ height

size_t mtcore::Bitset2D::height

Definition at line 480 of file bitset.hpp.

◆ underlying

Bitset mtcore::Bitset2D::underlying

Definition at line 478 of file bitset.hpp.

◆ width

size_t mtcore::Bitset2D::width

Definition at line 479 of file bitset.hpp.


The documentation for this struct was generated from the following file: