This commit is contained in:
mmlhbjk 2024-10-05 14:22:28 +08:00
commit 34b9ec0a18
46 changed files with 1065 additions and 0 deletions

131
Linux/BitSet.hpp Normal file
View File

@ -0,0 +1,131 @@
#pragma once
#include <iostream>
#include <vector>
#include <stdexcept>
namespace Lenyiin
{
class BitSet
{
private:
// 计算某个位属于哪个块,并且在该块中的哪个位置
std::pair<size_t, size_t> getPosition(size_t index) const
{
if (index >= _bitsize)
{
throw std::out_of_range("Index out of range");
}
// 确定映射在哪个 unsigned long 中
size_t block = index / (sizeof(unsigned long) * 8);
// 确定映射在 unsigned long 中的哪一位
size_t offset = index % (sizeof(unsigned long) * 8);
return {block, offset};
}
public:
BitSet(size_t size)
{
_bits.resize(size / sizeof(unsigned long) * 8 + 1, 0);
_bitsize = size;
}
// 置 1
void set(size_t index)
{
auto [block, offset] = getPosition(index);
// 左移右移这里的左右不是方向, 左移是向高位移, 右移是向低位移
// C语言设计的bug, 历史遗留问题, 容易让人误导, 计算机技术发展百花齐放, 再融合统一
_bits[block] |= (1UL << offset); // 第 pos 个位置 1
}
// 置 0
void reset(size_t index)
{
auto [block, offset] = getPosition(index);
_bits[block] &= ~(1UL << offset); // 第 pos 个位置 0
}
// 反转
void flip(size_t index)
{
auto [block, offset] = getPosition(index);
_bits[block] ^= (1UL << offset); // 使用位异或运算反转位
}
// 判断 x 在不在 (也就是说 x 映射的位置是否为 1)
bool test(size_t index) const
{
auto [block, offset] = getPosition(index);
// bool 0 就是 false, 非 0 就是 true
return _bits[block] & (1UL << offset);
}
// 全部置 1
void set()
{
for (auto &block : _bits)
{
block = ~0UL;
}
}
// 全部置 0
void reset()
{
for (auto &block : _bits)
{
block = 0;
}
}
// 全部反转
void flip()
{
for (auto &block : _bits)
{
block = ~block;
}
}
// 获取位数
size_t size() const
{
return _bitsize;
}
// 统计所有位中为 1 的个数
size_t count() const
{
size_t cnt = 0;
for (auto block : _bits)
{
cnt += __builtin_popcountl(block); // 使用 GCC 内置函数统计1的个数
}
return cnt;
}
// 打印内容(调试用途)
void print()
{
for (size_t i = 0; i < _bitsize; i++)
{
std::cout << (test(i) ? '1' : '0');
if ((i + 1) % 8 == 0)
{
std::cout << " ";
}
}
std::cout << std::endl;
}
private:
// int* _bits;
std::vector<unsigned long> _bits;
size_t _bitsize; // 映射存储的多少个数据
};
}

137
Linux/BloomFilter.hpp Normal file
View File

@ -0,0 +1,137 @@
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include "BitSet.hpp"
namespace Lenyiin
{
struct HashStr1
{
// BKDR
size_t operator()(const std::string &str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 131;
hash += str[i];
}
return hash;
}
};
struct HashStr2
{
// SDBM
size_t operator()(const std::string &str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 65599;
hash += str[i];
}
return hash;
}
};
struct HashStr3
{
// RS
size_t operator()(const std::string &str)
{
size_t hash = 0;
size_t magic = 63689; // 魔数
for (size_t i = 0; i < str.size(); i++)
{
hash *= magic;
hash += str[i];
magic *= 378551;
}
return hash;
}
};
// 布隆过滤器底层是个位图
template <class K = std::string, class Hash1 = HashStr1, class Hash2 = HashStr2, class Hash3 = HashStr3>
class BloomFilter
{
public:
BloomFilter(size_t num)
: _bs(5 * num), _size(5 * num)
{
}
// 插入元素
void insert(const K &key)
{
// 将 key 映射到三张位图上
size_t index1 = Hash1()(key) % _size; // Hash1() 是仿函数类型, Hash1()() 是仿函数匿名对象
size_t index2 = Hash2()(key) % _size;
size_t index3 = Hash3()(key) % _size;
// std::cout << index1 << std::endl;
// std::cout << index2 << std::endl;
// std::cout << index3 << std::endl;
_bs.set(index1);
_bs.set(index2);
_bs.set(index3);
}
// 查询元素是否存在
bool contains(const K &key)
{
size_t index1 = Hash1()(key) % _size;
if (_bs.test(index1) == false)
{
return false;
}
size_t index2 = Hash2()(key) % _size;
if (_bs.test(index2) == false)
{
return false;
}
size_t index3 = Hash3()(key) % _size;
if (_bs.test(index3) == false)
{
return false;
}
return true; // 但是这里也不一定是真的在, 还是可能存在误判
// 判断在, 是不准确的, 可能存在误判, 判断不在是准确的
}
// 删除元素
void reset(const K &key)
{
// 将映射的位置给置零就可以?
// 不支持删除, 可能会存在误删。
// 一般布隆过滤器不支持删除操作。
// size_t index1 = Hash1()(key) % _size;
// _bs.reset(index1);
// size_t index2 = Hash2()(key) % _size;
// _bs.reset(index2);
// size_t index3 = Hash3()(key) % _size;
// _bs.reset(index3);
}
// 获取位数组的大小
size_t size() const
{
return _size;
}
private:
BitSet _bs; // 位图
size_t _size;
};
}

