-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.cpp
242 lines (180 loc) · 7.19 KB
/
utils.cpp
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
#include "utils.h"
#include<iostream>
#include<fstream>
#include<assert.h>
#include<vector>
namespace aknnspace {
utils::utils()
{
}
utils::~utils()
{
}
void utils::load_xvecs(const char * filename, unsigned *& knngraph, unsigned k)
{
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary|std::ios_base::in);
// check if open successfully
assert(infile.is_open());
// every first 4 bytes of each vector represents its dimensionality
unsigned dimensionality;
infile.read((char*)&dimensionality, 4);
// calculate number of sample
// jump to end of file to calculate its length
infile.seekg(0, std::ios_base::end);
std::ios::pos_type size_tmp = infile.tellg();
// convert into byte unit
size_t size_in_byte = (size_t)size_tmp;
// every vector contains 4 + d*4 bytes (dimensionality(4 bytes) + features(4 bytes per element))
unsigned num = (unsigned)(size_in_byte / (dimensionality + 1) / 4);
// allocate memory for data
// '.ivecs' require unsigned or int type
// read first k element only
unsigned diff = dimensionality - k;
knngraph = new unsigned[num*k];
// return to begin of the file
infile.seekg(0, std::ios_base::beg);
for (size_t i = 0; i < num; i++) {
// step over first 4 bytes in every vector
infile.seekg(4, std::ios_base::cur);
// read k * 4 bytes every time
infile.read((char*)(knngraph + i * k), k* sizeof(unsigned));
// step over the rest data
infile.seekg(diff * 4, std::ios_base::cur);
}
infile.close();
printf("done!\n");
}
void utils::load_xvecs(const char *filename, unsigned* &ivecs) {
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary);
// check if open successfully
assert(infile.is_open());
// every first 4 bytes of each vector represents its dimensionality
unsigned dimensionality;
infile.read((char*)&dimensionality, 4);
// calculate number of sample
// jump to end of file to calculate its length
infile.seekg(0, std::ios_base::end);
std::ios::pos_type size_tmp = infile.tellg();
// convert into byte unit
size_t size_in_byte = (size_t)size_tmp;
// every vector contains 4 + d*4 bytes (dimensionality(4 bytes) + features(4 bytes per element))
unsigned num = (unsigned)(size_in_byte / (dimensionality + 1) / 4);
// allocate memory for data
// '.ivecs' require unsigned or int type
ivecs = new unsigned[num*dimensionality];
// return to begin of the file
infile.seekg(0, std::ios_base::beg);
for (size_t i = 0; i < num; i++) {
// step over first 4 bytes in every vector
infile.seekg(4, std::ios_base::cur);
// read d*4 bytes every time
infile.read((char*)(ivecs + i * dimensionality), dimensionality * sizeof(unsigned));
}
infile.close();
printf("done!\n");
}
void utils::load_xvecs(const char *filename, float* &fvecs, unsigned &dimension, unsigned &number) {
// most parts are similar to load_ivecs() except data type is float
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary);
// check if open successfully
assert(infile.is_open());
// every first 4 bytes of each vector represents its dimensionality
unsigned dimensionality;
infile.read((char*)&dimensionality, 4);
// calculate number of sample
// jump to end of file to calculate its length
infile.seekg(0, std::ios_base::end);
std::ios::pos_type size_tmp = infile.tellg();
// convert into byte unit
size_t size_in_byte = (size_t)size_tmp;
// every vector contains 4 + d*4 bytes (dimensionality(4 bytes) + features(4 bytes per element))
unsigned num = (unsigned)(size_in_byte / (dimensionality + 1) / 4);
//num = 10000;
// allocate memory for data
// (.fvecs require float type)
fvecs = new float[num*dimensionality];
// return to begin of the file
infile.seekg(0, std::ios_base::beg);
for (size_t i = 0; i < num; i++) {
// step over first 4 bytes in every vector
infile.seekg(4, std::ios_base::cur);
// read d*4 bytes every time
infile.read((char*)(fvecs + i * dimensionality), dimensionality * sizeof(float));
}
infile.close();
dimension = dimensionality;
number = num;
printf("done!\n");
}
void utils::load_my_binary_file(const char * filename, float * queryR, unsigned query_num, unsigned dimension)
{
printf("loading data from %s\t", filename);
std::fstream infile(filename, std::ios_base::binary | std::ios_base::in);
// check if open successfully
assert(infile.is_open());
// read data into one-dim array,
// shape: query_num * dimension (float32)
for (unsigned i = 0; i < query_num; i++)
for (unsigned j = 0; j < dimension; j++) {
infile.read((char*)(queryR + i * dimension + j), 4);
}
infile.close();
printf("done!\n");
}
void utils::load_my_binary_file(const char *filename, uint8_t *codebook, unsigned num_of_subspace, unsigned num_of_sample)
{
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary | std::ios_base::in);
// check if open successfully
assert(infile.is_open());
// read data into one-dim array,
// shape: num_of_sample * num_of_subspace (uint8)
for (unsigned tmp_i = 0; tmp_i < num_of_sample; tmp_i++) {
for (unsigned tmp_j = 0; tmp_j < num_of_subspace; tmp_j++) {
infile.read((char*)(codebook + tmp_i * num_of_subspace + tmp_j), 1);
}
}
infile.close();
printf("done!\n");
}
void utils::load_my_binary_file(const char * filename, float * lookuptable, unsigned num_of_subspace)
{
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary | std::ios_base::in);
// check if open successfully
assert(infile.is_open());
// read data into one-dim array,
// shape: 256 * 256 * num_of_subspace (float32)
for (unsigned tmp_k = 0; tmp_k < num_of_subspace; tmp_k++)
for (unsigned tmp_i = 0; tmp_i < 256; tmp_i++)
for (unsigned tmp_j = 0; tmp_j < 256; tmp_j++)
infile.read((char*)(lookuptable + tmp_k * 65536 + tmp_i * 256 + tmp_j), 4);
infile.close();
printf("done!\n");
}
void utils::load_my_binary_file(const char * filename, float * centroids, unsigned dimension, bool iscentroid)
{
printf("loading data from %s\t", filename);
std::ifstream infile(filename, std::ios_base::binary | std::ios_base::in);
// check if open successfully
assert(infile.is_open());
// read data into one-dim array,
// shape: 256 * dimension (float32)
for (unsigned tmp_i = 0; tmp_i < 256; tmp_i++)
for (unsigned tmp_j = 0; tmp_j < dimension; tmp_j++)
infile.read((char*)(centroids + tmp_i * dimension + tmp_j), 4);
// infile >> centroids[tmp_i*dimension + tmp_j];
infile.close();
printf("done!\n");
}
void utils::load_u8code(const char * filename, uint8_t * codebook, unsigned length)
{// DEPRECATED
std::fstream infile(filename, std::ios_base::binary | std::ios_base::in);
for (int i = 0; i < length; i++)
infile.read((char*)(codebook + i), 1);
return;
}
}