package HashMap import NoWurst import HashList import public TypeCasting import public Table /** Generic Table Wrapper */ public class HashMap extends Table protected int size = 0 /** Whether a value exists under the given key or not */ function has(K key) returns boolean return hasInt(key castTo int) /** Saves the given value under the given key */ function put(K key, V value) if not has(key) size++ saveInt(key castTo int, value castTo int) /** Retrieves the value saved under the given key */ function get(K key) returns V return loadInt(key castTo int) castTo V /** Removes the value saved under the given key */ function remove(K key) if has(key) size-- removeInt(key castTo int) /** Retrieves the value saved under the given key and removes it */ function getAndRemove(K key) returns V let result = get(key) remove(key) return result function size() returns int return size override function flush() size = 0 super.flush() /** Iterable generic Table Wrapper */ public class IterableMap extends HashMap protected let keys = new HashList private bool _destroyed = false construct() /** Create a new map by copying all elements from another list into it */ construct(thistype base) for key in base put(key, base.get(key)) /** Saves the given value under the given key */ override function put(K key, V value) super.put(key, value) if not hasKey(key) keys.add(key) /** Removes the key-value pair saved under the given key */ override function remove(K key) super.remove(key) if hasKey(key) keys.remove(key) /** Remove all data from this map */ override function flush() if not _destroyed keys.clear() super.flush() /** Retrieves the value saved under the given key and removes it */ override function getAndRemove(K key) returns V let result = super.getAndRemove(key) keys.remove(key) return result /** Returns an iterator that iterates over the map's keys */ function iterator() returns HLIterator return keys.iterator() /** Returns whether this map uses the given key */ function hasKey(K key) returns bool return keys.has(key) /** Removes either a single occurence or all occurences from of the value from the map */ function removeValue(V val, bool all) K array toRemove int num = 0 for elem in this if get(elem) == val if all toRemove[num] = elem num++ if all and num > 0 for i = 0 to num - 1 remove(toRemove[i]) /** Copies all elements from another map into this one Use with caution, since it will replace elements that were under the same key! */ function addAll(IterableMap map) for key in map put(key, map.get(key)) /** Returns a shallow copy of this map */ function copy() returns IterableMap let map = new IterableMap for key in this map.put(key, get(key)) return map /** Returns the length of this IterableMap **/ override function size() returns int return keys.size() /** Executes the closure for each key-value pair */ function forEach(IMItrClosure itr) returns IterableMap for i = 0 to size() - 1 let key = keys.get(i) let value = this.get(key) itr.run(key, value) destroy itr return this ondestroy destroy keys // Makes sure that no attempts to .clear() a destroyed group are made // since .flush() is called after this destructor _destroyed = true public interface IMItrClosure function run(K key, V value)