aoc_framework_utils/iterators/
mod.rs

1use std::rc::Rc;
2
3/// A collection of views into a generic value that covers all possible continuos sizes
4pub trait GrowingWindow<T> {
5  /// Generates all views
6  fn growing_window(&self) -> Vec<Rc<&[T]>>;
7}
8
9impl <T> GrowingWindow<T> for Vec<T> {
10  fn growing_window(&self) -> Vec<Rc<&[T]>> {
11    let mut windows = vec![];
12    for i in 1..self.len() {
13      windows.push(Rc::new(&self[0..i]));
14    }
15    return windows
16  }
17}
18
19pub trait ListSelectors<T> {
20  fn first_that<F>(&self, predicate: F) -> Option<(&T, usize)> where F: Fn(&T) -> bool;
21  fn last_that<F>(&self, predicate: F) -> Option<(&T, usize)> where F: Fn(&T) -> bool;
22}
23
24impl <T> ListSelectors<T> for Vec<T> {
25  fn first_that<F>(&self, predicate: F) -> Option<(&T, usize)> where F: Fn(&T) -> bool {
26    for i in 0..self.len() {
27      if predicate(&self[i]) {
28        return Some((&self[i], i))
29      }
30    }
31    None
32  }
33
34  fn last_that<F>(&self, predicate: F) -> Option<(&T, usize)> where F: Fn(&T) -> bool {
35    for i in self.len()..0 {
36      if predicate(&self[i]) {
37        return Some((&self[i], i))
38      }
39    }
40    None
41  }
42}
43
44pub trait CharsToString {
45  fn to_string(&self) -> String;
46}
47
48impl CharsToString for [char] {
49  fn to_string(&self) -> String {
50    self.iter().collect()
51  }
52}