-
Notifications
You must be signed in to change notification settings - Fork 4
/
pri_queue.cc
84 lines (73 loc) · 2.14 KB
/
pri_queue.cc
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
#include "pri_queue.h"
// -----------------------------------------------------------------------------
MinK_List::MinK_List( // constructor (given max size)
int max) // max size
{
num_ = 0;
k_ = max;
list_ = new Result[max + 1];
}
// -----------------------------------------------------------------------------
MinK_List::~MinK_List() // destructor
{
if (list_ != NULL) {
delete[] list_; list_ = NULL;
}
}
// -----------------------------------------------------------------------------
bool MinK_List::isFull() // is full?
{
if (num_ >= k_) return true;
else return false;
}
// -----------------------------------------------------------------------------
Scalar MinK_List::insert( // insert item (inline for speed)
Scalar key, // key of item
int id) // id of item
{
int i = 0;
for (i = num_; i > 0; --i) {
if (key < list_[i-1].key_) list_[i] = list_[i - 1];
else break;
}
list_[i].key_ = key; // store new item here
list_[i].id_ = id;
if (num_ < k_) num_++; // increase the number of items
return max_key();
}
// -----------------------------------------------------------------------------
MaxK_List::MaxK_List( // constructor (given max size)
int max) // max size
{
num_ = 0;
k_ = max;
list_ = new Result[max + 1];
}
// -----------------------------------------------------------------------------
MaxK_List::~MaxK_List() // destructor
{
if (list_ != NULL) {
delete[] list_; list_ = NULL;
}
}
// -----------------------------------------------------------------------------
bool MaxK_List::isFull() // is full?
{
if (num_ >= k_) return true;
else return false;
}
// -----------------------------------------------------------------------------
Scalar MaxK_List::insert( // insert item
Scalar key, // key of item
int id) // id of item
{
int i = 0;
for (i = num_; i > 0; i--) {
if (list_[i-1].key_ < key) list_[i] = list_[i - 1];
else break;
}
list_[i].key_ = key; // store new item here
list_[i].id_ = id;
if (num_ < k_) num_++; // increase the number of items
return min_key();
}