-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhalfvec.hpp
More file actions
81 lines (69 loc) · 1.79 KB
/
Copy pathhalfvec.hpp
File metadata and controls
81 lines (69 loc) · 1.79 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* 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>
#if __STDCPP_FLOAT16_T__
#include <stdfloat>
#else
#define __STDC_WANT_IEC_60559_TYPES_EXT__
#include <cfloat>
#endif
namespace pgvector {
/// A half vector element.
#if __STDCPP_FLOAT16_T__
using Half = std::float16_t;
#elif defined(__FLT16_MAX__)
using Half = _Float16;
#else
using Half = float;
#endif
/// A half vector.
class HalfVector {
public:
/// Creates a half vector from a `std::vector`.
explicit HalfVector(const std::vector<Half>& value) : value_{value} {}
/// Creates a half vector from a `std::vector`.
explicit HalfVector(std::vector<Half>&& value) : value_{std::move(value)} {}
/// Creates a half vector from a span.
explicit HalfVector(std::span<const Half> value) :
value_{std::vector<Half>(value.begin(), value.end())} {}
/// Returns the number of dimensions.
size_t dimensions() const {
return value_.size();
}
/// Returns the values.
const std::vector<Half>& values() const {
return value_;
}
friend bool operator==(const HalfVector& lhs, const HalfVector& rhs) {
return lhs.value_ == rhs.value_;
}
friend std::ostream& operator<<(std::ostream& os, const HalfVector& value) {
os << "[";
// TODO use std::views::enumerate for C++23
size_t i = 0;
for (auto v : value.value_) {
if (i > 0) {
os << ",";
}
#if __STDCPP_FLOAT16_T__
os << v;
#else
os << static_cast<float>(v);
#endif
i++;
}
os << "]";
return os;
}
private:
std::vector<Half> value_;
};
} // namespace pgvector