| 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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 217 | 217 |
| 218 // In debug mode, verify that some invariant properties of the class | 218 // In debug mode, verify that some invariant properties of the class |
| 219 // are being maintained. This is called at the end of the constructor | 219 // are being maintained. This is called at the end of the constructor |
| 220 // and at the beginning and end of all public non-const member functions. | 220 // and at the beginning and end of all public non-const member functions. |
| 221 inline void DebugCheckInvariants() const; | 221 inline void DebugCheckInvariants() const; |
| 222 | 222 |
| 223 int size_; | 223 int size_; |
| 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 | 228 |
| 228 DISALLOW_EVIL_CONSTRUCTORS(SparseArray); | 229 DISALLOW_EVIL_CONSTRUCTORS(SparseArray); |
| 229 }; | 230 }; |
| 230 | 231 |
| 231 template<typename Value> | 232 template<typename Value> |
| 232 SparseArray<Value>::SparseArray() | 233 SparseArray<Value>::SparseArray() |
| 233 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_() {} | 234 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(), valgrind_(Runnin
gOnValgrind()) {} |
| 234 | 235 |
| 235 // IndexValue pairs: exposed in SparseArray::iterator. | 236 // IndexValue pairs: exposed in SparseArray::iterator. |
| 236 template<typename Value> | 237 template<typename Value> |
| 237 class SparseArray<Value>::IndexValue { | 238 class SparseArray<Value>::IndexValue { |
| 238 friend class SparseArray; | 239 friend class SparseArray; |
| 239 public: | 240 public: |
| 240 typedef int first_type; | 241 typedef int first_type; |
| 241 typedef Value second_type; | 242 typedef Value second_type; |
| 242 | 243 |
| 243 IndexValue() {} | 244 IndexValue() {} |
| (...skipping 21 matching lines...) Expand all Loading... |
| 265 // Change the maximum size of the array. | 266 // Change the maximum size of the array. |
| 266 // Invalidates all iterators. | 267 // Invalidates all iterators. |
| 267 template<typename Value> | 268 template<typename Value> |
| 268 void SparseArray<Value>::resize(int new_max_size) { | 269 void SparseArray<Value>::resize(int new_max_size) { |
| 269 DebugCheckInvariants(); | 270 DebugCheckInvariants(); |
| 270 if (new_max_size > max_size_) { | 271 if (new_max_size > max_size_) { |
| 271 int* a = new int[new_max_size]; | 272 int* a = new int[new_max_size]; |
| 272 if (sparse_to_dense_) { | 273 if (sparse_to_dense_) { |
| 273 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); | 274 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); |
| 274 // Don't need to zero the memory but appease Valgrind. | 275 // Don't need to zero the memory but appease Valgrind. |
| 275 if (RunningOnValgrind()) { | 276 if (valgrind_) { |
| 276 for (int i = max_size_; i < new_max_size; i++) | 277 for (int i = max_size_; i < new_max_size; i++) |
| 277 a[i] = 0xababababU; | 278 a[i] = 0xababababU; |
| 278 } | 279 } |
| 279 delete[] sparse_to_dense_; | 280 delete[] sparse_to_dense_; |
| 280 } | 281 } |
| 281 sparse_to_dense_ = a; | 282 sparse_to_dense_ = a; |
| 282 | 283 |
| 283 dense_.resize(new_max_size); | 284 dense_.resize(new_max_size); |
| 284 } | 285 } |
| 285 max_size_ = new_max_size; | 286 max_size_ = new_max_size; |
| (...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 DCHECK(!has_index(i)); | 411 DCHECK(!has_index(i)); |
| 411 DCHECK_LT(size_, max_size_); | 412 DCHECK_LT(size_, max_size_); |
| 412 sparse_to_dense_[i] = size_; | 413 sparse_to_dense_[i] = size_; |
| 413 dense_[size_].index_ = i; | 414 dense_[size_].index_ = i; |
| 414 size_++; | 415 size_++; |
| 415 } | 416 } |
| 416 | 417 |
| 417 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { | 418 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { |
| 418 max_size_ = max_size; | 419 max_size_ = max_size; |
| 419 sparse_to_dense_ = new int[max_size]; | 420 sparse_to_dense_ = new int[max_size]; |
| 421 valgrind_ = RunningOnValgrind(); |
| 420 dense_.resize(max_size); | 422 dense_.resize(max_size); |
| 421 // Don't need to zero the new memory, but appease Valgrind. | 423 // Don't need to zero the new memory, but appease Valgrind. |
| 422 if (RunningOnValgrind()) { | 424 if (valgrind_) { |
| 423 for (int i = 0; i < max_size; i++) { | 425 for (int i = 0; i < max_size; i++) { |
| 424 sparse_to_dense_[i] = 0xababababU; | 426 sparse_to_dense_[i] = 0xababababU; |
| 425 dense_[i].index_ = 0xababababU; | 427 dense_[i].index_ = 0xababababU; |
| 426 } | 428 } |
| 427 } | 429 } |
| 428 size_ = 0; | 430 size_ = 0; |
| 429 DebugCheckInvariants(); | 431 DebugCheckInvariants(); |
| 430 } | 432 } |
| 431 | 433 |
| 432 template<typename Value> SparseArray<Value>::~SparseArray() { | 434 template<typename Value> SparseArray<Value>::~SparseArray() { |
| 433 DebugCheckInvariants(); | 435 DebugCheckInvariants(); |
| 434 delete[] sparse_to_dense_; | 436 delete[] sparse_to_dense_; |
| 435 } | 437 } |
| 436 | 438 |
| 437 template<typename Value> void SparseArray<Value>::DebugCheckInvariants() const { | 439 template<typename Value> void SparseArray<Value>::DebugCheckInvariants() const { |
| 438 DCHECK_LE(0, size_); | 440 DCHECK_LE(0, size_); |
| 439 DCHECK_LE(size_, max_size_); | 441 DCHECK_LE(size_, max_size_); |
| 440 DCHECK(size_ == 0 || sparse_to_dense_ != NULL); | 442 DCHECK(size_ == 0 || sparse_to_dense_ != NULL); |
| 441 } | 443 } |
| 442 | 444 |
| 443 // Comparison function for sorting. | 445 // Comparison function for sorting. |
| 444 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, | 446 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, |
| 445 const IndexValue& b) { | 447 const IndexValue& b) { |
| 446 return a.index_ < b.index_; | 448 return a.index_ < b.index_; |
| 447 } | 449 } |
| 448 | 450 |
| 449 } // namespace re2 | 451 } // namespace re2 |
| 450 | 452 |
| 451 #endif // RE2_UTIL_SPARSE_ARRAY_H__ | 453 #endif // RE2_UTIL_SPARSE_ARRAY_H__ |
| OLD | NEW |