bucket_ranges.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/metrics/bucket_ranges.h"
  5. #include <cmath>
  6. #include "base/metrics/crc32.h"
  7. namespace base {
  8. BucketRanges::BucketRanges(size_t num_ranges)
  9. : ranges_(num_ranges, 0),
  10. checksum_(0) {}
  11. BucketRanges::~BucketRanges() = default;
  12. uint32_t BucketRanges::CalculateChecksum() const {
  13. // Crc of empty ranges_ happens to be 0. This early exit prevents trying to
  14. // take the address of ranges_[0] which will fail for an empty vector even
  15. // if that address is never used.
  16. const size_t ranges_size = ranges_.size();
  17. if (ranges_size == 0)
  18. return 0;
  19. // Checksum is seeded with the ranges "size".
  20. return Crc32(static_cast<uint32_t>(ranges_size), &ranges_[0],
  21. sizeof(ranges_[0]) * ranges_size);
  22. }
  23. bool BucketRanges::HasValidChecksum() const {
  24. return CalculateChecksum() == checksum_;
  25. }
  26. void BucketRanges::ResetChecksum() {
  27. checksum_ = CalculateChecksum();
  28. }
  29. bool BucketRanges::Equals(const BucketRanges* other) const {
  30. if (checksum_ != other->checksum_)
  31. return false;
  32. if (ranges_.size() != other->ranges_.size())
  33. return false;
  34. for (size_t index = 0; index < ranges_.size(); ++index) {
  35. if (ranges_[index] != other->ranges_[index])
  36. return false;
  37. }
  38. return true;
  39. }
  40. } // namespace base