引入位图
This commit is contained in:
@@ -8,6 +8,8 @@ add_library(DataStruct
|
|||||||
src/HeapSort.cpp
|
src/HeapSort.cpp
|
||||||
include/HeapSort.h
|
include/HeapSort.h
|
||||||
src/mutex/shared_mutex.cpp
|
src/mutex/shared_mutex.cpp
|
||||||
|
src/Bitmap.cpp
|
||||||
|
include/Bitmap.h
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(DataStruct PUBLIC include)
|
target_include_directories(DataStruct PUBLIC include)
|
||||||
|
|||||||
37
include/Bitmap.h
Normal file
37
include/Bitmap.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
// 版权所有 (c) ling 保留所有权利。
|
||||||
|
// 除非另行说明,否则仅允许在DataStruct中使用此文件中的代码。
|
||||||
|
//
|
||||||
|
// 由 ling 创建于 24-9-7.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef DATASTRUCT_BITMAP_H_48
|
||||||
|
#define DATASTRUCT_BITMAP_H_48
|
||||||
|
|
||||||
|
#include <cstddef>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace ling {
|
||||||
|
/**
|
||||||
|
* 位图
|
||||||
|
*/
|
||||||
|
class Bitmap {
|
||||||
|
private:
|
||||||
|
size_t size;
|
||||||
|
int32_t *buf;
|
||||||
|
public:
|
||||||
|
explicit Bitmap(size_t size);
|
||||||
|
|
||||||
|
virtual ~Bitmap();
|
||||||
|
|
||||||
|
bool test(size_t index) const;
|
||||||
|
|
||||||
|
void set(size_t index);
|
||||||
|
|
||||||
|
void clear(size_t index);
|
||||||
|
|
||||||
|
size_t getSize() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // ling
|
||||||
|
|
||||||
|
#endif //DATASTRUCT_BITMAP_H_48
|
||||||
14
main.cpp
14
main.cpp
@@ -6,6 +6,7 @@
|
|||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
#include <mutex/shared_mutex.h>
|
#include <mutex/shared_mutex.h>
|
||||||
|
#include <Bitmap.h>
|
||||||
|
|
||||||
class RedBlackTree : public ling::RedBlackTree<unsigned long long> {
|
class RedBlackTree : public ling::RedBlackTree<unsigned long long> {
|
||||||
protected:
|
protected:
|
||||||
@@ -257,3 +258,16 @@ TEST(红黑树, 搜索测试) {
|
|||||||
ASSERT_FALSE(node == nullptr);
|
ASSERT_FALSE(node == nullptr);
|
||||||
ASSERT_EQ(node->value, 50);
|
ASSERT_EQ(node->value, 50);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(位图, 位图测试) {
|
||||||
|
ling::Bitmap bitmap(100);
|
||||||
|
for (int i = 0; i < bitmap.getSize(); i++)
|
||||||
|
ASSERT_FALSE(bitmap.test(i)) << "i = " << i;
|
||||||
|
bitmap.set(20);
|
||||||
|
ASSERT_TRUE(bitmap.test(20));
|
||||||
|
ASSERT_FALSE(bitmap.test(19));
|
||||||
|
ASSERT_FALSE(bitmap.test(21));
|
||||||
|
bitmap.clear(20);
|
||||||
|
for (int i = 0; i < bitmap.getSize(); i++)
|
||||||
|
ASSERT_FALSE(bitmap.test(i));
|
||||||
|
}
|
||||||
41
src/Bitmap.cpp
Normal file
41
src/Bitmap.cpp
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
// 版权所有 (c) ling 保留所有权利。
|
||||||
|
// 除非另行说明,否则仅允许在DataStruct中使用此文件中的代码。
|
||||||
|
//
|
||||||
|
// 由 ling 创建于 24-9-7.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Bitmap.h"
|
||||||
|
|
||||||
|
#define BYTE_TO_BIT(byte) (sizeof(byte)*8)
|
||||||
|
|
||||||
|
namespace ling {
|
||||||
|
Bitmap::Bitmap(size_t size) {
|
||||||
|
this->size = size;
|
||||||
|
size_t len = size / BYTE_TO_BIT(int32_t) + 1;
|
||||||
|
this->buf = new int32_t[len];
|
||||||
|
memset(this->buf, 0, len * sizeof(int32_t));
|
||||||
|
}
|
||||||
|
|
||||||
|
Bitmap::~Bitmap() {
|
||||||
|
delete[] this->buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Bitmap::test(size_t index) const {
|
||||||
|
return this->buf[index / BYTE_TO_BIT(int32_t)] & (1 << (index % BYTE_TO_BIT(int32_t)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bitmap::set(size_t index) {
|
||||||
|
//不进行溢出检测,调用方有义务保证index在索引范围内
|
||||||
|
this->buf[index / BYTE_TO_BIT(int32_t)] |= (1 << (index % BYTE_TO_BIT(int32_t)));
|
||||||
|
}
|
||||||
|
|
||||||
|
void Bitmap::clear(size_t index) {
|
||||||
|
this->buf[index / BYTE_TO_BIT(int32_t)] &= ~(1 << (index % BYTE_TO_BIT(int32_t)));
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t Bitmap::getSize() const {
|
||||||
|
return this->size;
|
||||||
|
}
|
||||||
|
} // ling
|
||||||
Reference in New Issue
Block a user