@@ -60,6 +60,9 @@ class BloomFilter {
6060 // Add an item to the filter.
6161 Status Add (const ItemType &item);
6262
63+ // Add multiple items to the filter.
64+ Status AddAll (const vector<ItemType> data, const size_t start, const size_t end);
65+
6366 // Report if the item is inserted, with false positive rate.
6467 Status Contain (const ItemType &item) const ;
6568
@@ -92,6 +95,51 @@ Status BloomFilter<ItemType, bits_per_item, HashFamily, k>::Add(
9295 return Ok;
9396}
9497
98+ #define BLOCK_SHIFT 18
99+ #define BLOCK_LEN (1 << BLOCK_SHIFT)
100+
101+ void applyBlock (uint64_t * tmp, int block, int len, uint64_t *data) {
102+ for (int i = 0 ; i < len; i += 2 ) {
103+ int index = tmp[(block << BLOCK_SHIFT) + i];
104+ uint64_t bits = tmp[(block << BLOCK_SHIFT) + i + 1 ];
105+ data[index] |= bits;
106+ }
107+ }
108+
109+ template <typename ItemType, size_t bits_per_item,
110+ typename HashFamily, int k>
111+ Status BloomFilter<ItemType, bits_per_item, HashFamily, k>::AddAll(
112+ const vector<ItemType> keys, const size_t start, const size_t end) {
113+ int blocks = 1 + arrayLength / BLOCK_LEN;
114+ uint64_t * tmp = new uint64_t [blocks * BLOCK_LEN];
115+ int * tmpLen = new int [blocks]();
116+ for (size_t i = start; i < end; i++) {
117+ uint64_t key = keys[i];
118+ uint64_t hash = hasher (key);
119+ uint32_t a = (uint32_t ) (hash >> 32 );
120+ uint32_t bb = (uint32_t ) hash;
121+ for (int j = 0 ; j < k; j++) {
122+ int index = reduce (a, this ->arrayLength );
123+ int block = index >> BLOCK_SHIFT;
124+ int len = tmpLen[block];
125+ tmp[(block << BLOCK_SHIFT) + len] = index;
126+ tmp[(block << BLOCK_SHIFT) + len + 1 ] = getBit (a);
127+ tmpLen[block] = len + 2 ;
128+ if (len + 2 == BLOCK_LEN) {
129+ applyBlock (tmp, block, len + 2 , data);
130+ tmpLen[block] = 0 ;
131+ }
132+ a += bb;
133+ }
134+ }
135+ for (int block = 0 ; block < blocks; block++) {
136+ applyBlock (tmp, block, tmpLen[block], data);
137+ }
138+ delete[] tmp;
139+ delete[] tmpLen;
140+ return Ok;
141+ }
142+
95143template <typename ItemType, size_t bits_per_item,
96144 typename HashFamily, int k>
97145Status BloomFilter<ItemType, bits_per_item, HashFamily, k>::Contain(
0 commit comments