Commit c343f44c authored by xep's avatar xep
Browse files

2021.6.8 modify BitSet

parent 91e3afc2
......@@ -10,6 +10,7 @@
class BitSet {
public:
using buffer_type = uint8_t;
static_assert(std::is_unsigned<buffer_type>::value, "unsigned buffer type must be used!!");
BitSet(int size_);
BitSet(const BitSet& bs);
......@@ -19,7 +20,9 @@ public:
BitSet& operator=(const BitSet& bs) = delete;
BitSet& operator=(BitSet&& bs)noexcept = delete;
bool test(int i)const;
inline bool test(int i)const {
return buffer[get_buffer_pos(i)] & get_bit_one_buffer(i);
}
inline bool operator[](int i)const {
//note: not assignmentable
return test(i);
......@@ -77,11 +80,6 @@ BitSet::~BitSet()
}
}
bool BitSet::test(int i) const
{
return buffer[get_buffer_pos(i)] & get_bit_one_buffer(i);
}
BitSet& BitSet::set(int i)
{
buffer[get_buffer_pos(i)] |= get_bit_one_buffer(i);
......@@ -153,13 +151,16 @@ constexpr typename BitSet::buffer_type BitSet::get_bit_zero_buffer(int i)
return ~get_bit_one_buffer(i);
}
class BitMatrix :private BitSet {
const int row, col;
public:
BitMatrix(int row_, int col_):row(row_),col(col_),BitSet(row_*col_){}
BitMatrix(const BitMatrix& bm):row(bm.row),col(bm.col),BitSet(bm){}
BitMatrix(BitMatrix&& bm)noexcept:row(bm.row),col(bm.col),BitSet(bm){}
~BitMatrix() = default;
BitMatrix& operator=(const BitMatrix&) = delete;
BitMatrix& operator=(BitMatrix&&) = delete;
inline bool test(int i,int j)const {
return BitSet::test(get_index(i, j));
......
......@@ -2,6 +2,7 @@
#include <utility>
#include <stdint.h>
#include <limits>
#include <type_traits>
/// <summary>
......@@ -10,7 +11,8 @@
class BitSet {
public:
using buffer_type = uint8_t;
static_assert(std::is_unsigned<buffer_type>::value, "unsigned buffer type must be used!!");
BitSet(int size_);
BitSet(const BitSet& bs);
BitSet(BitSet&& bs)noexcept;
......@@ -18,8 +20,10 @@ public:
BitSet& operator=(const BitSet& bs) = delete;
BitSet& operator=(BitSet&& bs)noexcept = delete;
bool test(int i)const;
inline bool test(int i)const {
return buffer[get_buffer_pos(i)] & get_bit_one_buffer(i);
}
inline bool operator[](int i)const {
//note: not assignmentable
return test(i);
......@@ -46,7 +50,7 @@ private:
static constexpr buffer_type
BUF_ONE = std::numeric_limits<buffer_type>::max(),
BUF_ZERO = std::numeric_limits<buffer_type>::min();
static constexpr int get_buffer_pos(int i);
static constexpr buffer_type get_bit_one_buffer(int i);
static constexpr buffer_type get_bit_zero_buffer(int i);
......@@ -57,14 +61,14 @@ BitSet::BitSet(int size_) :size(size_), bufsize((size_ - 1) / buf_bits + 1)
buffer = new buffer_type[bufsize];
}
BitSet::BitSet(const BitSet& bs):size(bs.size),bufsize(bs.bufsize)
BitSet::BitSet(const BitSet& bs) : size(bs.size), bufsize(bs.bufsize)
{
buffer = new buffer_type[bufsize];
std::copy(bs.buffer, bs.buffer + bufsize, buffer);
}
BitSet::BitSet(BitSet&& bs) noexcept:
size(bs.size),bufsize(bs.bufsize),buffer(bs.buffer)
BitSet::BitSet(BitSet&& bs) noexcept :
size(bs.size), bufsize(bs.bufsize), buffer(bs.buffer)
{
bs.buffer = nullptr;
}
......@@ -77,11 +81,6 @@ BitSet::~BitSet()
}
}
bool BitSet::test(int i) const
{
return buffer[get_buffer_pos(i)] & get_bit_one_buffer(i);
}
BitSet& BitSet::set(int i)
{
buffer[get_buffer_pos(i)] |= get_bit_one_buffer(i);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment