-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcbf.cpp
More file actions
188 lines (150 loc) · 5 KB
/
Copy pathcbf.cpp
File metadata and controls
188 lines (150 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#pragma once
#include "cbf.h"
#include <sstream>
#include <functional>
#include <vector>
#include <json/json.h>
using namespace std;
// 计数布隆过滤器类
// 计数布隆过滤器类方法实现
CountingBloomFilter::CountingBloomFilter(size_t numCounters, size_t numHashFunctions)
: m(numCounters), k(numHashFunctions), CBF(numCounters, 0) {
generateHashFunctions(k);
}
void CountingBloomFilter::init(size_t k) {
this->k = k;
generateHashFunctions(k);
}
void CountingBloomFilter::construct(const std::string& data) {
// 直接将输入的字符串作为哈希输入
update(data, "add");
}
void CountingBloomFilter::generateHashFunctions(size_t k) {
for (size_t i = 0; i < k; ++i) {
// 显式转换为 std::function
std::function<size_t(const std::string&)> hashFunc = [this, i](const std::string& element) {
return std::hash<std::string>{}(element + to_string(i)) % m;
};
hashFunctions.push_back(hashFunc); // push_back 一个 std::function
}
}
size_t CountingBloomFilter::hash(const std::string& data, size_t index) {
// 基于 string 的哈希计算
return hashFunctions[index](data);
}
void CountingBloomFilter::update(const std::string& data, const std::string& op) {
for (size_t i = 0; i < k; ++i) {
size_t idx = hash(data, i);
if (op == "addw") {
//cout << idx << endl;
CBF[idx] += 1;
} else if (op == "dele") {
if (CBF[idx] > 0) {
CBF[idx] -= 1;
}
}
}
}
bool CountingBloomFilter::check(const std::string& data) {
for (size_t i = 0; i < k; ++i) {
size_t idx = hash(data, i);
if (CBF[idx] == 0) {
return false; // 如果某个计数器为0,则表示该元素不在布隆过滤器中
}
}
return true;
}
bool CountingBloomFilter::repeatCheck(const std::string& data, size_t r) {
for (size_t i = 0; i < k; ++i) {
size_t idx = hash(data, i);
//cout << "count:" << CBF[idx] << endl;
if (CBF[idx] < r) {
return false; // 元素出现次数超过阈值
}
}
return true;
}
Json::Value CountingBloomFilter::toJson() const {
Json::Value jsonArray(Json::arrayValue);
for (int count : CBF) {
jsonArray.append(count); // 将计数器数组转为 JSON 数组
}
return jsonArray;
}
void CountingBloomFilter::fromJson(const Json::Value& json) {
//// 获取计数布隆过滤器的计数器数量和哈希函数数量
//size_t numCounters = json["numCounters"].asUInt();
//size_t numHashFunctions = json["numHashFunctions"].asUInt();
//// 创建一个新的 CountingBloomFilter 对象
//CountingBloomFilter cbf(numCounters, numHashFunctions);
//// 解析计数器数组
//const Json::Value& jsonArray = json["CBF"];
//if (jsonArray.isArray()) {
// // 从 JSON 数组中恢复计数器的值
// for (Json::Value::ArrayIndex i = 0; i < jsonArray.size(); ++i) {
// int count = jsonArray[i].asInt();
// cbf.CBF[i] = count; // 恢复计数器值
// }
//}
//return cbf;
if (!json.isArray()) {
throw std::invalid_argument("Invalid JSON format for CountingBloomFilter.");
}
for (Json::Value::ArrayIndex i = 0; i < json.size(); ++i) {
this->CBF[i] = json[i].asInt();
}
return ;
}
// 赋值运算符重载
CountingBloomFilter& CountingBloomFilter::operator=(const CountingBloomFilter& other) {
// 先检查自我赋值
if (this != &other) {
// 复制计数器数组
this->CBF = other.CBF;
// 复制哈希函数列表
this->hashFunctions = other.hashFunctions;
// 复制其他成员变量
this->m = other.m;
this->k = other.k;
}
return *this; // 返回当前对象的引用
}
// 判断运算符重载
bool CountingBloomFilter::operator==(const CountingBloomFilter& other) const {
return (m == other.m) && (k == other.k) && (CBF == other.CBF);
}
// 计数布隆过滤器的合并运算符重载
CountingBloomFilter CountingBloomFilter::operator+(const CountingBloomFilter& other) const {
// 确保两个布隆过滤器的参数一致
if (this->m != other.m || this->k != other.k) {
throw std::invalid_argument("Cannot merge CountingBloomFilters with different sizes or hash functions.");
}
// 创建一个新的 CountingBloomFilter 来存储合并结果
CountingBloomFilter result(this->m, this->k);
// 合并计数器数据
for (size_t i = 0; i < this->m; ++i) {
result.CBF[i] = this->CBF[i] + other.CBF[i]; // 对应位置的计数器相加
}
// 合并哈希函数(假设我们只取两者的哈希函数并列)
result.hashFunctions = this->hashFunctions;
//result.hashFunctions.insert(result.hashFunctions.end(), other.hashFunctions.begin(), other.hashFunctions.end());
return result;
}
//int main() {
//
// vector<string> Ind = { "a", "b", "c", "d" };
// CountingBloomFilter RCBF(3500, 7);
//
// for (const std::string& item : Ind) {
// cout << "insert to RCBF: " << item << endl;
// RCBF.update(item, "addw");
// }
// if (RCBF.check("a") == true)
// {
// cout << "a is in RCBF" << endl;
// }
// else
// {
// cout << "a is not in RCBF" << endl;
// }
//}