Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: third_party/re2/util/sparse_set.h

Issue 1530113002: Revert of Update re2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/re2/util/sparse_array_test.cc ('k') | third_party/re2/util/stringpiece.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2006 The RE2 Authors. All Rights Reserved. 1 // Copyright 2006 The RE2 Authors. All Rights Reserved.
2 // Use of this source code is governed by a BSD-style 2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file. 3 // license that can be found in the LICENSE file.
4 4
5 // DESCRIPTION 5 // DESCRIPTION
6 // 6 //
7 // SparseSet<T>(m) is a set of integers in [0, m). 7 // SparseSet<T>(m) is a set of integers in [0, m).
8 // It requires sizeof(int)*m memory, but it provides 8 // It requires sizeof(int)*m memory, but it provides
9 // fast iteration through the elements in the set and fast clearing 9 // fast iteration through the elements in the set and fast clearing
10 // of the set. 10 // of the set.
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 #ifndef RE2_UTIL_SPARSE_SET_H__ 47 #ifndef RE2_UTIL_SPARSE_SET_H__
48 #define RE2_UTIL_SPARSE_SET_H__ 48 #define RE2_UTIL_SPARSE_SET_H__
49 49
50 #include "util/util.h" 50 #include "util/util.h"
51 51
52 namespace re2 { 52 namespace re2 {
53 53
54 class SparseSet { 54 class SparseSet {
55 public: 55 public:
56 SparseSet() 56 SparseSet()
57 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(NULL) {} 57 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(NULL),
58 valgrind_(RunningOnValgrindOrMemorySanitizer()) {}
58 59
59 SparseSet(int max_size) { 60 SparseSet(int max_size) {
60 max_size_ = max_size; 61 max_size_ = max_size;
61 sparse_to_dense_ = new int[max_size]; 62 sparse_to_dense_ = new int[max_size];
62 dense_ = new int[max_size]; 63 dense_ = new int[max_size];
64 valgrind_ = RunningOnValgrindOrMemorySanitizer();
63 // Don't need to zero the memory, but do so anyway 65 // Don't need to zero the memory, but do so anyway
64 // to appease Valgrind. 66 // to appease Valgrind.
65 if (InitMemory()) { 67 if (valgrind_) {
66 for (int i = 0; i < max_size; i++) { 68 for (int i = 0; i < max_size; i++) {
67 dense_[i] = 0xababababU; 69 dense_[i] = 0xababababU;
68 sparse_to_dense_[i] = 0xababababU; 70 sparse_to_dense_[i] = 0xababababU;
69 } 71 }
70 } 72 }
71 size_ = 0; 73 size_ = 0;
72 } 74 }
73 75
74 ~SparseSet() { 76 ~SparseSet() {
75 delete[] sparse_to_dense_; 77 delete[] sparse_to_dense_;
(...skipping 11 matching lines...) Expand all
87 89
88 // Change the maximum size of the array. 90 // Change the maximum size of the array.
89 // Invalidates all iterators. 91 // Invalidates all iterators.
90 void resize(int new_max_size) { 92 void resize(int new_max_size) {
91 if (size_ > new_max_size) 93 if (size_ > new_max_size)
92 size_ = new_max_size; 94 size_ = new_max_size;
93 if (new_max_size > max_size_) { 95 if (new_max_size > max_size_) {
94 int* a = new int[new_max_size]; 96 int* a = new int[new_max_size];
95 if (sparse_to_dense_) { 97 if (sparse_to_dense_) {
96 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); 98 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]);
97 if (InitMemory()) { 99 if (valgrind_) {
98 for (int i = max_size_; i < new_max_size; i++) 100 for (int i = max_size_; i < new_max_size; i++)
99 a[i] = 0xababababU; 101 a[i] = 0xababababU;
100 } 102 }
101 delete[] sparse_to_dense_; 103 delete[] sparse_to_dense_;
102 } 104 }
103 sparse_to_dense_ = a; 105 sparse_to_dense_ = a;
104 106
105 a = new int[new_max_size]; 107 a = new int[new_max_size];
106 if (dense_) { 108 if (dense_) {
107 memmove(a, dense_, size_*sizeof a[0]); 109 memmove(a, dense_, size_*sizeof a[0]);
108 if (InitMemory()) { 110 if (valgrind_) {
109 for (int i = size_; i < new_max_size; i++) 111 for (int i = size_; i < new_max_size; i++)
110 a[i] = 0xababababU; 112 a[i] = 0xababababU;
111 } 113 }
112 delete[] dense_; 114 delete[] dense_;
113 } 115 }
114 dense_ = a; 116 dense_ = a;
115 } 117 }
116 max_size_ = new_max_size; 118 max_size_ = new_max_size;
117 } 119 }
118 120
119 // Return the maximum size of the array. 121 // Return the maximum size of the array.
120 // Indices can be in the range [0, max_size). 122 // Indices can be in the range [0, max_size).
121 int max_size() const { return max_size_; } 123 int max_size() const { return max_size_; }
122 124
123 // Clear the array. 125 // Clear the array.
124 void clear() { size_ = 0; } 126 void clear() { size_ = 0; }
125 127
126 // Check whether i is in the array. 128 // Check whether i is in the array.
127 bool contains(int i) const { 129 bool contains(int i) const {
128 DCHECK_GE(i, 0); 130 DCHECK_GE(i, 0);
129 DCHECK_LT(i, max_size_); 131 DCHECK_LT(i, max_size_);
130 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { 132 if (static_cast<uint>(i) >= max_size_) {
131 return false; 133 return false;
132 } 134 }
133 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0. 135 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0.
134 return (uint)sparse_to_dense_[i] < (uint)size_ && 136 return (uint)sparse_to_dense_[i] < (uint)size_ &&
135 dense_[sparse_to_dense_[i]] == i; 137 dense_[sparse_to_dense_[i]] == i;
136 } 138 }
137 139
138 // Adds i to the set. 140 // Adds i to the set.
139 void insert(int i) { 141 void insert(int i) {
140 if (!contains(i)) 142 if (!contains(i))
141 insert_new(i); 143 insert_new(i);
142 } 144 }
143 145
144 // Set the value at the new index i to v. 146 // Set the value at the new index i to v.
145 // Fast but unsafe: only use if contains(i) is false. 147 // Fast but unsafe: only use if contains(i) is false.
146 void insert_new(int i) { 148 void insert_new(int i) {
147 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { 149 if (static_cast<uint>(i) >= max_size_) {
148 // Semantically, end() would be better here, but we already know 150 // Semantically, end() would be better here, but we already know
149 // the user did something stupid, so begin() insulates them from 151 // the user did something stupid, so begin() insulates them from
150 // dereferencing an invalid pointer. 152 // dereferencing an invalid pointer.
151 return; 153 return;
152 } 154 }
153 DCHECK(!contains(i)); 155 DCHECK(!contains(i));
154 DCHECK_LT(size_, max_size_); 156 DCHECK_LT(size_, max_size_);
155 sparse_to_dense_[i] = size_; 157 sparse_to_dense_[i] = size_;
156 dense_[size_] = i; 158 dense_[size_] = i;
157 size_++; 159 size_++;
158 } 160 }
159 161
160 // Comparison function for sorting. 162 // Comparison function for sorting.
161 // Can sort the sparse array so that future iterations 163 // Can sort the sparse array so that future iterations
162 // will visit indices in increasing order using 164 // will visit indices in increasing order using
163 // sort(arr.begin(), arr.end(), arr.less); 165 // sort(arr.begin(), arr.end(), arr.less);
164 static bool less(int a, int b) { return a < b; } 166 static bool less(int a, int b) { return a < b; }
165 167
166 private: 168 private:
167 static bool InitMemory() {
168 #ifdef MEMORY_SANITIZER
169 return true;
170 #else
171 return RunningOnValgrind();
172 #endif
173 }
174
175 int size_; 169 int size_;
176 int max_size_; 170 int max_size_;
177 int* sparse_to_dense_; 171 int* sparse_to_dense_;
178 int* dense_; 172 int* dense_;
173 bool valgrind_;
179 174
180 DISALLOW_COPY_AND_ASSIGN(SparseSet); 175 DISALLOW_EVIL_CONSTRUCTORS(SparseSet);
181 }; 176 };
182 177
183 } // namespace re2 178 } // namespace re2
184 179
185 #endif // RE2_UTIL_SPARSE_SET_H__ 180 #endif // RE2_UTIL_SPARSE_SET_H__
OLDNEW
« no previous file with comments | « third_party/re2/util/sparse_array_test.cc ('k') | third_party/re2/util/stringpiece.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698