« Messages Instead of Events | Main | Flex - A Third Party Perspective »
December 11, 2003
A Map Interface For Flash
I know you can accomplish the same things with associative arrays and objects in Flash, but I'm used to Map-like interfaces, so before I could get any real work done, I had to write SimpleMap.as. As it's name implies, it's a simple implementation which gives you put, get, remove, size, keys, values and toString functions. I prefer it over using objects and associative arrays, and it makes a good base class for building specific kinds of Maps. I haven't written any others yet, but I can envision a SortableMap, ExpiringMap (where entries are automatically removed after a specified amount of time), ArrayMap (which automatically creates arrays for entries with duplicate keys), etc.
Be warned: this code is my own invention, and has not been QAed, so put it through its paces before incorporating it into your own project.
Posted by cantrell at December 11, 2003 11:13 AM | References
Comments
Why do you prefer this over plain objects? Seems like a lot of extra overhead without enough benefit.
On the code, I think put needs to validate that value isn't undefined and on remove, you can just delete the key and decrement len (if the key existed).
Posted by: Sam at December 11, 2003 11:59 AM
now if you wanted a "real" map interface for flash: http://www.geoclip.net/an/
Posted by: PaulH at December 11, 2003 01:45 PM
Hi Christian,
I don't think put should increment length when an existing element is being overwritten.
Instead of:
public function put(name:String, value:Object):Void
{
this.map[name] = value;
++this.len;
}
Maybe you need something like:
public function put(name:String, value:Object):Void
{
if (typeof this.map[name] == "undefined")
this.len++
this.map[name] = value;
}
Yours truly,
-Brian
Posted by: Brian Lesser at December 11, 2003 02:17 PM
Chris is giving us some free code with a disclaimer, stop whining and fix it for yourselves or simply don't use it if you're not happy.
Posted by: nick at December 11, 2003 04:37 PM
Nick,
I really don't think any of the previous posts were whining. Christian posted some code. We're commenting on that code. That's what COMMUNITY is all about.
If someone found a bug in code I posted on my blog I sure would want them to tell me about it so I could fix my own version. General comments are equally welcome.
Best regards,
Sam
Posted by: Sam at December 11, 2003 06:46 PM
x free code!
Posted by: escuchar musica at January 13, 2004 01:40 PM