OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 | |
7 #include "base/logging.h" | |
8 #include "components/rappor/bloom_filter.h" | |
9 #include "third_party/smhasher/src/MurmurHash3.h" | |
10 | |
11 namespace { | |
12 | |
13 // Distinct seeds are used to create unique hash functions for the bloom filter. | |
14 const uint32_t kHashSeedCount = 4; | |
Alexei Svitkine (slow)
2014/01/09 19:23:09
You can probably get rid of kHashSeedCount and jus
Steven Holte
2014/01/09 22:03:01
Done.
| |
15 const uint32_t kHashSeeds[kHashSeedCount] = {0xd123957d, 0x6752fc9b, | |
16 0xcb6a0102, 0x1a82ea95}; | |
17 | |
18 uint32_t MurmurHash3String(const std::string& str, uint32_t seed) { | |
19 uint32_t output = 0; | |
20 // This function is optimized for x86_32, but should work on any platform. | |
21 MurmurHash3_x86_32(str.data(), str.size(), seed, &output); | |
22 return output; | |
23 } | |
24 | |
25 } // namespace | |
26 | |
27 namespace rappor { | |
28 | |
29 BloomFilter::BloomFilter(uint32_t bytes_size, uint32_t hash_count) | |
30 : bytes_(bytes_size), hash_count_(hash_count) { | |
31 DCHECK_LE(hash_count, kHashSeedCount); | |
32 } | |
33 | |
34 BloomFilter::~BloomFilter() {} | |
35 | |
36 void BloomFilter::AddString(const std::string& str) { | |
37 for (size_t j = 0; j < hash_count_; ++j) { | |
38 uint32_t index = MurmurHash3String(str, kHashSeeds[j]); | |
39 uint32_t byte_index = (index / 8) % bytes_.size(); | |
40 uint32_t bit_index = index % 8; | |
41 bytes_[byte_index] |= 1 << bit_index; | |
42 } | |
43 } | |
44 | |
45 void BloomFilter::AddStrings(const std::vector<std::string>& strings) { | |
46 for (size_t i = 0, len = strings.size(); i < len; ++i) | |
Alexei Svitkine (slow)
2014/01/09 19:23:09
Nit: No need for |len|, just use |strings.size()|
Steven Holte
2014/01/09 22:03:01
Done.
| |
47 AddString(strings[i]); | |
48 } | |
49 | |
50 } // namespace rappor | |
OLD | NEW |