-
Notifications
You must be signed in to change notification settings - Fork 76
Add incremental support for malachite #714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| use crate::error::{EvalErr, Result}; | ||
| use crate::number::{Number, number_from_u8}; | ||
| use crate::number::{Malachite, Number, malachite_number_from_u8, number_from_u8}; | ||
| use chia_bls::{G1Element, G2Element}; | ||
| use num_traits::ToPrimitive; | ||
| use std::borrow::Borrow; | ||
| use std::fmt; | ||
| use std::hash::Hash; | ||
|
|
@@ -472,22 +473,47 @@ impl Allocator { | |
| } | ||
|
|
||
| pub fn new_number(&mut self, v: Number) -> Result<NodePtr> { | ||
| use num_traits::ToPrimitive; | ||
| if let Some(val) = v.to_u32() | ||
| if let Some(ptr) = self.new_small_number_from_bigint(&v)? { | ||
| return Ok(ptr); | ||
| } | ||
|
|
||
| let bytes = v.to_signed_bytes_be(); | ||
|
|
||
| self.new_atom_from_bigint_bytes(bytes.as_slice()) | ||
| } | ||
|
|
||
| pub fn new_malachite_number(&mut self, v: Malachite) -> Result<NodePtr> { | ||
| if let Some(ptr) = self.new_small_number_from_bigint(&v)? { | ||
| return Ok(ptr); | ||
| } | ||
|
|
||
| let bytes = v.to_signed_bytes_be(); | ||
|
|
||
| self.new_atom_from_bigint_bytes(bytes.as_slice()) | ||
| } | ||
|
|
||
| fn new_small_number_from_bigint( | ||
| &mut self, | ||
| value: &impl ToPrimitive, | ||
| ) -> Result<Option<NodePtr>> { | ||
| if let Some(val) = value.to_u32() | ||
| && val <= NODE_PTR_IDX_MASK | ||
| { | ||
| return self.new_small_number(val); | ||
| return Ok(Some(self.new_small_number(val)?)); | ||
| } | ||
| let bytes: Vec<u8> = v.to_signed_bytes_be(); | ||
| let mut slice = bytes.as_slice(); | ||
|
|
||
| Ok(None) | ||
| } | ||
|
|
||
| fn new_atom_from_bigint_bytes(&mut self, mut slice: &[u8]) -> Result<NodePtr> { | ||
| // make number minimal by removing leading zeros | ||
| while (!slice.is_empty()) && (slice[0] == 0) { | ||
| if slice.len() > 1 && (slice[1] & 0x80 == 0x80) { | ||
| break; | ||
| } | ||
| slice = &slice[1..]; | ||
| } | ||
|
|
||
| self.new_atom(slice) | ||
| } | ||
|
|
||
|
|
@@ -831,6 +857,24 @@ impl Allocator { | |
| } | ||
| } | ||
|
|
||
| pub fn malachite_number(&self, node: NodePtr) -> Malachite { | ||
| #[cfg(feature = "allocator-debug")] | ||
| self.validate_node(node); | ||
|
|
||
| let index = node.index(); | ||
|
|
||
| match node.object_type() { | ||
| ObjectType::Bytes => { | ||
| let atom = self.atom_vec[index as usize]; | ||
| malachite_number_from_u8(&self.u8_vec[atom.start as usize..atom.end as usize]) | ||
| } | ||
| ObjectType::SmallAtom => Malachite::from(index), | ||
| _ => { | ||
| panic!("number() called on pair"); | ||
| } | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unused public
|
||
|
|
||
| pub fn g1(&self, node: NodePtr) -> Result<G1Element> { | ||
| #[cfg(feature = "allocator-debug")] | ||
| self.validate_node(node); | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated
new_numberandnew_malachite_numberimplementationsLow Severity
new_malachite_numberis an exact copy ofnew_numberwith only the parameter type changed. Similarly,malachite_number_from_u8duplicatesnumber_from_u8, andmalachite_numberduplicatesnumber. BothNumberandMalachitealready shareToPrimitive(used innew_small_number_from_bigint); a similar trait-based approach forto_signed_bytes_be/from_signed_bytes_becould unify these three pairs of functions.Additional Locations (2)
src/allocator.rs#L474-L483src/number.rs#L17-L25