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 255 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
266 | 266 |
267 // Change the maximum size of the array. | 267 // Change the maximum size of the array. |
268 // Invalidates all iterators. | 268 // Invalidates all iterators. |
269 template<typename Value> | 269 template<typename Value> |
270 void SparseArray<Value>::resize(int new_max_size) { | 270 void SparseArray<Value>::resize(int new_max_size) { |
271 DebugCheckInvariants(); | 271 DebugCheckInvariants(); |
272 if (new_max_size > max_size_) { | 272 if (new_max_size > max_size_) { |
273 int* a = new int[new_max_size]; | 273 int* a = new int[new_max_size]; |
274 if (sparse_to_dense_) { | 274 if (sparse_to_dense_) { |
275 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); | 275 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); |
276 // Don't need to zero the memory but appease Valgrind. | |
277 if (valgrind_) { | |
278 for (int i = max_size_; i < new_max_size; i++) | |
279 a[i] = 0xababababU; | |
280 } | |
281 delete[] sparse_to_dense_; | 276 delete[] sparse_to_dense_; |
282 } | 277 } |
| 278 // Don't need to zero the memory but appease Valgrind. |
| 279 if (valgrind_) { |
| 280 for (int i = max_size_; i < new_max_size; i++) |
| 281 a[i] = 0xababababU; |
| 282 } |
283 sparse_to_dense_ = a; | 283 sparse_to_dense_ = a; |
284 | 284 |
285 dense_.resize(new_max_size); | 285 dense_.resize(new_max_size); |
286 } | 286 } |
287 max_size_ = new_max_size; | 287 max_size_ = new_max_size; |
288 if (size_ > max_size_) | 288 if (size_ > max_size_) |
289 size_ = max_size_; | 289 size_ = max_size_; |
290 DebugCheckInvariants(); | 290 DebugCheckInvariants(); |
291 } | 291 } |
292 | 292 |
(...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
445 | 445 |
446 // Comparison function for sorting. | 446 // Comparison function for sorting. |
447 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, | 447 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, |
448 const IndexValue& b) { | 448 const IndexValue& b) { |
449 return a.index_ < b.index_; | 449 return a.index_ < b.index_; |
450 } | 450 } |
451 | 451 |
452 } // namespace re2 | 452 } // namespace re2 |
453 | 453 |
454 #endif // RE2_UTIL_SPARSE_ARRAY_H__ | 454 #endif // RE2_UTIL_SPARSE_ARRAY_H__ |
OLD | NEW |