-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBloomFilter.java
More file actions
56 lines (52 loc) · 1.29 KB
/
BloomFilter.java
File metadata and controls
56 lines (52 loc) · 1.29 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
public class BloomFilter
{
public int filter_len;
byte [] bits;
public BloomFilter(int f_len)
{
filter_len = f_len;
// создаём битовый массив длиной f_len ...
bits = new byte[filter_len];
}
// хэш-функции
public int hash1(String str1)
{
// 17
int code = 0;
for(int i = 0; i < str1.length(); i++)
{
code += (int)str1.charAt(i);
}
// реализация ...
code *= 17;
code %= filter_len;
return code;
}
public int hash2(String str1)
{
// 223
// реализация ...
int code = 0;
for (int i = 0; i < str1.length(); i++){
code += (int)str1.charAt(i);
}
code *= 223;
code %= filter_len;
return code;
}
public void add(String str1)
{
// добавляем строку str1 в фильтр
bits[hash1(str1)] = 1;
bits[hash2(str1)] = 1;
}
public boolean isValue(String str1)
{
// проверка, имеется ли строка str1 в фильтре
if (bits[hash1(str1)] == 1 || bits[hash2(str1)] == 1) {
return true;
}
else
return false;
}
}