74 lines
2.4 KiB
C++
74 lines
2.4 KiB
C++
![]() |
#include "BitSet.hpp"
|
||
|
#include "BloomFilter.hpp"
|
||
|
#include <bitset>
|
||
|
#include <chrono>
|
||
|
#include <random>
|
||
|
#include <unordered_set>
|
||
|
|
||
|
using namespace Lenyiin;
|
||
|
|
||
|
void test_bitset()
|
||
|
{
|
||
|
std::bitset<8> bs("11001010"); // 使用二进制字符串初始化
|
||
|
std::cout << "Bitset:\t\t\t" << bs << std::endl; // 输出: 11001010
|
||
|
|
||
|
bs.set(2); // 将第 2 位置为 1
|
||
|
std::cout << "After setting bit 2:\t" << bs << std::endl; // 11001110
|
||
|
|
||
|
bs.reset(3); // 将第 3 位清除为 0
|
||
|
std::cout << "After resetting bit 3:\t" << bs << std::endl; // 11000110
|
||
|
|
||
|
bs.flip(1); // 翻转第 1 位
|
||
|
std::cout << "After flipping bit 1:\t" << bs << std::endl; // 11000100
|
||
|
|
||
|
std::cout << "Bit 4 is:\t\t" << bs.test(4) << std::endl; // 检查第 4 位,结果为 0
|
||
|
std::cout << "count():\t\t" << bs.count() << std::endl; // 返回 1 的数量,结果为 3
|
||
|
std::cout << "size():\t\t\t" << bs.size() << std::endl; // 返回集合长度,结果为 8
|
||
|
}
|
||
|
|
||
|
void test_BitSet()
|
||
|
{
|
||
|
// BitSet bs(-1); // -1 就能开整型的最大值个空间
|
||
|
// BitSet bs(pow());
|
||
|
// BitSet bs(0xffffffff);// 这三个都可以表示最大值
|
||
|
|
||
|
BitSet bset(32); // 创建一个包含 32 个位的bitset
|
||
|
bset.set(1); // 将第 1 位设为 1
|
||
|
bset.set(5); // 将第 5 位设为 1
|
||
|
bset.flip(6); // 反转第 6 位
|
||
|
bset.print(); // 打印 bitset 的状态
|
||
|
|
||
|
std::cout << "Count of 1s: " << bset.count() << std::endl; // 输出1的个数
|
||
|
}
|
||
|
|
||
|
void test_BloomFilter()
|
||
|
{
|
||
|
BloomFilter<std::string> bf(100);
|
||
|
|
||
|
bf.insert("set");
|
||
|
bf.insert("map");
|
||
|
bf.insert("hash");
|
||
|
bf.insert("unordered_map");
|
||
|
bf.insert("unordered_set");
|
||
|
|
||
|
std::cout << bf.contains("set") << std::endl;
|
||
|
std::cout << bf.contains("map") << std::endl;
|
||
|
std::cout << bf.contains("hash") << std::endl;
|
||
|
std::cout << bf.contains("unordered_map") << std::endl;
|
||
|
std::cout << bf.contains("unordered_set") << std::endl;
|
||
|
std::cout << bf.contains("vector") << std::endl;
|
||
|
std::cout << bf.contains("string") << std::endl;
|
||
|
std::cout << bf.contains("list") << std::endl;
|
||
|
std::cout << bf.contains("deque") << std::endl;
|
||
|
std::cout << bf.contains("stack") << std::endl;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
// test_bitset();
|
||
|
|
||
|
// test_BitSet();
|
||
|
test_BloomFilter();
|
||
|
|
||
|
return 0;
|
||
|
}
|