Skip to content

Commit dc31167

Browse files
committed
Refactorings
1 parent 568ae3d commit dc31167

File tree

9 files changed

+195
-180
lines changed

9 files changed

+195
-180
lines changed

src/cpp/flann/algorithms/autotuned_index.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ class AutotunedIndex : public NNIndex<Distance>
129129
return new AutotunedIndex(*this);
130130
}
131131

132-
133-
using NNIndex<Distance>::buildIndex;
134132
/**
135133
* Method responsible with building the index.
136134
*/
@@ -343,6 +341,17 @@ class AutotunedIndex : public NNIndex<Distance>
343341
return FLANN_INDEX_AUTOTUNED;
344342
}
345343

344+
protected:
345+
void buildIndexImpl()
346+
{
347+
/* nothing to do here */
348+
}
349+
350+
void freeIndex()
351+
{
352+
/* nothing to do here */
353+
}
354+
346355
private:
347356

348357
struct CostData

src/cpp/flann/algorithms/composite_index.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ class CompositeIndex : public NNIndex<Distance>
215215
std::swap(kdtree_index_, other.kdtree_index_);
216216
}
217217

218+
void buildIndexImpl()
219+
{
220+
/* nothing to do here */
221+
}
222+
223+
void freeIndex()
224+
{
225+
/* nothing to do here */
226+
}
227+
228+
218229
private:
219230
/** The k-means index */
220231
KMeansIndex<Distance>* kmeans_index_;

src/cpp/flann/algorithms/hierarchical_clustering_index.h

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
9494
* @param d
9595
*/
9696
HierarchicalClusteringIndex(const IndexParams& index_params = HierarchicalClusteringIndexParams(), Distance d = Distance())
97-
: BaseClass(index_params, d), size_at_build_(0)
97+
: BaseClass(index_params, d)
9898
{
9999
memoryCounter_ = 0;
100100

@@ -116,7 +116,7 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
116116
*/
117117
HierarchicalClusteringIndex(const Matrix<ElementType>& inputData, const IndexParams& index_params = HierarchicalClusteringIndexParams(),
118118
Distance d = Distance())
119-
: BaseClass(index_params, d), size_at_build_(0)
119+
: BaseClass(index_params, d)
120120
{
121121
memoryCounter_ = 0;
122122

@@ -133,7 +133,6 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
133133

134134

135135
HierarchicalClusteringIndex(const HierarchicalClusteringIndex& other) : BaseClass(other),
136-
size_at_build_(other.size_at_build_),
137136
memoryCounter_(other.memoryCounter_),
138137
branching_(other.branching_),
139138
trees_(other.trees_),
@@ -196,32 +195,9 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
196195
{
197196
return pool_.usedMemory+pool_.wastedMemory+memoryCounter_;
198197
}
199-
200-
using NNIndex<Distance>::buildIndex;
201-
/**
202-
* Builds the index
203-
*/
204-
void buildIndex()
205-
{
206-
freeIndex();
207-
cleanRemovedPoints();
208-
209-
if (branching_<2) {
210-
throw FLANNException("Branching factor must be at least 2");
211-
}
212-
tree_roots_.resize(trees_);
213-
std::vector<int> indices(size_);
214-
for (int i=0; i<trees_; ++i) {
215-
for (size_t j=0; j<size_; ++j) {
216-
indices[j] = j;
217-
}
218-
tree_roots_[i] = new(pool_) Node();
219-
computeClustering(tree_roots_[i], &indices[0], size_);
220-
}
221-
size_at_build_ = size_;
222-
}
223-
224198

199+
using BaseClass::buildIndex;
200+
225201
void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
226202
{
227203
assert(points.cols==veclen_);
@@ -313,6 +289,27 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
313289
}
314290
}
315291

292+
protected:
293+
294+
/**
295+
* Builds the index
296+
*/
297+
void buildIndexImpl()
298+
{
299+
if (branching_<2) {
300+
throw FLANNException("Branching factor must be at least 2");
301+
}
302+
tree_roots_.resize(trees_);
303+
std::vector<int> indices(size_);
304+
for (int i=0; i<trees_; ++i) {
305+
for (size_t j=0; j<size_; ++j) {
306+
indices[j] = j;
307+
}
308+
tree_roots_[i] = new(pool_) Node();
309+
computeClustering(tree_roots_[i], &indices[0], size_);
310+
}
311+
}
312+
316313
private:
317314

318315
struct PointInfo
@@ -644,7 +641,6 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
644641
BaseClass::swap(other);
645642

646643
std::swap(tree_roots_, other.tree_roots_);
647-
std::swap(size_at_build_, other.size_at_build_);
648644
std::swap(pool_, other.pool_);
649645
std::swap(memoryCounter_, other.memoryCounter_);
650646
std::swap(branching_, other.branching_);
@@ -661,11 +657,6 @@ class HierarchicalClusteringIndex : public NNIndex<Distance>
661657
*/
662658
std::vector<Node*> tree_roots_;
663659

664-
/**
665-
* Number of features in the dataset when the index was last built.
666-
*/
667-
size_t size_at_build_;
668-
669660
/**
670661
* Pooled memory allocator.
671662
*

src/cpp/flann/algorithms/kdtree_index.h

Lines changed: 31 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class KDTreeIndex : public NNIndex<Distance>
8787
* params = parameters passed to the kdtree algorithm
8888
*/
8989
KDTreeIndex(const IndexParams& params = KDTreeIndexParams(), Distance d = Distance() ) :
90-
BaseClass(params, d), size_at_build_(0), mean_(NULL), var_(NULL)
90+
BaseClass(params, d), mean_(NULL), var_(NULL)
9191
{
9292
trees_ = get_param(index_params_,"trees",4);
9393
}
@@ -101,16 +101,15 @@ class KDTreeIndex : public NNIndex<Distance>
101101
* params = parameters passed to the kdtree algorithm
102102
*/
103103
KDTreeIndex(const Matrix<ElementType>& dataset, const IndexParams& params = KDTreeIndexParams(),
104-
Distance d = Distance() ) : BaseClass(params,d ), size_at_build_(0), mean_(NULL), var_(NULL)
104+
Distance d = Distance() ) : BaseClass(params,d ), mean_(NULL), var_(NULL)
105105
{
106106
trees_ = get_param(index_params_,"trees",4);
107107

108108
setDataset(dataset);
109109
}
110110

