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; | |
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(hash_count <= kHashSeedCount); | |
Alexei Svitkine (slow)
2013/12/19 19:47:02
DCHECK_LE
Steven Holte
2013/12/20 03:03:55
Done.
| |
32 } | |
33 | |
34 void BloomFilter::AddString(const std::string& str) { | |
35 for (size_t j = 0; j < hash_count_; ++j) { | |
36 uint32_t index = MurmurHash3String(str, kHashSeeds[j]); | |
37 uint32_t byte_index = (index / 8) % bytes_.size(); | |
38 uint32_t bit_index = index % 8; | |
39 bytes_[byte_index] |= 1 << bit_index; | |
40 } | |
41 } | |
42 | |
43 void BloomFilter::AddStrings(const std::vector<std::string>& strings) { | |
44 for (size_t i = 0, len = strings.size(); i < len; ++i) | |
45 AddString(strings[i]); | |
46 } | |
47 | |
48 const ByteVector& BloomFilter::bytes() const { return bytes_; } | |
49 | |
50 uint32_t BloomFilter::hash_count() const { return hash_count_; } | |
Alexei Svitkine (slow)
2013/12/19 19:47:02
These can be inlined in the header.
Steven Holte
2013/12/20 03:03:55
Done.
| |
51 | |
52 } // namespace rappor | |
OLD | NEW |