chore(rs): adjust the skeleton

* add data structures as a module with min-heap
* switch ‹.next_vec› to ‹.collect› that also supports other collections
  than ‹Vec<T>›

Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2023-08-05 21:48:51 +02:00
parent d7af3b2543
commit efed3b5976
Signed by: mfocko
GPG key ID: 7C47D46246790496

View file

@ -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<T> = VecDeque<T>;
}
mod data_structures {
use std::cmp::{Ord, Reverse};
use std::collections::BinaryHeap;
#[derive(Debug)]
pub struct MinHeap<T> {
heap: BinaryHeap<Reverse<T>>,
}
impl<T: Ord> MinHeap<T> {
pub fn new() -> MinHeap<T> {
MinHeap {
heap: BinaryHeap::new(),
}
}
pub fn push(&mut self, item: T) {
self.heap.push(Reverse(item))
}
pub fn pop(&mut self) -> Option<T> {
self.heap.pop().map(|Reverse(x)| x)
}
}
impl<T: Ord> Default for MinHeap<T> {
fn default() -> Self {
Self::new()
}
}
}
#[allow(dead_code)]
mod input {
use std::collections::VecDeque;
@ -76,7 +109,7 @@ mod input {
front.parse::<T>().ok().unwrap()
}
pub fn next_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
pub fn collect<C: FromIterator<T>, T: FromStr>(&mut self, n: usize) -> C {
(0..n).map(|_| self.next()).collect()
}
}