pub enum NumberValue {
Integer(i64),
Float(f64),
}Expand description
Specialized representation for numeric values to optimize memory usage.
This enum provides different representations for integers and floating-point values, allowing for more efficient memory usage and operations.
Variants§
Implementations§
Source§impl NumberValue
impl NumberValue
Sourcepub fn from_i64(value: i64) -> Self
pub fn from_i64(value: i64) -> Self
Creates a new NumberValue from an i64.
Examples found in repository?
examples/custom_advance.rs (line 19)
12 fn evaluate<'a>(
13 &self,
14 args: &'a [DataValue<'a>],
15 arena: &'a DataArena,
16 ) -> Result<&'a DataValue<'a>> {
17 // Default to 1 if no arguments provided
18 if args.is_empty() {
19 return Ok(arena.alloc(DataValue::Number(NumberValue::from_i64(1))));
20 }
21
22 // Calculate product of all numeric values
23 let mut product = 1.0;
24 for arg in args {
25 if let Some(n) = arg.as_f64() {
26 product *= n;
27 }
28 }
29
30 // Return the result
31 Ok(arena.alloc(DataValue::Number(NumberValue::from_f64(product))))
32 }
33}
34
35// Define a custom operator that returns the median of a set of numbers
36#[derive(Debug)]
37struct Median;
38
39impl CustomOperator for Median {
40 fn evaluate<'a>(
41 &self,
42 args: &'a [DataValue<'a>],
43 arena: &'a DataArena,
44 ) -> Result<&'a DataValue<'a>> {
45 // Collect all numeric values
46 let mut numbers = Vec::new();
47
48 // Handle the case where a single array is passed
49 if args.len() == 1 && args[0].is_array() {
50 if let Some(items) = args[0].as_array() {
51 for item in items {
52 if let Some(n) = item.as_f64() {
53 numbers.push(n);
54 }
55 }
56 }
57 } else {
58 // Handle the case where multiple arguments are passed
59 for arg in args {
60 if let Some(n) = arg.as_f64() {
61 numbers.push(n);
62 }
63 }
64 }
65
66 // Return 0 for empty arrays
67 if numbers.is_empty() {
68 return Ok(arena.alloc(DataValue::Number(NumberValue::from_i64(0))));
69 }
70
71 // Sort the numbers
72 numbers.sort_by(|a, b| a.partial_cmp(b).unwrap());
73
74 // Calculate the median
75 let median = if numbers.len() % 2 == 0 {
76 // Even number of elements - average the middle two
77 let mid = numbers.len() / 2;
78 (numbers[mid - 1] + numbers[mid]) / 2.0
79 } else {
80 // Odd number of elements - take the middle one
81 numbers[numbers.len() / 2]
82 };
83
84 // Return the result
85 Ok(arena.alloc(DataValue::Number(NumberValue::from_f64(median))))
86 }Sourcepub fn from_f64(value: f64) -> Self
pub fn from_f64(value: f64) -> Self
Creates a new NumberValue from an f64.
Examples found in repository?
More examples
examples/custom_advance.rs (line 31)
12 fn evaluate<'a>(
13 &self,
14 args: &'a [DataValue<'a>],
15 arena: &'a DataArena,
16 ) -> Result<&'a DataValue<'a>> {
17 // Default to 1 if no arguments provided
18 if args.is_empty() {
19 return Ok(arena.alloc(DataValue::Number(NumberValue::from_i64(1))));
20 }
21
22 // Calculate product of all numeric values
23 let mut product = 1.0;
24 for arg in args {
25 if let Some(n) = arg.as_f64() {
26 product *= n;
27 }
28 }
29
30 // Return the result
31 Ok(arena.alloc(DataValue::Number(NumberValue::from_f64(product))))
32 }
33}
34
35// Define a custom operator that returns the median of a set of numbers
36#[derive(Debug)]
37struct Median;
38
39impl CustomOperator for Median {
40 fn evaluate<'a>(
41 &self,
42 args: &'a [DataValue<'a>],
43 arena: &'a DataArena,
44 ) -> Result<&'a DataValue<'a>> {
45 // Collect all numeric values
46 let mut numbers = Vec::new();
47
48 // Handle the case where a single array is passed
49 if args.len() == 1 && args[0].is_array() {
50 if let Some(items) = args[0].as_array() {
51 for item in items {
52 if let Some(n) = item.as_f64() {
53 numbers.push(n);
54 }
55 }
56 }
57 } else {
58 // Handle the case where multiple arguments are passed
59 for arg in args {
60 if let Some(n) = arg.as_f64() {
61 numbers.push(n);
62 }
63 }
64 }
65
66 // Return 0 for empty arrays
67 if numbers.is_empty() {
68 return Ok(arena.alloc(DataValue::Number(NumberValue::from_i64(0))));
69 }
70
71 // Sort the numbers
72 numbers.sort_by(|a, b| a.partial_cmp(b).unwrap());
73
74 // Calculate the median
75 let median = if numbers.len() % 2 == 0 {
76 // Even number of elements - average the middle two
77 let mid = numbers.len() / 2;
78 (numbers[mid - 1] + numbers[mid]) / 2.0
79 } else {
80 // Odd number of elements - take the middle one
81 numbers[numbers.len() / 2]
82 };
83
84 // Return the result
85 Ok(arena.alloc(DataValue::Number(NumberValue::from_f64(median))))
86 }Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if the value is an integer.
Sourcepub fn add(&self, other: &NumberValue) -> NumberValue
pub fn add(&self, other: &NumberValue) -> NumberValue
Adds another NumberValue to this one.
Sourcepub fn subtract(&self, other: &NumberValue) -> NumberValue
pub fn subtract(&self, other: &NumberValue) -> NumberValue
Subtracts another NumberValue from this one.
Sourcepub fn multiply(&self, other: &NumberValue) -> NumberValue
pub fn multiply(&self, other: &NumberValue) -> NumberValue
Multiplies this NumberValue by another.
Sourcepub fn divide(&self, other: &NumberValue) -> Option<NumberValue>
pub fn divide(&self, other: &NumberValue) -> Option<NumberValue>
Divides this NumberValue by another.
Sourcepub fn modulo(&self, other: &NumberValue) -> Option<NumberValue>
pub fn modulo(&self, other: &NumberValue) -> Option<NumberValue>
Returns the modulo of this NumberValue by another.
Trait Implementations§
Source§impl Clone for NumberValue
impl Clone for NumberValue
Source§fn clone(&self) -> NumberValue
fn clone(&self) -> NumberValue
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for NumberValue
impl Debug for NumberValue
Source§impl Display for NumberValue
impl Display for NumberValue
Source§impl PartialEq for NumberValue
impl PartialEq for NumberValue
Source§impl PartialOrd for NumberValue
impl PartialOrd for NumberValue
impl Copy for NumberValue
impl Eq for NumberValue
Auto Trait Implementations§
impl Freeze for NumberValue
impl RefUnwindSafe for NumberValue
impl Send for NumberValue
impl Sync for NumberValue
impl Unpin for NumberValue
impl UnwindSafe for NumberValue
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more