-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPGhalfvec.java
More file actions
103 lines (94 loc) · 2.25 KB
/
PGhalfvec.java
File metadata and controls
103 lines (94 loc) · 2.25 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.pgvector;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import org.postgresql.PGConnection;
import org.postgresql.util.PGobject;
/**
* A half vector.
*/
public class PGhalfvec extends PGobject implements Serializable, Cloneable {
/*
* Use float and text format for now since Float.float16ToFloat/floatToFloat16
* are not available until Java 20.
*/
private float[] vec;
/**
* @hidden
*/
public PGhalfvec() {
type = "halfvec";
}
/**
* Creates a half vector from an array.
*
* @param v float array
*/
public PGhalfvec(float[] v) {
this();
vec = v;
}
/**
* Creates a half vector from a list.
*
* @param <T> number
* @param v list of numbers
*/
public <T extends Number> PGhalfvec(List<T> v) {
this();
if (Objects.isNull(v)) {
vec = null;
} else {
vec = new float[v.size()];
int i = 0;
for (T f : v) {
vec[i++] = f.floatValue();
}
}
}
/**
* Creates a half vector from a text representation.
*
* @param s text representation of a half vector
* @throws SQLException exception
*/
public PGhalfvec(String s) throws SQLException {
this();
setValue(s);
}
/**
* Sets the value from a text representation of a half vector.
*/
public void setValue(String s) throws SQLException {
if (s == null) {
vec = null;
} else {
String[] sp = s.substring(1, s.length() - 1).split(",");
vec = new float[sp.length];
for (int i = 0; i < sp.length; i++) {
vec[i] = Float.parseFloat(sp[i]);
}
}
}
/**
* Returns the text representation of a half vector.
*/
public String getValue() {
if (vec == null) {
return null;
} else {
return Arrays.toString(vec).replace(" ", "");
}
}
/**
* Returns an array.
*
* @return an array
*/
public float[] toArray() {
return vec;
}
}