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.

6 Responses to A Map Interface For Flash

  1. Sam says:

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

  2. PaulH says:

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

  3. Brian Lesser says:

    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

  4. nick says:

    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.

  5. Sam says:

    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