From 5ed8773cb6defd4be0106eb5be18532dfe72b17f Mon Sep 17 00:00:00 2001 From: Thomas Knudsen Date: Tue, 19 Mar 2024 15:42:46 +0100 Subject: [PATCH] Add `stomp()` method to `CoordinateSet` --- src/coordinate/mod.rs | 10 ++++++++++ src/inner_op/stack.rs | 15 +++------------ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/coordinate/mod.rs b/src/coordinate/mod.rs index fcae9c20..7ce29bdc 100644 --- a/src/coordinate/mod.rs +++ b/src/coordinate/mod.rs @@ -87,10 +87,20 @@ impl CoordinateMetadata for T where T: ?Sized {} /// access any user provided data model by iterating over its elements, /// represented as a `Coor4D` pub trait CoordinateSet: CoordinateMetadata { + /// Number of coordinate tuples in the set fn len(&self) -> usize; + /// Access the `index`th coordinate tuple fn get_coord(&self, index: usize) -> Coor4D; + /// Overwrite the `index`th coordinate tuple fn set_coord(&mut self, index: usize, value: &Coor4D); fn is_empty(&self) -> bool { self.len() == 0 } + /// Set all coordinate tuples in the set to NaN + fn stomp(&mut self) { + let nanny = Coor4D::nan(); + for i in 0..self.len() { + self.set_coord(i, &nanny); + } + } } diff --git a/src/inner_op/stack.rs b/src/inner_op/stack.rs index c7fe4553..c61bb4ab 100644 --- a/src/inner_op/stack.rs +++ b/src/inner_op/stack.rs @@ -269,10 +269,7 @@ fn stack_flip(stack: &mut [Vec], operands: &mut dyn CoordinateSet, args: &[ // In case of underflow, we stomp on all input coordinates if stack_depth < number_of_flips { warn!("Stack flip underflow in pipeline"); - let nanny = Coor4D::nan(); - for i in 0..number_of_operands { - operands.set_coord(i, &nanny); - } + operands.stomp(); return 0; } @@ -311,10 +308,7 @@ fn stack_roll(stack: &mut Vec>, operands: &mut dyn CoordinateSet, args: if m > depth { warn!("Roll too deep"); - let nanny = Coor4D::nan(); - for i in 0..operands.len() { - operands.set_coord(i, &nanny); - } + operands.stomp(); return 0; } @@ -335,10 +329,7 @@ fn stack_pop(stack: &mut Vec>, operands: &mut dyn CoordinateSet, args: // In case of underflow, we stomp on all input coordinates if stack_depth < number_of_pops { warn!("Stack underflow in pipeline"); - let nanny = Coor4D::nan(); - for i in 0..number_of_operands { - operands.set_coord(i, &nanny); - } + operands.stomp(); return 0; }