|
@@ -1,13 +1,13 @@
|
|
//! Bits
|
|
//! Bits
|
|
-//!
|
|
|
|
|
|
+//!
|
|
//! Handles if a certain bit is set. Vec<bool>
|
|
//! Handles if a certain bit is set. Vec<bool>
|
|
|
|
|
|
-use std::ops::RangeBounds;
|
|
|
|
use std::fmt;
|
|
use std::fmt;
|
|
|
|
+use std::ops::RangeBounds;
|
|
// use std::iter::{Map, Filter, Enumerate};
|
|
// use std::iter::{Map, Filter, Enumerate};
|
|
|
|
|
|
/// Flags - track what is set
|
|
/// Flags - track what is set
|
|
-///
|
|
|
|
|
|
+///
|
|
/// A vector of bool with helper functions to track what is set (true) and not (false).
|
|
/// A vector of bool with helper functions to track what is set (true) and not (false).
|
|
#[derive(Clone)]
|
|
#[derive(Clone)]
|
|
pub struct Flags(pub Vec<bool>);
|
|
pub struct Flags(pub Vec<bool>);
|
|
@@ -22,7 +22,7 @@ impl Flags {
|
|
#[inline]
|
|
#[inline]
|
|
pub fn clear(&mut self) {
|
|
pub fn clear(&mut self) {
|
|
self.0.fill(false);
|
|
self.0.fill(false);
|
|
- /*
|
|
|
|
|
|
+ /*
|
|
for i in 0..self.0.len() {
|
|
for i in 0..self.0.len() {
|
|
self.0[i] = false;
|
|
self.0[i] = false;
|
|
}
|
|
}
|
|
@@ -101,14 +101,34 @@ impl Flags {
|
|
}
|
|
}
|
|
|
|
|
|
impl fmt::Debug for Flags {
|
|
impl fmt::Debug for Flags {
|
|
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
- write!(f, "({})", self.0.iter().enumerate().filter(|x| *x.1).map(|x| x.0.to_string()).collect::<Vec<_>>().join(","))
|
|
|
|
- }
|
|
|
|
|
|
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
+ write!(
|
|
|
|
+ f,
|
|
|
|
+ "({})",
|
|
|
|
+ self.0
|
|
|
|
+ .iter()
|
|
|
|
+ .enumerate()
|
|
|
|
+ .filter(|x| *x.1)
|
|
|
|
+ .map(|x| x.0.to_string())
|
|
|
|
+ .collect::<Vec<_>>()
|
|
|
|
+ .join(",")
|
|
|
|
+ )
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
impl fmt::Display for Flags {
|
|
impl fmt::Display for Flags {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
- write!(f, "({})", self.0.iter().enumerate().filter(|x| *x.1).map(|x| x.0.to_string()).collect::<Vec<_>>().join(","))
|
|
|
|
|
|
+ write!(
|
|
|
|
+ f,
|
|
|
|
+ "({})",
|
|
|
|
+ self.0
|
|
|
|
+ .iter()
|
|
|
|
+ .enumerate()
|
|
|
|
+ .filter(|x| *x.1)
|
|
|
|
+ .map(|x| x.0.to_string())
|
|
|
|
+ .collect::<Vec<_>>()
|
|
|
|
+ .join(",")
|
|
|
|
+ )
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -133,13 +153,13 @@ use std::ops::{Index, IndexMut};
|
|
impl Index<usize> for Flags {
|
|
impl Index<usize> for Flags {
|
|
type Output = bool;
|
|
type Output = bool;
|
|
|
|
|
|
- fn index<'a>(&'a self, i:usize) -> &'a bool {
|
|
|
|
|
|
+ fn index<'a>(&'a self, i: usize) -> &'a bool {
|
|
&self.0[i]
|
|
&self.0[i]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
impl IndexMut<usize> for Flags {
|
|
impl IndexMut<usize> for Flags {
|
|
- fn index_mut<'a>(&'a mut self, i:usize) -> &'a mut bool {
|
|
|
|
|
|
+ fn index_mut<'a>(&'a mut self, i: usize) -> &'a mut bool {
|
|
&mut self.0[i]
|
|
&mut self.0[i]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
@@ -169,7 +189,7 @@ mod tests {
|
|
for i in 0..9 {
|
|
for i in 0..9 {
|
|
let mut result = p[i];
|
|
let mut result = p[i];
|
|
assert_eq!(result, false);
|
|
assert_eq!(result, false);
|
|
- p[i] =true;
|
|
|
|
|
|
+ p[i] = true;
|
|
result = p[i];
|
|
result = p[i];
|
|
assert_eq!(result, true);
|
|
assert_eq!(result, true);
|
|
}
|
|
}
|