summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorArne Juul <arnej@verizonmedia.com>2021-03-01 07:45:25 +0000
committerArne Juul <arnej@verizonmedia.com>2021-03-01 10:26:31 +0000
commit27d1885ca7ed987a9190343ec2eb45c11eb1aa48 (patch)
treed3dee0fbafe2e6fd7be40b9b4330bb3da85e6821 /vespalib
parent017d366cb993afce77bd631aa00ead12b021d786 (diff)
add back() methods
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/tests/small_vector/small_vector_test.cpp20
-rw-r--r--vespalib/src/vespa/vespalib/util/small_vector.h2
2 files changed, 22 insertions, 0 deletions
diff --git a/vespalib/src/tests/small_vector/small_vector_test.cpp b/vespalib/src/tests/small_vector/small_vector_test.cpp
index 808571acef6..6d8238fefb4 100644
--- a/vespalib/src/tests/small_vector/small_vector_test.cpp
+++ b/vespalib/src/tests/small_vector/small_vector_test.cpp
@@ -243,4 +243,24 @@ TEST(SmallVectorTest, equal_operator) {
SmallVector<EqOnly>({EqOnly{1},EqOnly{5},EqOnly{3}}));
}
+// to check "back() const"
+template<size_t N>
+int last_value_of(const SmallVector<int,N> &v) {
+ return v.back();
+}
+
+TEST(SmallVectorTest, check_back_method) {
+ SmallVector<int> vec;
+ for (int i = 0; i < 1000; ++i) {
+ vec.emplace_back(17);
+ EXPECT_EQ(vec.back(), 17);
+ EXPECT_EQ(last_value_of(vec), 17);
+ vec.back() = 42;
+ EXPECT_EQ(vec[i], 42);
+ vec.back() = i;
+ EXPECT_EQ(last_value_of(vec), i);
+ }
+ EXPECT_EQ(&vec.back(), vec.end() - 1);
+}
+
GTEST_MAIN_RUN_ALL_TESTS()
diff --git a/vespalib/src/vespa/vespalib/util/small_vector.h b/vespalib/src/vespa/vespalib/util/small_vector.h
index 21a21fc2cbb..9cbc10951cf 100644
--- a/vespalib/src/vespa/vespalib/util/small_vector.h
+++ b/vespalib/src/vespa/vespalib/util/small_vector.h
@@ -186,6 +186,8 @@ public:
const T *end() const { return (_data + _size); }
T &operator[](size_t idx) { return _data[idx]; }
const T &operator[](size_t idx) const { return _data[idx]; }
+ T &back() { return _data[_size - 1]; }
+ const T &back() const { return _data[_size - 1]; }
void clear() {
small_vector::destroy_objects(_data, _size);
_size = 0;