35 #ifndef SHARK_ALGORITHMS_NEARESTNEIGHBORS_SIMPLENEARESTNEIGHBORS_H 36 #define SHARK_ALGORITHMS_NEARESTNEIGHBORS_SIMPLENEARESTNEIGHBORS_H 50 template<
class InputType,
class LabelType>
67 :m_dataset(dataset), mep_metric(metric){
72 std::vector<DistancePair>
getNeighbors(BatchInputType
const& patterns, std::size_t k)
const{
73 std::size_t numPatterns =
batchSize(patterns);
81 std::vector<DistancePair> heaps(k*numPatterns*maxThreads,
DistancePair(std::numeric_limits<double>::max(),LabelType()));
82 typedef typename std::vector<DistancePair>::iterator iterator;
90 for(std::size_t p = 0; p != numPatterns; ++p){
91 std::size_t
batchSize = distances.size2();
95 iterator heapStart=heaps.begin()+heap*k;
96 iterator heapEnd=heapStart+k;
97 iterator biggest=heapEnd-1;
100 for(std::size_t i = 0; i !=
batchSize; ++i){
101 if(biggest->key >= distances(p,i)){
103 biggest->key=distances(p,i);
105 std::push_heap(heapStart,heapEnd);
108 std::pop_heap(heapStart,heapEnd);
113 std::vector<DistancePair> results(k*numPatterns);
119 iterator heapStart=heaps.begin()+p*maxThreads*k;
120 iterator heapEnd=heapStart+maxThreads*k;
121 iterator neighborEnd=heapEnd-k;
122 iterator smallest=heapEnd-1;
125 std::make_heap(heapStart,heapEnd,std::greater<DistancePair>());
128 for(std::size_t i = 0;heapEnd!=neighborEnd;--heapEnd,--smallest,++i){
129 std::pop_heap(heapStart,heapEnd,std::greater<DistancePair>());
130 results[i+p*k].key = smallest->key;
131 results[i+p*k].value = smallest->value;
144 Metric
const* mep_metric;