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){}
70 std::vector<DistancePair>
getNeighbors(BatchInputType
const& patterns, std::size_t k)
const{
71 std::size_t numPatterns =
size(patterns);
80 typedef typename std::vector<DistancePair>::iterator iterator;
88 for(std::size_t p = 0; p != numPatterns; ++p){
89 std::size_t batchSize = distances.size2();
93 iterator heapStart=heaps.begin()+heap*k;
94 iterator heapEnd=heapStart+k;
95 iterator biggest=heapEnd-1;
98 for(std::size_t i = 0; i != batchSize; ++i){
99 if(biggest->key >= distances(p,i)){
101 biggest->key=distances(p,i);
102 biggest->value=
get(m_dataset.
batch(b).label,i);
103 std::push_heap(heapStart,heapEnd);
106 std::pop_heap(heapStart,heapEnd);
111 std::vector<DistancePair> results(k*numPatterns);
117 iterator heapStart=heaps.begin()+p*maxThreads*k;
118 iterator heapEnd=heapStart+maxThreads*k;
119 iterator neighborEnd=heapEnd-k;
120 iterator smallest=heapEnd-1;
123 std::make_heap(heapStart,heapEnd,std::greater<DistancePair>());
126 for(std::size_t i = 0;heapEnd!=neighborEnd;--heapEnd,--smallest,++i){
127 std::pop_heap(heapStart,heapEnd,std::greater<DistancePair>());
128 results[i+p*k].key = smallest->key;
129 results[i+p*k].value = smallest->value;
142 Metric
const* mep_metric;