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:
@@ -210,12 +210,7 @@ public class Hashtable
|
||||
for (var ii :int = _entries.length - 1; ii >= 0; ii--) {
|
||||
for (var e :Entry = (_entries[ii] as Entry); e != null;
|
||||
e = e.next) {
|
||||
if (e.key is KeyWrapper) {
|
||||
keys.push((e.key as KeyWrapper).key);
|
||||
|
||||
} else {
|
||||
keys.push(e.key)
|
||||
}
|
||||
keys.push(e.getOriginalKey());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -223,6 +218,7 @@ public class Hashtable
|
||||
return keys;
|
||||
}
|
||||
|
||||
// documentation inherited from interface Map
|
||||
public function values () :Array
|
||||
{
|
||||
var vals :Array = new Array();
|
||||
@@ -247,6 +243,25 @@ public class Hashtable
|
||||
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.
|
||||
*/
|
||||
@@ -348,6 +363,17 @@ class Entry
|
||||
this.value = value;
|
||||
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
|
||||
|
||||
@@ -21,6 +21,12 @@ public interface Map
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
@@ -47,6 +53,11 @@ public interface Map
|
||||
* Return the current size of the map.
|
||||
*/
|
||||
function size () :int;
|
||||
|
||||
/**
|
||||
* Return all the values in this Map, in Array form.
|
||||
*/
|
||||
function values () :Array;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user