Skip to content

Commit 2a5bac6

Browse files
committed
Added std::span constructor to HalfVector and SparseVector
1 parent 1c76ec5 commit 2a5bac6

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## 0.2.2 (unreleased)
22

33
- Added map constructor to `SparseVector`
4+
- Added `std::span` constructor to `HalfVector` and `SparseVector`
45

56
## 0.2.1 (2025-01-15)
67

include/pgvector/halfvec.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include <ostream>
1010
#include <vector>
1111

12+
#if __cplusplus >= 202002L
13+
#include <span>
14+
#endif
15+
1216
namespace pgvector {
1317
/// A half vector.
1418
class HalfVector {
@@ -28,6 +32,13 @@ class HalfVector {
2832
value_ = std::vector<float>{value, value + n};
2933
}
3034

35+
#if __cplusplus >= 202002L
36+
/// Creates a half vector from a span.
37+
HalfVector(std::span<const float> value) {
38+
value_ = std::vector<float>(value.begin(), value.end());
39+
}
40+
#endif
41+
3142
/// Returns the number of dimensions.
3243
size_t dimensions() const {
3344
return value_.size();

include/pgvector/sparsevec.hpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ class SparseVector {
3838
}
3939
}
4040

41+
#if __cplusplus >= 202002L
42+
/// Creates a sparse vector from a span.
43+
SparseVector(std::span<const float> value) {
44+
dimensions_ = value.size();
45+
for (size_t i = 0; i < value.size(); i++) {
46+
float v = value[i];
47+
if (v != 0) {
48+
indices_.push_back(i);
49+
values_.push_back(v);
50+
}
51+
}
52+
}
53+
#endif
54+
4155
/// Creates a sparse vector from a map of non-zero elements.
4256
SparseVector(const std::unordered_map<int, float>& map, int dimensions) {
4357
if (dimensions < 1) {

include/pgvector/vector.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Vector {
3333
}
3434

3535
#if __cplusplus >= 202002L
36+
/// Creates a vector from a span.
3637
Vector(std::span<const float> value) {
3738
value_ = std::vector<float>(value.begin(), value.end());
3839
}

test/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,27 @@ void test_halfvec() {
3232
assert(vec.dimensions() == 3);
3333
}
3434

35+
#if __cplusplus >= 202002L
36+
void test_halfvec_span() {
37+
auto vec = HalfVector(std::span<const float>({1, 2, 3}));
38+
assert(vec.dimensions() == 3);
39+
}
40+
#endif
41+
3542
void test_sparsevec() {
3643
auto vec = SparseVector({1, 0, 2, 0, 3, 0});
3744
assert(vec.dimensions() == 6);
3845
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
3946
assert(vec.values() == (std::vector<float>{1, 2, 3}));
4047
}
4148

49+
#if __cplusplus >= 202002L
50+
void test_sparsevec_span() {
51+
auto vec = SparseVector(std::span<const float>({1, 0, 2, 0, 3, 0}));
52+
assert(vec.dimensions() == 6);
53+
}
54+
#endif
55+
4256
void test_sparsevec_map() {
4357
std::unordered_map<int, float> map = {{2, 2}, {4, 3}, {3, 0}, {0, 1}};
4458
auto vec = SparseVector(map, 6);
@@ -54,6 +68,12 @@ int main() {
5468
test_vector_span();
5569
#endif
5670
test_halfvec();
71+
#if __cplusplus >= 202002L
72+
test_halfvec_span();
73+
#endif
5774
test_sparsevec();
75+
#if __cplusplus >= 202002L
76+
test_sparsevec_span();
77+
#endif
5878
return 0;
5979
}

0 commit comments

Comments
 (0)