Rename Type to Object; change Block to little-endian encoding.

This commit is contained in:
yukirij 2023-11-17 02:06:16 -08:00
parent 93f2746f05
commit 41331e8e2b
3 changed files with 48 additions and 52 deletions

View File

@ -6,7 +6,7 @@ use crate::{
type_key, type_innerkey, SparseHeader, type_key, type_innerkey, SparseHeader,
}, },
tag, tag,
Type, Object,
util::*, util::*,
}; };
use crate::kind; use crate::kind;
@ -179,25 +179,25 @@ pub fn decode_tag(data:&Vec<u8>, index:&mut usize) -> Result<usize,()>
else { Err(()) } else { Err(()) }
} }
pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Type,()> pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Object,()>
{ {
match unsafe {type_key(type_id)} { match unsafe {type_key(type_id)} {
tag::VARYING => { tag::VARYING => {
return match decode(data, index) { return match decode(data, index) {
Ok(value) => { Ok(value) => {
Ok(Type::Varying(Varying::with(value.get()))) Ok(Object::Varying(Varying::with(value.get())))
} }
Err(_) => Err(()), Err(_) => Err(()),
} }
} }
tag::BOOLEAN => { 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 => { tag::NATURAL => {
return Ok(Type::Natural(Natural::with(unpack_natural(data, index)))); return Ok(Object::Natural(Natural::with(unpack_natural(data, index))));
} }
tag::INTEGER => { tag::INTEGER => {
return Ok(Type::Integer(Integer::with(unpack_integer(data, index)))); return Ok(Object::Integer(Integer::with(unpack_integer(data, index))));
} }
tag::DECIMAL => { tag::DECIMAL => {
let mantissa = unsafe {type_innerkey(type_id)}; let mantissa = unsafe {type_innerkey(type_id)};
@ -205,7 +205,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
let fract = unpack_natural(data, index).reverse_bits() >> (u64::BITS as usize - mantissa); let fract = unpack_natural(data, index).reverse_bits() >> (u64::BITS as usize - mantissa);
let raw = (whole << mantissa) | fract as i64; 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 => { tag::SIGNIFICANT => {
return Err(()) return Err(())
@ -219,7 +219,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
*index += 1; *index += 1;
} }
} }
return Ok(Type::Block(Block::with(size, bytes))); return Ok(Object::Block(Block::with(size, bytes)));
} }
tag::SEQUENCE => { tag::SEQUENCE => {
let size = unpack_natural(data, index) as usize; let size = unpack_natural(data, index) as usize;
@ -230,7 +230,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
*index += 1; *index += 1;
} }
} }
return Ok(Type::Sequence(Sequence::with_raw(bytes))); return Ok(Object::Sequence(Sequence::with_raw(bytes)));
} }
tag::ARRAY => { tag::ARRAY => {
let length = unsafe {type_innerkey(type_id)}; let length = unsafe {type_innerkey(type_id)};
@ -248,7 +248,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
} }
} }
return Ok(Type::Array(result)); return Ok(Object::Array(result));
} }
tag::LIST => { tag::LIST => {
let inner = unsafe {type_inner(type_id)}; let inner = unsafe {type_inner(type_id)};
@ -268,7 +268,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
} }
} }
return Ok(Type::List(result)); return Ok(Object::List(result));
} }
tag::SPARSE => { tag::SPARSE => {
let inner = unsafe {type_inner(type_id)}; let inner = unsafe {type_inner(type_id)};
@ -300,7 +300,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
} }
} }
return Ok(Type::Sparse(result)); return Ok(Object::Sparse(result));
} }
tag::RECORD => { tag::RECORD => {
return match Record::new(unsafe {type_innerkey(type_id)}) { return match Record::new(unsafe {type_innerkey(type_id)}) {
@ -313,7 +313,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
Err(_) => return Err(()) Err(_) => return Err(())
} }
} }
Ok(Type::Record(value)) Ok(Object::Record(value))
} }
Err(_) => Err(()), Err(_) => Err(()),
} }
@ -346,7 +346,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
result.assign(&key, class); result.assign(&key, class);
} }
return Ok(Type::Schema(result)); return Ok(Object::Schema(result));
} }
_ => { } _ => { }
} }
@ -354,7 +354,7 @@ pub fn decode_data(data:&Vec<u8>, type_id:usize, index:&mut usize) -> Result<Typ
return Err(()); return Err(());
} }
pub fn decode(data:&Vec<u8>, index:&mut usize) -> Result<Type,()> pub fn decode(data:&Vec<u8>, index:&mut usize) -> Result<Object,()>
{ {
return match decode_tag(data, index) { return match decode_tag(data, index) {
Ok(type_id) => decode_data(data, type_id, index), Ok(type_id) => decode_data(data, type_id, index),

View File

@ -18,7 +18,7 @@ mod sparse; pub use sparse::Sparse;
mod schema; pub use schema::Schema; mod schema; pub use schema::Schema;
mod record; pub use record::Record; mod record; pub use record::Record;
pub enum Type { pub enum Object {
Null, Null,
Varying(Varying), Varying(Varying),
Boolean(Boolean), Boolean(Boolean),
@ -34,42 +34,42 @@ pub enum Type {
Record(Record), Record(Record),
Schema(Schema), Schema(Schema),
} }
impl Type { impl Object {
pub fn from(addr:Reference) -> Self pub fn from(addr:Reference) -> Self
{ {
match unsafe {type_key(addr.class)} { match unsafe {type_key(addr.class)} {
tag::NULL => Type::Null, tag::NULL => Object::Null,
tag::VARYING => Type::Varying(Varying::try_from(addr).unwrap()), tag::VARYING => Object::Varying(Varying::try_from(addr).unwrap()),
tag::BOOLEAN => Type::Boolean(Boolean::try_from(addr).unwrap()), tag::BOOLEAN => Object::Boolean(Boolean::try_from(addr).unwrap()),
tag::NATURAL => Type::Natural(Natural::try_from(addr).unwrap()), tag::NATURAL => Object::Natural(Natural::try_from(addr).unwrap()),
tag::INTEGER => Type::Integer(Integer::try_from(addr).unwrap()), tag::INTEGER => Object::Integer(Integer::try_from(addr).unwrap()),
tag::DECIMAL => Type::Decimal(Decimal::try_from(addr).unwrap()), tag::DECIMAL => Object::Decimal(Decimal::try_from(addr).unwrap()),
tag::SIGNIFICANT => Type::Significant(Significant::try_from(addr).unwrap()), tag::SIGNIFICANT => Object::Significant(Significant::try_from(addr).unwrap()),
tag::BLOCK => Type::Block(Block::try_from(addr).unwrap()), tag::BLOCK => Object::Block(Block::try_from(addr).unwrap()),
tag::SEQUENCE => Type::Sequence(Sequence::try_from(addr).unwrap()), tag::SEQUENCE => Object::Sequence(Sequence::try_from(addr).unwrap()),
tag::ARRAY => Type::Array(Array::try_from(addr).unwrap()), tag::ARRAY => Object::Array(Array::try_from(addr).unwrap()),
tag::LIST => Type::List(List::try_from(addr).unwrap()), tag::LIST => Object::List(List::try_from(addr).unwrap()),
tag::SPARSE => Type::Sparse(Sparse::try_from(addr).unwrap()), tag::SPARSE => Object::Sparse(Sparse::try_from(addr).unwrap()),
tag::RECORD => Type::Record(Record::try_from(addr).unwrap()), tag::RECORD => Object::Record(Record::try_from(addr).unwrap()),
_ => Type::Null, _ => Object::Null,
} }
} }
pub fn get(&self) -> Reference pub fn get(&self) -> Reference
{ {
match &self { match &self {
Type::Varying(obj) => **obj, Object::Varying(obj) => **obj,
Type::Boolean(obj) => **obj, Object::Boolean(obj) => **obj,
Type::Natural(obj) => **obj, Object::Natural(obj) => **obj,
Type::Integer(obj) => **obj, Object::Integer(obj) => **obj,
Type::Decimal(obj) => **obj, Object::Decimal(obj) => **obj,
Type::Significant(obj) => **obj, Object::Significant(obj) => **obj,
Type::Block(obj) => **obj, Object::Block(obj) => **obj,
Type::Sequence(obj) => **obj, Object::Sequence(obj) => **obj,
Type::Array(obj) => **obj, Object::Array(obj) => **obj,
Type::List(obj) => **obj, Object::List(obj) => **obj,
Type::Sparse(obj) => **obj, Object::Sparse(obj) => **obj,
Type::Record(obj) => **obj, Object::Record(obj) => **obj,
_ => Reference::null(), _ => Reference::null(),
} }
} }

View File

@ -556,8 +556,7 @@ extern "C" uint64_t block_get_natural(Reference addr)
uint64_t result = 0; uint64_t result = 0;
for(size_t i = 0; i < length; ++i) { for(size_t i = 0; i < length; ++i) {
result <<= 8; result |= data[i] << (8 * i);
result |= data[i];
} }
return result; return result;
@ -570,8 +569,7 @@ extern "C" int64_t block_get_integer(Reference addr)
int64_t result = 0; int64_t result = 0;
for(size_t i = 0; i < length; ++i) { for(size_t i = 0; i < length; ++i) {
result <<= 8; result |= data[i] << (8 * i);
result |= data[i];
} }
return result; return result;
@ -616,9 +614,8 @@ extern "C" void block_set_natural(Reference addr, uint64_t data)
size_t length = type_innerkey(addr.type); size_t length = type_innerkey(addr.type);
uint8_t* dest = reinterpret_cast<uint8_t*>(addr.address); uint8_t* dest = reinterpret_cast<uint8_t*>(addr.address);
for(size_t i = length; i > 0; --i) { for(size_t i = 0; i < length; ++i) {
dest[i - 1] = data & 0xFF; dest[i] = (data >> (i * 8)) & 0xFF;
data >>= 8;
} }
} }
@ -628,8 +625,7 @@ extern "C" void block_set_integer(Reference addr, int64_t data)
uint8_t* dest = reinterpret_cast<uint8_t*>(addr.address); uint8_t* dest = reinterpret_cast<uint8_t*>(addr.address);
for(size_t i = length; i > 0; --i) { for(size_t i = length; i > 0; --i) {
dest[i - 1] = data & 0xFF; dest[i] = (data >> (i * 8)) & 0xFF;
data >>= 8;
} }
} }