From dac504c7bb50dadc9eee71f61e66db3b34e71fce Mon Sep 17 00:00:00 2001 From: Michael Bayne Date: Tue, 21 Aug 2001 00:58:26 +0000 Subject: [PATCH] Provide support for iterating over a DSet. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@283 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- .../com/threerings/presents/dobj/DSet.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/src/java/com/threerings/presents/dobj/DSet.java b/src/java/com/threerings/presents/dobj/DSet.java index 54cce14e3..87724e7ae 100644 --- a/src/java/com/threerings/presents/dobj/DSet.java +++ b/src/java/com/threerings/presents/dobj/DSet.java @@ -1,11 +1,12 @@ // -// $Id: DSet.java,v 1.4 2001/08/20 21:44:10 mdb Exp $ +// $Id: DSet.java,v 1.5 2001/08/21 00:58:26 mdb Exp $ package com.threerings.cocktail.cher.dobj; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; +import java.util.Iterator; import com.threerings.cocktail.cher.Log; import com.threerings.cocktail.cher.io.Streamable; @@ -145,6 +146,49 @@ public class DSet return null; } + /** + * Returns an iterator over the elements of this set. It does not + * support modification (nor iteration while modifications are being + * made to the set). It should not be kept around as it can quickly + * become out of date. + */ + public Iterator elements () + { + return new Iterator() { + public boolean hasNext () + { + // we need to scan to the next element the first time + if (_index < 0) { + scanToNext(); + } + return (_index < _elements.length); + } + + public Object next () + { + Object val = _elements[_index]; + scanToNext(); + return val; + } + + public void remove () + { + throw new UnsupportedOperationException(); + } + + protected void scanToNext () + { + while (_index < _elements.length) { + if (_elements[++_index] != null) { + return; + } + } + } + + int _index = -1; + }; + } + /** * Adds the specified element to the set. This should not be called * directly, instead the associated addTo{Set}() method