OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // We want to measure the similarity of two sequences of bytes as a surrogate | 5 // We want to measure the similarity of two sequences of bytes as a surrogate |
6 // for measuring how well a second sequence will compress differentially to the | 6 // for measuring how well a second sequence will compress differentially to the |
7 // first sequence. | 7 // first sequence. |
8 | 8 |
9 #include "courgette/difference_estimator.h" | 9 #include "courgette/difference_estimator.h" |
10 | 10 |
11 #include "base/containers/hash_tables.h" | 11 #include "base/containers/hash_tables.h" |
12 | 12 |
13 namespace courgette { | 13 namespace courgette { |
14 | 14 |
15 // Our difference measure is the number of k-tuples that occur in Subject that | 15 // Our difference measure is the number of k-tuples that occur in Subject that |
16 // don't occur in Base. | 16 // don't occur in Base. |
17 const int kTupleSize = 4; | 17 const int kTupleSize = 4; |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 COMPILE_ASSERT(kTupleSize >= 4 && kTupleSize <= 8, kTupleSize_between_4_and_8); | 21 static_assert(kTupleSize >= 4 && kTupleSize <= 8, |
| 22 "kTupleSize should be between 4 and 8"); |
22 | 23 |
23 size_t HashTuple(const uint8* source) { | 24 size_t HashTuple(const uint8* source) { |
24 size_t hash1 = *reinterpret_cast<const uint32*>(source); | 25 size_t hash1 = *reinterpret_cast<const uint32*>(source); |
25 size_t hash2 = *reinterpret_cast<const uint32*>(source + kTupleSize - 4); | 26 size_t hash2 = *reinterpret_cast<const uint32*>(source + kTupleSize - 4); |
26 size_t hash = ((hash1 * 17 + hash2 * 37) + (hash1 >> 17)) ^ (hash2 >> 23); | 27 size_t hash = ((hash1 * 17 + hash2 * 37) + (hash1 >> 17)) ^ (hash2 >> 23); |
27 return hash; | 28 return hash; |
28 } | 29 } |
29 | 30 |
30 bool RegionsEqual(const Region& a, const Region& b) { | 31 bool RegionsEqual(const Region& a, const Region& b) { |
31 if (a.length() != b.length()) | 32 if (a.length() != b.length()) |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 | 110 |
110 if (mismatches == 0) { | 111 if (mismatches == 0) { |
111 if (RegionsEqual(base->region(), subject->region())) | 112 if (RegionsEqual(base->region(), subject->region())) |
112 return 0; | 113 return 0; |
113 } | 114 } |
114 ++mismatches; // Guarantee not zero. | 115 ++mismatches; // Guarantee not zero. |
115 return mismatches; | 116 return mismatches; |
116 } | 117 } |
117 | 118 |
118 } // namespace | 119 } // namespace |
OLD | NEW |