arnaucube
arnaucube/testudoPublic Self-hosted repository
testudo/src/math.rs
src/math.rsRaw
 1pub trait Math {
 2  fn square_root(self) -> usize;
 3  fn pow2(self) -> usize;
 4  fn get_bits(self, num_bits: usize) -> Vec<bool>;
 5}
 6
 7impl Math for usize {
 8  #[inline]
 9  fn square_root(self) -> usize {
10    (self as f64).sqrt() as usize
11  }
12
13  #[inline]
14  fn pow2(self) -> usize {
15    let base: usize = 2;
16    base.pow(self as u32)
17  }
18
19  /// Returns the num_bits from n in a canonical order
20  fn get_bits(self, num_bits: usize) -> Vec<bool> {
21    (0..num_bits)
22      .map(|shift_amount| ((self & (1 << (num_bits - shift_amount - 1))) > 0))
23      .collect::<Vec<bool>>()
24  }
25}