From c16de5994e9b78daba12a3a05075c50d25db4f94 Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Tue, 27 Oct 2009 00:57:48 +0000 Subject: [PATCH] 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 --- src/as/com/threerings/util/ArrayIterator.as | 81 +++++++++++++++++++++ src/as/com/threerings/util/Iterator.as | 46 ++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 src/as/com/threerings/util/ArrayIterator.as create mode 100644 src/as/com/threerings/util/Iterator.as diff --git a/src/as/com/threerings/util/ArrayIterator.as b/src/as/com/threerings/util/ArrayIterator.as new file mode 100644 index 000000000..2b7ca545c --- /dev/null +++ b/src/as/com/threerings/util/ArrayIterator.as @@ -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; +} +} diff --git a/src/as/com/threerings/util/Iterator.as b/src/as/com/threerings/util/Iterator.as new file mode 100644 index 000000000..1d4235db1 --- /dev/null +++ b/src/as/com/threerings/util/Iterator.as @@ -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; +} +}