Commit 273d706b authored by xep's avatar xep
Browse files

2021.6.4 add BitSet

parent c7c5fe1f
......@@ -4,7 +4,6 @@
/// <summary>
/// 单链表实现版本 链式栈
/// </summary>
/// <typeparam name="T"></typeparam>
template <typename T>
class Stack {
public:
......@@ -87,8 +86,6 @@ T& Stack<T>::top()
/// <summary>
/// AVL树标准实现
/// </summary>
/// <typeparam name="K"></typeparam>
/// <typeparam name="V"></typeparam>
template <typename K,typename V>
class AVLTree {
public:
......
#include <iostream>
#include <utility>
#include <stdint.h>
#include <limits>
/// <summary>
/// 具有运行时常量大小的位向量。
/// </summary>
class BitSet {
public:
using buffer_type = uint8_t;
BitSet(int size_);
BitSet(const BitSet& bs);
BitSet(BitSet&& bs)noexcept;
~BitSet();
BitSet& operator=(const BitSet& bs) = delete;
BitSet& operator=(BitSet&& bs)noexcept = delete;
bool test(int i)const;
inline bool operator[](int i)const {
//note: not assignmentable
return test(i);
}
BitSet& set(int i);
BitSet& reset(int i);
BitSet& flip(int i);
BitSet& set();
BitSet& reset();
BitSet& flip();
inline int get_size()const {
return size;
}
void show()const;
private:
const int size, bufsize;
buffer_type* buffer;
static constexpr int buf_bits = 8 * static_cast<int>(sizeof(buffer_type));
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);
};
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)
{
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)
{
bs.buffer = nullptr;
}
BitSet::~BitSet()
{
if (buffer) {
delete[] buffer;
buffer = nullptr;
}
}
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);
return *this;
}
BitSet& BitSet::reset(int i)
{
buffer[get_buffer_pos(i)] &= get_bit_zero_buffer(i);
return *this;
}
BitSet& BitSet::flip(int i)
{
buffer[get_buffer_pos(i)] ^= get_bit_one_buffer(i);
return *this;
}
BitSet& BitSet::set()
{
for (int i = 0; i < bufsize - 1; i++) {
buffer[i] = BUF_ONE;
}
for (int i = (bufsize - 1) * buf_bits; i < size; i++) {
set(i);
}
return *this;
}
BitSet& BitSet::reset()
{
for (int i = 0; i < bufsize - 1; i++) {
buffer[i] = BUF_ZERO;
}
for (int i = (bufsize - 1) * buf_bits; i < size; i++) {
reset(i);
}
return *this;
}
BitSet& BitSet::flip()
{
for (int i = 0; i < bufsize; i++) {
buffer[i] = ~buffer[i];
}
return *this;
}
void BitSet::show() const
{
for (int i = 0; i < size; i++) {
std::cout << (int)test(i);
}
std::cout << std::endl;
}
constexpr int BitSet::get_buffer_pos(int i)
{
return i / buf_bits; //int div
}
constexpr typename BitSet::buffer_type BitSet::get_bit_one_buffer(int i)
{
return static_cast<buffer_type>(1) << (buf_bits - 1 - (i % buf_bits));
}
constexpr typename BitSet::buffer_type BitSet::get_bit_zero_buffer(int i)
{
return ~get_bit_one_buffer(i);
}
\ No newline at end of file
......@@ -7,8 +7,6 @@
/// 具有固定结点数的有向图的邻接多重表实现 或十字链表
/// 仍然采用int在内部表示结点
/// </summary>
/// <typeparam name="V"></typeparam>
/// <typeparam name="E"></typeparam>
template <typename V, typename E>
class CrossGraph {
public:
......
......@@ -7,8 +7,6 @@
/// 有向图的邻接表表示。暂时只考虑插入的情况。
/// 只维护出边而不维护入边。
/// </summary>
/// <typeparam name="V"></typeparam>
/// <typeparam name="E"></typeparam>
template <typename V, typename E>
class Graph {
public:
......
......@@ -3,7 +3,6 @@
/// <summary>
/// 单链表的实现
/// </summary>
/// <typeparam name="T"></typeparam>
template <typename T>
class List {
public:
......
......@@ -4,7 +4,6 @@
/// 固定数组大小的SparseMatrix
/// 采用三元组的标准实现
/// </summary>
/// <typeparam name="T"></typeparam>
template <typename T>
class SparseMatrix {
struct Item {
......
......@@ -2,7 +2,6 @@
/// <summary>
/// 单链表实现版本 链式栈
/// </summary>
/// <typeparam name="T"></typeparam>
template <typename T>
class Stack {
public:
......
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