Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: trying to make a BitArray class #62

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
43 changes: 43 additions & 0 deletions cc3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,49 @@ class DisjointSet {
// Will be O(n).
};

class BitArray {
public:
size_t length;
char* array;

BitArray(size_t N) {
length = N;
array = new char[(N + 4) / 8]();
}

BitArray(char* bytes, size_t N) {
length = N;
array = bytes;
}

~BitArray() {
delete[] array;
}

bool operator[](size_t i) const {
if (i > N || i < 0) {
throw std::runtime_error("Index out of range.");
}
size_t bkt = i >> 3;
uint8_t bits = reinterpret_cast<uint8_t>(array[bkt]);
uint8_t bit_idx = static_cast<uint8_t>(i - (bkt << 3));
return (bits >> bit_idx) & 0x1;
}

void set(size_t i, bool val) {
if (i > N || i < 0) {
throw std::runtime_error("Index out of range.");
}
size_t bkt = i >> 3;
uint8_t bit_idx = static_cast<uint8_t>(i - (bkt << 3));
char field = (1 << bit_idx);
if (!val) {
field = ~field;
}
array[bkt] = array[bkt] & field;
}
}

// This is the original Wu et al decision tree but without
// any copy operations, only union find. We can decompose the problem
// into the z - 1 problem unified with the original 2D algorithm.
Expand Down