-
Notifications
You must be signed in to change notification settings - Fork 109
/
USearchIndex.cs
322 lines (277 loc) 路 9.09 KB
/
USearchIndex.cs
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.Runtime.InteropServices;
using static Cloud.Unum.USearch.NativeMethods;
namespace Cloud.Unum.USearch;
public class USearchIndex : IDisposable
{
private IntPtr _index;
private bool _disposedValue = false;
private ulong _cachedDimensions;
public USearchIndex(
MetricKind metricKind,
ScalarKind quantization,
ulong dimensions,
ulong connectivity = 0,
ulong expansionAdd = 0,
ulong expansionSearch = 0,
bool multi = false
//CustomDistanceFunction? customMetric = null
)
{
IndexOptions initOptions = new()
{
metric_kind = metricKind,
metric = default,
quantization = quantization,
dimensions = dimensions,
connectivity = connectivity,
expansion_add = expansionAdd,
expansion_search = expansionSearch,
multi = multi
};
this._index = usearch_init(ref initOptions, out IntPtr error);
HandleError(error);
this._cachedDimensions = dimensions;
}
public USearchIndex(IndexOptions options)
{
this._index = usearch_init(ref options, out IntPtr error);
HandleError(error);
this._cachedDimensions = options.dimensions;
}
public USearchIndex(string path, bool view = false)
{
IndexOptions initOptions = new();
this._index = usearch_init(ref initOptions, out IntPtr error);
HandleError(error);
if (view)
{
usearch_view(this._index, path, out error);
}
else
{
usearch_load(this._index, path, out error);
}
HandleError(error);
this._cachedDimensions = this.Dimensions();
}
public void Save(string path)
{
usearch_save(this._index, path, out IntPtr error);
HandleError(error);
}
public ulong Size()
{
ulong size = (ulong)usearch_size(this._index, out IntPtr error);
HandleError(error);
return size;
}
public ulong Capacity()
{
ulong capacity = (ulong)usearch_capacity(this._index, out IntPtr error);
HandleError(error);
return capacity;
}
public ulong Dimensions()
{
ulong dimensions = (ulong)usearch_dimensions(this._index, out IntPtr error);
HandleError(error);
return dimensions;
}
public ulong Connectivity()
{
ulong connectivity = (ulong)usearch_connectivity(this._index, out IntPtr error);
HandleError(error);
return connectivity;
}
public bool Contains(ulong key)
{
bool result = usearch_contains(this._index, key, out IntPtr error);
HandleError(error);
return result;
}
public int Count(ulong key)
{
int count = checked((int)usearch_count(this._index, key, out IntPtr error));
HandleError(error);
return count;
}
private void IncreaseCapacity(ulong size)
{
usearch_reserve(this._index, (UIntPtr)(this.Size() + size), out IntPtr error);
HandleError(error);
}
private void CheckIncreaseCapacity(ulong size_increase)
{
ulong size_demand = this.Size() + size_increase;
if (this.Capacity() < size_demand)
{
this.IncreaseCapacity(size_increase);
}
}
public void Add(ulong key, float[] vector)
{
this.CheckIncreaseCapacity(1);
usearch_add(this._index, key, vector, ScalarKind.Float32, out IntPtr error);
HandleError(error);
}
public void Add(ulong key, double[] vector)
{
this.CheckIncreaseCapacity(1);
usearch_add(this._index, key, vector, ScalarKind.Float64, out IntPtr error);
HandleError(error);
}
public void Add(ulong[] keys, float[][] vectors)
{
this.CheckIncreaseCapacity((ulong)vectors.Length);
for (int i = 0; i < vectors.Length; i++)
{
usearch_add(this._index, keys[i], vectors[i], ScalarKind.Float32, out IntPtr error);
HandleError(error);
}
}
public void Add(ulong[] keys, double[][] vectors)
{
this.CheckIncreaseCapacity((ulong)vectors.Length);
for (int i = 0; i < vectors.Length; i++)
{
usearch_add(this._index, keys[i], vectors[i], ScalarKind.Float64, out IntPtr error);
HandleError(error);
}
}
public int Get(ulong key, out float[] vector)
{
vector = new float[this._cachedDimensions];
int foundVectorsCount = checked((int)usearch_get(this._index, key, (UIntPtr)1, vector, ScalarKind.Float32, out IntPtr error));
HandleError(error);
if (foundVectorsCount < 1)
{
vector = null;
}
return foundVectorsCount;
}
public int Get(ulong key, int count, out float[][] vectors)
{
var flattenVectors = new float[count * (int)this._cachedDimensions];
int foundVectorsCount = checked((int)usearch_get(this._index, key, (UIntPtr)count, flattenVectors, ScalarKind.Float32, out IntPtr error));
HandleError(error);
if (foundVectorsCount < 1)
{
vectors = null;
}
else
{
vectors = new float[foundVectorsCount][];
for (int i = 0; i < foundVectorsCount; i++)
{
vectors[i] = new float[this._cachedDimensions];
Array.Copy(flattenVectors, i * (int)this._cachedDimensions, vectors[i], 0, (int)this._cachedDimensions);
}
}
return foundVectorsCount;
}
public int Get(ulong key, out double[] vector)
{
vector = new double[this._cachedDimensions];
int foundVectorsCount = checked((int)usearch_get(this._index, key, (UIntPtr)1, vector, ScalarKind.Float64, out IntPtr error));
HandleError(error);
if (foundVectorsCount < 1)
{
vector = null;
}
return foundVectorsCount;
}
public int Get(ulong key, int count, out double[][] vectors)
{
var flattenVectors = new double[count * (int)this._cachedDimensions];
int foundVectorsCount = checked((int)usearch_get(this._index, key, (UIntPtr)count, flattenVectors, ScalarKind.Float64, out IntPtr error));
HandleError(error);
if (foundVectorsCount < 1)
{
vectors = null;
}
else
{
vectors = new double[foundVectorsCount][];
for (int i = 0; i < foundVectorsCount; i++)
{
vectors[i] = new double[this._cachedDimensions];
Array.Copy(flattenVectors, i * (int)this._cachedDimensions, vectors[i], 0, (int)this._cachedDimensions);
}
}
return foundVectorsCount;
}
private int Search<T>(T[] queryVector, int count, out ulong[] keys, out float[] distances, ScalarKind scalarKind)
{
keys = new ulong[count];
distances = new float[count];
GCHandle handle = GCHandle.Alloc(queryVector, GCHandleType.Pinned);
int matches = 0;
try
{
IntPtr queryVectorPtr = handle.AddrOfPinnedObject();
matches = checked((int)usearch_search(this._index, queryVectorPtr, scalarKind, (UIntPtr)count, keys, distances, out IntPtr error));
HandleError(error);
}
finally
{
handle.Free();
}
if (matches < count)
{
Array.Resize(ref keys, (int)matches);
Array.Resize(ref distances, (int)matches);
}
return matches;
}
public int Search(float[] queryVector, int count, out ulong[] keys, out float[] distances)
{
return this.Search(queryVector, count, out keys, out distances, ScalarKind.Float32);
}
public int Search(double[] queryVector, int count, out ulong[] keys, out float[] distances)
{
return this.Search(queryVector, count, out keys, out distances, ScalarKind.Float64);
}
public int Remove(ulong key)
{
int removedCount = checked((int)usearch_remove(this._index, key, out IntPtr error));
HandleError(error);
return removedCount;
}
public int Rename(ulong keyFrom, ulong keyTo)
{
int foundVectorsCount = checked((int)usearch_rename(this._index, keyFrom, keyTo, out IntPtr error));
HandleError(error);
return foundVectorsCount;
}
private static void HandleError(IntPtr error)
{
if (error != IntPtr.Zero)
{
throw new USearchException($"USearch operation failed: {Marshal.PtrToStringAnsi(error)}");
}
}
private void FreeIndex()
{
if (this._index != IntPtr.Zero)
{
usearch_free(this._index, out IntPtr error);
HandleError(error);
this._index = IntPtr.Zero;
}
}
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposedValue)
{
this.FreeIndex();
this._disposedValue = true;
}
}
~USearchIndex() => this.Dispose(false);
}