Add methods to Sparse, fix bug in Decimal.

This commit is contained in:
yukirij 2023-09-04 23:33:34 -07:00
parent 8a833b41e0
commit c5429b7e21
6 changed files with 63 additions and 43 deletions

View File

@ -1,41 +1 @@
fn print_bytes(bytes:&Vec<u8>) fn main() { }
{
for b in bytes {
print!("{:02x} ", *b);
} print!("\n");
}
fn main() {
let a = szun::Decimal::with(32, 128.5);
let b = szun::Decimal::with(24, 16.0);
let c = a.copy() / b.copy();
println!("A = {} (.{})", a.get(), a.mantissa());
println!("B = {} (.{})", b.get(), b.mantissa());
print!("\n");
println!("A + B = {}", (a.copy() + b.copy()).get());
println!("A - B = {}", (a.copy() - b.copy()).get());
println!("A * B = {}", (a.copy() * b.copy()).get());
println!("A / B = {}", (a.copy() / b.copy()).get());
println!("A % B = {}", (a.copy() % b.copy()).get());
print!("\n");
println!("B + A = {}", (b.copy() + a.copy()).get());
println!("B - A = {}", (b.copy() - a.copy()).get());
println!("B * A = {}", (b.copy() * a.copy()).get());
println!("B / A = {}", (b.copy() / a.copy()).get());
println!("B % A = {}", (b.copy() % a.copy()).get());
print!("\n");
let out = szun::encode(*c);
println!("c = {}", c.get());
print_bytes(&out);
match szun::decode(&out, &mut 0) {
Ok(ty) => match ty {
szun::Type::Decimal(val) => {
println!("decode = {}", val.get());
}
_ => { println!("other"); }
}
Err(_) => { println!("failed"); }
}
}

View File

@ -76,9 +76,8 @@ impl Decimal {
pub fn to_base(&self, mantissa:usize) -> Self pub fn to_base(&self, mantissa:usize) -> Self
{ {
let from_exp = self.mantissa();
let mut result = Decimal::new(mantissa); let mut result = Decimal::new(mantissa);
result.set_raw(Self::rebase(from_exp, mantissa, self.get_raw())); result.set_raw(Self::rebase(mantissa, self.mantissa(), self.get_raw()));
return result; return result;
} }

View File

@ -3,6 +3,7 @@ use crate::runtime::{
acquire, release, acquire, release,
type_inner, type_key, type_inner, type_key,
sparse_length, sparse_length,
sparse_first, sparse_next, sparse_last,
sparse_at, sparse_get, sparse_at, sparse_get,
sparse_clear, sparse_clear,
sparse_set, sparse_unset, sparse_set, sparse_unset,
@ -38,6 +39,21 @@ impl Sparse {
unsafe {sparse_length(self.addr)} unsafe {sparse_length(self.addr)}
} }
pub fn first(&self) -> usize
{
unsafe {sparse_first(self.addr)}
}
pub fn next(&self) -> usize
{
unsafe {sparse_next(self.addr)}
}
pub fn last(&self) -> usize
{
unsafe {sparse_last(self.addr)}
}
pub fn at(&self, index:usize) -> Reference pub fn at(&self, index:usize) -> Reference
{ {
unsafe {sparse_at(self.addr, index)} unsafe {sparse_at(self.addr, index)}

View File

@ -824,6 +824,45 @@ extern "C" void sparse_clear(Reference addr)
rawlist_clear(list.header); rawlist_clear(list.header);
} }
extern "C" size_t sparse_first(Reference addr)
{
auto& list = *reinterpret_cast<Type::Sparse*>(addr.address);
if(list.header.length > 0) {
auto cell = reinterpret_cast<Type::SparseHeader*>(rawlist_cell(list.header, sizeof(Type::SparseHeader), 0));
if(cell != nullptr) {
return cell->start;
}
}
return 0;
}
extern "C" size_t sparse_next(Reference addr)
{
auto& list = *reinterpret_cast<Type::Sparse*>(addr.address);
if(list.header.length > 0) {
auto cell = reinterpret_cast<Type::SparseHeader*>(rawlist_cell(list.header, sizeof(Type::SparseHeader), 0));
if(cell != nullptr) {
return cell->start + cell->length;
}
}
return 0;
}
extern "C" size_t sparse_last(Reference addr)
{
auto& list = *reinterpret_cast<Type::Sparse*>(addr.address);
if(list.header.length > 0) {
auto cell = reinterpret_cast<Type::SparseHeader*>(rawlist_cell(list.header, sizeof(Type::SparseHeader), list.header.length - 1));
if(cell != nullptr) {
return cell->start + cell->length - 1;
}
}
return 0;
}
extern "C" Reference sparse_at(Reference addr, size_t index) extern "C" Reference sparse_at(Reference addr, size_t index)
{ {
Reference result {0}; Reference result {0};

View File

@ -107,6 +107,9 @@ extern "C" size_t sparse_header_length(Reference addr);
extern "C" Type::SparseHeader sparse_header_data(Reference addr, size_t index); extern "C" Type::SparseHeader sparse_header_data(Reference addr, size_t index);
extern "C" size_t sparse_length(Reference addr); extern "C" size_t sparse_length(Reference addr);
extern "C" void sparse_clear(Reference addr); extern "C" void sparse_clear(Reference addr);
extern "C" size_t sparse_first(Reference addr);
extern "C" size_t sparse_next(Reference addr);
extern "C" size_t sparse_last(Reference addr);
extern "C" Reference sparse_at(Reference addr, size_t index); extern "C" Reference sparse_at(Reference addr, size_t index);
extern "C" Reference sparse_get(Reference addr, size_t index); extern "C" Reference sparse_get(Reference addr, size_t index);
extern "C" void sparse_set(Reference addr, size_t index, Reference source); extern "C" void sparse_set(Reference addr, size_t index, Reference source);

View File

@ -121,6 +121,9 @@ extern "C" {
pub fn sparse_header_data(addr:Reference, index:usize) -> SparseHeader; pub fn sparse_header_data(addr:Reference, index:usize) -> SparseHeader;
pub fn sparse_length(addr:Reference) -> usize; pub fn sparse_length(addr:Reference) -> usize;
pub fn sparse_clear(addr:Reference); pub fn sparse_clear(addr:Reference);
pub fn sparse_first(addr:Reference) -> usize;
pub fn sparse_next(addr:Reference) -> usize;
pub fn sparse_last(addr:Reference) -> usize;
pub fn sparse_get(addr:Reference, index:usize) -> Reference; pub fn sparse_get(addr:Reference, index:usize) -> Reference;
pub fn sparse_at(addr:Reference, index:usize) -> Reference; pub fn sparse_at(addr:Reference, index:usize) -> Reference;
pub fn sparse_set(addr:Reference, index:usize, source:Reference); pub fn sparse_set(addr:Reference, index:usize, source:Reference);