-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvector.hpp
More file actions
61 lines (51 loc) · 1.39 KB
/
vector.hpp
File metadata and controls
61 lines (51 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* pgvector-cpp v0.3.0
* https://github.com/pgvector/pgvector-cpp
* MIT License
*/
#pragma once
#include <cstddef>
#include <ostream>
#include <span>
#include <utility>
#include <vector>
namespace pgvector {
/// A vector.
class Vector {
public:
/// Creates a vector from a `std::vector`.
explicit Vector(const std::vector<float>& value) : value_{value} {}
/// Creates a vector from a `std::vector`.
explicit Vector(std::vector<float>&& value) : value_{std::move(value)} {}
/// Creates a vector from a span.
explicit Vector(std::span<const float> value) :
value_{std::vector<float>(value.begin(), value.end())} {}
/// Returns the number of dimensions.
size_t dimensions() const {
return value_.size();
}
/// Returns the values.
const std::vector<float>& values() const {
return value_;
}
friend bool operator==(const Vector& lhs, const Vector& rhs) {
return lhs.value_ == rhs.value_;
}
friend std::ostream& operator<<(std::ostream& os, const Vector& value) {
os << "[";
// TODO use std::views::enumerate for C++23
size_t i = 0;
for (auto v : value.value_) {
if (i > 0) {
os << ",";
}
os << v;
i++;
}
os << "]";
return os;
}
private:
std::vector<float> value_;
};
} // namespace pgvector