OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "components/rappor/bloom_filter.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "third_party/smhasher/src/MurmurHash3.h" |
| 9 |
| 10 namespace { |
| 11 |
| 12 uint32_t MurmurHash3String(const std::string& str, uint32_t seed) { |
| 13 uint32_t output = 0; |
| 14 // This function is optimized for x86_32, but should work on any platform. |
| 15 MurmurHash3_x86_32(str.data(), str.size(), seed, &output); |
| 16 return output; |
| 17 } |
| 18 |
| 19 } // namespace |
| 20 |
| 21 namespace rappor { |
| 22 |
| 23 BloomFilter::BloomFilter(uint32_t bytes_size, |
| 24 uint32_t hash_function_count, |
| 25 uint32_t hash_seed_offset) |
| 26 : bytes_(bytes_size), |
| 27 hash_function_count_(hash_function_count), |
| 28 hash_seed_offset_(hash_seed_offset) { |
| 29 DCHECK_GT(bytes_size, 0u); |
| 30 } |
| 31 |
| 32 BloomFilter::~BloomFilter() {} |
| 33 |
| 34 void BloomFilter::AddString(const std::string& str) { |
| 35 for (size_t i = 0; i < hash_function_count_; ++i) { |
| 36 uint32_t index = MurmurHash3String(str, hash_seed_offset_ + i); |
| 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 } // namespace rappor |
OLD | NEW |