[ADT] Pass ArrayRef::slice size_t instead of unsigned.

Also fix slice wrappers drop_front and drop_back.
The unittests are pretty awkward, but do the job; alternatives
welcome!

..and yes, I do have ArrayRefs with more than 4 billion elements.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@271546 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Ahmed Bougacha 2016-06-02 17:26:03 +00:00
parent 396d48087d
commit 408a761951
2 changed files with 23 additions and 8 deletions

View File

@ -161,26 +161,26 @@ namespace llvm {
} }
/// slice(n) - Chop off the first N elements of the array. /// slice(n) - Chop off the first N elements of the array.
ArrayRef<T> slice(unsigned N) const { ArrayRef<T> slice(size_t N) const {
assert(N <= size() && "Invalid specifier"); assert(N <= size() && "Invalid specifier");
return ArrayRef<T>(data()+N, size()-N); return ArrayRef<T>(data()+N, size()-N);
} }
/// slice(n, m) - Chop off the first N elements of the array, and keep M /// slice(n, m) - Chop off the first N elements of the array, and keep M
/// elements in the array. /// elements in the array.
ArrayRef<T> slice(unsigned N, unsigned M) const { ArrayRef<T> slice(size_t N, size_t M) const {
assert(N+M <= size() && "Invalid specifier"); assert(N+M <= size() && "Invalid specifier");
return ArrayRef<T>(data()+N, M); return ArrayRef<T>(data()+N, M);
} }
/// \brief Drop the first \p N elements of the array. /// \brief Drop the first \p N elements of the array.
ArrayRef<T> drop_front(unsigned N = 1) const { ArrayRef<T> drop_front(size_t N = 1) const {
assert(size() >= N && "Dropping more elements than exist"); assert(size() >= N && "Dropping more elements than exist");
return slice(N, size() - N); return slice(N, size() - N);
} }
/// \brief Drop the last \p N elements of the array. /// \brief Drop the last \p N elements of the array.
ArrayRef<T> drop_back(unsigned N = 1) const { ArrayRef<T> drop_back(size_t N = 1) const {
assert(size() >= N && "Dropping more elements than exist"); assert(size() >= N && "Dropping more elements than exist");
return slice(0, size() - N); return slice(0, size() - N);
} }
@ -279,25 +279,25 @@ namespace llvm {
} }
/// slice(n) - Chop off the first N elements of the array. /// slice(n) - Chop off the first N elements of the array.
MutableArrayRef<T> slice(unsigned N) const { MutableArrayRef<T> slice(size_t N) const {
assert(N <= this->size() && "Invalid specifier"); assert(N <= this->size() && "Invalid specifier");
return MutableArrayRef<T>(data()+N, this->size()-N); return MutableArrayRef<T>(data()+N, this->size()-N);
} }
/// slice(n, m) - Chop off the first N elements of the array, and keep M /// slice(n, m) - Chop off the first N elements of the array, and keep M
/// elements in the array. /// elements in the array.
MutableArrayRef<T> slice(unsigned N, unsigned M) const { MutableArrayRef<T> slice(size_t N, size_t M) const {
assert(N+M <= this->size() && "Invalid specifier"); assert(N+M <= this->size() && "Invalid specifier");
return MutableArrayRef<T>(data()+N, M); return MutableArrayRef<T>(data()+N, M);
} }
/// \brief Drop the first \p N elements of the array. /// \brief Drop the first \p N elements of the array.
MutableArrayRef<T> drop_front(unsigned N = 1) const { MutableArrayRef<T> drop_front(size_t N = 1) const {
assert(this->size() >= N && "Dropping more elements than exist"); assert(this->size() >= N && "Dropping more elements than exist");
return slice(N, this->size() - N); return slice(N, this->size() - N);
} }
MutableArrayRef<T> drop_back(unsigned N = 1) const { MutableArrayRef<T> drop_back(size_t N = 1) const {
assert(this->size() >= N && "Dropping more elements than exist"); assert(this->size() >= N && "Dropping more elements than exist");
return slice(0, this->size() - N); return slice(0, this->size() - N);
} }

View File

@ -65,6 +65,10 @@ TEST(ArrayRefTest, DropBack) {
ArrayRef<int> AR1(TheNumbers); ArrayRef<int> AR1(TheNumbers);
ArrayRef<int> AR2(TheNumbers, AR1.size() - 1); ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
EXPECT_TRUE(AR1.drop_back().equals(AR2)); EXPECT_TRUE(AR1.drop_back().equals(AR2));
// Check that drop_back accepts size_t-sized numbers.
ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
EXPECT_EQ(1U, AR3.drop_back(AR3.size() - 1).size());
} }
TEST(ArrayRefTest, DropFront) { TEST(ArrayRefTest, DropFront) {
@ -72,6 +76,10 @@ TEST(ArrayRefTest, DropFront) {
ArrayRef<int> AR1(TheNumbers); ArrayRef<int> AR1(TheNumbers);
ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2); ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
EXPECT_TRUE(AR1.drop_front(2).equals(AR2)); EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
// Check that drop_front accepts size_t-sized numbers.
ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size());
} }
TEST(ArrayRefTest, Equals) { TEST(ArrayRefTest, Equals) {
@ -101,6 +109,13 @@ TEST(ArrayRefTest, EmptyEquals) {
EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>()); EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
} }
TEST(ArrayRefTest, Slice) {
// Check that slice accepts size_t-sized numbers.
ArrayRef<char> AR((const char *)0x10000, SIZE_MAX - 0x10000);
EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
}
TEST(ArrayRefTest, ConstConvert) { TEST(ArrayRefTest, ConstConvert) {
int buf[4]; int buf[4];
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)