Skip to content

Commit 24c3844

Browse files
committed
Updated style [skip ci]
1 parent cc35e6f commit 24c3844

File tree

7 files changed

+60
-60
lines changed

7 files changed

+60
-60
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ tx.exec("CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))");
6161
Insert a vector
6262

6363
```cpp
64-
pgvector::Vector embedding({1, 2, 3});
64+
pgvector::Vector embedding{{1, 2, 3}};
6565
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
6666
```
6767
@@ -87,7 +87,7 @@ Use `std::optional<pgvector::Vector>` if the value could be `NULL`
8787
Create a vector from a `std::vector<float>`
8888

8989
```cpp
90-
pgvector::Vector vec(std::vector<float>{1, 2, 3});
90+
pgvector::Vector vec{std::vector<float>{1, 2, 3}};
9191
```
9292
9393
Convert to a `std::vector<float>`
@@ -101,7 +101,7 @@ auto float_vec = static_cast<std::vector<float>>(vec);
101101
Create a half vector from a `std::vector<float>`
102102

103103
```cpp
104-
pgvector::HalfVector vec(std::vector<float>{1, 2, 3});
104+
pgvector::HalfVector vec{std::vector<float>{1, 2, 3}};
105105
```
106106
107107
Convert to a `std::vector<float>`
@@ -115,14 +115,14 @@ auto float_vec = static_cast<std::vector<float>>(vec);
115115
Create a sparse vector from a `std::vector<float>`
116116

117117
```cpp
118-
pgvector::SparseVector vec({1, 0, 2, 0, 3, 0});
118+
pgvector::SparseVector vec{{1, 0, 2, 0, 3, 0}};
119119
```
120120
121121
Or a map of non-zero elements
122122
123123
```cpp
124124
std::unordered_map<int, float> map{{0, 1}, {2, 2}, {4, 3}};
125-
pgvector::SparseVector vec(map, 6);
125+
pgvector::SparseVector vec{map, 6};
126126
```
127127

128128
Note: Indices start at 0

include/pgvector/pqxx.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ template <> struct string_traits<pgvector::Vector> {
4141
}
4242
values.push_back(string_traits<float>::from_string(inner.substr(start), c));
4343
}
44-
return pgvector::Vector(std::move(values));
44+
return pgvector::Vector{std::move(values)};
4545
}
4646

4747
static std::string_view to_buf(std::span<char> buf, const pgvector::Vector& value, ctx c = {}) {
48-
auto& values = value.as_vector();
48+
const std::vector<float>& values = value.as_vector();
4949

5050
// important! size_buffer cannot throw an exception on overflow
5151
// so perform this check before writing any data
@@ -69,7 +69,7 @@ template <> struct string_traits<pgvector::Vector> {
6969
}
7070

7171
static size_t size_buffer(const pgvector::Vector& value) noexcept {
72-
auto& values = value.as_vector();
72+
const std::vector<float>& values = value.as_vector();
7373

7474
// cannot throw an exception here on overflow
7575
// so throw in into_buf
@@ -105,11 +105,11 @@ template <> struct string_traits<pgvector::HalfVector> {
105105
}
106106
values.push_back(static_cast<pgvector::Half>(string_traits<float>::from_string(inner.substr(start), c)));
107107
}
108-
return pgvector::HalfVector(std::move(values));
108+
return pgvector::HalfVector{std::move(values)};
109109
}
110110

