| 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 // SparseArray<T>(m) is a map from integers in [0, m) to T values. | 7 // SparseArray<T>(m) is a map from integers in [0, m) to T values. |
| 8 // It requires (sizeof(T)+sizeof(int))*m memory, but it provides | 8 // It requires (sizeof(T)+sizeof(int))*m memory, but it provides |
| 9 // fast iteration through the elements in the array and fast clearing | 9 // fast iteration through the elements in the array and fast clearing |
| 10 // of the array. The array has a concept of certain elements being | 10 // of the array. The array has a concept of certain elements being |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 224 int max_size_; | 224 int max_size_; |
| 225 int* sparse_to_dense_; | 225 int* sparse_to_dense_; |
| 226 vector<IndexValue> dense_; | 226 vector<IndexValue> dense_; |
| 227 bool valgrind_; | 227 bool valgrind_; |
| 228 | 228 |
| 229 DISALLOW_EVIL_CONSTRUCTORS(SparseArray); | 229 DISALLOW_EVIL_CONSTRUCTORS(SparseArray); |
| 230 }; | 230 }; |
| 231 | 231 |
| 232 template<typename Value> | 232 template<typename Value> |
| 233 SparseArray<Value>::SparseArray() | 233 SparseArray<Value>::SparseArray() |
| 234 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(), valgrind_(Runnin
gOnValgrind()) {} | 234 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(), |
| 235 valgrind_(RunningOnValgrindOrMemorySanitizer()) {} |
| 235 | 236 |
| 236 // IndexValue pairs: exposed in SparseArray::iterator. | 237 // IndexValue pairs: exposed in SparseArray::iterator. |
| 237 template<typename Value> | 238 template<typename Value> |
| 238 class SparseArray<Value>::IndexValue { | 239 class SparseArray<Value>::IndexValue { |
| 239 friend class SparseArray; | 240 friend class SparseArray; |
| 240 public: | 241 public: |
| 241 typedef int first_type; | 242 typedef int first_type; |
| 242 typedef Value second_type; | 243 typedef Value second_type; |
| 243 | 244 |
| 244 IndexValue() {} | 245 IndexValue() {} |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 DCHECK(!has_index(i)); | 412 DCHECK(!has_index(i)); |
| 412 DCHECK_LT(size_, max_size_); | 413 DCHECK_LT(size_, max_size_); |
| 413 sparse_to_dense_[i] = size_; | 414 sparse_to_dense_[i] = size_; |
| 414 dense_[size_].index_ = i; | 415 dense_[size_].index_ = i; |
| 415 size_++; | 416 size_++; |
| 416 } | 417 } |
| 417 | 418 |
| 418 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { | 419 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { |
| 419 max_size_ = max_size; | 420 max_size_ = max_size; |
| 420 sparse_to_dense_ = new int[max_size]; | 421 sparse_to_dense_ = new int[max_size]; |
| 421 valgrind_ = RunningOnValgrind(); | 422 valgrind_ = RunningOnValgrindOrMemorySanitizer(); |
| 422 dense_.resize(max_size); | 423 dense_.resize(max_size); |
| 423 // Don't need to zero the new memory, but appease Valgrind. | 424 // Don't need to zero the new memory, but appease Valgrind. |
| 424 if (valgrind_) { | 425 if (valgrind_) { |
| 425 for (int i = 0; i < max_size; i++) { | 426 for (int i = 0; i < max_size; i++) { |
| 426 sparse_to_dense_[i] = 0xababababU; | 427 sparse_to_dense_[i] = 0xababababU; |
| 427 dense_[i].index_ = 0xababababU; | 428 dense_[i].index_ = 0xababababU; |
| 428 } | 429 } |
| 429 } | 430 } |
| 430 size_ = 0; | 431 size_ = 0; |
| 431 DebugCheckInvariants(); | 432 DebugCheckInvariants(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 444 | 445 |
| 445 // Comparison function for sorting. | 446 // Comparison function for sorting. |
| 446 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, | 447 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, |
| 447 const IndexValue& b) { | 448 const IndexValue& b) { |
| 448 return a.index_ < b.index_; | 449 return a.index_ < b.index_; |
| 449 } | 450 } |
| 450 | 451 |
| 451 } // namespace re2 | 452 } // namespace re2 |
| 452 | 453 |
| 453 #endif // RE2_UTIL_SPARSE_ARRAY_H__ | 454 #endif // RE2_UTIL_SPARSE_ARRAY_H__ |
| OLD | NEW |