diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2023-08-09 11:18:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-08-09 11:18:52 +0200 |
commit | 1bb8be8b551ee6b788896bb01c8822e0c1f4a0cb (patch) | |
tree | ba7f59dce97ed60e062722b41b0e39b10900fa7f /vespalib | |
parent | 5767a9ea8c430f0c7765dc29cce49985779bf890 (diff) | |
parent | bb78b4a6b6943a40b1a12db8996472aaa6bb1970 (diff) |
Merge pull request #27988 from vespa-engine/balder/prepare-for-better-stats-reset-code
Balder/prepare for better stats reset code
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/arrayref/arrayref_test.cpp | 20 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/util/arrayref.h | 5 |
2 files changed, 25 insertions, 0 deletions
diff --git a/vespalib/src/tests/arrayref/arrayref_test.cpp b/vespalib/src/tests/arrayref/arrayref_test.cpp index bd8646b2f99..8c41d38b292 100644 --- a/vespalib/src/tests/arrayref/arrayref_test.cpp +++ b/vespalib/src/tests/arrayref/arrayref_test.cpp @@ -53,4 +53,24 @@ TEST("require that references can be unconstified") { EXPECT_EQUAL(data[1], 5); } +TEST("require that std::array references can be constified") { + std::array<int,3> data({1,2,3}); + const ArrayRef<int> array_ref(data); + ConstArrayRef<int> const_ref(array_ref); + EXPECT_EQUAL(const_ref.size(), 3u); + EXPECT_EQUAL(const_ref.end() - const_ref.begin(), 3); + EXPECT_EQUAL(const_ref[2], 3); +} + +TEST("require that references can be unconstified") { + std::array<int, 3> data({1,2,3}); + const ConstArrayRef<int> const_ref(data); + ArrayRef<int> array_ref = unconstify(const_ref); + EXPECT_EQUAL(array_ref.size(), 3u); + EXPECT_EQUAL(array_ref.end() - array_ref.begin(), 3); + EXPECT_EQUAL(array_ref[1], 2); + array_ref[1] = 5; + EXPECT_EQUAL(data[1], 5); +} + TEST_MAIN() { TEST_RUN_ALL(); } diff --git a/vespalib/src/vespa/vespalib/util/arrayref.h b/vespalib/src/vespa/vespalib/util/arrayref.h index 319947e4cd9..9057f56fac0 100644 --- a/vespalib/src/vespa/vespalib/util/arrayref.h +++ b/vespalib/src/vespa/vespalib/util/arrayref.h @@ -3,6 +3,7 @@ #include <cstddef> #include <vector> +#include <array> namespace vespalib { @@ -17,6 +18,8 @@ public: constexpr ArrayRef(T * v, size_t sz) noexcept : _v(v), _sz(sz) { } template<typename A=std::allocator<T>> ArrayRef(std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { } + template<size_t SZ> + ArrayRef(std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { } T & operator [] (size_t i) noexcept { return _v[i]; } const T & operator [] (size_t i) const noexcept { return _v[i]; } T * data() noexcept { return _v; } @@ -36,6 +39,8 @@ public: constexpr ConstArrayRef(const T *v, size_t sz) noexcept : _v(v), _sz(sz) { } template<typename A=std::allocator<T>> ConstArrayRef(const std::vector<T, A> & v) noexcept : _v(v.data()), _sz(v.size()) { } + template<size_t SZ> + ConstArrayRef(const std::array<T, SZ> & v) noexcept : _v(v.data()), _sz(SZ) { } ConstArrayRef(const ArrayRef<T> & v) noexcept : _v(v.data()), _sz(v.size()) { } constexpr ConstArrayRef() noexcept : _v(nullptr), _sz(0) {} const T & operator [] (size_t i) const noexcept { return _v[i]; } |