111111
static std::string_view to_buf(std::span<char> buf, const pgvector::HalfVector& value, ctx c = {}) {
112-
auto& values = value.as_vector();
112+
const std::vector<pgvector::Half>& values = value.as_vector();
113113

114114
// important! size_buffer cannot throw an exception on overflow
115115
// so perform this check before writing any data
@@ -133,7 +133,7 @@ template <> struct string_traits<pgvector::HalfVector> {
133133
}
134134

135135
static size_t size_buffer(const pgvector::HalfVector& value) noexcept {
136-
auto& values = value.as_vector();
136+
const std::vector<pgvector::Half>& values = value.as_vector();
137137

138138
// cannot throw an exception here on overflow
139139
// so throw in into_buf
@@ -199,13 +199,13 @@ template <> struct string_traits<pgvector::SparseVector> {
199199
add_element(inner.substr(start));
200200
}
201201

202-
return pgvector::SparseVector(dimensions, std::move(indices), std::move(values));
202+
return pgvector::SparseVector{dimensions, std::move(indices), std::move(values)};
203203
}
204204

205205
static std::string_view to_buf(std::span<char> buf, const pgvector::SparseVector& value, ctx c = {}) {
206206
int dimensions = value.dimensions();
207-
auto& indices = value.indices();
208-
auto& values = value.values();
207+
const std::vector<int>& indices = value.indices();
208+
const std::vector<float>& values = value.values();
209209
size_t nnz = indices.size();
210210

211211
// important! size_buffer cannot throw an exception on overflow
@@ -235,8 +235,8 @@ template <> struct string_traits<pgvector::SparseVector> {
235235

236236
static size_t size_buffer(const pgvector::SparseVector& value) noexcept {
237237
int dimensions = value.dimensions();
238-
auto& indices = value.indices();
239-
auto& values = value.values();
238+
const std::vector<int>& indices = value.indices();
239+
const std::vector<float>& values = value.values();
240240
size_t nnz = indices.size();
241241

242242
// cannot throw an exception here on overflow

include/pgvector/sparsevec.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class SparseVector {
5959
}
6060
dimensions_ = dimensions;
6161

62-
for (const auto [i, v] : map) {
62+
for (const auto& [i, v] : map) {
6363
if (i < 0 || i >= dimensions) {
6464
throw std::invalid_argument("sparsevec index out of bounds");
6565
}

test/halfvec_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
using pgvector::HalfVector;
88

99
static void test_constructor_vector() {
10-
HalfVector vec(std::vector<pgvector::Half>{1, 2, 3});
10+
HalfVector vec{std::vector<pgvector::Half>{1, 2, 3}};
1111
assert_equal(vec.dimensions(), 3u);
1212
}
1313

1414
static void test_constructor_span() {
15-
HalfVector vec(std::span<const pgvector::Half>{{1, 2, 3}});
15+
HalfVector vec{std::span<const pgvector::Half>{{1, 2, 3}}};
1616
assert_equal(vec.dimensions(), 3u);
1717
}
1818

1919
static void test_as_vector() {
20-
HalfVector vec({1, 2, 3});
20+
HalfVector vec{{1, 2, 3}};
2121
assert_equal(vec.as_vector() == std::vector<pgvector::Half>{1, 2, 3}, true);
2222
}
2323

test/pqxx_test.cpp

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
#include "helper.hpp"
1010

1111
void setup(pqxx::connection &conn) {
12-
pqxx::nontransaction tx(conn);
12+
pqxx::nontransaction tx{conn};
1313
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
1414
tx.exec("DROP TABLE IF EXISTS items");
1515
tx.exec("CREATE TABLE items (id serial PRIMARY KEY, embedding vector(3), half_embedding halfvec(3), binary_embedding bit(3), sparse_embedding sparsevec(3))");
1616
}
1717

1818
void before_each(pqxx::connection &conn) {
19-
pqxx::nontransaction tx(conn);
19+
pqxx::nontransaction tx{conn};
2020
tx.exec("TRUNCATE items");
2121
}
2222

@@ -31,9 +31,9 @@ std::optional<std::string_view> float_error([[maybe_unused]] std::string_view me
3131
void test_vector(pqxx::connection &conn) {
3232
before_each(conn);
3333

34-
pqxx::nontransaction tx(conn);
35-
pgvector::Vector embedding({1, 2, 3});
36-
pgvector::Vector embedding2({4, 5, 6});
34+
pqxx::nontransaction tx{conn};
35+
pgvector::Vector embedding{{1, 2, 3}};
36+
pgvector::Vector embedding2{{4, 5, 6}};
3737
tx.exec("INSERT INTO items (embedding) VALUES ($1), ($2), ($3)", {embedding, embedding2, std::nullopt});
3838

3939
pqxx::result res = tx.exec("SELECT embedding FROM items ORDER BY embedding <-> $1", {embedding2});
@@ -46,9 +46,9 @@ void test_vector(pqxx::connection &conn) {
4646
void test_halfvec(pqxx::connection &conn) {
4747
before_each(conn);
4848

49-
pqxx::nontransaction tx(conn);
50-
pgvector::HalfVector embedding({1, 2, 3});
51-
pgvector::HalfVector embedding2({4, 5, 6});
49+
pqxx::nontransaction tx{conn};
50+
pgvector::HalfVector embedding{{1, 2, 3}};
51+
pgvector::HalfVector embedding2{{4, 5, 6}};
5252
tx.exec("INSERT INTO items (half_embedding) VALUES ($1), ($2), ($3)", {embedding, embedding2, std::nullopt});
5353

5454
pqxx::result res = tx.exec("SELECT half_embedding FROM items ORDER BY half_embedding <-> $1", {embedding2});
@@ -61,7 +61,7 @@ void test_halfvec(pqxx::connection &conn) {
6161
void test_bit(pqxx::connection &conn) {
6262
before_each(conn);
6363

64-
pqxx::nontransaction tx(conn);
64+
pqxx::nontransaction tx{conn};
6565
std::string embedding{"101"};
6666
std::string embedding2{"111"};
6767
tx.exec("INSERT INTO items (binary_embedding) VALUES ($1), ($2), ($3)", {embedding, embedding2, std::nullopt});
@@ -76,9 +76,9 @@ void test_bit(pqxx::connection &conn) {
7676
void test_sparsevec(pqxx::connection &conn) {
7777
before_each(conn);
7878

79-
pqxx::nontransaction tx(conn);
80-
pgvector::SparseVector embedding({1, 2, 3});
81-
pgvector::SparseVector embedding2({4, 5, 6});
79+
pqxx::nontransaction tx{conn};
80+
pgvector::SparseVector embedding{{1, 2, 3}};
81+
pgvector::SparseVector embedding2{{4, 5, 6}};
8282
tx.exec("INSERT INTO items (sparse_embedding) VALUES ($1), ($2), ($3)", {embedding, embedding2, std::nullopt});
8383

8484
pqxx::result res = tx.exec("SELECT sparse_embedding FROM items ORDER BY sparse_embedding <-> $1", {embedding2});
@@ -91,9 +91,9 @@ void test_sparsevec(pqxx::connection &conn) {
9191
void test_sparsevec_nnz(pqxx::connection &conn) {
9292
before_each(conn);
9393

94-
pqxx::nontransaction tx(conn);
94+
pqxx::nontransaction tx{conn};
9595
std::vector<float> vec(16001, 1);
96-
pgvector::SparseVector embedding(vec);
96+
pgvector::SparseVector embedding{vec};
9797
assert_exception<pqxx::conversion_overrun>([&] {
9898
tx.exec("INSERT INTO items (sparse_embedding) VALUES ($1)", {embedding});
9999
}, "sparsevec cannot have more than 16000 dimensions");
@@ -102,7 +102,7 @@ void test_sparsevec_nnz(pqxx::connection &conn) {
102102
void test_stream(pqxx::connection &conn) {
103103
before_each(conn);
104104

105-
pqxx::nontransaction tx(conn);
105+
pqxx::nontransaction tx{conn};
106106
pgvector::Vector embedding({1, 2, 3});
107107
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
108108
int count = 0;
@@ -116,10 +116,10 @@ void test_stream(pqxx::connection &conn) {
116116
void test_stream_to(pqxx::connection &conn) {
117117
before_each(conn);
118118

119-
pqxx::nontransaction tx(conn);
119+
pqxx::nontransaction tx{conn};
120120
pqxx::stream_to stream = pqxx::stream_to::table(tx, {"items"}, {"embedding"});
121-
stream.write_values(pgvector::Vector({1, 2, 3}));
122-
stream.write_values(pgvector::Vector({4, 5, 6}));
121+
stream.write_values(pgvector::Vector{{1, 2, 3}});
122+
stream.write_values(pgvector::Vector{{4, 5, 6}});
123123
stream.complete();
124124
pqxx::result res = tx.exec("SELECT embedding FROM items ORDER BY id");
125125
assert_equal(res[0][0].as<std::string>(), "[1,2,3]");
@@ -129,26 +129,26 @@ void test_stream_to(pqxx::connection &conn) {
129129
void test_precision(pqxx::connection &conn) {
130130
before_each(conn);
131131

132-
pqxx::nontransaction tx(conn);
133-
pgvector::Vector embedding({1.23456789, 0, 0});
132+
pqxx::nontransaction tx{conn};
133+
pgvector::Vector embedding{{1.23456789, 0, 0}};
134134
tx.exec("INSERT INTO items (embedding) VALUES ($1)", {embedding});
135135
tx.exec("SET extra_float_digits = 3");
136136
pqxx::result res = tx.exec("SELECT embedding FROM items ORDER BY id DESC LIMIT 1");
137137
assert_equal(res[0][0].as<pgvector::Vector>(), embedding);
138138
}
139139

140140
void test_vector_to_string() {
141-
assert_equal(pqxx::to_string(pgvector::Vector({1, 2, 3})), "[1,2,3]");
142-
assert_equal(pqxx::to_string(pgvector::Vector({-1.234567890123})), "[-1.2345679]");
141+
assert_equal(pqxx::to_string(pgvector::Vector{{1, 2, 3}}), "[1,2,3]");
142+
assert_equal(pqxx::to_string(pgvector::Vector{{-1.234567890123}}), "[-1.2345679]");
143143

144144
assert_exception<pqxx::conversion_overrun>([] {
145145
pqxx::to_string(pgvector::Vector(std::vector<float>(16001)));
146146
}, "vector cannot have more than 16000 dimensions");
147147
}
148148

149149
void test_vector_from_string() {
150-
assert_equal(pqxx::from_string<pgvector::Vector>("[1,2,3]"), pgvector::Vector({1, 2, 3}));
151-
assert_equal(pqxx::from_string<pgvector::Vector>("[]"), pgvector::Vector(std::vector<float>{}));
150+
assert_equal(pqxx::from_string<pgvector::Vector>("[1,2,3]"), pgvector::Vector{{1, 2, 3}});
151+
assert_equal(pqxx::from_string<pgvector::Vector>("[]"), pgvector::Vector{std::vector<float>{}});
152152

153153
assert_exception<pqxx::conversion_error>([] {
154154
auto _ = pqxx::from_string<pgvector::Vector>("");
@@ -178,9 +178,9 @@ void test_vector_from_string() {
178178
void test_halfvec_to_string() {
179179
assert_equal(pqxx::to_string(pgvector::HalfVector({1, 2, 3})), "[1,2,3]");
180180
#if __STDCPP_FLOAT16_T__ || defined(__FLT16_MAX__)
181-
assert_equal(pqxx::to_string(pgvector::HalfVector({static_cast<pgvector::Half>(-1.234567890123)})), "[-1.234375]");
181+
assert_equal(pqxx::to_string(pgvector::HalfVector{{static_cast<pgvector::Half>(-1.234567890123)}}), "[-1.234375]");
182182
#else
183-
assert_equal(pqxx::to_string(pgvector::HalfVector({-1.234567890123})), "[-1.2345679]");
183+
assert_equal(pqxx::to_string(pgvector::HalfVector{{-1.234567890123}}), "[-1.2345679]");
184184
#endif
185185

186186
assert_exception<pqxx::conversion_overrun>([] {
@@ -189,8 +189,8 @@ void test_halfvec_to_string() {
189189
}
190190

191191
void test_halfvec_from_string() {
192-
assert_equal(pqxx::from_string<pgvector::HalfVector>("[1,2,3]"), pgvector::HalfVector({1, 2, 3}));
193-
assert_equal(pqxx::from_string<pgvector::HalfVector>("[]"), pgvector::HalfVector(std::vector<pgvector::Half>{}));
192+
assert_equal(pqxx::from_string<pgvector::HalfVector>("[1,2,3]"), pgvector::HalfVector{{1, 2, 3}});
193+
assert_equal(pqxx::from_string<pgvector::HalfVector>("[]"), pgvector::HalfVector{std::vector<pgvector::Half>{}});
194194

195195
assert_exception<pqxx::conversion_error>([] {
196196
auto _ = pqxx::from_string<pgvector::HalfVector>("");
@@ -218,18 +218,18 @@ void test_halfvec_from_string() {
218218
}
219219

220220
void test_sparsevec_to_string() {
221-
assert_equal(pqxx::to_string(pgvector::SparseVector({1, 0, 2, 0, 3, 0})), "{1:1,3:2,5:3}/6");
222-
std::unordered_map<int, float> map = {{999999999, -1.234567890123}};
223-
assert_equal(pqxx::to_string(pgvector::SparseVector(map, 1000000000)), "{1000000000:-1.2345679}/1000000000");
221+
assert_equal(pqxx::to_string(pgvector::SparseVector{{1, 0, 2, 0, 3, 0}}), "{1:1,3:2,5:3}/6");
222+
std::unordered_map<int, float> map{{999999999, -1.234567890123}};
223+
assert_equal(pqxx::to_string(pgvector::SparseVector{map, 1000000000}), "{1000000000:-1.2345679}/1000000000");
224224

225225
assert_exception<pqxx::conversion_overrun>([] {
226226
pqxx::to_string(pgvector::SparseVector(std::vector<float>(16001, 1)));
227227
}, "sparsevec cannot have more than 16000 dimensions");
228228
}
229229

230230
void test_sparsevec_from_string() {
231-
assert_equal(pqxx::from_string<pgvector::SparseVector>("{1:1,3:2,5:3}/6"), pgvector::SparseVector({1, 0, 2, 0, 3, 0}));
232-
assert_equal(pqxx::from_string<pgvector::SparseVector>("{}/6"), pgvector::SparseVector({0, 0, 0, 0, 0, 0}));
231+
assert_equal(pqxx::from_string<pgvector::SparseVector>("{1:1,3:2,5:3}/6"), pgvector::SparseVector{{1, 0, 2, 0, 3, 0}});
232+
assert_equal(pqxx::from_string<pgvector::SparseVector>("{}/6"), pgvector::SparseVector{{0, 0, 0, 0, 0, 0}});
233233

234234
assert_exception<pqxx::conversion_error>([] {
235235
auto _ = pqxx::from_string<pgvector::SparseVector>("");
@@ -281,7 +281,7 @@ void test_sparsevec_from_string() {
281281
}
282282

283283
void test_pqxx() {
284-
pqxx::connection conn("dbname=pgvector_cpp_test");
284+
pqxx::connection conn{"dbname=pgvector_cpp_test"};
285285
setup(conn);
286286

287287
test_vector(conn);

test/sparsevec_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@
88
using pgvector::SparseVector;
99

1010
static void test_constructor_vector() {
11-
SparseVector vec(std::vector<float>{1, 0, 2, 0, 3, 0});
11+
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
1212
assert_equal(vec.dimensions(), 6);
1313
assert_equal(vec.indices() == std::vector<int>{0, 2, 4}, true);
1414
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);
1515
}
1616

1717
static void test_constructor_span() {
18-
SparseVector vec(std::span<const float>{{1, 0, 2, 0, 3, 0}});
18+
SparseVector vec{std::span<const float>{{1, 0, 2, 0, 3, 0}}};
1919
assert_equal(vec.dimensions(), 6);
2020
}
2121

2222
static void test_constructor_map() {
2323
std::unordered_map<int, float> map{{2, 2}, {4, 3}, {3, 0}, {0, 1}};
24-
SparseVector vec(map, 6);
24+
SparseVector vec{map, 6};
2525
assert_equal(vec.dimensions(), 6);
2626
assert_equal(vec.indices() == std::vector<int>{0, 2, 4}, true);
2727
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);

test/vector_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
using pgvector::Vector;
88

99
static void test_constructor_vector() {
10-
Vector vec(std::vector<float>{1, 2, 3});
10+
Vector vec{std::vector<float>{1, 2, 3}};
1111
assert_equal(vec.dimensions(), 3u);
1212
}
1313

1414
static void test_constructor_span() {
15-
Vector vec(std::span<const float>{{1, 2, 3}});
15+
Vector vec{std::span<const float>{{1, 2, 3}}};
1616
assert_equal(vec.dimensions(), 3u);
1717
}
1818

1919
static void test_as_vector() {
20-
Vector vec({1, 2, 3});
20+
Vector vec{{1, 2, 3}};
2121
assert_equal(vec.as_vector() == std::vector<float>{1, 2, 3}, true);
2222
}
2323

0 commit comments

Comments
 (0)