Krotos Modules 3
Loading...
Searching...
No Matches
bruteforce.h
Go to the documentation of this file.
1#pragma once
2#include <unordered_map>
3#include <fstream>
4#include <mutex>
5#include <algorithm>
6
7namespace hnswlib {
8 template<typename dist_t>
9 class BruteforceSearch : public AlgorithmInterface<dist_t> {
10 public:
11 BruteforceSearch(SpaceInterface <dist_t> */*s*/) {
12
13 }
14 BruteforceSearch(SpaceInterface<dist_t> *s, const std::string &location) {
15 loadIndex(location, s);
16 }
17
18 BruteforceSearch(SpaceInterface <dist_t> *s, size_t maxElements) {
19 maxelements_ = maxElements;
24 data_ = (char *) malloc(maxElements * size_per_element_);
25 if (data_ == nullptr)
26 std::runtime_error("Not enough memory: BruteforceSearch failed to allocate data");
28 }
29
31 free(data_);
32 }
33
34 char *data_;
38
39 size_t data_size_;
42 std::mutex index_lock;
43
44 std::unordered_map<labeltype,size_t > dict_external_to_internal;
45
46 void addPoint(const void *datapoint, labeltype label) {
47
48 size_t idx;
49 {
50 std::unique_lock<std::mutex> lock(index_lock);
51
52
53
54 auto search=dict_external_to_internal.find(label);
55 if (search != dict_external_to_internal.end()) {
56 idx=search->second;
57 }
58 else{
60 throw std::runtime_error("The number of elements exceeds the specified limit\n");
61 }
63 dict_external_to_internal[label] = idx;
65 }
66 }
67 memcpy(data_ + size_per_element_ * idx + data_size_, &label, sizeof(labeltype));
68 memcpy(data_ + size_per_element_ * idx, datapoint, data_size_);
69
70
71
72
73 };
74
75 void removePoint(labeltype cur_external) {
76 size_t cur_c=dict_external_to_internal[cur_external];
77
78 dict_external_to_internal.erase(cur_external);
79
81 dict_external_to_internal[label]=cur_c;
82 memcpy(data_ + size_per_element_ * cur_c,
84 data_size_+sizeof(labeltype));
86
87 }
88
89
90 std::priority_queue<std::pair<dist_t, labeltype >>
91 searchKnn(const void *query_data, size_t k) const {
92 std::priority_queue<std::pair<dist_t, labeltype >> topResults;
93 if (cur_element_count == 0) return topResults;
94 for (int i = 0; i < k; i++) {
95 dist_t dist = fstdistfunc_(query_data, data_ + size_per_element_ * i, dist_func_param_);
96 topResults.push(std::pair<dist_t, labeltype>(dist, *((labeltype *) (data_ + size_per_element_ * i +
97 data_size_))));
98 }
99 dist_t lastdist = topResults.top().first;
100 for (size_t i = k; i < cur_element_count; i++) {
101 dist_t dist = fstdistfunc_(query_data, data_ + size_per_element_ * i, dist_func_param_);
102 if (dist <= lastdist) {
103 topResults.push(std::pair<dist_t, labeltype>(dist, *((labeltype *) (data_ + size_per_element_ * i +
104 data_size_))));
105 if (topResults.size() > k)
106 topResults.pop();
107 lastdist = topResults.top().first;
108 }
109
110 }
111 return topResults;
112 };
113
114 void saveIndex(const std::string &location) {
115 std::ofstream output(location, std::ios::binary);
116 std::streampos position;
117
121
122 output.write(data_, maxelements_ * size_per_element_);
123
124 output.close();
125 }
126
127 void loadIndex(const std::string &location, SpaceInterface<dist_t> *s) {
128
129
130 std::ifstream input(location, std::ios::binary);
131 std::streampos position;
132
136
141 data_ = (char *) malloc(maxelements_ * size_per_element_);
142 if (data_ == nullptr)
143 std::runtime_error("Not enough memory: loadIndex failed to allocate data");
144
145 input.read(data_, maxelements_ * size_per_element_);
146
147 input.close();
148
149 }
150
151 };
152}
Definition hnswlib.h:155
Definition bruteforce.h:9
void * dist_func_param_
Definition bruteforce.h:41
std::unordered_map< labeltype, size_t > dict_external_to_internal
Definition bruteforce.h:44
size_t size_per_element_
Definition bruteforce.h:37
~BruteforceSearch()
Definition bruteforce.h:30
DISTFUNC< dist_t > fstdistfunc_
Definition bruteforce.h:40
BruteforceSearch(SpaceInterface< dist_t > *s, const std::string &location)
Definition bruteforce.h:14
BruteforceSearch(SpaceInterface< dist_t > *s, size_t maxElements)
Definition bruteforce.h:18
void saveIndex(const std::string &location)
Definition bruteforce.h:114
char * data_
Definition bruteforce.h:34
size_t cur_element_count
Definition bruteforce.h:36
void loadIndex(const std::string &location, SpaceInterface< dist_t > *s)
Definition bruteforce.h:127
void addPoint(const void *datapoint, labeltype label)
Definition bruteforce.h:46
BruteforceSearch(SpaceInterface< dist_t > *)
Definition bruteforce.h:11
std::priority_queue< std::pair< dist_t, labeltype > > searchKnn(const void *query_data, size_t k) const
Definition bruteforce.h:91
size_t maxelements_
Definition bruteforce.h:35
void removePoint(labeltype cur_external)
Definition bruteforce.h:75
std::mutex index_lock
Definition bruteforce.h:42
size_t data_size_
Definition bruteforce.h:39
Definition hnswlib.h:142
virtual size_t get_data_size()=0
virtual void * get_dist_func_param()=0
virtual DISTFUNC< MTYPE > get_dist_func()=0
Definition bruteforce.h:7
size_t labeltype
Definition hnswlib.h:117
static void writeBinaryPOD(std::ostream &out, const T &podRef)
Definition hnswlib.h:128
static void readBinaryPOD(std::istream &in, T &podRef)
Definition hnswlib.h:133
MTYPE(*)(const void *, const void *, const void *) DISTFUNC
Definition hnswlib.h:138