Skip to content

Commit 7c946e1

Browse files
committed
Updated SparseVector constructor to support zero dimensions like other constructors [skip ci]
1 parent 4ccdf52 commit 7c946e1

3 files changed

Lines changed: 5 additions & 5 deletions

File tree

include/pgvector/sparsevec.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class SparseVector {
3939

4040
/// Creates a sparse vector from a map of non-zero elements.
4141
SparseVector(const std::unordered_map<int, float>& map, int dimensions) {
42-
if (dimensions < 1) {
43-
throw std::invalid_argument{"sparsevec must have at least 1 dimension"};
42+
if (dimensions < 0) {
43+
throw std::invalid_argument{"sparsevec dimensions cannot be negative"};
4444
}
4545
dimensions_ = dimensions;
4646

test/pqxx_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void test_sparsevec_from_string() {
246246

247247
assert_exception<pqxx::conversion_error>([] {
248248
auto _ = pqxx::from_string<pgvector::SparseVector>("{}/-1");
249-
}, "sparsevec must have at least 1 dimension");
249+
}, "sparsevec dimensions cannot be negative");
250250

251251
assert_exception<pqxx::conversion_error>([] {
252252
auto _ = pqxx::from_string<pgvector::SparseVector>("{:}/1");

test/sparsevec_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ static void test_constructor_map() {
2727
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);
2828

2929
assert_exception<std::invalid_argument>([&]{
30-
SparseVector(map, 0);
31-
}, "sparsevec must have at least 1 dimension");
30+
SparseVector(map, -1);
31+
}, "sparsevec dimensions cannot be negative");
3232

3333
assert_exception<std::invalid_argument>([&]{
3434
SparseVector(map, 4);

0 commit comments

Comments
 (0)