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
use std::cmp; pub fn min_and_max<T, I>(e: I) -> Option<(T, Option<T>)> where T: cmp::PartialOrd, I: IntoIterator<Item=T>, { e.into_iter() .fold(None, |a, x| Some(match a { Some((min, Some(max))) => { if x < min { (x, Some(max)) } else if x > max { (min, Some(x)) } else { (min, Some(max)) } }, Some((both, None)) => { if x < both { (x, Some(both)) } else if x > both { (both, Some(x)) } else { (both, None) } }, None => { { (x, None) } }, })) } pub fn min_and_max_by_key<T, I, U, F>(e: I, k: F) -> Option<(T, Option<T>)> where I: IntoIterator<Item=T>, U: cmp::PartialOrd, F: Fn(&T) -> U, { e.into_iter() .fold(None, |a, x| Some(match a { Some((min, Some(max))) => { if k(&x) < k(&min) { (x, Some(max)) } else if k(&x) > k(&max) { (min, Some(x)) } else { (min, Some(max)) } }, Some((both, None)) => { if k(&x) < k(&both) { (x, Some(both)) } else if k(&x) > k(&both) { (both, Some(x)) } else { (both, None) } }, None => { { (x, None) } }, })) } #[derive(Copy, Clone, PartialEq, Eq, Debug, Hash)] pub struct Bits { v: usize, length: usize, i: usize, } impl Bits { pub fn new(v: usize, length: usize) -> Bits { Bits { v, length, i: 0 } } } impl Iterator for Bits { type Item = bool; fn next(&mut self) -> Option<Self::Item> { if self.i == self.length { return None; } let bit: usize = 1 << (self.length - self.i - 1); self.i += 1; return Some((self.v & bit) > 0) } }