Created
July 19, 2011 08:44
-
-
Save bustardcelly/1091741 to your computer and use it in GitHub Desktop.
A lightweight iterator for the LinkedList of Flex SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* <p>Original Author: toddanderson</p> | |
* <p>Class File: LinkedListIterator.as</p> | |
* <p>Version: 0.1</p> | |
* | |
* <p>Permission is hereby granted, free of charge, to any person obtaining a copy | |
* of this software and associated documentation files (the "Software"), to deal | |
* in the Software without restriction, including without limitation the rights | |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
* copies of the Software, and to permit persons to whom the Software is | |
* furnished to do so, subject to the following conditions:</p> | |
* | |
* <p>The above copyright notice and this permission notice shall be included in | |
* all copies or substantial portions of the Software.</p> | |
* | |
* <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
* THE SOFTWARE.</p> | |
*/ | |
package com.custardbelly.data | |
{ | |
import mx.utils.LinkedList; | |
import mx.utils.LinkedListNode; | |
/** | |
* LinkedListIterator is a simple iterator for a LinkedList from the Flex SDK, that provides a lightweight traversal API for data held in a linked list. | |
* Heavily influenced by the Data Structure source created by Michael Baczynski: http://www.polygonal.de. | |
* Examples of polygonal Linked List can be found here: http://lab.polygonal.de/2007/08/13/data-structures-example-linked-lists/ | |
* The source for polygonal as3ds can be found here: http://lab.polygonal.de/as3ds/ | |
* | |
* <pre> | |
* var list:LinkedList = new LinkedList(); | |
* list.push( "foo" ); | |
* list.push( "bar" ); | |
* list.push( "baz" ); | |
* | |
* var iterator:LinkedListIterator = new LinkedListIterator( list ); | |
* while( iterator.hasNext() ) | |
* { | |
* trace( "> Node value: " + iterator.next() ); | |
* } | |
* | |
* iterator.tail(); | |
* while( iterator.hasPrevious() ) | |
* { | |
* trace( "< Node value: " + iterator.previous() ); | |
* } | |
* </pre> | |
* | |
* @author toddanderson | |
*/ | |
public class LinkedListIterator | |
{ | |
protected var _list:LinkedList | |
protected var _node:LinkedListNode; | |
/** | |
* Constructor. | |
* | |
* @param list The source LinkedList to provide a traversal API level on top of. | |
* @param node Optional. The node pointer to start. If null, defaults to head. | |
*/ | |
public function LinkedListIterator( list:LinkedList, node:LinkedListNode = null ) | |
{ | |
_list = list; | |
_node = node; | |
if( !isValid() ) | |
{ | |
start(); | |
} | |
} | |
/** | |
* @private | |
* | |
* Returns flag of current node pointer being non-null. | |
* @return Boolean | |
*/ | |
protected function isValid():Boolean | |
{ | |
return _node != null; | |
} | |
/** | |
* Returns the current node value and moves node pointer forward in the list. | |
* @return * | |
*/ | |
public function next():* | |
{ | |
var data:*; | |
if( isValid() ) | |
{ | |
data = _node.value; | |
_node = _node.next; | |
} | |
return data; | |
} | |
/** | |
* Returns the previous node value and moves node pointer backward in the list. | |
* @return * | |
*/ | |
public function previous():* | |
{ | |
var data:*; | |
if( isValid() ) | |
{ | |
data = _node.value; | |
_node = _node.prev; | |
} | |
return data; | |
} | |
/** | |
* Returns flag of current node having a pointer to the next node in the list. | |
* @return Boolean | |
*/ | |
public function hasNext():Boolean | |
{ | |
return _node.next != null; | |
} | |
/** | |
* Returns flag of current node having pointer to the previous node in the list. | |
* @return Boolean | |
*/ | |
public function hasPrevious():Boolean | |
{ | |
return _node.prev != null; | |
} | |
/** | |
* Sets the node pointer to the head of the list source. | |
*/ | |
public function start():void | |
{ | |
_node = _list.head; | |
} | |
/** | |
* Sets the node pointer to the tail of the list source. | |
*/ | |
public function end():void | |
{ | |
_node = _list.tail; | |
} | |
/** | |
* Returns the data at the current node. | |
* @return * | |
*/ | |
public function get data():* | |
{ | |
return (_node) ? _node.value : null; | |
} | |
/** | |
* Removes any held references to free up for GC. | |
*/ | |
public function dispose():void | |
{ | |
_list = null; | |
_node = null; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment