This repository was archived by the owner on Jul 7, 2020. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed
main/java/com/clearspring/analytics/stream/membership
test/java/com/clearspring/analytics/stream/membership Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -83,6 +83,12 @@ public boolean isPresent(byte[] key) {
83
83
return true ;
84
84
}
85
85
86
+ public long cardinality () {
87
+ int m = buckets ();
88
+ double fractionOfBits = ((double ) filter_ .cardinality ()) / m ;
89
+ return Math .round (-m * Math .log1p (-fractionOfBits ) / hashCount );
90
+ }
91
+
86
92
/*
87
93
@param key -- value whose hash is used to fill
88
94
the filter_.
Original file line number Diff line number Diff line change 15
15
* KIND, either express or implied. See the License for the
16
16
* specific language governing permissions and limitations
17
17
* under the License.
18
- *
18
+ *
19
19
*/
20
20
package com .clearspring .analytics .stream .membership ;
21
21
@@ -111,6 +111,27 @@ public void testSerialize() throws IOException {
111
111
FilterTest .testSerialize (bf );
112
112
}
113
113
114
+ @ Test
115
+ public void testCardinalityOne () {
116
+ bf .add ("a" );
117
+ assertEquals (1 , bf .cardinality ());
118
+ }
119
+
120
+ @ Test
121
+ public void testCardinalityTwo () {
122
+ bf .add ("a" );
123
+ bf .add ("b" );
124
+ assertEquals (2 , bf .cardinality ());
125
+ }
126
+
127
+ @ Test
128
+ public void testCardinalityHundred () {
129
+ for (int i = 0 ; i < 100 ; ++i ) {
130
+ bf .add (Integer .toString (i ));
131
+ }
132
+ assertEquals (100 , bf .cardinality ());
133
+ }
134
+
114
135
@ Test
115
136
public void testGetFalsePositiveProbability () {
116
137
// These probabilities are taken from the bloom filter probability table at
@@ -204,7 +225,7 @@ public void testHashCount() {
204
225
205
226
instance = new BloomFilter (1 , 7 );
206
227
assertEquals (5 , instance .getHashCount ());
207
-
228
+
208
229
/*
209
230
* Although technically 8*ln(2) = 5.545...
210
231
* we round down here for speed
You can’t perform that action at this time.
0 commit comments