74
Linux/Main.cc Normal file
View File

@ -0,0 +1,74 @@
#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;
}

6
Linux/Makefile Normal file
View File

@ -0,0 +1,6 @@
main:Main.cc
g++ -o $@ $^
.PHONY:clean
clean:
rm -f main

BIN
Linux/main Normal file

Binary file not shown.

View File

@ -0,0 +1,83 @@
{
"Version": 1,
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:BloomFilter.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:Main.cc||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:BitSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 267,
"SelectedChildIndex": 4,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:128:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "Main.cc",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc",
"RelativeDocumentMoniker": "Main.cc",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc",
"RelativeToolTip": "Main.cc",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-10-05T06:20:16.153Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "BloomFilter.hpp",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp",
"RelativeDocumentMoniker": "BloomFilter.hpp",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp",
"RelativeToolTip": "BloomFilter.hpp",
"ViewState": "AgIAAHUAAAAAAAAAAAAswIgAAAAWAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-10-05T06:20:06.324Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "BitSet.hpp",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp",
"RelativeDocumentMoniker": "BitSet.hpp",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp",
"RelativeToolTip": "BitSet.hpp",
"ViewState": "AgIAAGQAAAAAAAAAAAAswHcAAAABAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-10-05T06:19:52.34Z",
"EditorCaption": ""
}
]
}
]
}
]
}

View File

@ -0,0 +1,83 @@
{
"Version": 1,
"WorkspaceRootPath": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\",
"Documents": [
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:BloomFilter.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:Main.cc||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
},
{
"AbsoluteMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}",
"RelativeMoniker": "D:0:0:{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}|Windows_BitSet_BloomFilter.vcxproj|solutionrelative:BitSet.hpp||{D0E1A5C6-B359-4E41-9B60-3365922C2A22}"
}
],
"DocumentGroupContainers": [
{
"Orientation": 0,
"VerticalTabListWidth": 256,
"DocumentGroups": [
{
"DockedWidth": 267,
"SelectedChildIndex": 4,
"Children": [
{
"$type": "Bookmark",
"Name": "ST:128:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:129:0:{116d2292-e37d-41cd-a077-ebacac4c8cc4}"
},
{
"$type": "Bookmark",
"Name": "ST:128:0:{1fc202d4-d401-403c-9834-5b218574bb67}"
},
{
"$type": "Document",
"DocumentIndex": 1,
"Title": "Main.cc",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc",
"RelativeDocumentMoniker": "Main.cc",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\Main.cc",
"RelativeToolTip": "Main.cc",
"ViewState": "AgIAAAAAAAAAAAAAAAAAAAEAAAAJAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000677|",
"WhenOpened": "2024-10-05T06:20:16.153Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 0,
"Title": "BloomFilter.hpp",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp",
"RelativeDocumentMoniker": "BloomFilter.hpp",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BloomFilter.hpp",
"RelativeToolTip": "BloomFilter.hpp",
"ViewState": "AgIAAHUAAAAAAAAAAAAswIgAAAAWAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-10-05T06:20:06.324Z",
"EditorCaption": ""
},
{
"$type": "Document",
"DocumentIndex": 2,
"Title": "BitSet.hpp",
"DocumentMoniker": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp",
"RelativeDocumentMoniker": "BitSet.hpp",
"ToolTip": "E:\\Git \u4ED3\u5E93\\\u516C\u5F00\u4ED3\u5E93\\11_BitSet\\Windows_BitSet_BloomFilter\\BitSet.hpp",
"RelativeToolTip": "BitSet.hpp",
"ViewState": "AgIAAGQAAAAAAAAAAAAswHcAAAABAAAAAAAAAA==",
"Icon": "ae27a6b0-e345-4288-96df-5eaf394ee369.000680|",
"WhenOpened": "2024-10-05T06:19:52.34Z",
"EditorCaption": ""
}
]
}
]
}
]
}

