
+ class FlatMap[K, T] {
    - _keys : Array[K] (.{})
    - _values : Array[T] (.{})

    + static fn new() SELF {
        return SELF{}
    }
    + macro "{" repeat(items, ws V:v1 ws "=>" ws V:v2 ws optional(",") ws, "}")
    <{
    	let m = SELF.new()
    	#loop macro_values items as item
    	m.set(item.v1, item.v2)
    	#end
    	return m
    }

    + get length : uint {
        return this._keys.length
    }
    + fn keys() Array[K] {
        return this._keys.copy()
    }
    + fn sort_keys() SELF {
        this._keys.sort()
        return this
    }
    + fn values() Array[T] {
        return this._values.copy()
    }

    + mut fn set(key: K, value: T) SELF $offset_assign {
        let index = this._keys.index_of(key) ! {
            // Set new key / value
            this._keys.append(key)
            this._values.append(value)
            return this
        }
        // Update existing key / value
        this._values.set(index, value) _
        return this
    }

    + mut fn set_many(map: SELF) SELF {
        each map as v, k {
            this.set(k, v)
        }
        return this
    }

    + mut fn set_unique(key: K, value: T) !not_unique {
        if this._keys.contains(key) : throw not_unique
        this._keys.append(key)
        this._values.append(value)
    }

    + fn get(key: K) T !not_found $offset {
        let index = this._keys.index_of(key) ! { throw not_found }
        return this._values.get(index) ! { throw not_found }
    }

    + fn has(key: K) bool {
        return this._keys.contains(key)
    }
    + fn has_value(value: T) bool {
        return this._values.contains(value)
    }

    + mut fn remove(key: K) SELF {
        let index = this._keys.index_of(key) ! return this
        this._keys.remove(index)
        this._values.remove(index)
        return this
    }

    + mut fn clear() SELF {
        this._keys.clear()
        this._values.clear()
        return this
    }

    + fn merge(map: SELF) SELF $add {
        return this.copy().set_many(map)
    }

    + fn copy() SELF {
        return SELF{
            _keys: this._keys.copy()
            _values: this._values.copy()
        }
    }

    fn each_get(index: uint) (T, K) !end {
        let k = this._keys.get(index) ! throw end
        let v = this._values.get(index) ! throw end
        return v, k
    }
}
