| 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), valgrind_(Ru
nningOnValgrind()) {} |
| 58 | 58 |
| 59 SparseSet(int max_size) { | 59 SparseSet(int max_size) { |
| 60 max_size_ = max_size; | 60 max_size_ = max_size; |
| 61 sparse_to_dense_ = new int[max_size]; | 61 sparse_to_dense_ = new int[max_size]; |
| 62 dense_ = new int[max_size]; | 62 dense_ = new int[max_size]; |
| 63 valgrind_ = RunningOnValgrind(); |
| 63 // Don't need to zero the memory, but do so anyway | 64 // Don't need to zero the memory, but do so anyway |
| 64 // to appease Valgrind. | 65 // to appease Valgrind. |
| 65 if (RunningOnValgrind()) { | 66 if (valgrind_) { |
| 66 for (int i = 0; i < max_size; i++) { | 67 for (int i = 0; i < max_size; i++) { |
| 67 dense_[i] = 0xababababU; | 68 dense_[i] = 0xababababU; |
| 68 sparse_to_dense_[i] = 0xababababU; | 69 sparse_to_dense_[i] = 0xababababU; |
| 69 } | 70 } |
| 70 } | 71 } |
| 71 size_ = 0; | 72 size_ = 0; |
| 72 } | 73 } |
| 73 | 74 |
| 74 ~SparseSet() { | 75 ~SparseSet() { |
| 75 delete[] sparse_to_dense_; | 76 delete[] sparse_to_dense_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 87 | 88 |
| 88 // Change the maximum size of the array. | 89 // Change the maximum size of the array. |
| 89 // Invalidates all iterators. | 90 // Invalidates all iterators. |
| 90 void resize(int new_max_size) { | 91 void resize(int new_max_size) { |
| 91 if (size_ > new_max_size) | 92 if (size_ > new_max_size) |
| 92 size_ = new_max_size; | 93 size_ = new_max_size; |
| 93 if (new_max_size > max_size_) { | 94 if (new_max_size > max_size_) { |
| 94 int* a = new int[new_max_size]; | 95 int* a = new int[new_max_size]; |
| 95 if (sparse_to_dense_) { | 96 if (sparse_to_dense_) { |
| 96 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); | 97 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); |
| 97 if (RunningOnValgrind()) { | 98 if (valgrind_) { |
| 98 for (int i = max_size_; i < new_max_size; i++) | 99 for (int i = max_size_; i < new_max_size; i++) |
| 99 a[i] = 0xababababU; | 100 a[i] = 0xababababU; |
| 100 } | 101 } |
| 101 delete[] sparse_to_dense_; | 102 delete[] sparse_to_dense_; |
| 102 } | 103 } |
| 103 sparse_to_dense_ = a; | 104 sparse_to_dense_ = a; |
| 104 | 105 |
| 105 a = new int[new_max_size]; | 106 a = new int[new_max_size]; |
| 106 if (dense_) { | 107 if (dense_) { |
| 107 memmove(a, dense_, size_*sizeof a[0]); | 108 memmove(a, dense_, size_*sizeof a[0]); |
| 108 if (RunningOnValgrind()) { | 109 if (valgrind_) { |
| 109 for (int i = size_; i < new_max_size; i++) | 110 for (int i = size_; i < new_max_size; i++) |
| 110 a[i] = 0xababababU; | 111 a[i] = 0xababababU; |
| 111 } | 112 } |
| 112 delete[] dense_; | 113 delete[] dense_; |
| 113 } | 114 } |
| 114 dense_ = a; | 115 dense_ = a; |
| 115 } | 116 } |
| 116 max_size_ = new_max_size; | 117 max_size_ = new_max_size; |
| 117 } | 118 } |
| 118 | 119 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 // Can sort the sparse array so that future iterations | 162 // Can sort the sparse array so that future iterations |
| 162 // will visit indices in increasing order using | 163 // will visit indices in increasing order using |
| 163 // sort(arr.begin(), arr.end(), arr.less); | 164 // sort(arr.begin(), arr.end(), arr.less); |
| 164 static bool less(int a, int b) { return a < b; } | 165 static bool less(int a, int b) { return a < b; } |
| 165 | 166 |
| 166 private: | 167 private: |
| 167 int size_; | 168 int size_; |
| 168 int max_size_; | 169 int max_size_; |
| 169 int* sparse_to_dense_; | 170 int* sparse_to_dense_; |
| 170 int* dense_; | 171 int* dense_; |
| 172 bool valgrind_; |
| 171 | 173 |
| 172 DISALLOW_EVIL_CONSTRUCTORS(SparseSet); | 174 DISALLOW_EVIL_CONSTRUCTORS(SparseSet); |
| 173 }; | 175 }; |
| 174 | 176 |
| 175 } // namespace re2 | 177 } // namespace re2 |
| 176 | 178 |
| 177 #endif // RE2_UTIL_SPARSE_SET_H__ | 179 #endif // RE2_UTIL_SPARSE_SET_H__ |
| OLD | NEW |