8331193: Return references when possible in GrowableArray

Reviewed-by: stefank, kbarrett, epeter
This commit is contained in:
Johan Sjölen 2024-06-04 10:53:26 +00:00
parent 64bbae7512
commit 0f4154a9e9
2 changed files with 46 additions and 5 deletions
src/hotspot/share/utilities
test/hotspot/gtest/utilities

@ -153,17 +153,31 @@ public:
return &_data[i];
}
E first() const {
E& first() {
assert(_len > 0, "empty");
return _data[0];
}
E top() const {
E const& first() const {
assert(_len > 0, "empty");
return _data[_len-1];
return _data[0];
}
E last() const {
E& top() {
assert(_len > 0, "empty");
return _data[_len - 1];
}
E const& top() const {
assert(_len > 0, "empty");
return _data[_len - 1];
}
E& last() {
return top();
}
E const& last() const {
return top();
}
@ -410,7 +424,7 @@ public:
void push(const E& elem) { append(elem); }
E at_grow(int i, const E& fill = E()) {
E& at_grow(int i, const E& fill = E()) {
assert(0 <= i, "negative index %d", i);
if (i >= this->_len) {
if (i >= this->_capacity) grow(i);

@ -663,3 +663,30 @@ TEST(GrowableArrayCHeap, find_from_end_if) {
ASSERT_EQ(index, -1);
}
}
TEST(GrowableArrayCHeap, returning_references_works_as_expected) {
GrowableArrayCHeap<int, mtTest> arr(8, 8, -1); // Pre-fill with 8 -1s
int& x = arr.at_grow(9, -1);
EXPECT_EQ(-1, arr.at(9));
EXPECT_EQ(-1, x);
x = 2;
EXPECT_EQ(2, arr.at(9));
int& x2 = arr.top();
EXPECT_EQ(2, arr.at(9));
x2 = 5;
EXPECT_EQ(5, arr.at(9));
int y = arr.at_grow(10, -1);
EXPECT_EQ(-1, arr.at(10));
y = arr.top();
EXPECT_EQ(-1, arr.at(10));
GrowableArrayCHeap<int, mtTest> arr2(1, 1, -1);
int& first = arr2.first();
int& last = arr2.last();
EXPECT_EQ(-1, first);
EXPECT_EQ(-1, last);
first = 5;
EXPECT_EQ(5, first);
EXPECT_EQ(5, last);
}