diff --git a/.common/rust/src/main.rs b/.common/rust/src/main.rs index 48c7902..439730a 100644 --- a/.common/rust/src/main.rs +++ b/.common/rust/src/main.rs @@ -2,6 +2,7 @@ // region ‹use› use self::aliases::*; +use self::data_structures::*; use self::input::*; use self::math::*; use self::output::*; @@ -44,6 +45,38 @@ mod aliases { pub type Q = VecDeque; } +mod data_structures { + use std::cmp::{Ord, Reverse}; + use std::collections::BinaryHeap; + + #[derive(Debug)] + pub struct MinHeap { + heap: BinaryHeap>, + } + + impl MinHeap { + pub fn new() -> MinHeap { + MinHeap { + heap: BinaryHeap::new(), + } + } + + pub fn push(&mut self, item: T) { + self.heap.push(Reverse(item)) + } + + pub fn pop(&mut self) -> Option { + self.heap.pop().map(|Reverse(x)| x) + } + } + + impl Default for MinHeap { + fn default() -> Self { + Self::new() + } + } +} + #[allow(dead_code)] mod input { use std::collections::VecDeque; @@ -76,7 +109,7 @@ mod input { front.parse::().ok().unwrap() } - pub fn next_vec(&mut self, n: usize) -> Vec { + pub fn collect, T: FromStr>(&mut self, n: usize) -> C { (0..n).map(|_| self.next()).collect() } }