Base implementation for worlds/blocks
This commit is contained in:
parent
eaea15e4a1
commit
ddf3a7981c
|
@ -0,0 +1,25 @@
|
||||||
|
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! create_ids {
|
||||||
|
($t:ty, ) => ();
|
||||||
|
($t:ty, prev($prev:ident), $name:ident) => (
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
pub const $name: $t = $prev + 1;
|
||||||
|
);
|
||||||
|
($t:ty, prev($prev:ident), $name:ident, $($n:ident),+) => (
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
pub const $name: $t = $prev + 1;
|
||||||
|
create_ids!($t, prev($name), $($n),+);
|
||||||
|
);
|
||||||
|
($t:ty, $name:ident, $($n:ident),+) => (
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
pub const $name: $t = 0;
|
||||||
|
create_ids!($t, prev($name), $($n),+);
|
||||||
|
);
|
||||||
|
($t:ty, $name:ident) => (
|
||||||
|
#[allow(non_upper_case_globals)]
|
||||||
|
pub const $name: $t = 0;
|
||||||
|
);
|
||||||
|
}
|
|
@ -14,7 +14,7 @@
|
||||||
|
|
||||||
pub struct Map {
|
pub struct Map {
|
||||||
bits: Vec<u64>,
|
bits: Vec<u64>,
|
||||||
bit_size: usize,
|
pub bit_size: usize,
|
||||||
length: usize,
|
length: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ impl Map {
|
||||||
map
|
map
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resize(self, size: usize) -> Map {
|
pub fn resize(&self, size: usize) -> Map {
|
||||||
let mut n = Map::new(self.length, size);
|
let mut n = Map::new(self.length, size);
|
||||||
for i in 0..self.length {
|
for i in 0..self.length {
|
||||||
n.set(i, self.get(i));
|
n.set(i, self.get(i));
|
||||||
|
|
|
@ -19,3 +19,4 @@ mod metadata;
|
||||||
pub use self::metadata::*;
|
pub use self::metadata::*;
|
||||||
|
|
||||||
pub mod bit;
|
pub mod bit;
|
||||||
|
pub mod nibble;
|
||||||
|
|
|
@ -0,0 +1,45 @@
|
||||||
|
// 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 Array {
|
||||||
|
data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Array {
|
||||||
|
pub fn new(size: usize) -> Array {
|
||||||
|
Array {
|
||||||
|
data: vec![0; (size + 1) >> 1],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, idx: usize) -> u8 {
|
||||||
|
let val = self.data[idx>>1];
|
||||||
|
if idx&1 == 0 {
|
||||||
|
val & 0xF
|
||||||
|
} else {
|
||||||
|
val >> 4
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set(&mut self, idx: usize, val: u8) {
|
||||||
|
let i = idx >> 1;
|
||||||
|
let old = self.data[i];
|
||||||
|
if idx&1 == 0 {
|
||||||
|
self.data[i] = (old & 0xF0) | (val & 0xF);
|
||||||
|
} else {
|
||||||
|
self.data[i] = (old & 0x0F) | ((val & 0xF) << 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue