| OLD | NEW |
| 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 Loading... |
| 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()) {} | |
| 59 | 58 |
| 60 SparseSet(int max_size) { | 59 SparseSet(int max_size) { |
| 61 max_size_ = max_size; | 60 max_size_ = max_size; |
| 62 sparse_to_dense_ = new int[max_size]; | 61 sparse_to_dense_ = new int[max_size]; |
| 63 dense_ = new int[max_size]; | 62 dense_ = new int[max_size]; |
| 64 valgrind_ = RunningOnValgrindOrMemorySanitizer(); | |
| 65 // Don't need to zero the memory, but do so anyway | 63 // Don't need to zero the memory, but do so anyway |
| 66 // to appease Valgrind. | 64 // to appease Valgrind. |
| 67 if (valgrind_) { | 65 if (InitMemory()) { |
| 68 for (int i = 0; i < max_size; i++) { | 66 for (int i = 0; i < max_size; i++) { |
| 69 dense_[i] = 0xababababU; | 67 dense_[i] = 0xababababU; |
| 70 sparse_to_dense_[i] = 0xababababU; | 68 sparse_to_dense_[i] = 0xababababU; |
| 71 } | 69 } |
| 72 } | 70 } |
| 73 size_ = 0; | 71 size_ = 0; |
| 74 } | 72 } |
| 75 | 73 |
| 76 ~SparseSet() { | 74 ~SparseSet() { |
| 77 delete[] sparse_to_dense_; | 75 delete[] sparse_to_dense_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 89 | 87 |
| 90 // Change the maximum size of the array. | 88 // Change the maximum size of the array. |
| 91 // Invalidates all iterators. | 89 // Invalidates all iterators. |
| 92 void resize(int new_max_size) { | 90 void resize(int new_max_size) { |
| 93 if (size_ > new_max_size) | 91 if (size_ > new_max_size) |
| 94 size_ = new_max_size; | 92 size_ = new_max_size; |
| 95 if (new_max_size > max_size_) { | 93 if (new_max_size > max_size_) { |
| 96 int* a = new int[new_max_size]; | 94 int* a = new int[new_max_size]; |
| 97 if (sparse_to_dense_) { | 95 if (sparse_to_dense_) { |
| 98 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); | 96 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); |
| 99 if (valgrind_) { | 97 if (InitMemory()) { |
| 100 for (int i = max_size_; i < new_max_size; i++) | 98 for (int i = max_size_; i < new_max_size; i++) |
| 101 a[i] = 0xababababU; | 99 a[i] = 0xababababU; |
| 102 } | 100 } |
| 103 delete[] sparse_to_dense_; | 101 delete[] sparse_to_dense_; |
| 104 } | 102 } |
| 105 sparse_to_dense_ = a; | 103 sparse_to_dense_ = a; |
| 106 | 104 |
| 107 a = new int[new_max_size]; | 105 a = new int[new_max_size]; |
| 108 if (dense_) { | 106 if (dense_) { |
| 109 memmove(a, dense_, size_*sizeof a[0]); | 107 memmove(a, dense_, size_*sizeof a[0]); |
| 110 if (valgrind_) { | 108 if (InitMemory()) { |
| 111 for (int i = size_; i < new_max_size; i++) | 109 for (int i = size_; i < new_max_size; i++) |
| 112 a[i] = 0xababababU; | 110 a[i] = 0xababababU; |
| 113 } | 111 } |
| 114 delete[] dense_; | 112 delete[] dense_; |
| 115 } | 113 } |
| 116 dense_ = a; | 114 dense_ = a; |
| 117 } | 115 } |
| 118 max_size_ = new_max_size; | 116 max_size_ = new_max_size; |
| 119 } | 117 } |
| 120 | 118 |
| 121 // Return the maximum size of the array. | 119 // Return the maximum size of the array. |
| 122 // Indices can be in the range [0, max_size). | 120 // Indices can be in the range [0, max_size). |
| 123 int max_size() const { return max_size_; } | 121 int max_size() const { return max_size_; } |
| 124 | 122 |
| 125 // Clear the array. | 123 // Clear the array. |
| 126 void clear() { size_ = 0; } | 124 void clear() { size_ = 0; } |
| 127 | 125 |
| 128 // Check whether i is in the array. | 126 // Check whether i is in the array. |
| 129 bool contains(int i) const { | 127 bool contains(int i) const { |
| 130 DCHECK_GE(i, 0); | 128 DCHECK_GE(i, 0); |
| 131 DCHECK_LT(i, max_size_); | 129 DCHECK_LT(i, max_size_); |
| 132 if (static_cast<uint>(i) >= max_size_) { | 130 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { |
| 133 return false; | 131 return false; |
| 134 } | 132 } |
| 135 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0. | 133 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0. |
| 136 return (uint)sparse_to_dense_[i] < (uint)size_ && | 134 return (uint)sparse_to_dense_[i] < (uint)size_ && |
| 137 dense_[sparse_to_dense_[i]] == i; | 135 dense_[sparse_to_dense_[i]] == i; |
| 138 } | 136 } |
| 139 | 137 |
| 140 // Adds i to the set. | 138 // Adds i to the set. |
| 141 void insert(int i) { | 139 void insert(int i) { |
| 142 if (!contains(i)) | 140 if (!contains(i)) |
| 143 insert_new(i); | 141 insert_new(i); |
| 144 } | 142 } |
| 145 | 143 |
| 146 // Set the value at the new index i to v. | 144 // Set the value at the new index i to v. |
| 147 // Fast but unsafe: only use if contains(i) is false. | 145 // Fast but unsafe: only use if contains(i) is false. |
| 148 void insert_new(int i) { | 146 void insert_new(int i) { |
| 149 if (static_cast<uint>(i) >= max_size_) { | 147 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { |
| 150 // Semantically, end() would be better here, but we already know | 148 // Semantically, end() would be better here, but we already know |
| 151 // the user did something stupid, so begin() insulates them from | 149 // the user did something stupid, so begin() insulates them from |
| 152 // dereferencing an invalid pointer. | 150 // dereferencing an invalid pointer. |
| 153 return; | 151 return; |
| 154 } | 152 } |
| 155 DCHECK(!contains(i)); | 153 DCHECK(!contains(i)); |
| 156 DCHECK_LT(size_, max_size_); | 154 DCHECK_LT(size_, max_size_); |
| 157 sparse_to_dense_[i] = size_; | 155 sparse_to_dense_[i] = size_; |
| 158 dense_[size_] = i; | 156 dense_[size_] = i; |
| 159 size_++; | 157 size_++; |
| 160 } | 158 } |
| 161 | 159 |
| 162 // Comparison function for sorting. | 160 // Comparison function for sorting. |
| 163 // Can sort the sparse array so that future iterations | 161 // Can sort the sparse array so that future iterations |
| 164 // will visit indices in increasing order using | 162 // will visit indices in increasing order using |
| 165 // sort(arr.begin(), arr.end(), arr.less); | 163 // sort(arr.begin(), arr.end(), arr.less); |
| 166 static bool less(int a, int b) { return a < b; } | 164 static bool less(int a, int b) { return a < b; } |
| 167 | 165 |
| 168 private: | 166 private: |
| 167 static bool InitMemory() { |
| 168 #ifdef MEMORY_SANITIZER |
| 169 return true; |
| 170 #else |
| 171 return RunningOnValgrind(); |
| 172 #endif |
| 173 } |
| 174 |
| 169 int size_; | 175 int size_; |
| 170 int max_size_; | 176 int max_size_; |
| 171 int* sparse_to_dense_; | 177 int* sparse_to_dense_; |
| 172 int* dense_; | 178 int* dense_; |
| 173 bool valgrind_; | |
| 174 | 179 |
| 175 DISALLOW_EVIL_CONSTRUCTORS(SparseSet); | 180 DISALLOW_COPY_AND_ASSIGN(SparseSet); |
| 176 }; | 181 }; |
| 177 | 182 |
| 178 } // namespace re2 | 183 } // namespace re2 |
| 179 | 184 |
| 180 #endif // RE2_UTIL_SPARSE_SET_H__ | 185 #endif // RE2_UTIL_SPARSE_SET_H__ |
| OLD | NEW |