From eba9f4e1a6c347eba767c79ba0db4b1e6169fa56 Mon Sep 17 00:00:00 2001 From: William Silversmith Date: Sat, 9 Jan 2021 01:10:33 -0500 Subject: [PATCH] wip: trying to make a BitArray class --- cc3d.hpp | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/cc3d.hpp b/cc3d.hpp index e613976..3c3e746 100644 --- a/cc3d.hpp +++ b/cc3d.hpp @@ -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(array[bkt]); + uint8_t bit_idx = static_cast(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(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.