/
ColdWeb-0.2/
ColdWeb-0.2/root/
parent $root
object $dictionary

method to_list
    arg dict;
    var list, x, k;
    
    // merges into an associated list.
    k = dict_keys(dict);
    list = [];
    for x in (k)
        list = [@list, [x, dict[x]]];
    return list;
.

method union
    arg dict1, dict2;
    var key;
    
    // like union() but for dictionaries.  adds any keys from dict2 that don't
    // already exist in dict1 to dict1 and returns the result.  Order of keys in
    // result is not guaranteed.
    for key in (dict1)
        dict2 = dict_add(dict2, key[1], key[2]);
    return dict2;
.

method values
    arg dict;
    var list, x, k;
    
    // returns values same as dict_keys() returns keys.
    k = dict_keys(dict);
    list = [];
    for x in (k)
        list = [@list, dict[x]];
    return list;
.

method replace
    arg dict, key, value;
    
    dict = (> dict_del(dict, key) <);
    dict = (> dict_add(dict, key, value) <);
    return dict;
.

method invert
    arg dict;
    var inverted, x;
    
    // Invert a dict (keys<->values)
    inverted = #[];
    for x in (dict_keys(dict))
        inverted = dict_add(inverted, dict[x], x);
    return inverted;
.

method add_elem
    arg dict, key, elem;
    var value;
    
    // same as old dict_add_elem
    value = (| dict[key] |);
    if (type(value) != 'list && type(value) != 'error)
        throw(~type, "Value for key " + $parse.unparse(key) + " (" + $parse.unparse(value) + ") is not a list.");
    if (value)
        value = [@value, elem];
    else
        value = [elem];
    return dict_add(dict, key, value);
.

method del_elem
    arg dict, key, elem;
    var value;
    
    value = (| dict[key] |);
    if (type(value) != 'list && type(value) != 'error)
        throw(~type, "Value for key " + $parse.unparse(key) + " (" + $parse.unparse(value) + ") is not a list.");
    value = setremove(value, elem);
    if (!value)
        return dict_del(dict, key);
    return dict_add(dict, key, value);
.

method add
    arg [args];
    
    return (> dict_add(@args) <);
.

method del
    arg [args];
    
    return (> dict_del(@args) <);
.

method keys
    arg [args];
    
    return (> dict_keys(@args) <);
.

method contains
    arg [args];
    
    return (> dict_contains(@args) <);
.