Commit 27147e68 authored by Jan Wilken Dörrie's avatar Jan Wilken Dörrie Committed by Commit Bot

[base] Rename CheckedRandomAccessIterator to CheckedContiguousIterator

This change renames base::CheckedRandomAccess(Const)Iterator to
base::CheckedContiguous(Const)Iterator.

This change is motivated by the fact that this iterator not only
fulfils the requirements of RandomAccessIterator [1], but also
ContiguousIterator [2].

Furthermore, the implementation of the iterator assumes it is pointing
into a single contiguous array, and thus is unsuitable for containers
that have random access, but don't operate on an underlying array, such
as std::deque.

[1] https://en.cppreference.com/w/cpp/named_req/RandomAccessIterator
[2] https://en.cppreference.com/w/cpp/named_req/ContiguousIterator

Bug: 817982
Change-Id: I7186dfc3b2849818b9f83d62e05ebc5a79aaf5ec
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1811377
Commit-Queue: Jan Wilken Dörrie <jdoerrie@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarChris Palmer <palmer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#697793}
parent d851e3e0
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
namespace base { namespace base {
template <typename T> template <typename T>
class CheckedRandomAccessIterator { class CheckedContiguousIterator {
public: public:
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
using value_type = std::remove_cv_t<T>; using value_type = std::remove_cv_t<T>;
...@@ -25,18 +25,17 @@ class CheckedRandomAccessIterator { ...@@ -25,18 +25,17 @@ class CheckedRandomAccessIterator {
// Required for converting constructor below. // Required for converting constructor below.
template <typename U> template <typename U>
friend class CheckedRandomAccessIterator; friend class CheckedContiguousIterator;
CheckedRandomAccessIterator() = default; CheckedContiguousIterator() = default;
CheckedRandomAccessIterator(T* start, const T* end) CheckedContiguousIterator(T* start, const T* end)
: CheckedRandomAccessIterator(start, start, end) {} : CheckedContiguousIterator(start, start, end) {}
CheckedRandomAccessIterator(const T* start, T* current, const T* end) CheckedContiguousIterator(const T* start, T* current, const T* end)
: start_(start), current_(current), end_(end) { : start_(start), current_(current), end_(end) {
CHECK(start <= current); CHECK(start <= current);
CHECK(current <= end); CHECK(current <= end);
} }
CheckedRandomAccessIterator(const CheckedRandomAccessIterator& other) = CheckedContiguousIterator(const CheckedContiguousIterator& other) = default;
default;
// Converting constructor allowing conversions like CRAI<T> to CRAI<const T>, // Converting constructor allowing conversions like CRAI<T> to CRAI<const T>,
// but disallowing CRAI<const T> to CRAI<T> or CRAI<Derived> to CRAI<Base>, // but disallowing CRAI<const T> to CRAI<T> or CRAI<Derived> to CRAI<Base>,
...@@ -46,7 +45,7 @@ class CheckedRandomAccessIterator { ...@@ -46,7 +45,7 @@ class CheckedRandomAccessIterator {
template < template <
typename U, typename U,
std::enable_if_t<std::is_convertible<U (*)[], T (*)[]>::value>* = nullptr> std::enable_if_t<std::is_convertible<U (*)[], T (*)[]>::value>* = nullptr>
CheckedRandomAccessIterator(const CheckedRandomAccessIterator<U>& other) CheckedContiguousIterator(const CheckedContiguousIterator<U>& other)
: start_(other.start_), current_(other.current_), end_(other.end_) { : start_(other.start_), current_(other.current_), end_(other.end_) {
// We explicitly don't delegate to the 3-argument constructor here. Its // We explicitly don't delegate to the 3-argument constructor here. Its
// CHECKs would be redundant, since we expect |other| to maintain its own // CHECKs would be redundant, since we expect |other| to maintain its own
...@@ -55,66 +54,66 @@ class CheckedRandomAccessIterator { ...@@ -55,66 +54,66 @@ class CheckedRandomAccessIterator {
DCHECK(other.current_ <= other.end_); DCHECK(other.current_ <= other.end_);
} }
~CheckedRandomAccessIterator() = default; ~CheckedContiguousIterator() = default;
CheckedRandomAccessIterator& operator=( CheckedContiguousIterator& operator=(const CheckedContiguousIterator& other) =
const CheckedRandomAccessIterator& other) = default; default;
bool operator==(const CheckedRandomAccessIterator& other) const { bool operator==(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ == other.current_; return current_ == other.current_;
} }
bool operator!=(const CheckedRandomAccessIterator& other) const { bool operator!=(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ != other.current_; return current_ != other.current_;
} }
bool operator<(const CheckedRandomAccessIterator& other) const { bool operator<(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ < other.current_; return current_ < other.current_;
} }
bool operator<=(const CheckedRandomAccessIterator& other) const { bool operator<=(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ <= other.current_; return current_ <= other.current_;
} }
bool operator>(const CheckedRandomAccessIterator& other) const { bool operator>(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ > other.current_; return current_ > other.current_;
} }
bool operator>=(const CheckedRandomAccessIterator& other) const { bool operator>=(const CheckedContiguousIterator& other) const {
CheckComparable(other); CheckComparable(other);
return current_ >= other.current_; return current_ >= other.current_;
} }
CheckedRandomAccessIterator& operator++() { CheckedContiguousIterator& operator++() {
CHECK(current_ != end_); CHECK(current_ != end_);
++current_; ++current_;
return *this; return *this;
} }
CheckedRandomAccessIterator operator++(int) { CheckedContiguousIterator operator++(int) {
CheckedRandomAccessIterator old = *this; CheckedContiguousIterator old = *this;
++*this; ++*this;
return old; return old;
} }
CheckedRandomAccessIterator& operator--() { CheckedContiguousIterator& operator--() {
CHECK(current_ != start_); CHECK(current_ != start_);
--current_; --current_;
return *this; return *this;
} }
CheckedRandomAccessIterator& operator--(int) { CheckedContiguousIterator& operator--(int) {
CheckedRandomAccessIterator old = *this; CheckedContiguousIterator old = *this;
--*this; --*this;
return old; return old;
} }
CheckedRandomAccessIterator& operator+=(difference_type rhs) { CheckedContiguousIterator& operator+=(difference_type rhs) {
if (rhs > 0) { if (rhs > 0) {
CHECK_LE(rhs, end_ - current_); CHECK_LE(rhs, end_ - current_);
} else { } else {
...@@ -124,13 +123,13 @@ class CheckedRandomAccessIterator { ...@@ -124,13 +123,13 @@ class CheckedRandomAccessIterator {
return *this; return *this;
} }
CheckedRandomAccessIterator operator+(difference_type rhs) const { CheckedContiguousIterator operator+(difference_type rhs) const {
CheckedRandomAccessIterator it = *this; CheckedContiguousIterator it = *this;
it += rhs; it += rhs;
return it; return it;
} }
CheckedRandomAccessIterator& operator-=(difference_type rhs) { CheckedContiguousIterator& operator-=(difference_type rhs) {
if (rhs < 0) { if (rhs < 0) {
CHECK_LE(rhs, end_ - current_); CHECK_LE(rhs, end_ - current_);
} else { } else {
...@@ -140,14 +139,14 @@ class CheckedRandomAccessIterator { ...@@ -140,14 +139,14 @@ class CheckedRandomAccessIterator {
return *this; return *this;
} }
CheckedRandomAccessIterator operator-(difference_type rhs) const { CheckedContiguousIterator operator-(difference_type rhs) const {
CheckedRandomAccessIterator it = *this; CheckedContiguousIterator it = *this;
it -= rhs; it -= rhs;
return it; return it;
} }
friend difference_type operator-(const CheckedRandomAccessIterator& lhs, friend difference_type operator-(const CheckedContiguousIterator& lhs,
const CheckedRandomAccessIterator& rhs) { const CheckedContiguousIterator& rhs) {
CHECK(lhs.start_ == rhs.start_); CHECK(lhs.start_ == rhs.start_);
CHECK(lhs.end_ == rhs.end_); CHECK(lhs.end_ == rhs.end_);
return lhs.current_ - rhs.current_; return lhs.current_ - rhs.current_;
...@@ -169,9 +168,9 @@ class CheckedRandomAccessIterator { ...@@ -169,9 +168,9 @@ class CheckedRandomAccessIterator {
return current_[rhs]; return current_[rhs];
} }
static bool IsRangeMoveSafe(const CheckedRandomAccessIterator& from_begin, static bool IsRangeMoveSafe(const CheckedContiguousIterator& from_begin,
const CheckedRandomAccessIterator& from_end, const CheckedContiguousIterator& from_end,
const CheckedRandomAccessIterator& to) const CheckedContiguousIterator& to)
WARN_UNUSED_RESULT { WARN_UNUSED_RESULT {
if (from_end < from_begin) if (from_end < from_begin)
return false; return false;
...@@ -186,7 +185,7 @@ class CheckedRandomAccessIterator { ...@@ -186,7 +185,7 @@ class CheckedRandomAccessIterator {
} }
private: private:
void CheckComparable(const CheckedRandomAccessIterator& other) const { void CheckComparable(const CheckedContiguousIterator& other) const {
CHECK_EQ(start_, other.start_); CHECK_EQ(start_, other.start_);
CHECK_EQ(end_, other.end_); CHECK_EQ(end_, other.end_);
} }
...@@ -197,7 +196,7 @@ class CheckedRandomAccessIterator { ...@@ -197,7 +196,7 @@ class CheckedRandomAccessIterator {
}; };
template <typename T> template <typename T>
using CheckedRandomAccessConstIterator = CheckedRandomAccessIterator<const T>; using CheckedContiguousConstIterator = CheckedContiguousIterator<const T>;
} // namespace base } // namespace base
......
...@@ -224,8 +224,8 @@ class span : public internal::ExtentStorage<Extent> { ...@@ -224,8 +224,8 @@ class span : public internal::ExtentStorage<Extent> {
using difference_type = ptrdiff_t; using difference_type = ptrdiff_t;
using pointer = T*; using pointer = T*;
using reference = T&; using reference = T&;
using iterator = CheckedRandomAccessIterator<T>; using iterator = CheckedContiguousIterator<T>;
using const_iterator = CheckedRandomAccessConstIterator<T>; using const_iterator = CheckedContiguousConstIterator<T>;
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = Extent; static constexpr index_type extent = Extent;
......
...@@ -1260,47 +1260,47 @@ TEST(SpanTest, IteratorIsRangeMoveSafe) { ...@@ -1260,47 +1260,47 @@ TEST(SpanTest, IteratorIsRangeMoveSafe) {
// Overlapping ranges. // Overlapping ranges.
for (const int dest_start_index : kOverlappingStartIndexes) { for (const int dest_start_index : kOverlappingStartIndexes) {
EXPECT_FALSE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe( EXPECT_FALSE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.begin(), span.end(), span.begin(), span.end(),
CheckedRandomAccessIterator<const int>( CheckedContiguousIterator<const int>(
span.data() + dest_start_index, span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements))); span.data() + dest_start_index + kNumElements)));
EXPECT_FALSE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe( EXPECT_FALSE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cend(), span.cbegin(), span.cend(),
CheckedRandomAccessConstIterator<const int>( CheckedContiguousConstIterator<const int>(
span.data() + dest_start_index, span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements))); span.data() + dest_start_index + kNumElements)));
} }
// Non-overlapping ranges. // Non-overlapping ranges.
for (const int dest_start_index : kNonOverlappingStartIndexes) { for (const int dest_start_index : kNonOverlappingStartIndexes) {
EXPECT_TRUE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe( EXPECT_TRUE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.begin(), span.end(), span.begin(), span.end(),
CheckedRandomAccessIterator<const int>( CheckedContiguousIterator<const int>(
span.data() + dest_start_index, span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements))); span.data() + dest_start_index + kNumElements)));
EXPECT_TRUE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe( EXPECT_TRUE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cend(), span.cbegin(), span.cend(),
CheckedRandomAccessConstIterator<const int>( CheckedContiguousConstIterator<const int>(
span.data() + dest_start_index, span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements))); span.data() + dest_start_index + kNumElements)));
} }
// IsRangeMoveSafe is true if the length to be moved is 0. // IsRangeMoveSafe is true if the length to be moved is 0.
EXPECT_TRUE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe( EXPECT_TRUE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.begin(), span.begin(), span.begin(), span.begin(),
CheckedRandomAccessIterator<const int>(span.data(), span.data()))); CheckedContiguousIterator<const int>(span.data(), span.data())));
EXPECT_TRUE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe( EXPECT_TRUE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cbegin(), span.cbegin(), span.cbegin(),
CheckedRandomAccessConstIterator<const int>(span.data(), span.data()))); CheckedContiguousConstIterator<const int>(span.data(), span.data())));
// IsRangeMoveSafe is false if end < begin. // IsRangeMoveSafe is false if end < begin.
EXPECT_FALSE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe( EXPECT_FALSE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.end(), span.begin(), span.end(), span.begin(),
CheckedRandomAccessIterator<const int>(span.data(), span.data()))); CheckedContiguousIterator<const int>(span.data(), span.data())));
EXPECT_FALSE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe( EXPECT_FALSE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cend(), span.cbegin(), span.cend(), span.cbegin(),
CheckedRandomAccessConstIterator<const int>(span.data(), span.data()))); CheckedContiguousConstIterator<const int>(span.data(), span.data())));
} }
TEST(SpanTest, Sort) { TEST(SpanTest, Sort) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment