diff options
6 files changed, 42 insertions, 14 deletions
diff --git a/eval/src/tests/instruction/generic_rename/generic_rename_test.cpp b/eval/src/tests/instruction/generic_rename/generic_rename_test.cpp index dedb22f2763..9c17dce972f 100644 --- a/eval/src/tests/instruction/generic_rename/generic_rename_test.cpp +++ b/eval/src/tests/instruction/generic_rename/generic_rename_test.cpp @@ -53,8 +53,8 @@ TEST(GenericRenameTest, dense_rename_plan_can_be_created_and_executed) { std::vector<vespalib::string> to({"f", "a", "b"}); ValueType renamed = lhs.rename(from, to); auto plan = DenseRenamePlan(lhs, renamed, from, to); - std::vector<size_t> expect_loop = {15,2,7}; - std::vector<size_t> expect_stride = {7,105,1}; + SmallVector<size_t> expect_loop = {15,2,7}; + SmallVector<size_t> expect_stride = {7,105,1}; EXPECT_EQ(plan.subspace_size, 210); EXPECT_EQ(plan.loop_cnt, expect_loop); EXPECT_EQ(plan.stride, expect_stride); @@ -84,7 +84,7 @@ TEST(GenericRenameTest, sparse_rename_plan_can_be_created) { ValueType renamed = lhs.rename(from, to); auto plan = SparseRenamePlan(lhs, renamed, from, to); EXPECT_EQ(plan.mapped_dims, 4); - std::vector<size_t> expect = {2,0,1,3}; + SmallVector<size_t> expect = {2,0,1,3}; EXPECT_EQ(plan.output_dimensions, expect); } diff --git a/eval/src/vespa/eval/instruction/generic_rename.cpp b/eval/src/vespa/eval/instruction/generic_rename.cpp index 4fe347375c4..8363d4db573 100644 --- a/eval/src/vespa/eval/instruction/generic_rename.cpp +++ b/eval/src/vespa/eval/instruction/generic_rename.cpp @@ -69,10 +69,10 @@ generic_rename(const Value &a, const ValueType &res_type, const ValueBuilderFactory &factory) { auto cells = a.cells().typify<CT>(); - std::vector<string_id> output_address(sparse_plan.mapped_dims); - std::vector<string_id*> input_address; + SmallVector<string_id> output_address(sparse_plan.mapped_dims); + SmallVector<string_id*> input_address; for (size_t maps_to : sparse_plan.output_dimensions) { - input_address.push_back(&output_address[maps_to]); + input_address.emplace_back(&output_address[maps_to]); } auto builder = factory.create_transient_value_builder<CT>(res_type, sparse_plan.mapped_dims, @@ -152,7 +152,7 @@ SparseRenamePlan::SparseRenamePlan(const ValueType &input_type, if (index != output_dimensions.size()) { can_forward_index = false; } - output_dimensions.push_back(index); + output_dimensions.emplace_back(index); } assert(output_dimensions.size() == mapped_dims); } @@ -172,8 +172,8 @@ DenseRenamePlan::DenseRenamePlan(const ValueType &lhs_type, const auto out_dims = output_type.nontrivial_indexed_dimensions(); size_t num_dense_dims = lhs_dims.size(); assert(num_dense_dims == out_dims.size()); - std::vector<size_t> lhs_loopcnt(num_dense_dims); - std::vector<size_t> lhs_stride(num_dense_dims, 1); + SmallVector<size_t> lhs_loopcnt(num_dense_dims); + SmallVector<size_t> lhs_stride(num_dense_dims, 1); size_t lhs_size = 1; for (size_t i = num_dense_dims; i-- > 0; ) { lhs_stride[i] = lhs_size; @@ -191,8 +191,8 @@ DenseRenamePlan::DenseRenamePlan(const ValueType &lhs_type, loop_cnt.back() *= lhs_loopcnt[index]; stride.back() = lhs_stride[index]; } else { - loop_cnt.push_back(lhs_loopcnt[index]); - stride.push_back(lhs_stride[index]); + loop_cnt.emplace_back(lhs_loopcnt[index]); + stride.emplace_back(lhs_stride[index]); } prev_index = index; } diff --git a/eval/src/vespa/eval/instruction/generic_rename.h b/eval/src/vespa/eval/instruction/generic_rename.h index 7834c967488..4d8ca54a248 100644 --- a/eval/src/vespa/eval/instruction/generic_rename.h +++ b/eval/src/vespa/eval/instruction/generic_rename.h @@ -6,6 +6,7 @@ #include <vespa/eval/eval/value_type.h> #include <vespa/eval/eval/interpreted_function.h> #include <vespa/vespalib/stllike/string.h> +#include <vespa/vespalib/util/small_vector.h> #include <vector> namespace vespalib::eval { struct ValueBuilderFactory; } @@ -13,8 +14,8 @@ namespace vespalib::eval { struct ValueBuilderFactory; } namespace vespalib::eval::instruction { struct DenseRenamePlan { - std::vector<size_t> loop_cnt; - std::vector<size_t> stride; + SmallVector<size_t> loop_cnt; + SmallVector<size_t> stride; const size_t subspace_size; DenseRenamePlan(const ValueType &lhs_type, const ValueType &output_type, @@ -28,7 +29,7 @@ struct DenseRenamePlan { struct SparseRenamePlan { size_t mapped_dims; - std::vector<size_t> output_dimensions; + SmallVector<size_t> output_dimensions; bool can_forward_index; SparseRenamePlan(const ValueType &input_type, const ValueType &output_type, 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/arrayref.h b/vespalib/src/vespa/vespalib/util/arrayref.h index 03634a7a094..2433f9251cf 100644 --- a/vespalib/src/vespa/vespalib/util/arrayref.h +++ b/vespalib/src/vespa/vespalib/util/arrayref.h @@ -2,6 +2,7 @@ #pragma once #include "array.h" +#include "small_vector.h" #include <vector> namespace vespalib { @@ -17,6 +18,8 @@ public: 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[0]), _sz(v.size()) { } + template<size_t N> + ArrayRef(SmallVector<T, N> &v) noexcept : _v(&v[0]), _sz(v.size()) { } ArrayRef(Array<T> &v) noexcept : _v(&v[0]), _sz(v.size()) { } T & operator [] (size_t i) { return _v[i]; } const T & operator [] (size_t i) const { return _v[i]; } @@ -35,6 +38,8 @@ public: 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[0]), _sz(v.size()) { } + template<size_t N> + ConstArrayRef(const SmallVector<T, N> &v) noexcept : _v(&v[0]), _sz(v.size()) { } ConstArrayRef(const ArrayRef<T> & v) noexcept : _v(&v[0]), _sz(v.size()) { } ConstArrayRef(const Array<T> &v) noexcept : _v(&v[0]), _sz(v.size()) { } ConstArrayRef() noexcept : _v(nullptr), _sz(0) {} 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; |