forked from DataIntelligenceCrew/go-faiss
-
Notifications
You must be signed in to change notification settings - Fork 3
/
selector.go
95 lines (81 loc) · 2.02 KB
/
selector.go
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
package faiss
/*
#include <faiss/c_api/impl/AuxIndexStructures_c.h>
*/
import "C"
type Selector interface {
Get() *C.FaissIDSelector
Delete()
}
// IDSelector represents a set of IDs to remove.
type IDSelector struct {
sel *C.FaissIDSelector
}
// Delete frees the memory associated with s.
func (s *IDSelector) Delete() {
if s == nil || s.sel == nil {
return
}
C.faiss_IDSelector_free(s.sel)
}
func (s *IDSelector) Get() *C.FaissIDSelector {
return s.sel
}
type IDSelectorNot struct {
sel *C.FaissIDSelector
batchSel *C.FaissIDSelector
}
// Delete frees the memory associated with s.
func (s *IDSelectorNot) Delete() {
if s == nil {
return
}
if s.sel != nil {
C.faiss_IDSelector_free(s.sel)
}
if s.batchSel != nil {
C.faiss_IDSelector_free(s.batchSel)
}
}
func (s *IDSelectorNot) Get() *C.FaissIDSelector {
return s.sel
}
// NewIDSelectorRange creates a selector that removes IDs on [imin, imax).
func NewIDSelectorRange(imin, imax int64) (Selector, error) {
var sel *C.FaissIDSelectorRange
c := C.faiss_IDSelectorRange_new(&sel, C.idx_t(imin), C.idx_t(imax))
if c != 0 {
return nil, getLastError()
}
return &IDSelector{(*C.FaissIDSelector)(sel)}, nil
}
// NewIDSelectorBatch creates a new batch selector.
func NewIDSelectorBatch(indices []int64) (Selector, error) {
var sel *C.FaissIDSelectorBatch
if c := C.faiss_IDSelectorBatch_new(
&sel,
C.size_t(len(indices)),
(*C.idx_t)(&indices[0]),
); c != 0 {
return nil, getLastError()
}
return &IDSelector{(*C.FaissIDSelector)(sel)}, nil
}
// NewIDSelectorNot creates a new Not selector, wrapped around a
// batch selector, with the IDs in 'exclude'.
func NewIDSelectorNot(exclude []int64) (Selector, error) {
batchSelector, err := NewIDSelectorBatch(exclude)
if err != nil {
return nil, err
}
var sel *C.FaissIDSelectorNot
if c := C.faiss_IDSelectorNot_new(
&sel,
batchSelector.Get(),
); c != 0 {
batchSelector.Delete()
return nil, getLastError()
}
return &IDSelectorNot{sel: (*C.FaissIDSelector)(sel),
batchSel: batchSelector.Get()}, nil
}