OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // A read-only set implementation for |SBPrefix| items. Prefixes are | 5 // A read-only set implementation for |SBPrefix| items. Prefixes are |
6 // sorted and stored as 16-bit deltas from the previous prefix. An | 6 // sorted and stored as 16-bit deltas from the previous prefix. An |
7 // index structure provides quick random access, and also handles | 7 // index structure provides quick random access, and also handles |
8 // cases where 16 bits cannot encode a delta. | 8 // cases where 16 bits cannot encode a delta. |
9 // | 9 // |
10 // For example, the sequence {20, 25, 41, 65432, 150000, 160000} would | 10 // For example, the sequence {20, 25, 41, 65432, 150000, 160000} would |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
52 #pragma once | 52 #pragma once |
53 | 53 |
54 #include <vector> | 54 #include <vector> |
55 | 55 |
56 #include "chrome/browser/safe_browsing/safe_browsing_util.h" | 56 #include "chrome/browser/safe_browsing/safe_browsing_util.h" |
57 | 57 |
58 namespace safe_browsing { | 58 namespace safe_browsing { |
59 | 59 |
60 class PrefixSet { | 60 class PrefixSet { |
61 public: | 61 public: |
62 // |prefixes| must be sorted. | |
lzheng
2011/03/03 22:30:07
How about rename the "prefixes" to "sorted_prefix
| |
62 explicit PrefixSet(const std::vector<SBPrefix>& prefixes); | 63 explicit PrefixSet(const std::vector<SBPrefix>& prefixes); |
63 ~PrefixSet(); | 64 ~PrefixSet(); |
64 | 65 |
65 // |true| if |prefix| was in |prefixes| passed to the constructor. | 66 // |true| if |prefix| was in |prefixes| passed to the constructor. |
66 bool Exists(SBPrefix prefix) const; | 67 bool Exists(SBPrefix prefix) const; |
67 | 68 |
69 // Regenerate the vector of prefixes passed to the constructor into | |
70 // |prefixes|. Prefixes will be added in sorted order. | |
71 void GetPrefixes(std::vector<SBPrefix>* prefixes); | |
72 | |
68 private: | 73 private: |
69 // Maximum delta that can be encoded in a 16-bit unsigned. | 74 // Maximum delta that can be encoded in a 16-bit unsigned. |
70 static const unsigned kMaxDelta = 256 * 256; | 75 static const unsigned kMaxDelta = 256 * 256; |
71 | 76 |
72 // Maximum number of consecutive deltas to encode before generating | 77 // Maximum number of consecutive deltas to encode before generating |
73 // a new index entry. This helps keep the worst-case performance | 78 // a new index entry. This helps keep the worst-case performance |
74 // for |Exists()| under control. | 79 // for |Exists()| under control. |
75 static const size_t kMaxRun = 100; | 80 static const size_t kMaxRun = 100; |
76 | 81 |
77 // Top-level index of prefix to offset in |deltas_|. Each pair | 82 // Top-level index of prefix to offset in |deltas_|. Each pair |
78 // indicates a base prefix and where the deltas from that prefix | 83 // indicates a base prefix and where the deltas from that prefix |
79 // begin in |deltas_|. The deltas for a pair end at the next pair's | 84 // begin in |deltas_|. The deltas for a pair end at the next pair's |
80 // index into |deltas_|. | 85 // index into |deltas_|. |
81 std::vector<std::pair<SBPrefix,size_t> > index_; | 86 std::vector<std::pair<SBPrefix,size_t> > index_; |
82 | 87 |
83 // Deltas which are added to the prefix in |index_| to generate | 88 // Deltas which are added to the prefix in |index_| to generate |
84 // prefixes. Deltas are only valid between consecutive items from | 89 // prefixes. Deltas are only valid between consecutive items from |
85 // |index_|, or the end of |deltas_| for the last |index_| pair. | 90 // |index_|, or the end of |deltas_| for the last |index_| pair. |
86 std::vector<uint16> deltas_; | 91 std::vector<uint16> deltas_; |
87 | 92 |
88 DISALLOW_COPY_AND_ASSIGN(PrefixSet); | 93 DISALLOW_COPY_AND_ASSIGN(PrefixSet); |
89 }; | 94 }; |
90 | 95 |
91 } // namespace safe_browsing | 96 } // namespace safe_browsing |
92 | 97 |
93 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ | 98 #endif // CHROME_BROWSER_SAFE_BROWSING_PREFIX_SET_H_ |
OLD | NEW |