TreeEnumerator
| Kind of class: | class |
|---|---|
| Inherits from: | Enumerator < Dispatcher |
| Known subclasses: | |
| Version: | 20 July 2005 |
| Author: | Arthur Clemens |
| Classpath: | playground.classes.data.tree.TreeEnumerator |
| File last modified: | Friday, 14 July 2006, 00:17:08 |
Straightforward enumeration (iterator) class for Tree objects.
TreeEnumerator has one way of iterating: forward (getNextObject). . For more options see TraverseTreeEnumerator.Important note: the Tree object should not be modified while iterating over it.
Usage:
var myTree:Tree = new Tree(); // fill the tree ... // Create an enumerator and pass the start node to traverse: var enumerator:TreeEnumerator = new TreeEnumerator(myTree.root); var node:TreeNode; while (node = TreeNode(enumerator.getNextObject())) { if (node == myTree.root) continue; trace(node.name); }
Summary
Constructor
Instance methods
Instance methods inherited from Enumerator
Constructor
TreeEnumerator
function TreeEnumerator (
inRootNode:TreeNode)
Creates a TreeEnumerator.
Parameters:
inRootNode:
the root node of the enumerator; you may pass any node of the tree if the enumerator should start at that root - but the enumerator will traverse that branch only and will not traverse upwards to a node with a lower depth.
Instance methods
getAllObjects
function getAllObjects (
inStartNode:TreeNode) : Array
Collects all nodes of the iterator's tree, or of a particular branche.
Parameters:
inStartNode:
(optional) If a node is passed,
getAllObjects collects all nodes of the current branch, taking inRootNode as the root node (and setting inRootNode as the first node of the returned array); when not defined the TreeEnumerator's root node is used.Returns:
An array with all TreeNode objects.
Example:
The following line creates an array with all the nodes starting with the node named "Games":
var gameNode:TreeNode = tree.getNode("Games"); var gameNodes:Array = myEnumerator.getAllObjects(gameNode);
getCurrentObject
function getCurrentObject (
) : TreeNode
Returns the current node.
Usage note:
When the enumerator is created, the enumerator position is at a virtual node ZeroNode one place before the first tree node. At this 'null position', calling
The method reset brings the enumerator back to the null position.
getCurrentObject returns an error. Therefore, you must always call getNextObject to advance the enumerator to the first node before reading the value of getCurrentObject.The method reset brings the enumerator back to the null position.
Returns:
The current TreeNode node.
getNextObject
function getNextObject (
) : TreeNode
Moves the enumerator one position forward and returns that node.
Returns:
The new current node; when no valid node is found, returns null.
Example:
var e:TreeEnumerator = new TreeEnumerator(mTree.root); var node:TreeNode; while (node = e.getNextObject()) { // }
Implementation note:
Calls update.
hasNextObject
function hasNextObject (
) : Boolean
Checks if there is an object after the current object.
Returns:
True: there is a next object; false: the current object is the last.
reset
function reset (
) : Void
Puts the enumerator just before the first tree node. At this point calling getCurrentObject will generate an error; you must first move the enumerator using getNextObject.
Implementation note:
Calls update.
Overrides:
setCurrentObject
function setCurrentObject (
inTreeNode:TreeNode) : Void
Sets the enumerator's current object to another TreeNode.
Parameters:
inTreeNode:
the new tree node
Implementation note:
Calls update.
Example:
var newNode:TreeNode = mTree.getNode("Help"); mEnumerator.setCurrentObject(newNode);
setRoot
function setRoot (
inRootNode:TreeNode,
updateCurrentObjectToRoot:Boolean) : Void
Sets a different node as root node.
Parameters:
inRootNode :
the new root node
updateCurrentObjectToRoot:
sets the current object to the new root node