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,
},
tag,
Type,
Object,
util::*,
};
use crate::kind;
@ -179,25 +179,25 @@ pub fn decode_tag(data:&Vec<u8>, index:&mut usize) -> Result<usize,()>
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)} {
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<u8>, type_id:usize, index:&mut usize) -> Result<Typ
let fract = unpack_natural(data, index).reverse_bits() >> (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<u8>, type_id:usize, index:&mut usize) -> Result<Typ
*index += 1;
}
}
return Ok(Type::Block(Block::with(size, bytes)));
return Ok(Object::Block(Block::with(size, bytes)));
}
tag::SEQUENCE => {
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;
}
}
return Ok(Type::Sequence(Sequence::with_raw(bytes)));
return Ok(Object::Sequence(Sequence::with_raw(bytes)));
}
tag::ARRAY => {
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 => {
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 => {
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 => {
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(())
}
}
Ok(Type::Record(value))
Ok(Object::Record(value))
}
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);
}
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(());
}
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) {
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 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(),
}
}

View File

@ -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<uint8_t*>(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<uint8_t*>(addr.address);
for(size_t i = length; i > 0; --i) {
dest[i - 1] = data & 0xFF;
data >>= 8;
dest[i] = (data >> (i * 8)) & 0xFF;
}
}