ArrayIterator can be unmodifiable.

git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4550 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2007-02-10 03:35:37 +00:00
parent 15c0bbbe1c
commit d1ff63d62d
2 changed files with 10 additions and 6 deletions
+1 -1
View File
@@ -85,7 +85,7 @@ public class DSet
*/ */
public function iterator () :Iterator public function iterator () :Iterator
{ {
return new ArrayIterator(_entries); return new ArrayIterator(_entries, false);
} }
/** /**
+8 -4
View File
@@ -12,10 +12,11 @@ public class ArrayIterator
/** /**
* Create an ArrayIterator. * Create an ArrayIterator.
*/ */
public function ArrayIterator (arr :Array) public function ArrayIterator (arr :Array, allowRemove :Boolean = true)
{ {
_arr = arr; _arr = arr;
_index = 0; _index = 0;
_lastIndex = allowRemove ? -1 : -2;
} }
// documentation inherited from interface Iterator // documentation inherited from interface Iterator
@@ -27,14 +28,16 @@ public class ArrayIterator
// documentation inherited from interface Iterator // documentation inherited from interface Iterator
public function next () :Object public function next () :Object
{ {
if (_lastIndex != -2) {
_lastIndex = _index; _lastIndex = _index;
}
return _arr[_index++]; return _arr[_index++];
} }
// documentation inherited from interface Iterator // documentation inherited from interface Iterator
public function remove () :void public function remove () :void
{ {
if (_lastIndex == -1) { if (_lastIndex < 0) {
throw new IllegalOperationError(); throw new IllegalOperationError();
} else { } else {
@@ -50,7 +53,8 @@ public class ArrayIterator
/** The current index. */ /** The current index. */
protected var _index :int; protected var _index :int;
/** The last-removed index. */ /** The last-removed index.
protected var _lastIndex :int = -1; * Or -1 for already removed, -2 for no removals allowed. */
protected var _lastIndex :int;
} }
} }