-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.h
86 lines (73 loc) · 2.07 KB
/
utils.h
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
#ifndef PQTABLE_UTILS_H
#define PQTABLE_UTILS_H
//#include <opencv2/opencv.hpp>
#include <fstream>
#include <time.h>
#include <vector>
namespace pqtable {
// Iterative reader class for reading .ivecs or .fvecs files.
// The next vector (std::vector<float>) is read by Next() function.
//
// Usage:
// ItrReader reader("data.fvecs", "fvecs");
// while(!reader.IsEnd()){
// std::vector<float> vec = reader.Next();
// /* some stuff for vec */
// }
//
// Optional wrapper interface:
// int top_n = 100;
// std::vector<std::vector<float> > vecs = ReadTopN("data.fvecs", "fvecs", top_n);
// Interface (abstract basic class) of iterative reader
class I_ItrReader {
public:
virtual ~I_ItrReader() {}
virtual bool IsEnd() = 0;
virtual std::vector<float> Next()=0;
virtual std::vector<int> NextInt()=0;
};
// Iterative reader for fvec file
class FvecsItrReader : I_ItrReader {
public:
FvecsItrReader(std::string filename);
bool IsEnd();
std::vector<float> Next();
std::vector<int> NextInt();
private:
FvecsItrReader(); // prohibit default construct
std::ifstream ifs;
std::vector<float> vec; // store the next vec
bool eof_flag;
};
// Iterative reader for ivec file
class IvecsItrReader : I_ItrReader {
public:
IvecsItrReader(std::string filename);
bool IsEnd();
std::vector<float> Next();
std::vector<int> NextInt();
private:
IvecsItrReader(); // prohibit default construct
std::ifstream ifs;
std::vector<int> vec; // store the next vec
bool eof_flag;
};
// Proxy class
class ItrReader {
public:
// ext must be "fvecs" or "ivecs"
ItrReader(std::string filename, std::string ext);
~ItrReader();
bool IsEnd();
std::vector<float> Next();
std::vector<int> NextInt();
private:
ItrReader();
I_ItrReader *m_reader;
};
// Wrapper. Read top-N vectors
// If top_n = -1, then read all vectors
std::vector<std::vector<float> > ReadTopN(std::string filename, std::string ext, int top_n = -1);
std::vector<std::vector<int> > ReadTopNI(std::string filename, std::string ext, int top_n = -1);
}
#endif // PQTABLE_UTILS_H