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 @@
namespace base {
template <typename T>
class CheckedRandomAccessIterator {
class CheckedContiguousIterator {
public:
using difference_type = std::ptrdiff_t;
using value_type = std::remove_cv_t<T>;
......@@ -25,18 +25,17 @@ class CheckedRandomAccessIterator {
// Required for converting constructor below.
template <typename U>
friend class CheckedRandomAccessIterator;
friend class CheckedContiguousIterator;
CheckedRandomAccessIterator() = default;
CheckedRandomAccessIterator(T* start, const T* end)
: CheckedRandomAccessIterator(start, start, end) {}
CheckedRandomAccessIterator(const T* start, T* current, const T* end)
CheckedContiguousIterator() = default;
CheckedContiguousIterator(T* start, const T* end)
: CheckedContiguousIterator(start, start, end) {}
CheckedContiguousIterator(const T* start, T* current, const T* end)
: start_(start), current_(current), end_(end) {
CHECK(start <= current);
CHECK(current <= end);
}
CheckedRandomAccessIterator(const CheckedRandomAccessIterator& other) =
default;
CheckedContiguousIterator(const CheckedContiguousIterator& other) = default;
// 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>,
......@@ -46,7 +45,7 @@ class CheckedRandomAccessIterator {
template <
typename U,
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_) {
// We explicitly don't delegate to the 3-argument constructor here. Its
// CHECKs would be redundant, since we expect |other| to maintain its own
......@@ -55,66 +54,66 @@ class CheckedRandomAccessIterator {
DCHECK(other.current_ <= other.end_);
}
~CheckedRandomAccessIterator() = default;
~CheckedContiguousIterator() = default;
CheckedRandomAccessIterator& operator=(
const CheckedRandomAccessIterator& other) = default;
CheckedContiguousIterator& operator=(const CheckedContiguousIterator& other) =
default;
bool operator==(const CheckedRandomAccessIterator& other) const {
bool operator==(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ == other.current_;
}
bool operator!=(const CheckedRandomAccessIterator& other) const {
bool operator!=(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ != other.current_;
}
bool operator<(const CheckedRandomAccessIterator& other) const {
bool operator<(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ < other.current_;
}
bool operator<=(const CheckedRandomAccessIterator& other) const {
bool operator<=(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ <= other.current_;
}
bool operator>(const CheckedRandomAccessIterator& other) const {
bool operator>(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ > other.current_;
}
bool operator>=(const CheckedRandomAccessIterator& other) const {
bool operator>=(const CheckedContiguousIterator& other) const {
CheckComparable(other);
return current_ >= other.current_;
}
CheckedRandomAccessIterator& operator++() {
CheckedContiguousIterator& operator++() {
CHECK(current_ != end_);
++current_;
return *this;
}
CheckedRandomAccessIterator operator++(int) {
CheckedRandomAccessIterator old = *this;
CheckedContiguousIterator operator++(int) {
CheckedContiguousIterator old = *this;
++*this;
return old;
}
CheckedRandomAccessIterator& operator--() {
CheckedContiguousIterator& operator--() {
CHECK(current_ != start_);
--current_;
return *this;
}
CheckedRandomAccessIterator& operator--(int) {
CheckedRandomAccessIterator old = *this;
CheckedContiguousIterator& operator--(int) {
CheckedContiguousIterator old = *this;
--*this;
return old;
}
CheckedRandomAccessIterator& operator+=(difference_type rhs) {
CheckedContiguousIterator& operator+=(difference_type rhs) {
if (rhs > 0) {
CHECK_LE(rhs, end_ - current_);
} else {
......@@ -124,13 +123,13 @@ class CheckedRandomAccessIterator {
return *this;
}
CheckedRandomAccessIterator operator+(difference_type rhs) const {
CheckedRandomAccessIterator it = *this;
CheckedContiguousIterator operator+(difference_type rhs) const {
CheckedContiguousIterator it = *this;
it += rhs;
return it;
}
CheckedRandomAccessIterator& operator-=(difference_type rhs) {
CheckedContiguousIterator& operator-=(difference_type rhs) {
if (rhs < 0) {
CHECK_LE(rhs, end_ - current_);
} else {
......@@ -140,14 +139,14 @@ class CheckedRandomAccessIterator {
return *this;
}
CheckedRandomAccessIterator operator-(difference_type rhs) const {
CheckedRandomAccessIterator it = *this;
CheckedContiguousIterator operator-(difference_type rhs) const {
CheckedContiguousIterator it = *this;
it -= rhs;
return it;
}
friend difference_type operator-(const CheckedRandomAccessIterator& lhs,
const CheckedRandomAccessIterator& rhs) {
friend difference_type operator-(const CheckedContiguousIterator& lhs,
const CheckedContiguousIterator& rhs) {
CHECK(lhs.start_ == rhs.start_);
CHECK(lhs.end_ == rhs.end_);
return lhs.current_ - rhs.current_;
......@@ -169,9 +168,9 @@ class CheckedRandomAccessIterator {
return current_[rhs];
}
static bool IsRangeMoveSafe(const CheckedRandomAccessIterator& from_begin,
const CheckedRandomAccessIterator& from_end,
const CheckedRandomAccessIterator& to)
static bool IsRangeMoveSafe(const CheckedContiguousIterator& from_begin,
const CheckedContiguousIterator& from_end,
const CheckedContiguousIterator& to)
WARN_UNUSED_RESULT {
if (from_end < from_begin)
return false;
......@@ -186,7 +185,7 @@ class CheckedRandomAccessIterator {
}
private:
void CheckComparable(const CheckedRandomAccessIterator& other) const {
void CheckComparable(const CheckedContiguousIterator& other) const {
CHECK_EQ(start_, other.start_);
CHECK_EQ(end_, other.end_);
}
......@@ -197,7 +196,7 @@ class CheckedRandomAccessIterator {
};
template <typename T>
using CheckedRandomAccessConstIterator = CheckedRandomAccessIterator<const T>;
using CheckedContiguousConstIterator = CheckedContiguousIterator<const T>;
} // namespace base
......
......@@ -224,8 +224,8 @@ class span : public internal::ExtentStorage<Extent> {
using difference_type = ptrdiff_t;
using pointer = T*;
using reference = T&;
using iterator = CheckedRandomAccessIterator<T>;
using const_iterator = CheckedRandomAccessConstIterator<T>;
using iterator = CheckedContiguousIterator<T>;
using const_iterator = CheckedContiguousConstIterator<T>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = Extent;
......
......@@ -1260,47 +1260,47 @@ TEST(SpanTest, IteratorIsRangeMoveSafe) {
// Overlapping ranges.
for (const int dest_start_index : kOverlappingStartIndexes) {
EXPECT_FALSE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe(
EXPECT_FALSE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.begin(), span.end(),
CheckedRandomAccessIterator<const int>(
CheckedContiguousIterator<const int>(
span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements)));
EXPECT_FALSE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe(
EXPECT_FALSE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cend(),
CheckedRandomAccessConstIterator<const int>(
CheckedContiguousConstIterator<const int>(
span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements)));
}
// Non-overlapping ranges.
for (const int dest_start_index : kNonOverlappingStartIndexes) {
EXPECT_TRUE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe(
EXPECT_TRUE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.begin(), span.end(),
CheckedRandomAccessIterator<const int>(
CheckedContiguousIterator<const int>(
span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements)));
EXPECT_TRUE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe(
EXPECT_TRUE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cend(),
CheckedRandomAccessConstIterator<const int>(
CheckedContiguousConstIterator<const int>(
span.data() + dest_start_index,
span.data() + dest_start_index + kNumElements)));
}
// 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(),
CheckedRandomAccessIterator<const int>(span.data(), span.data())));
EXPECT_TRUE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe(
CheckedContiguousIterator<const int>(span.data(), span.data())));
EXPECT_TRUE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cbegin(), span.cbegin(),
CheckedRandomAccessConstIterator<const int>(span.data(), span.data())));
CheckedContiguousConstIterator<const int>(span.data(), span.data())));
// IsRangeMoveSafe is false if end < begin.
EXPECT_FALSE(CheckedRandomAccessIterator<const int>::IsRangeMoveSafe(
EXPECT_FALSE(CheckedContiguousIterator<const int>::IsRangeMoveSafe(
span.end(), span.begin(),
CheckedRandomAccessIterator<const int>(span.data(), span.data())));
EXPECT_FALSE(CheckedRandomAccessConstIterator<const int>::IsRangeMoveSafe(
CheckedContiguousIterator<const int>(span.data(), span.data())));
EXPECT_FALSE(CheckedContiguousConstIterator<const int>::IsRangeMoveSafe(
span.cend(), span.cbegin(),
CheckedRandomAccessConstIterator<const int>(span.data(), span.data())));
CheckedContiguousConstIterator<const int>(span.data(), span.data())));
}
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