Skip to content

Commit 3e80a8d

Browse files
committed
refactor Improve the FilterMemory constructor parameter
1 parent 237f8ea commit 3e80a8d

File tree

4 files changed

+65
-255
lines changed

4 files changed

+65
-255
lines changed

src/BloomFilter/Configurations/FilterMemoryOptionsExtension.cs

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,7 @@ public void AddServices(IServiceCollection services)
2121
services.TryAddSingleton<IBloomFilterFactory, DefaultBloomFilterFactory>();
2222
services.AddSingleton<IBloomFilter, FilterMemory>(x =>
2323
{
24-
if (_options.Bits is not null)
25-
{
26-
return new FilterMemory(
27-
_options.Bits,
28-
_options.BitsMore,
29-
_options.Name,
30-
_options.ExpectedElements,
31-
_options.ErrorRate,
32-
HashFunction.Functions[_options.Method]);
33-
}
34-
35-
if (_options.Bytes is not null)
36-
{
37-
return new FilterMemory(
38-
_options.Bytes,
39-
_options.BytesMore,
40-
_options.Name,
41-
_options.ExpectedElements,
42-
_options.ErrorRate,
43-
HashFunction.Functions[_options.Method]);
44-
}
45-
46-
return new FilterMemory(
47-
_options.Name,
48-
_options.ExpectedElements,
49-
_options.ErrorRate,
50-
HashFunction.Functions[_options.Method]);
24+
return new FilterMemory(_options);
5125
});
5226
}
5327
}

src/BloomFilter/FilterBuilder.cs

Lines changed: 11 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections;
1+
using BloomFilter.Configurations;
22

33
namespace BloomFilter;
44

@@ -7,6 +7,16 @@ namespace BloomFilter;
77
/// </summary>
88
public partial class FilterBuilder
99
{
10+
/// <summary>
11+
/// Creates a BloomFilter for the specified expected element
12+
/// </summary>
13+
/// <param name="options"><see cref="FilterMemoryOptions"/></param>
14+
/// <returns></returns>
15+
public static IBloomFilter Build(FilterMemoryOptions options)
16+
{
17+
return new FilterMemory(options);
18+
}
19+
1020
/// <summary>
1121
/// Creates a BloomFilter for the specified expected element
1222
/// </summary>
@@ -42,60 +52,6 @@ public static IBloomFilter Build(long expectedElements, HashFunction hashFunctio
4252
return Build(expectedElements, 0.01, hashFunction, name);
4353
}
4454

45-
/// <summary>
46-
/// Creates a BloomFilter for the specified expected element
47-
/// </summary>
48-
/// <param name="bits">Sets the bit value</param>
49-
/// <param name="expectedElements">The expected elements.</param>
50-
/// <param name="errorRate">The error rate.</param>
51-
/// <param name="name"></param>
52-
/// <returns></returns>
53-
public static IBloomFilter Build(byte[] bits, long expectedElements, double errorRate, string name = BloomFilterConstValue.DefaultInMemoryName)
54-
{
55-
return Build(bits, expectedElements, errorRate, HashFunction.Functions[HashMethod.Murmur3], name);
56-
}
57-
58-
/// <summary>
59-
/// Creates a BloomFilter for the specified expected element
60-
/// </summary>
61-
/// <param name="bits">Sets the bit value</param>
62-
/// <param name="more">Sets more the bit value</param>
63-
/// <param name="expectedElements">The expected elements.</param>
64-
/// <param name="errorRate">The error rate.</param>
65-
/// <param name="name"></param>
66-
/// <returns></returns>
67-
public static IBloomFilter Build(byte[] bits, byte[] more, long expectedElements, double errorRate, string name = BloomFilterConstValue.DefaultInMemoryName)
68-
{
69-
return Build(bits, more, expectedElements, errorRate, HashFunction.Functions[HashMethod.Murmur3], name);
70-
}
71-
72-
/// <summary>
73-
/// Creates a BloomFilter for the specified expected element
74-
/// </summary>
75-
/// <param name="bits">Sets the bit value</param>
76-
/// <param name="expectedElements">The expected elements.</param>
77-
/// <param name="errorRate">The error rate.</param>
78-
/// <param name="name"></param>
79-
/// <returns></returns>
80-
public static IBloomFilter Build(BitArray bits, long expectedElements, double errorRate, string name = BloomFilterConstValue.DefaultInMemoryName)
81-
{
82-
return Build(bits, expectedElements, errorRate, HashFunction.Functions[HashMethod.Murmur3], name);
83-
}
84-
85-
/// <summary>
86-
/// Creates a BloomFilter for the specified expected element
87-
/// </summary>
88-
/// <param name="bits">Sets the bit value</param>
89-
/// <param name="more">Sets more the bit value</param>
90-
/// <param name="expectedElements">The expected elements.</param>
91-
/// <param name="errorRate">The error rate.</param>
92-
/// <param name="name"></param>
93-
/// <returns></returns>
94-
public static IBloomFilter Build(BitArray bits, BitArray more, long expectedElements, double errorRate, string name = BloomFilterConstValue.DefaultInMemoryName)
95-
{
96-
return Build(bits, more, expectedElements, errorRate, HashFunction.Functions[HashMethod.Murmur3], name);
97-
}
98-
9955
/// <summary>
10056
/// Creates a BloomFilter for the specified expected element
10157
/// </summary>
@@ -108,64 +64,6 @@ public static IBloomFilter Build(long expectedElements, double errorRate, string
10864
return Build(expectedElements, errorRate, HashFunction.Functions[HashMethod.Murmur3], name);
10965
}
11066

111-
/// <summary>
112-
/// Creates a BloomFilter for the specified expected element
113-
/// </summary>
114-
/// <param name="bits">Sets the bit value</param>
115-
/// <param name="expectedElements">The expected elements.</param>
116-
/// <param name="errorRate">The error rate.</param>
117-
/// <param name="hashMethod">The hash method.</param>
118-
/// <param name="name"></param>
119-
/// <returns></returns>
120-
public static IBloomFilter Build(byte[] bits, long expectedElements, double errorRate, HashMethod hashMethod, string name = BloomFilterConstValue.DefaultInMemoryName)
121-
{
122-
return new FilterMemory(bits, name, expectedElements, errorRate, HashFunction.Functions[hashMethod]);
123-
}
124-
125-
/// <summary>
126-
/// Creates a BloomFilter for the specified expected element
127-
/// </summary>
128-
/// <param name="bits">Sets the bit value</param>
129-
/// <param name="more">Sets more the bit value</param>
130-
/// <param name="expectedElements">The expected elements.</param>
131-
/// <param name="errorRate">The error rate.</param>
132-
/// <param name="hashMethod">The hash method.</param>
133-
/// <param name="name"></param>
134-
/// <returns></returns>
135-
public static IBloomFilter Build(byte[] bits, byte[]? more, long expectedElements, double errorRate, HashMethod hashMethod, string name = BloomFilterConstValue.DefaultInMemoryName)
136-
{
137-
return new FilterMemory(bits, more, name, expectedElements, errorRate, HashFunction.Functions[hashMethod]);
138-
}
139-
140-
/// <summary>
141-
/// Creates a BloomFilter for the specified expected element
142-
/// </summary>
143-
/// <param name="bits">Sets the bit value</param>
144-
/// <param name="expectedElements">The expected elements.</param>
145-
/// <param name="errorRate">The error rate.</param>
146-
/// <param name="hashMethod">The hash method.</param>
147-
/// <param name="name"></param>
148-
/// <returns></returns>
149-
public static IBloomFilter Build(BitArray bits, long expectedElements, double errorRate, HashMethod hashMethod, string name = BloomFilterConstValue.DefaultInMemoryName)
150-
{
151-
return new FilterMemory(bits, name, expectedElements, errorRate, HashFunction.Functions[hashMethod]);
152-
}
153-
154-
/// <summary>
155-
/// Creates a BloomFilter for the specified expected element
156-
/// </summary>
157-
/// <param name="bits">Sets the bit value</param>
158-
/// <param name="more">Sets more the bit value</param>
159-
/// <param name="expectedElements">The expected elements.</param>
160-
/// <param name="errorRate">The error rate.</param>
161-
/// <param name="hashMethod">The hash method.</param>
162-
/// <param name="name"></param>
163-
/// <returns></returns>
164-
public static IBloomFilter Build(BitArray bits, BitArray? more, long expectedElements, double errorRate, HashMethod hashMethod, string name = BloomFilterConstValue.DefaultInMemoryName)
165-
{
166-
return new FilterMemory(bits, more, name, expectedElements, errorRate, HashFunction.Functions[hashMethod]);
167-
}
168-
16967
/// <summary>
17068
/// Creates a BloomFilter for the specified expected element
17169
/// </summary>
@@ -179,65 +77,6 @@ public static IBloomFilter Build(long expectedElements, double errorRate, HashMe
17977
return new FilterMemory(name, expectedElements, errorRate, HashFunction.Functions[hashMethod]);
18078
}
18179

182-
183-
/// <summary>
184-
/// Creates a BloomFilter for the specified expected element
185-
/// </summary>
186-
/// <param name="bits">Sets the bit value</param>
187-
/// <param name="expectedElements">The expected elements.</param>
188-
/// <param name="errorRate">The error rate.</param>
189-
/// <param name="hashFunction">The hash function.</param>
190-
/// <param name="name"></param>
191-
/// <returns></returns>
192-
public static IBloomFilter Build(byte[] bits, long expectedElements, double errorRate, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName)
193-
{
194-
return new FilterMemory(bits, name, expectedElements, errorRate, hashFunction);
195-
}
196-
197-
/// <summary>
198-
/// Creates a BloomFilter for the specified expected element
199-
/// </summary>
200-
/// <param name="bits">Sets the bit value</param>
201-
/// <param name="more">Sets more the bit value</param>
202-
/// <param name="expectedElements">The expected elements.</param>
203-
/// <param name="errorRate">The error rate.</param>
204-
/// <param name="hashFunction">The hash function.</param>
205-
/// <param name="name"></param>
206-
/// <returns></returns>
207-
public static IBloomFilter Build(byte[] bits, byte[] more, long expectedElements, double errorRate, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName)
208-
{
209-
return new FilterMemory(bits, more, name, expectedElements, errorRate, hashFunction);
210-
}
211-
212-
/// <summary>
213-
/// Creates a BloomFilter for the specified expected element
214-
/// </summary>
215-
/// <param name="bits">Sets the bit value</param>
216-
/// <param name="expectedElements">The expected elements.</param>
217-
/// <param name="errorRate">The error rate.</param>
218-
/// <param name="hashFunction">The hash function.</param>
219-
/// <param name="name"></param>
220-
/// <returns></returns>
221-
public static IBloomFilter Build(BitArray bits, long expectedElements, double errorRate, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName)
222-
{
223-
return new FilterMemory(bits, name, expectedElements, errorRate, hashFunction);
224-
}
225-
226-
/// <summary>
227-
/// Creates a BloomFilter for the specified expected element
228-
/// </summary>
229-
/// <param name="bits">Sets the bit value</param>
230-
/// <param name="more">Sets more the bit value</param>
231-
/// <param name="expectedElements">The expected elements.</param>
232-
/// <param name="errorRate">The error rate.</param>
233-
/// <param name="hashFunction">The hash function.</param>
234-
/// <param name="name"></param>
235-
/// <returns></returns>
236-
public static IBloomFilter Build(BitArray bits, BitArray more, long expectedElements, double errorRate, HashFunction hashFunction, string name = BloomFilterConstValue.DefaultInMemoryName)
237-
{
238-
return new FilterMemory(bits, more, name, expectedElements, errorRate, hashFunction);
239-
}
240-
24180
/// <summary>
24281
/// Creates a BloomFilter for the specified expected element
24382
/// </summary>

src/BloomFilter/FilterMemory.cs

Lines changed: 24 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Linq;
66
using System.Threading.Tasks;
7+
using BloomFilter.Configurations;
78

89
namespace BloomFilter;
910

@@ -26,59 +27,30 @@ public class FilterMemory : Filter
2627
/// <summary>
2728
/// Initializes a new instance of the <see cref="FilterMemory"/> class.
2829
/// </summary>
29-
/// <param name="bits">Sets the bit value</param>
30-
/// <param name="name"></param>
31-
/// <param name="expectedElements">The expected elements.</param>
32-
/// <param name="errorRate">The error rate.</param>
33-
/// <param name="hashFunction">The hash function.</param>
34-
public FilterMemory(BitArray bits, string name, long expectedElements, double errorRate, HashFunction hashFunction)
35-
: base(name, expectedElements, errorRate, hashFunction)
36-
{
37-
Import(bits, null);
38-
}
39-
40-
/// <summary>
41-
/// Initializes a new instance of the <see cref="FilterMemory"/> class.
42-
/// </summary>
43-
/// <param name="bits1">Sets the bit value</param>
44-
/// <param name="more">Sets more the bit value</param>
45-
/// <param name="name"></param>
46-
/// <param name="expectedElements">The expected elements.</param>
47-
/// <param name="errorRate">The error rate.</param>
48-
/// <param name="hashFunction">The hash function.</param>
49-
public FilterMemory(BitArray bits1, BitArray? more, string name, long expectedElements, double errorRate, HashFunction hashFunction)
50-
: base(name, expectedElements, errorRate, hashFunction)
30+
/// <param name="options"><see cref="FilterMemoryOptions"/></param>
31+
public FilterMemory(FilterMemoryOptions options)
32+
: base(options.Name, options.ExpectedElements, options.ErrorRate, HashFunction.Functions[options.Method])
5133
{
52-
Import(bits1, more);
53-
}
54-
55-
/// <summary>
56-
/// Initializes a new instance of the <see cref="FilterMemory"/> class.
57-
/// </summary>
58-
/// <param name="bits">Sets the bit value</param>
59-
/// <param name="name"></param>
60-
/// <param name="expectedElements">The expected elements.</param>
61-
/// <param name="errorRate">The error rate.</param>
62-
/// <param name="hashFunction">The hash function.</param>
63-
public FilterMemory(byte[] bits, string name, long expectedElements, double errorRate, HashFunction hashFunction)
64-
: base(name, expectedElements, errorRate, hashFunction)
65-
{
66-
Import(bits, null);
67-
}
68-
69-
/// <summary>
70-
/// Initializes a new instance of the <see cref="FilterMemory"/> class.
71-
/// </summary>
72-
/// <param name="bits">Sets the bit value</param>
73-
/// <param name="more">Sets more the bit value</param>
74-
/// <param name="name"></param>
75-
/// <param name="expectedElements">The expected elements.</param>
76-
/// <param name="errorRate">The error rate.</param>
77-
/// <param name="hashFunction">The hash function.</param>
78-
public FilterMemory(byte[] bits, byte[]? more, string name, long expectedElements, double errorRate, HashFunction hashFunction)
79-
: base(name, expectedElements, errorRate, hashFunction)
80-
{
81-
Import(bits, more);
34+
if (options.Bits is not null)
35+
{
36+
Import(options.Bits, options.BitsMore);
37+
}
38+
else if (options.Bytes is not null)
39+
{
40+
Import(options.Bytes, options.BytesMore);
41+
}
42+
else
43+
{
44+
if (Capacity > MaxInt)
45+
{
46+
_hashBits1 = new BitArray(MaxInt);
47+
_hashBits2 = new BitArray((int)(Capacity - MaxInt));
48+
}
49+
else
50+
{
51+
_hashBits1 = new BitArray((int)Capacity);
52+
}
53+
}
8254
}
8355

8456
/// <summary>

0 commit comments

Comments
 (0)