Struct bet::BeTree

source ·
pub struct BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq, Atom: Debug + Clone,
{ /* private fields */ }
Expand description

An expression which may contain unary and binary operations

Implementations§

source§

impl<Op, Atom> BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq, Atom: Debug + Clone,

source

pub fn new() -> Self

create an empty expression, ready to be completed

source

pub fn node(&self, node_id: NodeId) -> Option<&Node<Op>>

source

pub fn atom(&self, atom_id: AtomId) -> Option<&Atom>

source

pub fn head(&self) -> &Node<Op>

source

pub fn is_empty(&self) -> bool

tells whether the expression is devoid of any atom

source

pub fn is_atomic(&self) -> bool

tell whether the tree is exactly one atom

source

pub fn atoms(self) -> Vec<Atom>

take the atoms of the tree

source

pub fn iter_atoms(&self) -> Iter<'_, Atom>

iterate on all atoms

source

pub fn current_atom(&self) -> Option<&Atom>

returns a reference to the last atom if it’s the last pushed token. Return none in other cases (including when no atom has been pushed at all)

source

pub fn get_openness(&self) -> usize

return the count of open parenthesis minus the one of closing parenthesis. Illegal closing parenthesis are ignored (hence why this count can be a usize)

source

pub fn push(&mut self, token: Token<Op, Atom>)

add one of the possible token: parenthesis, operator or atom

source

pub fn push_atom(&mut self, atom: Atom)

add an atom in a left-to-right expression building

source

pub fn mutate_or_create_atom<Create>(&mut self, create: Create) -> &mut Atom
where Create: Fn() -> Atom,

if the last change was an atom pushed or modified, return a mutable reference to this atom. If not, push a new atom and return a mutable reference to it.

source

pub fn open_par(&mut self)

add an opening parenthesis to the expression

source

pub fn close_par(&mut self)

add a closing parenthesis to the expression

source

pub fn push_operator(&mut self, operator: Op)

add an operator right of the expression

The context will decide whether it’s unary or binary

source

pub fn accept_unary_operator(&self) -> bool

tell whether it would make sense to push a unary operator at this point (for example it makes no sense just after an atom)

source

pub fn accept_binary_operator(&self) -> bool

tell whether it would make sense to push a binary operator at this point (for example it makes no sense just after another operator)

source

pub fn accept_atom(&self) -> bool

tell whether it would make sense to push an atom at this point (for example it makes no sense just after a closing parenthesis)

source

pub fn accept_opening_par(&self) -> bool

tell whether it would make sense to open a parenthesis at this point (for example it makes no sense just after a closing parenthesis)

source

pub fn accept_closing_par(&self) -> bool

tell whether it would make sense to close a parenthesis at this point (for example it makes no sense just after an operator or if there are more closing parenthesis than opening ones)

source

pub fn try_map_atoms<Atom2, Err, F>( &self, f: F ) -> Result<BeTree<Op, Atom2>, Err>
where Atom2: Debug + Clone, F: Fn(&Atom) -> Result<Atom2, Err>,

produce a new expression by applying a transformation on all atoms

The operation will stop at the first error

source

pub fn eval<R, EvalAtom, EvalOp, ShortCircuit>( &self, eval_atom: EvalAtom, eval_op: EvalOp, short_circuit: ShortCircuit ) -> Option<R>
where EvalAtom: Fn(&Atom) -> R, EvalOp: Fn(&Op, R, Option<R>) -> R, ShortCircuit: Fn(&Op, &R) -> bool,

evaluate the expression.

eval_atom will be called on all atoms (leafs) of the expression while eval_op will be used to join values until the final result is obtained.

short_circuit will be called on all binary operations with the operator and the left operands as arguments. If it returns true then the right operand isn’t evaluated (it’s guaranteed so it may serve as guard).

This function should be used when neither atom evaluation nor operator execution can raise errors (this usually means consistency checks have been done during parsing).

source

pub fn eval_faillible<Err, R, EvalAtom, EvalOp, ShortCircuit>( &self, eval_atom: EvalAtom, eval_op: EvalOp, short_circuit: ShortCircuit ) -> Result<Option<R>, Err>
where EvalAtom: Fn(&Atom) -> Result<R, Err>, EvalOp: Fn(&Op, R, Option<R>) -> Result<R, Err>, ShortCircuit: Fn(&Op, &R) -> bool,

evaluate the expression.

eval_atom will be called on all atoms (leafs) of the expression while eval_op will be used to join values until the final result is obtained.

short_circuit will be called on all binary operations with the operator and the left operands as arguments. If it returns true then the right operand isn’t evaluated (it’s guaranteed so it may serve as guard).

This function should be used when errors are expected during either atom evaluation or operator execution (for example because parsing was lax). The first Error returned by one of those functions breaks the evaluation and is returned.

source

pub fn simplify(&mut self)

source

pub fn print_child(&self, child: Child, indent: usize)

source

pub fn print_node(&self, node_id: NodeId, indent: usize)

source

pub fn print_tree(&self)

Trait Implementations§

source§

impl<Op, Atom> Clone for BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq + Clone, Atom: Debug + Clone + Clone,

source§

fn clone(&self) -> BeTree<Op, Atom>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<Op, Atom> Debug for BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq + Debug, Atom: Debug + Clone + Debug,

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<Op, Atom> Default for BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq, Atom: Debug + Clone,

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<Op, Atom> PartialEq for BeTree<Op, Atom>
where Op: Debug + Clone + PartialEq, Atom: Debug + Clone + PartialEq,

source§

fn eq(&self, other: &Self) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<Op, Atom> Freeze for BeTree<Op, Atom>

§

impl<Op, Atom> RefUnwindSafe for BeTree<Op, Atom>
where Atom: RefUnwindSafe, Op: RefUnwindSafe,

§

impl<Op, Atom> Send for BeTree<Op, Atom>
where Atom: Send, Op: Send,

§

impl<Op, Atom> Sync for BeTree<Op, Atom>
where Atom: Sync, Op: Sync,

§

impl<Op, Atom> Unpin for BeTree<Op, Atom>
where Atom: Unpin, Op: Unpin,

§

impl<Op, Atom> UnwindSafe for BeTree<Op, Atom>
where Atom: UnwindSafe, Op: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.