by Christian Cantrell

 Comments (6)

Created

December 11, 2003

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.

COMMENTS

  • By Sam - 11:59 AM on December 11, 2003   Reply

    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).

  • By PaulH - 1:45 PM on December 11, 2003   Reply

    now if you wanted a “real” map interface for flash: http://www.geoclip.net/an/

  • By Brian Lesser - 2:17 PM on December 11, 2003   Reply

    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

  • By nick - 4:37 PM on December 11, 2003   Reply

    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.

  • By Sam - 6:46 PM on December 11, 2003   Reply

    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

  • By escuchar musica - 1:40 PM on January 13, 2004   Reply

    x free code!

ADD A COMMENT