forked from tensorchord/pgvecto.rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
139 lines (107 loc) · 3.78 KB
/
mod.rs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
pub mod bvector;
pub mod svecf32;
pub mod vecf16;
pub mod vecf32;
pub use bvector::{BVectorBorrowed, BVectorOwned, BVECTOR_WIDTH};
pub use svecf32::{SVecf32Borrowed, SVecf32Owned};
pub use vecf16::{Vecf16Borrowed, Vecf16Owned};
pub use vecf32::{Vecf32Borrowed, Vecf32Owned};
use crate::scalar::ScalarLike;
use crate::scalar::F32;
use serde::{Deserialize, Serialize};
use std::ops::RangeBounds;
#[repr(u8)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum VectorKind {
Vecf32,
Vecf16,
SVecf32,
BVector,
}
pub trait VectorOwned: Clone + Serialize + for<'a> Deserialize<'a> + 'static {
type Scalar: ScalarLike;
type Borrowed<'a>: VectorBorrowed<Scalar = Self::Scalar, Owned = Self>;
const VECTOR_KIND: VectorKind;
fn as_borrowed(&self) -> Self::Borrowed<'_>;
}
pub trait VectorBorrowed: Copy + PartialEq + PartialOrd {
// it will be depcrated
type Scalar: ScalarLike;
// it will be depcrated
fn to_vec(&self) -> Vec<Self::Scalar>;
type Owned: VectorOwned<Scalar = Self::Scalar>;
fn own(&self) -> Self::Owned;
fn dims(&self) -> u32;
fn norm(&self) -> F32;
fn operator_dot(self, rhs: Self) -> F32;
fn operator_l2(self, rhs: Self) -> F32;
fn operator_cos(self, rhs: Self) -> F32;
fn operator_hamming(self, rhs: Self) -> F32;
fn operator_jaccard(self, rhs: Self) -> F32;
fn function_normalize(&self) -> Self::Owned;
fn operator_add(&self, rhs: Self) -> Self::Owned;
fn operator_minus(&self, rhs: Self) -> Self::Owned;
fn operator_mul(&self, rhs: Self) -> Self::Owned;
fn operator_and(&self, rhs: Self) -> Self::Owned;
fn operator_or(&self, rhs: Self) -> Self::Owned;
fn operator_xor(&self, rhs: Self) -> Self::Owned;
fn subvector(&self, bounds: impl RangeBounds<u32>) -> Option<Self::Owned>;
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OwnedVector {
Vecf32(Vecf32Owned),
Vecf16(Vecf16Owned),
SVecf32(SVecf32Owned),
BVector(BVectorOwned),
}
impl OwnedVector {
pub fn as_borrowed(&self) -> BorrowedVector<'_> {
match self {
OwnedVector::Vecf32(x) => BorrowedVector::Vecf32(x.as_borrowed()),
OwnedVector::Vecf16(x) => BorrowedVector::Vecf16(x.as_borrowed()),
OwnedVector::SVecf32(x) => BorrowedVector::SVecf32(x.as_borrowed()),
OwnedVector::BVector(x) => BorrowedVector::BVector(x.as_borrowed()),
}
}
}
impl PartialEq for OwnedVector {
fn eq(&self, other: &Self) -> bool {
self.as_borrowed().eq(&other.as_borrowed())
}
}
impl PartialOrd for OwnedVector {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_borrowed().partial_cmp(&other.as_borrowed())
}
}
#[derive(Debug, Clone, Copy)]
pub enum BorrowedVector<'a> {
Vecf32(Vecf32Borrowed<'a>),
Vecf16(Vecf16Borrowed<'a>),
SVecf32(SVecf32Borrowed<'a>),
BVector(BVectorBorrowed<'a>),
}
impl PartialEq for BorrowedVector<'_> {
fn eq(&self, other: &Self) -> bool {
use BorrowedVector::*;
match (self, other) {
(Vecf32(lhs), Vecf32(rhs)) => lhs == rhs,
(Vecf16(lhs), Vecf16(rhs)) => lhs == rhs,
(SVecf32(lhs), SVecf32(rhs)) => lhs == rhs,
(BVector(lhs), BVector(rhs)) => lhs == rhs,
_ => false,
}
}
}
impl PartialOrd for BorrowedVector<'_> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
use BorrowedVector::*;
match (self, other) {
(Vecf32(lhs), Vecf32(rhs)) => lhs.partial_cmp(rhs),
(Vecf16(lhs), Vecf16(rhs)) => lhs.partial_cmp(rhs),
(SVecf32(lhs), SVecf32(rhs)) => lhs.partial_cmp(rhs),
(BVector(lhs), BVector(rhs)) => lhs.partial_cmp(rhs),
_ => None,
}
}
}