From 41331e8e2b7a6d0526c63eaebf14e82defd37502 Mon Sep 17 00:00:00 2001 From: yukirij Date: Fri, 17 Nov 2023 02:06:16 -0800 Subject: [PATCH] Rename Type to Object; change Block to little-endian encoding. --- src/encoding/mod.rs | 30 ++++++++++++------------ src/interface/mod.rs | 56 ++++++++++++++++++++++---------------------- src/runtime/lib.cc | 14 ++++------- 3 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/encoding/mod.rs b/src/encoding/mod.rs index 2449a7c..3bf0ba6 100644 --- a/src/encoding/mod.rs +++ b/src/encoding/mod.rs @@ -6,7 +6,7 @@ use crate::{ type_key, type_innerkey, SparseHeader, }, tag, - Type, + Object, util::*, }; use crate::kind; @@ -179,25 +179,25 @@ pub fn decode_tag(data:&Vec, index:&mut usize) -> Result else { Err(()) } } -pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result +pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { match unsafe {type_key(type_id)} { tag::VARYING => { return match decode(data, index) { Ok(value) => { - Ok(Type::Varying(Varying::with(value.get()))) + Ok(Object::Varying(Varying::with(value.get()))) } Err(_) => Err(()), } } tag::BOOLEAN => { - return Ok(Type::Boolean(Boolean::with(unpack_natural(data, index) == 1))); + return Ok(Object::Boolean(Boolean::with(unpack_natural(data, index) == 1))); } tag::NATURAL => { - return Ok(Type::Natural(Natural::with(unpack_natural(data, index)))); + return Ok(Object::Natural(Natural::with(unpack_natural(data, index)))); } tag::INTEGER => { - return Ok(Type::Integer(Integer::with(unpack_integer(data, index)))); + return Ok(Object::Integer(Integer::with(unpack_integer(data, index)))); } tag::DECIMAL => { let mantissa = unsafe {type_innerkey(type_id)}; @@ -205,7 +205,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result> (u64::BITS as usize - mantissa); let raw = (whole << mantissa) | fract as i64; - return Ok(Type::Decimal(Decimal::with_raw(unsafe {type_innerkey(type_id)}, raw))); + return Ok(Object::Decimal(Decimal::with_raw(unsafe {type_innerkey(type_id)}, raw))); } tag::SIGNIFICANT => { return Err(()) @@ -219,7 +219,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { let size = unpack_natural(data, index) as usize; @@ -230,7 +230,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { let length = unsafe {type_innerkey(type_id)}; @@ -248,7 +248,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { let inner = unsafe {type_inner(type_id)}; @@ -268,7 +268,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { let inner = unsafe {type_inner(type_id)}; @@ -300,7 +300,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { return match Record::new(unsafe {type_innerkey(type_id)}) { @@ -313,7 +313,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result return Err(()) } } - Ok(Type::Record(value)) + Ok(Object::Record(value)) } Err(_) => Err(()), } @@ -346,7 +346,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result { } } @@ -354,7 +354,7 @@ pub fn decode_data(data:&Vec, type_id:usize, index:&mut usize) -> Result, index:&mut usize) -> Result +pub fn decode(data:&Vec, index:&mut usize) -> Result { return match decode_tag(data, index) { Ok(type_id) => decode_data(data, type_id, index), diff --git a/src/interface/mod.rs b/src/interface/mod.rs index aebd74d..75e97e9 100644 --- a/src/interface/mod.rs +++ b/src/interface/mod.rs @@ -18,7 +18,7 @@ mod sparse; pub use sparse::Sparse; mod schema; pub use schema::Schema; mod record; pub use record::Record; -pub enum Type { +pub enum Object { Null, Varying(Varying), Boolean(Boolean), @@ -34,42 +34,42 @@ pub enum Type { Record(Record), Schema(Schema), } -impl Type { +impl Object { pub fn from(addr:Reference) -> Self { match unsafe {type_key(addr.class)} { - tag::NULL => Type::Null, - tag::VARYING => Type::Varying(Varying::try_from(addr).unwrap()), - tag::BOOLEAN => Type::Boolean(Boolean::try_from(addr).unwrap()), - tag::NATURAL => Type::Natural(Natural::try_from(addr).unwrap()), - tag::INTEGER => Type::Integer(Integer::try_from(addr).unwrap()), - tag::DECIMAL => Type::Decimal(Decimal::try_from(addr).unwrap()), - tag::SIGNIFICANT => Type::Significant(Significant::try_from(addr).unwrap()), - tag::BLOCK => Type::Block(Block::try_from(addr).unwrap()), - tag::SEQUENCE => Type::Sequence(Sequence::try_from(addr).unwrap()), - tag::ARRAY => Type::Array(Array::try_from(addr).unwrap()), - tag::LIST => Type::List(List::try_from(addr).unwrap()), - tag::SPARSE => Type::Sparse(Sparse::try_from(addr).unwrap()), - tag::RECORD => Type::Record(Record::try_from(addr).unwrap()), - _ => Type::Null, + tag::NULL => Object::Null, + tag::VARYING => Object::Varying(Varying::try_from(addr).unwrap()), + tag::BOOLEAN => Object::Boolean(Boolean::try_from(addr).unwrap()), + tag::NATURAL => Object::Natural(Natural::try_from(addr).unwrap()), + tag::INTEGER => Object::Integer(Integer::try_from(addr).unwrap()), + tag::DECIMAL => Object::Decimal(Decimal::try_from(addr).unwrap()), + tag::SIGNIFICANT => Object::Significant(Significant::try_from(addr).unwrap()), + tag::BLOCK => Object::Block(Block::try_from(addr).unwrap()), + tag::SEQUENCE => Object::Sequence(Sequence::try_from(addr).unwrap()), + tag::ARRAY => Object::Array(Array::try_from(addr).unwrap()), + tag::LIST => Object::List(List::try_from(addr).unwrap()), + tag::SPARSE => Object::Sparse(Sparse::try_from(addr).unwrap()), + tag::RECORD => Object::Record(Record::try_from(addr).unwrap()), + _ => Object::Null, } } pub fn get(&self) -> Reference { match &self { - Type::Varying(obj) => **obj, - Type::Boolean(obj) => **obj, - Type::Natural(obj) => **obj, - Type::Integer(obj) => **obj, - Type::Decimal(obj) => **obj, - Type::Significant(obj) => **obj, - Type::Block(obj) => **obj, - Type::Sequence(obj) => **obj, - Type::Array(obj) => **obj, - Type::List(obj) => **obj, - Type::Sparse(obj) => **obj, - Type::Record(obj) => **obj, + Object::Varying(obj) => **obj, + Object::Boolean(obj) => **obj, + Object::Natural(obj) => **obj, + Object::Integer(obj) => **obj, + Object::Decimal(obj) => **obj, + Object::Significant(obj) => **obj, + Object::Block(obj) => **obj, + Object::Sequence(obj) => **obj, + Object::Array(obj) => **obj, + Object::List(obj) => **obj, + Object::Sparse(obj) => **obj, + Object::Record(obj) => **obj, _ => Reference::null(), } } diff --git a/src/runtime/lib.cc b/src/runtime/lib.cc index 8b96450..0e938d4 100644 --- a/src/runtime/lib.cc +++ b/src/runtime/lib.cc @@ -556,8 +556,7 @@ extern "C" uint64_t block_get_natural(Reference addr) uint64_t result = 0; for(size_t i = 0; i < length; ++i) { - result <<= 8; - result |= data[i]; + result |= data[i] << (8 * i); } return result; @@ -570,8 +569,7 @@ extern "C" int64_t block_get_integer(Reference addr) int64_t result = 0; for(size_t i = 0; i < length; ++i) { - result <<= 8; - result |= data[i]; + result |= data[i] << (8 * i); } return result; @@ -616,9 +614,8 @@ extern "C" void block_set_natural(Reference addr, uint64_t data) size_t length = type_innerkey(addr.type); uint8_t* dest = reinterpret_cast(addr.address); - for(size_t i = length; i > 0; --i) { - dest[i - 1] = data & 0xFF; - data >>= 8; + for(size_t i = 0; i < length; ++i) { + dest[i] = (data >> (i * 8)) & 0xFF; } } @@ -628,8 +625,7 @@ extern "C" void block_set_integer(Reference addr, int64_t data) uint8_t* dest = reinterpret_cast(addr.address); for(size_t i = length; i > 0; --i) { - dest[i - 1] = data & 0xFF; - data >>= 8; + dest[i] = (data >> (i * 8)) & 0xFF; } }