From 63731ca4503c2d1e76efb81c8a8ee4a162c43ed0 Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Mon, 21 Sep 2015 13:08:06 +0100 Subject: [PATCH] Basic logo work --- protocol/src/types/bit/map.rs | 90 +++++++++++++++++++++++++++++++++++ protocol/src/types/bit/mod.rs | 19 ++++++++ protocol/src/types/bit/set.rs | 56 ++++++++++++++++++++++ protocol/src/types/mod.rs | 2 + 4 files changed, 167 insertions(+) create mode 100644 protocol/src/types/bit/map.rs create mode 100644 protocol/src/types/bit/mod.rs create mode 100644 protocol/src/types/bit/set.rs diff --git a/protocol/src/types/bit/map.rs b/protocol/src/types/bit/map.rs new file mode 100644 index 0000000..8d289c0 --- /dev/null +++ b/protocol/src/types/bit/map.rs @@ -0,0 +1,90 @@ +// Copyright 2015 Matthew Collins +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub struct Map { + bits: Vec, + bit_size: usize, + length: usize +} + +#[test] +fn test_map() { + let mut map = Map::new(4096, 4); + for i in 0 .. 4096 { + for j in 0 .. 16 { + map.set(i, j); + if map.get(i) != j { + panic!("Fail"); + } + } + } +} + +#[test] +fn test_map_odd() { + for size in 1 .. 16 { + let mut map = Map::new(64*3, size); + let max = (1 << size) - 1; + for i in 0 .. 64*3 { + for j in 0 .. max { + map.set(i, j); + if map.get(i) != j { + panic!("Index: {} wanted {} and got {}", i, j, map.get(i)); + } + } + } + } +} + +impl Map { + pub fn new(len: usize, size: usize) -> Map { + let mut map = Map { + bit_size: size, + length: len, + bits: Vec::with_capacity((len*size)/64) + }; + for _ in 0 .. len { + map.bits.push(0) + } + map + } + + pub fn set(&mut self, i: usize, val: usize) { + let i = i * self.bit_size; + let pos = i / 64; + let mask = (1 << self.bit_size) - 1; + let ii = i % 64; + self.bits[pos] = (self.bits[pos] & !(mask << ii )) | ((val << ii) as u64); + let pos2 = (i + self.bit_size - 1) / 64; + if pos2 != pos { + let used = 64 - ii; + let rem = self.bit_size - used; + self.bits[pos2] = self.bits[pos2] >> rem << rem | (val as u64 >> used); + } + } + + pub fn get(&mut self, i: usize) -> usize { + let i = i * self.bit_size; + let pos = i / 64; + let mask = (1 << self.bit_size) - 1; + let ii = i % 64; + let pos2 = (i + self.bit_size - 1) / 64; + if pos2 != pos { + let used = 64 - ii; + (((self.bits[pos] >> ii) | (self.bits[pos2] << used)) & mask) as usize + } else { + ((self.bits[pos] >> ii) & mask) as usize + } + } +} diff --git a/protocol/src/types/bit/mod.rs b/protocol/src/types/bit/mod.rs new file mode 100644 index 0000000..bc345c6 --- /dev/null +++ b/protocol/src/types/bit/mod.rs @@ -0,0 +1,19 @@ +// Copyright 2015 Matthew Collins +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub mod set; +pub mod map; + +pub use self::set::Set; +pub use self::map::Map; diff --git a/protocol/src/types/bit/set.rs b/protocol/src/types/bit/set.rs new file mode 100644 index 0000000..995ce69 --- /dev/null +++ b/protocol/src/types/bit/set.rs @@ -0,0 +1,56 @@ +// Copyright 2015 Matthew Collins +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub struct Set { + data : Vec +} + +#[test] +fn test_set() { + let mut set = Set::new(200); + for i in 0 .. 200 { + if i % 3 == 0 { + set.set(i, true) + } + } + for i in 0 .. 200 { + if set.get(i) != (i%3 == 0) { + panic!("Fail") + } + } +} + +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 + } + + pub fn set(&mut self, i: usize, v: bool) { + if v { + self.data[i>>6] |= 1 << (i & 0x3F) + } else { + self.data[i>>6] &= !(1 << (i & 0x3F)) + } + } + + pub fn get(&mut self, i: usize) -> bool { + return (self.data[i>>6] & (1 << (i & 0x3F))) != 0 + } +} diff --git a/protocol/src/types/mod.rs b/protocol/src/types/mod.rs index 681b872..034015f 100644 --- a/protocol/src/types/mod.rs +++ b/protocol/src/types/mod.rs @@ -17,3 +17,5 @@ pub use self::blockpos::*; mod metadata; pub use self::metadata::*; + +pub mod bit; \ No newline at end of file