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 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 // Since it doesn't set the value associated with i, | 213 // Since it doesn't set the value associated with i, |
214 // this function is private, only intended as a helper | 214 // this function is private, only intended as a helper |
215 // for other methods. | 215 // for other methods. |
216 inline void create_index(int i); | 216 inline void create_index(int i); |
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 static bool InitMemory() { |
| 224 #ifdef MEMORY_SANITIZER |
| 225 return true; |
| 226 #else |
| 227 return RunningOnValgrind(); |
| 228 #endif |
| 229 } |
| 230 |
223 int size_; | 231 int size_; |
224 int max_size_; | 232 int max_size_; |
225 int* sparse_to_dense_; | 233 int* sparse_to_dense_; |
226 vector<IndexValue> dense_; | 234 vector<IndexValue> dense_; |
227 bool valgrind_; | |
228 | 235 |
229 DISALLOW_EVIL_CONSTRUCTORS(SparseArray); | 236 DISALLOW_COPY_AND_ASSIGN(SparseArray); |
230 }; | 237 }; |
231 | 238 |
232 template<typename Value> | 239 template<typename Value> |
233 SparseArray<Value>::SparseArray() | 240 SparseArray<Value>::SparseArray() |
234 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_(), | 241 : size_(0), max_size_(0), sparse_to_dense_(NULL), dense_() {} |
235 valgrind_(RunningOnValgrindOrMemorySanitizer()) {} | |
236 | 242 |
237 // IndexValue pairs: exposed in SparseArray::iterator. | 243 // IndexValue pairs: exposed in SparseArray::iterator. |
238 template<typename Value> | 244 template<typename Value> |
239 class SparseArray<Value>::IndexValue { | 245 class SparseArray<Value>::IndexValue { |
240 friend class SparseArray; | 246 friend class SparseArray; |
241 public: | 247 public: |
242 typedef int first_type; | 248 typedef int first_type; |
243 typedef Value second_type; | 249 typedef Value second_type; |
244 | 250 |
245 IndexValue() {} | 251 IndexValue() {} |
(...skipping 22 matching lines...) Expand all Loading... |
268 // Invalidates all iterators. | 274 // Invalidates all iterators. |
269 template<typename Value> | 275 template<typename Value> |
270 void SparseArray<Value>::resize(int new_max_size) { | 276 void SparseArray<Value>::resize(int new_max_size) { |
271 DebugCheckInvariants(); | 277 DebugCheckInvariants(); |
272 if (new_max_size > max_size_) { | 278 if (new_max_size > max_size_) { |
273 int* a = new int[new_max_size]; | 279 int* a = new int[new_max_size]; |
274 if (sparse_to_dense_) { | 280 if (sparse_to_dense_) { |
275 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); | 281 memmove(a, sparse_to_dense_, max_size_*sizeof a[0]); |
276 delete[] sparse_to_dense_; | 282 delete[] sparse_to_dense_; |
277 } | 283 } |
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; | 284 sparse_to_dense_ = a; |
284 | 285 |
285 dense_.resize(new_max_size); | 286 dense_.resize(new_max_size); |
| 287 |
| 288 // These don't need to be initialized for correctness, |
| 289 // but Valgrind will warn about use of uninitialized memory, |
| 290 // so initialize the new memory when compiling debug binaries. |
| 291 // Initialize it to garbage to detect bugs in the future. |
| 292 if (InitMemory()) { |
| 293 for (int i = max_size_; i < new_max_size; i++) { |
| 294 sparse_to_dense_[i] = 0xababababU; |
| 295 dense_[i].index_ = 0xababababU; |
| 296 } |
| 297 } |
286 } | 298 } |
287 max_size_ = new_max_size; | 299 max_size_ = new_max_size; |
288 if (size_ > max_size_) | 300 if (size_ > max_size_) |
289 size_ = max_size_; | 301 size_ = max_size_; |
290 DebugCheckInvariants(); | 302 DebugCheckInvariants(); |
291 } | 303 } |
292 | 304 |
293 // Check whether index i is in the array. | 305 // Check whether index i is in the array. |
294 template<typename Value> | 306 template<typename Value> |
295 bool SparseArray<Value>::has_index(int i) const { | 307 bool SparseArray<Value>::has_index(int i) const { |
296 DCHECK_GE(i, 0); | 308 DCHECK_GE(i, 0); |
297 DCHECK_LT(i, max_size_); | 309 DCHECK_LT(i, max_size_); |
298 if (static_cast<uint>(i) >= max_size_) { | 310 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { |
299 return false; | 311 return false; |
300 } | 312 } |
301 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0. | 313 // Unsigned comparison avoids checking sparse_to_dense_[i] < 0. |
302 return (uint)sparse_to_dense_[i] < (uint)size_ && | 314 return (uint)sparse_to_dense_[i] < (uint)size_ && |
303 dense_[sparse_to_dense_[i]].index_ == i; | 315 dense_[sparse_to_dense_[i]].index_ == i; |
304 } | 316 } |
305 | 317 |
306 // Set the value at index i to v. | 318 // Set the value at index i to v. |
307 template<typename Value> | 319 template<typename Value> |
308 typename SparseArray<Value>::iterator SparseArray<Value>::set(int i, Value v) { | 320 typename SparseArray<Value>::iterator SparseArray<Value>::set(int i, Value v) { |
309 DebugCheckInvariants(); | 321 DebugCheckInvariants(); |
310 if (static_cast<uint>(i) >= max_size_) { | 322 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { |
311 // Semantically, end() would be better here, but we already know | 323 // Semantically, end() would be better here, but we already know |
312 // the user did something stupid, so begin() insulates them from | 324 // the user did something stupid, so begin() insulates them from |
313 // dereferencing an invalid pointer. | 325 // dereferencing an invalid pointer. |
314 return begin(); | 326 return begin(); |
315 } | 327 } |
316 if (!has_index(i)) | 328 if (!has_index(i)) |
317 create_index(i); | 329 create_index(i); |
318 return set_existing(i, v); | 330 return set_existing(i, v); |
319 } | 331 } |
320 | 332 |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 DCHECK(has_index(i)); | 374 DCHECK(has_index(i)); |
363 dense_[sparse_to_dense_[i]].second = v; | 375 dense_[sparse_to_dense_[i]].second = v; |
364 DebugCheckInvariants(); | 376 DebugCheckInvariants(); |
365 return dense_.begin() + sparse_to_dense_[i]; | 377 return dense_.begin() + sparse_to_dense_[i]; |
366 } | 378 } |
367 | 379 |
368 template<typename Value> | 380 template<typename Value> |
369 typename SparseArray<Value>::iterator | 381 typename SparseArray<Value>::iterator |
370 SparseArray<Value>::set_new(int i, Value v) { | 382 SparseArray<Value>::set_new(int i, Value v) { |
371 DebugCheckInvariants(); | 383 DebugCheckInvariants(); |
372 if (static_cast<uint>(i) >= max_size_) { | 384 if (static_cast<uint>(i) >= static_cast<uint>(max_size_)) { |
373 // Semantically, end() would be better here, but we already know | 385 // Semantically, end() would be better here, but we already know |
374 // the user did something stupid, so begin() insulates them from | 386 // the user did something stupid, so begin() insulates them from |
375 // dereferencing an invalid pointer. | 387 // dereferencing an invalid pointer. |
376 return begin(); | 388 return begin(); |
377 } | 389 } |
378 DCHECK(!has_index(i)); | 390 DCHECK(!has_index(i)); |
379 create_index(i); | 391 create_index(i); |
380 return set_existing(i, v); | 392 return set_existing(i, v); |
381 } | 393 } |
382 | 394 |
(...skipping 29 matching lines...) Expand all Loading... |
412 DCHECK(!has_index(i)); | 424 DCHECK(!has_index(i)); |
413 DCHECK_LT(size_, max_size_); | 425 DCHECK_LT(size_, max_size_); |
414 sparse_to_dense_[i] = size_; | 426 sparse_to_dense_[i] = size_; |
415 dense_[size_].index_ = i; | 427 dense_[size_].index_ = i; |
416 size_++; | 428 size_++; |
417 } | 429 } |
418 | 430 |
419 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { | 431 template<typename Value> SparseArray<Value>::SparseArray(int max_size) { |
420 max_size_ = max_size; | 432 max_size_ = max_size; |
421 sparse_to_dense_ = new int[max_size]; | 433 sparse_to_dense_ = new int[max_size]; |
422 valgrind_ = RunningOnValgrindOrMemorySanitizer(); | |
423 dense_.resize(max_size); | 434 dense_.resize(max_size); |
424 // Don't need to zero the new memory, but appease Valgrind. | 435 // Don't need to zero the new memory, but appease Valgrind. |
425 if (valgrind_) { | 436 if (InitMemory()) { |
426 for (int i = 0; i < max_size; i++) { | 437 for (int i = 0; i < max_size; i++) { |
427 sparse_to_dense_[i] = 0xababababU; | 438 sparse_to_dense_[i] = 0xababababU; |
428 dense_[i].index_ = 0xababababU; | 439 dense_[i].index_ = 0xababababU; |
429 } | 440 } |
430 } | 441 } |
431 size_ = 0; | 442 size_ = 0; |
432 DebugCheckInvariants(); | 443 DebugCheckInvariants(); |
433 } | 444 } |
434 | 445 |
435 template<typename Value> SparseArray<Value>::~SparseArray() { | 446 template<typename Value> SparseArray<Value>::~SparseArray() { |
436 DebugCheckInvariants(); | 447 DebugCheckInvariants(); |
437 delete[] sparse_to_dense_; | 448 delete[] sparse_to_dense_; |
438 } | 449 } |
439 | 450 |
440 template<typename Value> void SparseArray<Value>::DebugCheckInvariants() const { | 451 template<typename Value> void SparseArray<Value>::DebugCheckInvariants() const { |
441 DCHECK_LE(0, size_); | 452 DCHECK_LE(0, size_); |
442 DCHECK_LE(size_, max_size_); | 453 DCHECK_LE(size_, max_size_); |
443 DCHECK(size_ == 0 || sparse_to_dense_ != NULL); | 454 DCHECK(size_ == 0 || sparse_to_dense_ != NULL); |
444 } | 455 } |
445 | 456 |
446 // Comparison function for sorting. | 457 // Comparison function for sorting. |
447 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, | 458 template<typename Value> bool SparseArray<Value>::less(const IndexValue& a, |
448 const IndexValue& b) { | 459 const IndexValue& b) { |
449 return a.index_ < b.index_; | 460 return a.index_ < b.index_; |
450 } | 461 } |
451 | 462 |
452 } // namespace re2 | 463 } // namespace re2 |
453 | 464 |
454 #endif // RE2_UTIL_SPARSE_ARRAY_H__ | 465 #endif // RE2_UTIL_SPARSE_ARRAY_H__ |
OLD | NEW |