Skip to content

Instantly share code, notes, and snippets.

@ruby0x1
Last active April 30, 2020 22:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ruby0x1/84341491930a5df325e7 to your computer and use it in GitHub Desktop.
Save ruby0x1/84341491930a5df325e7 to your computer and use it in GitHub Desktop.
A simple Haxe ordered Map implementation, MIT license
import Map;
class OrderedMapIterator<K,V> {
var map : OrderedMap<K,V>;
var index : Int = 0;
public function new(omap:OrderedMap<K,V>)
map = omap;
public function hasNext() : Bool
return index < map._keys.length;
public function next() : V
return map.get( map._keys[index++] );
} //OrderedMapIterator
class OrderedMap<K, V> implements IMap<K, V> {
var map:Map<K, V>;
@:allow(OrderedMapIterator)
var _keys:Array<K>;
var idx = 0;
public function new(_map) {
_keys = [];
map = _map;
}
public function set(key, value) {
if(_keys.indexOf(key) == -1) _keys.push(key);
map[key] = value;
}
public function toString() {
var _ret = ''; var _cnt = 0; var _len = _keys.length;
for(k in _keys) _ret += '$k => ${map.get(k)}${(_cnt++<_len-1?", ":"")}';
return '{$_ret}';
}
public function iterator() return new OrderedMapIterator<K,V>(this);
public function remove(key) return map.remove(key) && _keys.remove(key);
public function exists(key) return map.exists(key);
public function get(key) return map.get(key);
public inline function keys() return _keys.iterator();
} //OrderedMap
Main.hx:27: N 0 = 0
Main.hx:27: N 2 = 2
Main.hx:27: N 1 = 1
Main.hx:27: N 4 = 4
Main.hx:27: N 7 = 7
Main.hx:27: N 6 = 6
Main.hx:27: N 5 = 5
Main.hx:27: N 3 = 3
Main.hx:28: NV 0
Main.hx:28: NV 2
Main.hx:28: NV 1
Main.hx:28: NV 4
Main.hx:28: NV 7
Main.hx:28: NV 6
Main.hx:28: NV 5
Main.hx:28: NV 3
Main.hx:32: O 0 = 0
Main.hx:32: O 1 = 1
Main.hx:32: O 2 = 2
Main.hx:32: O 3 = 3
Main.hx:32: O 4 = 4
Main.hx:32: O 5 = 5
Main.hx:32: O 6 = 6
Main.hx:32: O 7 = 7
Main.hx:33: OV 0
Main.hx:33: OV 1
Main.hx:33: OV 2
Main.hx:33: OV 3
Main.hx:33: OV 4
Main.hx:33: OV 5
Main.hx:33: OV 6
Main.hx:33: OV 7
Main.hx:35: {0 => 0, 2 => 2, 1 => 1, 4 => 4, 7 => 7, 6 => 6, 5 => 5, 3 => 3}
Main.hx:36: {0 => 0, 1 => 1, 2 => 2, 3 => 3, 4 => 4, 5 => 5, 6 => 6, 7 => 7}
function run(count:Int = 8) {
var nmap = new Map<String, Int>();
for(i in 0 ... count) nmap.set(Std.string(i), i);
for(n in nmap.keys()) trace('N $n = ${nmap.get(n)}');
for(n in nmap) trace('NV ${n}');
var omap = new OrderedMap<String, Int>( new Map<String,Int>() );
for(i in 0 ... count) omap.set(Std.string(i), i);
for(n in omap.keys()) trace('O $n = ${omap.get(n)}');
for(n in omap) trace('OV ${n}');
trace(nmap);
trace(omap);
}
@lguzzon
Copy link

lguzzon commented Dec 18, 2014

Hi,
better rewrite

public function set(key, value) 
{
        if(_keys.indexOf(key) == -1) _keys.push(key);
        map[key] = value;
}

to

public function set(key, value)
{
        if (!map.exists(key)) _keys.push(key);
        map[key] = value;
}

Here a forked working gists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment