FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
pgvector-rust/src/halfvec.rs at master · psqlpy-python/pgvector-rust · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
psqlpy-python
/
pgvector-rust
Public
forked from
pgvector/pgvector-rust
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
pgvector-rust
/
src
/
halfvec.rs
Copy path
More file actions
More file actions
Latest commit
History
History
History
102 lines (88 loc) · 2.55 KB
Breadcrumbs
pgvector-rust
/
src
/
halfvec.rs
Copy path
File metadata and controls
102 lines (88 loc) · 2.55 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
use
half
::
f16
;
#
[
cfg
(
feature =
"diesel"
)
]
use
crate
::
diesel_ext
::
halfvec
::
HalfVectorType
;
#
[
cfg
(
feature =
"diesel"
)
]
use
diesel
::
{
deserialize
::
FromSqlRow
,
expression
::
AsExpression
}
;
/// A half vector.
#
[
derive
(
Clone
,
Debug
,
PartialEq
)
]
#
[
cfg_attr
(
feature =
"diesel"
,
derive
(
FromSqlRow
,
AsExpression
)
)
]
#
[
cfg_attr
(
feature =
"diesel"
,
diesel
(
sql_type =
HalfVectorType
)
)
]
pub
struct
HalfVector
(
pub
(
crate
)
Vec
<
f16
>
)
;
impl
From
<
Vec
<
f16
>
>
for
HalfVector
{
fn
from
(
v
:
Vec
<
f16
>
)
->
Self
{
HalfVector
(
v
)
}
}
impl
From
<
HalfVector
>
for
Vec
<
f16
>
{
fn
from
(
val
:
HalfVector
)
->
Self
{
val
.
0
}
}
impl
HalfVector
{
/// Returns a copy of the half vector as a `Vec<f16>`.
pub
fn
to_vec
(
&
self
)
->
Vec
<
f16
>
{
self
.
0
.
clone
(
)
}
/// Returns the half vector as a slice.
pub
fn
as_slice
(
&
self
)
->
&
[
f16
]
{
self
.
0
.
as_slice
(
)
}
#
[
cfg
(
any
(
feature =
"postgres"
,
feature =
"sqlx"
,
feature =
"diesel"
)
)
]
pub
(
crate
)
fn
from_sql
(
buf
:
&
[
u8
]
,
)
->
Result
<
HalfVector
,
Box
<
dyn
std
::
error
::
Error
+
Sync
+
Send
>
>
{
let
dim = u16
::
from_be_bytes
(
buf
[
0
..
2
]
.
try_into
(
)
?
)
.
into
(
)
;
let
unused = u16
::
from_be_bytes
(
buf
[
2
..
4
]
.
try_into
(
)
?
)
;
if
unused !=
0
{
return
Err
(
"expected unused to be 0"
.
into
(
)
)
;
}
let
mut
vec =
Vec
::
with_capacity
(
dim
)
;
for
i
in
0
..dim
{
let
s =
4
+
2
*
i
;
vec
.
push
(
f16
::
from_be_bytes
(
buf
[
s..s +
2
]
.
try_into
(
)
?
)
)
;
}
Ok
(
HalfVector
(
vec
)
)
}
}
#
[
cfg
(
test
)
]
mod
tests
{
use
crate
::
HalfVector
;
use
half
::
f16
;
#
[
test
]
fn
test_into
(
)
{
let
vec =
HalfVector
::
from
(
vec
!
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
,
]
)
;
let
f16_vec
:
Vec
<
f16
>
= vec
.
into
(
)
;
assert_eq
!
(
f16_vec
,
vec!
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
]
)
;
}
#
[
test
]
fn
test_to_vec
(
)
{
let
vec =
HalfVector
::
from
(
vec
!
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
,
]
)
;
assert_eq
!
(
vec
.
to_vec
(
)
,
vec!
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
]
)
;
}
#
[
test
]
fn
test_as_slice
(
)
{
let
vec =
HalfVector
::
from
(
vec
!
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
,
]
)
;
assert_eq
!
(
vec
.
as_slice
(
)
,
&
[
f16
::
from_f32
(
1.0
)
,
f16
::
from_f32
(
2.0
)
,
f16
::
from_f32
(
3.0
)
]
)
;
}
}
Back
|
FazBrowse Home
|
New Git URL