View File

@ -0,0 +1,120 @@
#pragma once
#include <iostream>
#include <vector>
#include <stdexcept>
namespace Lenyiin
{
class BitSet
{
private:
// 计算某个位属于哪个块,并且在该块中的哪个位置
std::pair<size_t, size_t> getPosition(size_t index) const
{
if (index >= _bitsize)
{
throw std::out_of_range("Index out of range");
}
// 确定映射在哪个 unsigned long 中
size_t block = index / (sizeof(unsigned long) * 8);
// 确定映射在 unsigned long 中的哪一位
size_t offset = index % (sizeof(unsigned long) * 8);
return { block, offset };
}
public:
BitSet(size_t size)
{
_bits.resize(size / sizeof(unsigned long) * 8 + 1, 0);
_bitsize = size;
}
// 置 1
void set(size_t index)
{
std::pair<size_t, size_t> pr = getPosition(index);
// 左移右移这里的左右不是方向, 左移是向高位移, 右移是向低位移
// C语言设计的bug, 历史遗留问题, 容易让人误导, 计算机技术发展百花齐放, 再融合统一
_bits[pr.first] |= (1UL << pr.second); // 第 pos 个位置 1
}
// 置 0
void reset(size_t index)
{
std::pair<size_t, size_t> pr = getPosition(index);
_bits[pr.first] &= ~(1UL << pr.second); // 第 pos 个位置 0
}
// 反转
void flip(size_t index)
{
std::pair<size_t, size_t> pr = getPosition(index);
_bits[pr.first] ^= (1UL << pr.second); // 使用位异或运算反转位
}
// 判断 x 在不在 (也就是说 x 映射的位置是否为 1)
bool test(size_t index) const
{
std::pair<size_t, size_t> pr = getPosition(index);
// bool 0 就是 false, 非 0 就是 true
return _bits[pr.first] & (1UL << pr.second);
}
// 全部置 1
void set()
{
for (auto& block : _bits)
{
block = ~0UL;
}
}
// 全部置 0
void reset()
{
for (auto& block : _bits)
{
block = 0;
}
}
// 全部反转
void flip()
{
for (auto& block : _bits)
{
block = ~block;
}
}
// 获取位数
size_t size() const
{
return _bitsize;
}
// 打印内容(调试用途)
void print()
{
for (size_t i = 0; i < _bitsize; i++)
{
std::cout << (test(i) ? '1' : '0');
if ((i + 1) % 8 == 0)
{
std::cout << " ";
}
}
std::cout << std::endl;
}
private:
// int* _bits;
std::vector<unsigned long> _bits;
size_t _bitsize; // 映射存储的多少个数据
};
}

View File

