-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtopk.h
92 lines (83 loc) · 1.98 KB
/
topk.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
87
88
89
90
91
92
#ifndef TOPK_H
#define TOPK_H
#include "nnitem.h"
class TopK
{
private:
unsigned topk;
list<NNItem*> topk_lst;
list<NNItem*>::reverse_iterator rear;
public:
TopK(const int k0)
{
topk = k0;
rear = topk_lst.rbegin();
}
void insert(const unsigned int idx, const float val)
{
if(topk_lst.size() < topk)
{
NNItem *crnt_item = new NNItem(idx, val);
topk_lst.push_front(crnt_item);
if(topk_lst.size() == topk)
{
topk_lst.sort(NNItem::LGcomparer);
rear = topk_lst.rbegin();
}
}
else
{
NNItem *_item = *rear;
if(_item->val < val)
{
NNItem *crnt_item = new NNItem(idx, val);
list<NNItem*>::iterator lit;
lit = topk_lst.begin();
_item = *lit;
while(_item->val > val && lit != topk_lst.end())
{
lit++;
_item = *lit;
}
topk_lst.insert(lit, crnt_item);
lit = topk_lst.end();
lit--;
_item = *lit;
delete _item;
topk_lst.erase(lit);
rear = topk_lst.rbegin();
}
}
}
void clear()
{
list<NNItem*>::iterator lit;
NNItem *_item;
for(lit = topk_lst.begin(); lit != topk_lst.end(); lit++)
{
_item = *lit;
delete _item;
}
topk_lst.clear();
}
list<NNItem*>::iterator begin()
{
return topk_lst.begin();
}
list<NNItem*>::iterator end()
{
return topk_lst.end();
}
~TopK()
{
list<NNItem*>::iterator lit;
NNItem *_item;
for(lit = topk_lst.begin(); lit != topk_lst.end(); lit++)
{
_item = *lit;
delete _item;
}
topk_lst.clear();
}
};
#endif