diff --git a/src/bin/main.rs b/src/bin/main.rs index d7f518f..8aa7997 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,41 +1 @@ -fn print_bytes(bytes:&Vec) -{ - 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"); } - } -} +fn main() { } diff --git a/src/interface/decimal.rs b/src/interface/decimal.rs index 4e9ac0f..e856274 100644 --- a/src/interface/decimal.rs +++ b/src/interface/decimal.rs @@ -76,9 +76,8 @@ impl Decimal { pub fn to_base(&self, mantissa:usize) -> Self { - let from_exp = self.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; } diff --git a/src/interface/sparse.rs b/src/interface/sparse.rs index cf1d18e..b03b113 100644 --- a/src/interface/sparse.rs +++ b/src/interface/sparse.rs @@ -3,6 +3,7 @@ use crate::runtime::{ acquire, release, type_inner, type_key, sparse_length, + sparse_first, sparse_next, sparse_last, sparse_at, sparse_get, sparse_clear, sparse_set, sparse_unset, @@ -38,6 +39,21 @@ impl Sparse { 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 { unsafe {sparse_at(self.addr, index)} diff --git a/src/runtime/lib.cc b/src/runtime/lib.cc index 21cb391..619e7b8 100644 --- a/src/runtime/lib.cc +++ b/src/runtime/lib.cc @@ -824,6 +824,45 @@ extern "C" void sparse_clear(Reference addr) rawlist_clear(list.header); } +extern "C" size_t sparse_first(Reference addr) +{ + auto& list = *reinterpret_cast(addr.address); + + if(list.header.length > 0) { + auto cell = reinterpret_cast(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(addr.address); + + if(list.header.length > 0) { + auto cell = reinterpret_cast(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(addr.address); + + if(list.header.length > 0) { + auto cell = reinterpret_cast(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) { Reference result {0}; diff --git a/src/runtime/lib.h b/src/runtime/lib.h index ce9f439..0289352 100644 --- a/src/runtime/lib.h +++ b/src/runtime/lib.h @@ -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" size_t sparse_length(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_get(Reference addr, size_t index); extern "C" void sparse_set(Reference addr, size_t index, Reference source); diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 4b2264d..0114152 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -121,6 +121,9 @@ extern "C" { pub fn sparse_header_data(addr:Reference, index:usize) -> SparseHeader; pub fn sparse_length(addr:Reference) -> usize; 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_at(addr:Reference, index:usize) -> Reference; pub fn sparse_set(addr:Reference, index:usize, source:Reference);