cc.redberry.core.tensor.iterator
Class TreeTraverseIterator<T extends Payload<T>>
- java.lang.Object
-
- cc.redberry.core.tensor.iterator.TreeTraverseIterator<T>
-
public final class TreeTraverseIterator<T extends Payload<T>> extends java.lang.ObjectAn iterator for tensors that allows the programmer to traverse the tensor tree structure, modify the tensor during iteration, and obtain information about iterator current position in the tensor. ATensorTreeIteratorhas current element, so all methods are defined in terms of the cursor position. *Example:
This code will print:Tensor tensor = Tensors.parse("Cos[a+b+Sin[x]]"); TensorIterator iterator = new TreeTraverseIterator(tensor); TraverseState state; while ((state = iterator.next()) != null) System.out.println(state + " " + iterator.depth() + " " + iterator.current());Entering Cos[a+b+Sin[y*c+x]] Entering a+b+Sin[y*c+x] Entering a Leaving a Entering b Leaving b Entering Sin[y*c+x] Entering y*c+x Entering y*c Entering y Leaving y Entering c Leaving c Leaving y*c Entering x Leaving x Leaving y*c+x Leaving Sin[y*c+x] Leaving a+b+Sin[y*c+x] Leaving Cos[a+b+Sin[y*c+x]]- Since:
- 1.0
-
-
Constructor Summary
Constructors Constructor and Description TreeTraverseIterator(Tensor tensor)TreeTraverseIterator(Tensor tensor, PayloadFactory<T> payloadFactory)TreeTraverseIterator(Tensor tensor, TraverseGuide guide)TreeTraverseIterator(Tensor tensor, TraverseGuide guide, PayloadFactory<T> payloadFactory)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description booleancheckLevel(Indicator<Tensor> indicator, int level)Checks specified condition at position specified by relative level to current cursor.Tensorcurrent()Returns current cursor.StackPosition<T>currentStackPosition()CurrentStackPositionintdepth()Returns depth in the tree, relatively to the current cursor position.booleanisUnder(Indicator<Tensor> indicator, int searchDepth)TraverseStatenext()Moves iterator to the next state.Tensorresult()Return the resulting tensor.voidset(Tensor tensor)Replaces the current cursor with the specified element.
-
-
-
Constructor Detail
-
TreeTraverseIterator
public TreeTraverseIterator(Tensor tensor, TraverseGuide guide, PayloadFactory<T> payloadFactory)
-
TreeTraverseIterator
public TreeTraverseIterator(Tensor tensor, TraverseGuide guide)
-
TreeTraverseIterator
public TreeTraverseIterator(Tensor tensor)
-
TreeTraverseIterator
public TreeTraverseIterator(Tensor tensor, PayloadFactory<T> payloadFactory)
-
-
Method Detail
-
next
public TraverseState next()
Moves iterator to the next state.- Returns:
- next traverse state or
nullif there is no next element
-
set
public void set(Tensor tensor)
Replaces the current cursor with the specified element.- Parameters:
tensor- the element with which to replace the current cursor
-
depth
public int depth()
Returns depth in the tree, relatively to the current cursor position. Note that depth is counted from zero (e.g. zero will be returned after first next()). If next() never called or last next() was null , this method returns -1.- Returns:
- depth in the tree relatively to the current cursor position
-
checkLevel
public boolean checkLevel(Indicator<Tensor> indicator, int level)
Checks specified condition at position specified by relative level to current cursor.- Parameters:
indicator- level relative position of element to be tested- Returns:
trueif condition satisfies at specified level
-
current
public Tensor current()
Returns current cursor.- Returns:
- current cursor.
-
result
public Tensor result()
Return the resulting tensor.- Returns:
- the resulting tensor
-
currentStackPosition
public StackPosition<T> currentStackPosition()
CurrentStackPosition- Returns:
- current
StackPosition
-
-
DataMelt 3.0 © DataMelt by jWork.ORG