Skip to content

Commit 04d6b33

Browse files
committed
Performing proper serialization of LSH index.
1 parent dc31167 commit 04d6b33

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

src/cpp/flann/algorithms/lsh_index.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,8 @@ class LshIndex : public NNIndex<Distance>
181181
ar & key_size_;
182182
ar & multi_probe_level_;
183183

184-
if (Archive::is_loading::value) {
185-
// Building the index is so fast we can afford not storing it
186-
buildIndex();
187-
}
184+
ar & xor_masks_;
185+
ar & tables_;
188186

189187
if (Archive::is_loading::value) {
190188
index_params_["algorithm"] = getType();

src/cpp/flann/util/lsh_table.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,33 @@ class LshTable
297297
}
298298
}
299299

300+
template<typename Archive>
301+
void serialize(Archive& ar)
302+
{
303+
int val;
304+
if (Archive::is_saving::value) {
305+
val = (int)speed_level_;
306+
}
307+
ar & val;
308+
if (Archive::is_loading::value) {
309+
speed_level_ = (SpeedLevel) val;
310+
}
311+
312+
ar & key_size_;
313+
ar & mask_;
314+
315+
if (speed_level_==kArray) {
316+
ar & buckets_speed_;
317+
}
318+
if (speed_level_==kBitsetHash || speed_level_==kHash) {
319+
ar & buckets_space_;
320+
}
321+
if (speed_level_==kBitsetHash) {
322+
ar & key_bitset_;
323+
}
324+
}
325+
friend struct serialization::access;
326+
300327
/** The vector of all the buckets if they are held for speed
301328
*/
302329
BucketsSpeed buckets_speed_;

src/cpp/flann/util/serialization.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define SERIALIZATION_H_
33

44
#include <vector>
5+
#include <map>
56
#include <stdio.h>
67

78
namespace flann
@@ -116,6 +117,36 @@ struct Serializer<std::vector<T> >
116117
}
117118
};
118119

120+
// serializer for std::vector
121+
template<typename K, typename V>
122+
struct Serializer<std::map<K,V> >
123+
{
124+
template<typename InputArchive>
125+
static inline void load(InputArchive& ar, std::map<K,V>& map_val)
126+
{
127+
size_t size;
128+
ar & size;
129+
for (size_t i = 0; i < size; ++i)
130+
{
131+
K key;
132+
ar & key;
133+
V value;
134+
ar & value;
135+
map_val[key] = value;
136+
}
137+
}
138+
139+
template<typename OutputArchive>
140+
static inline void save(OutputArchive& ar, const std::map<K,V>& map_val)
141+
{
142+
ar & map_val.size();
143+
for (typename std::map<K,V>::const_iterator i=map_val.begin(); i!=map_val.end(); ++i) {
144+
ar & i->first;
145+
ar & i->second;
146+
}
147+
}
148+
};
149+
119150
template<typename T>
120151
struct Serializer<T*>
121152
{

0 commit comments

Comments
 (0)