111111
KDTreeIndex(const KDTreeIndex& other) : BaseClass(other),
112-
trees_(other.trees_),
113-
size_at_build_(other.size_at_build_)
112+
trees_(other.trees_)
114113
{
115114
tree_roots_.resize(other.tree_roots_.size());
116115
for (size_t i=0;i<tree_roots_.size();++i) {
@@ -137,36 +136,7 @@ class KDTreeIndex : public NNIndex<Distance>
137136
return new KDTreeIndex(*this);
138137
}
139138

140-
using NNIndex<Distance>::buildIndex;
141-
/**
142-
* Builds the index
143-
*/
144-
void buildIndex()
145-
{
146-
freeIndex();
147-
cleanRemovedPoints();
148-
149-
// Create a permutable array of indices to the input vectors.
150-
std::vector<int> ind(size_);
151-
for (size_t i = 0; i < size_; ++i) {
152-
ind[i] = int(i);
153-
}
154-
155-
mean_ = new DistanceType[veclen_];
156-
var_ = new DistanceType[veclen_];
157-
158-
tree_roots_.resize(trees_);
159-
/* Construct the randomized trees. */
160-
for (int i = 0; i < trees_; i++) {
161-
/* Randomize the order of vectors to allow for unbiased sampling. */
162-
std::random_shuffle(ind.begin(), ind.end());
163-
tree_roots_[i] = divideTree(&ind[0], int(size_) );
164-
}
165-
delete[] mean_;
166-
delete[] var_;
167-
168-
size_at_build_ = size_;
169-
}
139+
using BaseClass::buildIndex;
170140

171141
void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
172142
{
@@ -176,8 +146,6 @@ class KDTreeIndex : public NNIndex<Distance>
176146
extendDataset(points);
177147

178148
if (rebuild_threshold>1 && size_at_build_*rebuild_threshold<size_) {
179-
freeIndex();
180-
cleanRemovedPoints();
181149
buildIndex();
182150
}
183151
else {
@@ -276,7 +244,32 @@ class KDTreeIndex : public NNIndex<Distance>
276244
}
277245
}
278246

279-
private:
247+
protected:
248+
249+
/**
250+
* Builds the index
251+
*/
252+
void buildIndexImpl()
253+
{
254+
// Create a permutable array of indices to the input vectors.
255+
std::vector<int> ind(size_);
256+
for (size_t i = 0; i < size_; ++i) {
257+
ind[i] = int(i);
258+
}
259+
260+
mean_ = new DistanceType[veclen_];
261+
var_ = new DistanceType[veclen_];
262+
263+
tree_roots_.resize(trees_);
264+
/* Construct the randomized trees. */
265+
for (int i = 0; i < trees_; i++) {
266+
/* Randomize the order of vectors to allow for unbiased sampling. */
267+
std::random_shuffle(ind.begin(), ind.end());
268+
tree_roots_[i] = divideTree(&ind[0], int(size_) );
269+
}
270+
delete[] mean_;
271+
delete[] var_;
272+
}
280273

281274
void freeIndex()
282275
{
@@ -288,7 +281,7 @@ class KDTreeIndex : public NNIndex<Distance>
288281
}
289282

290283

291-
284+
private:
292285

293286
/*--------------------- Internal Data Structures --------------------------*/
294287
struct Node
@@ -712,7 +705,6 @@ class KDTreeIndex : public NNIndex<Distance>
712705
{
713706
BaseClass::swap(other);
714707
std::swap(trees_, other.trees_);
715-
std::swap(size_at_build_, other.size_at_build_);
716708
std::swap(tree_roots_, other.tree_roots_);
717709
std::swap(pool_, other.pool_);
718710
}
@@ -743,8 +735,6 @@ class KDTreeIndex : public NNIndex<Distance>
743735
*/
744736
int trees_;
745737

746-
size_t size_at_build_;
747-
748738
DistanceType* mean_;
749739
DistanceType* var_;
750740

src/cpp/flann/algorithms/kdtree_single_index.h

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,33 +138,8 @@ class KDTreeSingleIndex : public NNIndex<Distance>
138138
return new KDTreeSingleIndex(*this);
139139
}
140140

