-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPGsparsevec.java
More file actions
283 lines (248 loc) · 7.28 KB
/
PGsparsevec.java
File metadata and controls
283 lines (248 loc) · 7.28 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package com.pgvector;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.postgresql.PGConnection;
import org.postgresql.util.ByteConverter;
import org.postgresql.util.PGBinaryObject;
import org.postgresql.util.PGobject;
/**
* A sparse vector.
*/
public class PGsparsevec extends PGobject implements PGBinaryObject, Serializable, Cloneable {
private int dimensions;
private int[] indices;
private float[] values;
/**
* @hidden
*/
public PGsparsevec() {
type = "sparsevec";
}
/**
* Creates a sparse vector from an array.
*
* @param v float array
*/
public PGsparsevec(float[] v) {
this();
int nnz = 0;
for (int i = 0; i < v.length; i++) {
if (v[i] != 0) {
nnz++;
}
}
dimensions = v.length;
indices = new int[nnz];
values = new float[nnz];
int j = 0;
for (int i = 0; i < v.length; i++) {
if (v[i] != 0) {
indices[j] = i;
values[j] = v[i];
j++;
}
}
}
/**
* Creates a sparse vector from a list.
*
* @param <T> number
* @param v list of numbers
*/
public <T extends Number> PGsparsevec(List<T> v) {
this();
if (Objects.isNull(v)) {
indices = null;
} else {
int nnz = 0;
for (T f : v) {
if (f.floatValue() != 0) {
nnz++;
}
}
dimensions = v.size();
indices = new int[nnz];
values = new float[nnz];
int i = 0;
int j = 0;
for (T f : v) {
float fv = f.floatValue();
if (fv != 0) {
indices[j] = i;
values[j] = fv;
j++;
}
i++;
}
}
}
/**
* Creates a sparse vector from a map of non-zero elements.
* <p>
* Indices start at 0.
*
* @param <T> number
* @param map map of non-zero elements
* @param dimensions number of dimensions
*/
public <T extends Number> PGsparsevec(Map<Integer, T> map, int dimensions) {
this();
ArrayList<Map.Entry<Integer, T>> elements = new ArrayList<Map.Entry<Integer, T>>();
if (!Objects.isNull(map)) {
elements.addAll(map.entrySet());
}
elements.removeIf((e) -> e.getValue().floatValue() == 0);
elements.sort((a, b) -> Integer.compare(a.getKey(), b.getKey()));
int nnz = elements.size();
indices = new int[nnz];
values = new float[nnz];
int i = 0;
for (Map.Entry<Integer, T> e : elements) {
indices[i] = e.getKey().intValue();
values[i] = e.getValue().floatValue();
i++;
}
this.dimensions = dimensions;
}
/**
* Creates a sparse vector from a text representation.
*
* @param s text representation of a sparse vector
* @throws SQLException exception
*/
public PGsparsevec(String s) throws SQLException {
this();
setValue(s);
}
/**
* Sets the value from a text representation of a sparse vector.
*/
public void setValue(String s) throws SQLException {
if (s == null) {
indices = null;
} else {
String[] sp = s.split("/", 2);
String[] elements = sp[0].substring(1, sp[0].length() - 1).split(",");
dimensions = Integer.parseInt(sp[1]);
indices = new int[elements.length];
values = new float[elements.length];
for (int i = 0; i < elements.length; i++) {
String[] ep = elements[i].split(":", 2);
indices[i] = Integer.parseInt(ep[0]) - 1;
values[i] = Float.parseFloat(ep[1]);
}
}
}
/**
* Returns the text representation of a sparse vector.
*/
public String getValue() {
if (indices == null) {
return null;
} else {
StringBuilder sb = new StringBuilder(13 + 27 * indices.length);
sb.append('{');
for (int i = 0; i < indices.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(indices[i] + 1);
sb.append(':');
sb.append(values[i]);
}
sb.append('}');
sb.append('/');
sb.append(dimensions);
return sb.toString();
}
}
/**
* Returns the number of bytes for the binary representation.
*/
public int lengthInBytes() {
return indices == null ? 0 : 12 + indices.length * 4 + values.length * 4;
}
/**
* Sets the value from a binary representation of a sparse vector.
*/
public void setByteValue(byte[] value, int offset) throws SQLException {
dimensions = ByteConverter.int4(value, offset);
int nnz = ByteConverter.int4(value, offset + 4);
int unused = ByteConverter.int4(value, offset + 8);
if (unused != 0) {
throw new SQLException("expected unused to be 0");
}
indices = new int[nnz];
for (int i = 0; i < nnz; i++) {
indices[i] = ByteConverter.int4(value, offset + 12 + i * 4);
}
values = new float[nnz];
for (int i = 0; i < nnz; i++) {
values[i] = ByteConverter.float4(value, offset + 12 + nnz * 4 + i * 4);
}
}
/**
* Writes the binary representation of a sparse vector.
*/
public void toBytes(byte[] bytes, int offset) {
if (indices == null) {
return;
}
// server will error on overflow due to unconsumed buffer
// could set to Integer.MAX_VALUE for friendlier error message
ByteConverter.int4(bytes, offset, dimensions);
ByteConverter.int4(bytes, offset + 4, indices.length);
ByteConverter.int4(bytes, offset + 8, 0);
for (int i = 0; i < indices.length; i++) {
ByteConverter.int4(bytes, offset + 12 + i * 4, indices[i]);
}
for (int i = 0; i < values.length; i++) {
ByteConverter.float4(bytes, offset + 12 + indices.length * 4 + i * 4, values[i]);
}
}
/**
* Returns an array.
*
* @return an array
*/
public float[] toArray() {
if (indices == null) {
return null;
}
float[] vec = new float[dimensions];
for (int i = 0; i < indices.length; i++) {
vec[indices[i]] = values[i];
}
return vec;
}
/**
* Returns the number of dimensions.
*
* @return the number of dimensions
*/
public int getDimensions() {
return dimensions;
}
/**
* Returns the non-zero indices.
*
* @return the non-zero indices
*/
public int[] getIndices() {
return indices;
}
/**
* Returns the non-zero values.
*
* @return the non-zero values
*/
public float[] getValues() {
return values;
}
}