Skip to content

Commit 2725765

Browse files
authored
[LLVM][ADT] Add consume_front and consume_back to ArrayRef (#146741)
Add `consume_front` that returns the first element and drops it from the current ArrayRef, and `consume_back` that returns the last element and drops it from the current ArrayRef.
1 parent 7fc50e9 commit 2725765

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

llvm/include/llvm/ADT/ArrayRef.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,20 @@ namespace llvm {
158158
return Data[Length-1];
159159
}
160160

161+
/// consume_front() - Returns the first element and drops it from ArrayRef.
162+
const T &consume_front() {
163+
const T &Ret = front();
164+
*this = drop_front();
165+
return Ret;
166+
}
167+
168+
/// consume_back() - Returns the last element and drops it from ArrayRef.
169+
const T &consume_back() {
170+
const T &Ret = back();
171+
*this = drop_back();
172+
return Ret;
173+
}
174+
161175
// copy - Allocate copy in Allocator and return ArrayRef<T> to it.
162176
template <typename Allocator> MutableArrayRef<T> copy(Allocator &A) {
163177
T *Buff = A.template Allocate<T>(Length);
@@ -352,6 +366,20 @@ namespace llvm {
352366
return data()[this->size()-1];
353367
}
354368

369+
/// consume_front() - Returns the first element and drops it from ArrayRef.
370+
T &consume_front() {
371+
T &Ret = front();
372+
*this = drop_front();
373+
return Ret;
374+
}
375+
376+
/// consume_back() - Returns the last element and drops it from ArrayRef.
377+
T &consume_back() {
378+
T &Ret = back();
379+
*this = drop_back();
380+
return Ret;
381+
}
382+
355383
/// slice(n, m) - Chop off the first N elements of the array, and keep M
356384
/// elements in the array.
357385
MutableArrayRef<T> slice(size_t N, size_t M) const {

llvm/unittests/ADT/ArrayRefTest.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,48 @@ TEST(ArrayRefTest, DropFront) {
104104
EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
105105
}
106106

107+
TEST(ArrayRefTest, ConsumeFront) {
108+
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
109+
ArrayRef<int> AR1(TheNumbers);
110+
ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
111+
EXPECT_EQ(&AR1.consume_front(), &TheNumbers[0]);
112+
EXPECT_EQ(&AR1.consume_front(), &TheNumbers[1]);
113+
EXPECT_TRUE(AR1.equals(AR2));
114+
}
115+
116+
TEST(ArrayRefTest, ConsumeBack) {
117+
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
118+
ArrayRef<int> AR1(TheNumbers);
119+
ArrayRef<int> AR2(TheNumbers, AR1.size() - 2);
120+
EXPECT_EQ(&AR1.consume_back(), &TheNumbers[5]);
121+
EXPECT_EQ(&AR1.consume_back(), &TheNumbers[4]);
122+
EXPECT_TRUE(AR1.equals(AR2));
123+
}
124+
125+
TEST(ArrayRefTest, MutableArryaRefConsumeFront) {
126+
int TheNumbers[] = {4, 8, 15, 16, 23, 42};
127+
MutableArrayRef<int> AR1(TheNumbers);
128+
MutableArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
129+
EXPECT_EQ(&AR1.consume_front(), &TheNumbers[0]);
130+
EXPECT_EQ(&AR1.consume_front(), &TheNumbers[1]);
131+
EXPECT_TRUE(AR1.equals(AR2));
132+
133+
AR1.consume_front() = 33;
134+
EXPECT_EQ(TheNumbers[2], 33);
135+
}
136+
137+
TEST(ArrayRefTest, MutableArryaRefConsumeBack) {
138+
int TheNumbers[] = {4, 8, 15, 16, 23, 42};
139+
MutableArrayRef<int> AR1(TheNumbers);
140+
MutableArrayRef<int> AR2(TheNumbers, AR1.size() - 2);
141+
EXPECT_EQ(&AR1.consume_back(), &TheNumbers[5]);
142+
EXPECT_EQ(&AR1.consume_back(), &TheNumbers[4]);
143+
EXPECT_TRUE(AR1.equals(AR2));
144+
145+
AR1.consume_back() = 33;
146+
EXPECT_EQ(TheNumbers[3], 33);
147+
}
148+
107149
TEST(ArrayRefTest, DropWhile) {
108150
static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
109151
ArrayRef<int> AR1(TheNumbers);

0 commit comments

Comments
 (0)