From 469afb228b83877b6c0823ac25cb03e55af5de6c Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Thu, 17 Mar 2016 22:18:25 +0000 Subject: [PATCH] Implementation of components for the entity component system --- protocol/src/types/bit/set.rs | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/protocol/src/types/bit/set.rs b/protocol/src/types/bit/set.rs index a650570..23ef4e8 100644 --- a/protocol/src/types/bit/set.rs +++ b/protocol/src/types/bit/set.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +#[derive(Clone)] pub struct Set { data: Vec, } @@ -33,11 +34,11 @@ fn test_set() { impl Set { pub fn new(size: usize) -> Set { - let mut set = Set { data: Vec::with_capacity(size) }; - for _ in 0..size { - set.data.push(0) - } - set + Set { data: vec![0; (size + 63) / 64] } + } + + pub fn resize(&mut self, new_size: usize) { + self.data.resize((new_size + 63) / 64, 0); } pub fn set(&mut self, i: usize, v: bool) { @@ -48,7 +49,17 @@ impl Set { } } - pub fn get(&mut self, i: usize) -> bool { + pub fn get(&self, i: usize) -> bool { (self.data[i >> 6] & (1 << (i & 0x3F))) != 0 } + + pub fn includes_set(&self, other: &Set) -> bool { + debug_assert!(self.data.len() == other.data.len()); + for (a, b) in self.data.iter().zip(&other.data) { + if a & b != *b { + return false; + } + } + true + } }