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
pub mod iter;
pub mod stable;
pub mod unique;
pub use stable::{max::StablePrioContainerMax, StablePrioContainer};
pub use unique::{
max::UniquePrioContainerMax, stable::StableUniquePrioContainer,
stable_max::StableUniquePrioContainerMax, UniquePrioContainer,
};
use iter::SortedHeapIter;
use std::{cmp::Reverse, collections::BinaryHeap};
pub struct PrioContainerMax<T> {
container: PrioContainer<Reverse<T>>,
}
impl<T: Ord> PrioContainerMax<T> {
#[inline]
pub fn new(capacity: usize) -> Self {
let container = PrioContainer::new(capacity);
Self { container }
}
#[inline]
pub fn new_allocated(capacity: usize) -> Self {
let container = PrioContainer::new_allocated(capacity);
Self { container }
}
#[inline]
pub fn insert(&mut self, item: T) -> bool {
self.container.insert(Reverse(item))
}
#[inline]
pub fn len(&self) -> usize {
self.container.len()
}
#[inline]
pub fn capacity(&self) -> usize {
self.container.capacity()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.container.is_empty()
}
#[inline]
pub fn total_pushed(&self) -> usize {
self.container.total_pushed()
}
}
impl<T: Ord> Extend<T> for PrioContainerMax<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for i in iter.into_iter() {
self.insert(i);
}
}
}
impl<T: Ord> IntoIterator for PrioContainerMax<T> {
type Item = Reverse<T>;
type IntoIter = SortedHeapIter<Reverse<T>>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
SortedHeapIter::new(self.container.heap)
}
}
pub struct PrioContainer<T> {
heap: BinaryHeap<T>,
capacity: usize,
pushed: usize,
}
impl<T: Ord> PrioContainer<T> {
#[inline]
pub fn new(capacity: usize) -> Self {
if capacity == 0 {
panic!("Capacity can't be zero");
}
let heap = BinaryHeap::new();
Self {
heap,
capacity,
pushed: 0,
}
}
#[inline]
pub fn new_allocated(capacity: usize) -> Self {
let mut queue = Self::new(capacity);
queue.heap.reserve(capacity);
queue
}
#[inline]
pub fn insert(&mut self, item: T) -> bool {
self.pushed += 1;
if self.heap.len() < self.capacity {
self.heap.push(item);
return true;
}
let mut max_item = unsafe { self.heap.peek_mut().unwrap_unchecked() };
if *max_item <= item {
return false;
}
*max_item = item;
true
}
#[inline]
pub fn len(&self) -> usize {
self.heap.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.len() == 0
}
#[inline]
pub fn capacity(&self) -> usize {
self.capacity
}
#[inline]
pub fn total_pushed(&self) -> usize {
self.pushed
}
#[inline]
pub fn into_sorted_vec(self) -> Vec<T> {
self.heap.into_sorted_vec()
}
}
impl<T: Ord> Extend<T> for PrioContainer<T> {
#[inline]
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I) {
for i in iter.into_iter() {
self.insert(i);
}
}
}
impl<T: Ord> IntoIterator for PrioContainer<T> {
type Item = T;
type IntoIter = SortedHeapIter<T>;
#[inline]
fn into_iter(self) -> Self::IntoIter {
SortedHeapIter::new(self.heap)
}
}