Added the values() function to the Map interface, as well as a new method,

forEach(), in which you pass a visitor function that will visit each key/value
stored in the map.


git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4257 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
Ray Greenwell
2006-07-11 18:22:29 +00:00
parent 489332edca
commit 78cefe9a8f
2 changed files with 43 additions and 6 deletions
+32 -6
View File
@@ -210,12 +210,7 @@ public class Hashtable
for (var ii :int = _entries.length - 1; ii >= 0; ii--) { for (var ii :int = _entries.length - 1; ii >= 0; ii--) {
for (var e :Entry = (_entries[ii] as Entry); e != null; for (var e :Entry = (_entries[ii] as Entry); e != null;
e = e.next) { e = e.next) {
if (e.key is KeyWrapper) { keys.push(e.getOriginalKey());
keys.push((e.key as KeyWrapper).key);
} else {
keys.push(e.key)
}
} }
} }
} }
@@ -223,6 +218,7 @@ public class Hashtable
return keys; return keys;
} }
// documentation inherited from interface Map
public function values () :Array public function values () :Array
{ {
var vals :Array = new Array(); var vals :Array = new Array();
@@ -247,6 +243,25 @@ public class Hashtable
return vals; return vals;
} }
// documentation inherited from interface Map
public function forEach (fn :Function) :void
{
if (_simpleData != null) {
for (var key :Object in _simpleData) {
fn(key, _simpleData[key]);
}
}
if (_entries != null) {
for (var ii :int = _entries.length - 1; ii >= 0; ii--) {
for (var e :Entry = (_entries[ii] as Entry); e != null;
e = e.next) {
fn(e.getOriginalKey(), e.value);
}
}
}
}
/** /**
* Return a Hashable that represents the key. * Return a Hashable that represents the key.
*/ */
@@ -348,6 +363,17 @@ class Entry
this.value = value; this.value = value;
this.next = next; this.next = next;
} }
/**
* Get the original key used to store this entry.
*/
public function getOriginalKey () :Object
{
if (key is KeyWrapper) {
return (key as KeyWrapper).key;
}
return key;
}
} }
class KeyWrapper class KeyWrapper
+11
View File
@@ -21,6 +21,12 @@ public interface Map
*/ */
function get (key :Object) :*; function get (key :Object) :*;
/**
* Call the specified function, which accepts to args: key and value,
* for every mapping.
*/
function forEach (fn :Function) :void;
/** /**
* Returns true if this map contains no elements. * Returns true if this map contains no elements.
*/ */
@@ -47,6 +53,11 @@ public interface Map
* Return the current size of the map. * Return the current size of the map.
*/ */
function size () :int; function size () :int;
/**
* Return all the values in this Map, in Array form.
*/
function values () :Array;
} }
} }