@ -0,0 +1,137 @@
#pragma once
#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include "BitSet.hpp"
namespace Lenyiin
{
struct HashStr1
{
// BKDR
size_t operator()(const std::string& str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 131;
hash += str[i];
}
return hash;
}
};
struct HashStr2
{
// SDBM
size_t operator()(const std::string& str)
{
size_t hash = 0;
for (size_t i = 0; i < str.size(); i++)
{
hash *= 65599;
hash += str[i];
}
return hash;
}
};
struct HashStr3
{
// RS
size_t operator()(const std::string& str)
{
size_t hash = 0;
size_t magic = 63689; // 魔数
for (size_t i = 0; i < str.size(); i++)
{
hash *= magic;
hash += str[i];
magic *= 378551;
}
return hash;
}
};
// 布隆过滤器底层是个位图
template <class K = std::string, class Hash1 = HashStr1, class Hash2 = HashStr2, class Hash3 = HashStr3>
class BloomFilter
{
public:
BloomFilter(size_t num)
: _bs(5 * num), _size(5 * num)
{
}
// 插入元素
void insert(const K& key)
{
// 将 key 映射到三张位图上
size_t index1 = Hash1()(key) % _size; // Hash1() 是仿函数类型, Hash1()() 是仿函数匿名对象
size_t index2 = Hash2()(key) % _size;
size_t index3 = Hash3()(key) % _size;
// std::cout << index1 << std::endl;
// std::cout << index2 << std::endl;
// std::cout << index3 << std::endl;
_bs.set(index1);
_bs.set(index2);
_bs.set(index3);
}
// 查询元素是否存在
bool contains(const K& key)
{
size_t index1 = Hash1()(key) % _size;
if (_bs.test(index1) == false)
{
return false;
}
size_t index2 = Hash2()(key) % _size;
if (_bs.test(index2) == false)
{
return false;
}
size_t index3 = Hash3()(key) % _size;
if (_bs.test(index3) == false)
{
return false;
}
return true; // 但是这里也不一定是真的在, 还是可能存在误判
// 判断在, 是不准确的, 可能存在误判, 判断不在是准确的
}
// 删除元素
void reset(const K& key)
{
// 将映射的位置给置零就可以?
// 不支持删除, 可能会存在误删。
// 一般布隆过滤器不支持删除操作。
// size_t index1 = Hash1()(key) % _size;
// _bs.reset(index1);
// size_t index2 = Hash2()(key) % _size;
// _bs.reset(index2);
// size_t index3 = Hash3()(key) % _size;
// _bs.reset(index3);
}
// 获取位数组的大小
size_t size() const
{
return _size;
}
private:
BitSet _bs; // 位图
size_t _size;
};
} // namespace Lenyiin

View File

@ -0,0 +1,72 @@
#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 的状态
}
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;
}

View File

@ -0,0 +1 @@
E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\Main.cc;E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\Windows_.84d1b6ed\x64\Debug\Main.obj

View File

@ -0,0 +1,2 @@
PlatformToolSet=v143:VCToolArchitecture=Native64Bit:VCToolsVersion=14.41.34120:TargetPlatformVersion=10.0.22000.0:
Debug|x64|E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\|

View File

@ -0,0 +1,2 @@
^E:\GIT 仓库\公开仓库\11_BITSET\WINDOWS_BITSET_BLOOMFILTER\WINDOWS_.84D1B6ED\X64\DEBUG\MAIN.OBJ
E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\Windows_.84d1b6ed\x64\Debug\Windows_BitSet_BloomFilter.ilk

View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<ProjectOutputs>
<ProjectOutput>
<FullPath>E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\x64\Debug\Windows_BitSet_BloomFilter.exe</FullPath>
</ProjectOutput>
</ProjectOutputs>
<ContentFiles />
<SatelliteDlls />
<NonRecipeFileRefs />
</Project>

View File

@ -0,0 +1,2 @@
 Main.cc
Windows_BitSet_BloomFilter.vcxproj -> E:\Git 仓库\公开仓库\11_BitSet\Windows_BitSet_BloomFilter\x64\Debug\Windows_BitSet_BloomFilter.exe

View File

@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35312.102
MinimumVisualStudioVersion = 10.0.40219.1
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Windows_BitSet_BloomFilter", "Windows_BitSet_BloomFilter.vcxproj", "{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Debug|x64.ActiveCfg = Debug|x64
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Debug|x64.Build.0 = Debug|x64
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Debug|x86.ActiveCfg = Debug|Win32
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Debug|x86.Build.0 = Debug|Win32
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Release|x64.ActiveCfg = Release|x64
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Release|x64.Build.0 = Release|x64
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Release|x86.ActiveCfg = Release|Win32
{84D1B6ED-7C9D-4DEB-B47B-0B3BB42CCA04}.Release|x86.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0EDD7A33-BF7F-4176-9C9B-D509285B54B2}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,139 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{84d1b6ed-7c9d-4deb-b47b-0b3bb42cca04}</ProjectGuid>
<RootNamespace>WindowsBitSetBloomFilter</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="BitSet.hpp" />
<ClInclude Include="BloomFilter.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cc" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="BitSet.hpp">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="BloomFilter.hpp">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="Main.cc">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>

View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup />
</Project>