141+
using BaseClass::buildIndex;
141142

142-
using NNIndex<Distance>::buildIndex;
143-
/**
144-
* Builds the index
145-
*/
146-
void buildIndex()
147-
{
148-
freeIndex();
149-
cleanRemovedPoints();
150-
151-
// Create a permutable array of indices to the input vectors.
152-
vind_.resize(size_);
153-
for (size_t i = 0; i < size_; i++) {
154-
vind_[i] = i;
155-
}
156-
157-
computeBoundingBox(root_bbox_);
158-
root_node_ = divideTree(0, size_, root_bbox_ ); // construct the tree
159-
160-
if (reorder_) {
161-
data_ = flann::Matrix<ElementType>(new ElementType[size_*veclen_], size_, veclen_);
162-
for (size_t i=0; i<size_; ++i) {
163-
std::copy(points_[vind_[i]], points_[vind_[i]]+veclen_, data_[i]);
164-
}
165-
}
166-
}
167-
168143
void addPoints(const Matrix<ElementType>& points, float rebuild_threshold = 2)
169144
{
170145
assert(points.cols==veclen_);
@@ -256,6 +231,30 @@ class KDTreeSingleIndex : public NNIndex<Distance>
256231
}
257232
}
258233

234+
protected:
235+
236+
/**
237+
* Builds the index
238+
*/
239+
void buildIndexImpl()
240+
{
241+
// Create a permutable array of indices to the input vectors.
242+
vind_.resize(size_);
243+
for (size_t i = 0; i < size_; i++) {
244+
vind_[i] = i;
245+
}
246+
247+
computeBoundingBox(root_bbox_);
248+
root_node_ = divideTree(0, size_, root_bbox_ ); // construct the tree
249+
250+
if (reorder_) {
251+
data_ = flann::Matrix<ElementType>(new ElementType[size_*veclen_], size_, veclen_);
252+
for (size_t i=0; i<size_; ++i) {
253+
std::copy(points_[vind_[i]], points_[vind_[i]]+veclen_, data_[i]);
254+
}
255+
}
256+
}
257+
259258
private:
260259

261260

0 commit comments

Comments
 (0)