-
Notifications
You must be signed in to change notification settings - Fork 10
/
minheap.cpp
198 lines (171 loc) · 4.07 KB
/
minheap.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <queue>
#include <iostream>
#include "minheap.h"
#include "channel.h"
const int MinHeap::kMinHeapInitSize = 8;
MinHeap::MinHeap() : m_data(nullptr), m_size(0), m_capacity(0)
{
}
MinHeap::~MinHeap()
{
if (nullptr != m_data)
{
free(m_data);
}
}
bool MinHeap::Greater(const ChannelPtr & lth, const ChannelPtr & rth)
{
const struct timeval & lthLastTime = lth->GetLastActiveTime();
const struct timeval & rthLastTime = rth->GetLastActiveTime();
return TimevalGreater(lthLastTime, rthLastTime);
}
void MinHeap::ShiftUp(ChannelPtr ptChannel)
{
int index = ptChannel->GetMinHeapIndex();
// channel has been removed, ignore
if (-1 == index)
{
return;
}
while(1 < index && Greater(m_data[index / 2], ptChannel))
{
m_data[index / 2]->SetMinHeapIndex(index);
m_data[index] = m_data[index / 2];
index /= 2;
}
m_data[index] = ptChannel;
ptChannel->SetMinHeapIndex(index);
}
void MinHeap::ShiftDown(ChannelPtr ptChannel)
{
int index = ptChannel->GetMinHeapIndex();
// channel has been removed, ignore
if (-1 == index)
{
return;
}
while(2 * index <= m_size)
{
int minIndex = 2 * index;
if (minIndex < m_size && Greater(m_data[minIndex], m_data[minIndex + 1]))
{
++minIndex;
}
if (Greater(m_data[minIndex], ptChannel))
{
break;
}
m_data[minIndex]->SetMinHeapIndex(index);
m_data[index] = m_data[minIndex];
index = minIndex;
}
m_data[index] = ptChannel;
ptChannel->SetMinHeapIndex(index);
}
void MinHeap::Push(ChannelPtr ptChannel, const struct timeval & stActiveTime)
{
if (-1 != ptChannel->GetMinHeapIndex())
{
Change(ptChannel, stActiveTime);
return;
}
if (m_size + 1 >= m_capacity)
{
int newCapacity = (0 == m_size ? kMinHeapInitSize : (2 * m_size));
ChannelPtr * newData = static_cast<ChannelPtr *>(realloc(m_data, sizeof(ChannelPtr) * newCapacity));
// ignore this channel if realloc failed
if (nullptr == newData)
{
return;
}
// realloc does not need free
m_data = newData;
m_capacity = newCapacity;
}
++m_size;
new (&m_data[m_size]) ChannelPtr(nullptr);
m_data[m_size] = ptChannel;
ptChannel->SetLastActiveTime(stActiveTime);
ptChannel->SetMinHeapIndex(m_size);
ShiftUp(m_data[m_size]);
}
void MinHeap::Pop()
{
m_data[m_size]->SetMinHeapIndex(1);
m_data[1]->SetMinHeapIndex(-1);
m_data[1] = m_data[m_size];
--m_size;
ShiftDown(m_data[1]);
m_data[m_size + 1] = nullptr;
}
MinHeap::ChannelPtr MinHeap::Top()
{
return m_data[1];
}
void MinHeap::Change(ChannelPtr ptChannel, const struct timeval & stActiveTime)
{
if (-1 == ptChannel->GetMinHeapIndex())
{
Push(ptChannel, stActiveTime);
return;
}
const struct timeval oldTimeval = ptChannel->GetLastActiveTime();
ptChannel->SetLastActiveTime(stActiveTime);
if (TimevalGreater(oldTimeval, stActiveTime))
{
ShiftUp(ptChannel);
}
else
{
ShiftDown(ptChannel);
}
}
void MinHeap::Delete(ChannelPtr ptChannel)
{
int index = ptChannel->GetMinHeapIndex();
// channel has been removed, ignore
if (-1 == index)
{
return;
}
else if (1 == index)
{
Pop();
}
else
{
ptChannel->SetMinHeapIndex(-1);
m_data[m_size]->SetMinHeapIndex(index);
m_data[index] = m_data[m_size];
--m_size;
ShiftDown(m_data[index]);
m_data[m_size + 1] = nullptr;
}
}
bool MinHeap::Empty()
{
return 0 == m_size;
}
void MinHeap::LevelPrint()
{
if (0 != m_size)
{
std::queue<int> que;
que.push(1);
while (!que.empty())
{
int front = que.front();
std::cout << m_data[front]->GetLastActiveTime().tv_sec << " ";
que.pop();
if (2 * front <= m_size)
{
que.push(2 * front);
if (2 * front + 1 <= m_size)
{
que.push(2 * front + 1);
}
}
}
std::cout << std::endl;
}
}