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