Moved these here to narya.

Lately I'm trying to do things more "actionscripty", and so we just
use the "natural" iteration on an object (either 'for' or 'for each').
Plus, we never use these Iterators anywhere except in presents-based code.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@5983 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2009-10-27 00:57:48 +00:00
parent 6137fbfb79
commit c16de5994e
2 changed files with 127 additions and 0 deletions
@@ -0,0 +1,81 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.util {
import flash.errors.IllegalOperationError;
/**
* Provides a generic iterator for an Array.
* No co-modification checking is done.
*/
public class ArrayIterator
implements Iterator
{
/**
* Create an ArrayIterator.
*/
public function ArrayIterator (arr :Array, allowRemove :Boolean = true)
{
_arr = arr;
_index = 0;
_lastIndex = allowRemove ? -1 : -2;
}
// documentation inherited from interface Iterator
public function hasNext () :Boolean
{
return (_index < _arr.length);
}
// documentation inherited from interface Iterator
public function next () :Object
{
if (_lastIndex != -2) {
_lastIndex = _index;
}
return _arr[_index++];
}
// documentation inherited from interface Iterator
public function remove () :void
{
if (_lastIndex < 0) {
throw new IllegalOperationError();
} else {
_arr.splice(_lastIndex, 1);
_lastIndex = -1;
_index--; // since _lastIndex is always before _index
}
}
/** The array we're iterating over. */
protected var _arr :Array;
/** The current index. */
protected var _index :int;
/** The last-removed index.
* Or -1 for already removed, -2 for no removals allowed. */
protected var _lastIndex :int;
}
}
+46
View File
@@ -0,0 +1,46 @@
//
// $Id$
//
// Narya library - tools for developing networked games
// Copyright (C) 2002-2009 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/narya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
package com.threerings.util {
/**
* An Iterator interface for ActionScript.
* Flex defines IViewCursor, but it defines 14 methods and 5 read-only properties, which
* is a large implementation burden. This provides a simpler alternative.
*/
public interface Iterator
{
/**
* Is there another element available?
*/
function hasNext () :Boolean;
/**
* Returns the next element.
*/
function next () :Object;
/**
* Remove the last returned element.
*/
function remove () :void;
}
}