[haxe] HDictionary

When trying to implement a new event system that support custom parameters in Haxe, I come across the need of AS3 Dictionary. After doing a quick search I found that there are no Haxe equivalent (yes, there is TypedDictionary but it’s only available if you are targeting Flash). To me, there is no point to write in Haxe if we can only target one platform, so I bring it up to create a HDictionary that works similar to AS3 Dictionary in pure Haxe hopefully this will be helpful to someone out there.

The basic idea is simple, as we do not has any kind of object signature (a unique string equivalent, usually its memory address) to use as the key, we need to store both key and value and then make a loop through all the items, doing comparison until we come across the correct one. You might think that this will cause much overhead (searching a lot) but remember that whatever type of script we used, this overhead can not be avoided.

For better performance (prevent searching whenever possible), I added a property checkExistance when adding a key (default to true) so you can turn off the default behavior by setting it to false if you sure that the key has not yet been used (of course, this can break the code if you don’t use it correctly so be careful when using this).

Here is the usage syntax (it’s quite similar to AS3) :

var d = new HDictionary();

//to set a key :
d.setKey(key, value, checkExistance);

//to get value from a key :
d.getValue(key); //should return value

//to check for key existance
d.hasKey(key); //return true / false

//to delete a key
d.deleteKey(key);

Here is the class itself (quite simple, though) :

package vn.core;

class HDictionary {
	private var _map 	: List<Pair>;
	
	public function new() {
		_map  = new List<Pair>();
	}
	
	private function getPair(key: Dynamic): Pair {
		for (p in _map) {
			if (p.key == key) return p;
		}
		return null;
	}
	
	/*******************************
	 * 		PUBLIC APIS
	 ******************************/
	
	public function hasKey(key: Dynamic): Bool {
		var p = getPair(key);
		return p != null;
	}
	
	public function deleteKey(key: Dynamic): Void {
		var p = getPair(key);
		if (p != null) _map.remove(p);
	}
	
	public function getValue(key: Dynamic): Dynamic {
		var p = getPair(key);
		return p == null ? null : p.value;
	}
	
	public function setKey(key: Dynamic, value: Dynamic, checkExistance: Bool = true): Void {
		if (!checkExistance) {//prevent looking up overhead
			_map.push( { key: key, value: value} );
		} else {
			var o = getPair(key);
			if (o != null) {//modify value if key already exist
				o.value = value;
			} else {//create new Pair if key not yet exist
				_map.push( { key: key, value: value} );
			}
		}
	}
}

typedef Pair = { key: Dynamic, value: Dynamic }

so, that’s it, try out and let me know if it’s working for you. Happy haxing everyone !

Download HDictionary.hx

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s