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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use core::cmp::Ordering;
use core::mem::MaybeUninit;
use core::ptr;
#[derive(Copy, Clone, Debug)]
#[repr(C)]
pub struct Handle {
index: u32,
unique: u32,
}
pub struct BinaryHeap<T: Ord> {
data: Vec<MaybeUninit<T>>,
heap_index: Vec<Handle>,
heap: Vec<u32>,
heap_len: u32,
}
impl<T: Ord> BinaryHeap<T> {
pub fn new() -> BinaryHeap<T> {
let mut b = BinaryHeap {
data: Vec::new(),
heap_index: Vec::new(),
heap: Vec::with_capacity(1),
heap_len: 0,
};
b.heap.push(0xFFFFFFFF);
return b;
}
pub fn with_capacity(capacity: u32) -> BinaryHeap<T> {
let mut b = BinaryHeap {
data: Vec::with_capacity(capacity as usize),
heap_index: Vec::with_capacity(capacity as usize),
heap: Vec::with_capacity(capacity as usize + 1),
heap_len: 0,
};
b.heap.push(0xFFFFFFFF);
return b;
}
pub fn capacity(&self) -> u32 {
return self.data.capacity() as u32;
}
pub fn len(&self) -> u32 {
return self.heap_len;
}
#[inline(always)]
fn get_parent_index(index: u32) -> u32 {
return index >> 1;
}
#[inline(always)]
fn get_right_index(index: u32) -> u32 {
return (index << 1) | 1;
}
#[inline(always)]
fn get_left_index(index: u32) -> u32 {
return index << 1;
}
#[inline(always)]
fn get_data(&self, index: u32) -> &T {
return unsafe { self.data[index as usize].as_ptr().as_ref().unwrap() };
}
pub fn clear(&mut self) {
self.data.clear();
self.heap_index.clear();
self.heap.clear();
self.heap.push(0xFFFFFFFF);
self.heap_len = 0;
}
fn sift_up(&mut self, mut index: u32) {
let data_index = self.heap[index as usize];
let data: &T = unsafe { self.data[data_index as usize].as_ptr().as_ref().unwrap() };
while index > 1 {
let parent_index = BinaryHeap::<T>::get_parent_index(index);
let parent_data_index = self.heap[parent_index as usize];
if data < self.get_data(parent_data_index) {
self.heap.swap(index as usize, parent_index as usize);
self.heap_index
.swap(data_index as usize, parent_data_index as usize);
index = parent_index;
} else {
return;
}
}
}
fn heapify(&mut self, index: u32) {
let mut best_index = BinaryHeap::<T>::get_left_index(index);
if best_index > self.heap_len {
return;
}
let mut best_data_index = self.heap[best_index as usize];
let mut best_data: &T = &self.get_data(best_data_index);
let right_index = BinaryHeap::<T>::get_right_index(index);
if right_index <= self.heap_len {
let right_data_index = self.heap[right_index as usize];
let right_data: &T = &self.get_data(right_data_index);
if right_data < best_data {
best_data = right_data;
best_index = right_index;
best_data_index = right_data_index
}
}
let data_index = self.heap[index as usize];
let data: &T = &self.get_data(data_index);
if best_data < data {
self.heap.swap(index as usize, best_index as usize);
self.heap_index
.swap(data_index as usize, best_data_index as usize);
}
self.heapify(best_index);
}
fn heap_remove(&mut self, index: u32) -> T {
let data_index = self.heap[index as usize];
let last_index = self.heap_len;
let last_data_index = self.heap[last_index as usize];
self.heap.swap(index as usize, last_index as usize);
self.heap_index
.swap(data_index as usize, last_data_index as usize);
self.heap_len -= 1;
self.heapify(1);
self.heap_index[data_index as usize].unique += 1;
return unsafe { ptr::read(self.data[data_index as usize].as_mut_ptr()) };
}
pub fn push(&mut self, item: T) -> Handle {
let prev_heap_len = self.data.len() as u32;
let handle: Handle;
if self.heap_len == prev_heap_len {
self.data.push(MaybeUninit::new(item));
self.heap_len += 1;
handle = Handle {
index: prev_heap_len,
unique: 0,
};
self.heap_index.push(Handle {
index: self.heap_len,
unique: 0,
});
self.heap.push(prev_heap_len);
} else {
self.heap_len += 1;
let data_index = self.heap[self.heap_len as usize];
self.heap_index[data_index as usize].index = self.heap_len;
handle = Handle {
index: data_index,
unique: self.heap_index[data_index as usize].unique,
};
self.data[data_index as usize] = MaybeUninit::new(item);
}
self.sift_up(self.heap_len);
return handle;
}
pub fn get(&self, handle: Handle) -> Option<&T> {
if handle.index >= self.heap_index.len() as u32 {
return None;
}
let h = self.heap_index[handle.index as usize];
if h.unique != handle.unique {
return None;
}
return unsafe { self.data[handle.index as usize].as_ptr().as_ref() };
}
pub fn replace(&mut self, handle: Handle, item: T) -> bool {
if handle.index >= self.heap_index.len() as u32 {
return false;
}
let h = self.heap_index[handle.index as usize];
if h.unique != handle.unique {
return false;
}
let ordering = item.cmp(self.get_data(handle.index));
self.data[handle.index as usize] = MaybeUninit::new(item);
match ordering {
Ordering::Less => self.sift_up(h.index),
Ordering::Greater => self.heapify(h.index),
Ordering::Equal => {}
}
return true;
}
pub fn pop(&mut self) -> Option<T> {
if self.heap_len == 0 {
return None;
}
return Some(self.heap_remove(1));
}
}
#[cfg(test)]
mod test;