/
CDC-1.2b/
CDC-1.2b/src/
parent $libraries
object $dictionary

var $root child_index 0
var $root owners [$dictionary]
var $root fertile 0
var $root inited 1
var $root owned [$dictionary]
var $root manager $dictionary
var $root writable [$dictionary]
var $root readable ['parameters, 'methods, 'code]
var $root dbref 'dictionary

method map_method
    arg ls, what;
    var x, dict;
    
    // args[1] == list of objects
    // args[2] == symbol for method on objects
    // it will create a dictionary out of the two.
    dict = #[];
    
    // Get list's method(whatever) and add it to the dictionary
    for x in [1 .. listlen(ls)]
        dict = dict_add(dict, ls[x], (ls[x]).(what)());
    return dict;
.

method merge
    arg [args];
    var x, dict, z, tule, dz, axz, keys;
    
    // merges all dictionaries into a single one, if they have the same key's --
    // basing off of args[1] (this should be the longest list (i know, bad Lynx).
    dict = args[1];
    keys = dict_keys(args[1]);
    for x in [2 .. listlen(args)] {
        for z in (keys) {
            dz = dict[z];
            axz = (args[x])[z];
            if (type(dict[z]) == 'list)
                tule = dz;
            else
                tule = [dz];
            tule = [@tule, axz];
            dict = dict_add(dict, z, tule);
        }
    }
    return dict;
.

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 merge_to_list
    arg [args];
    var x, dict, z, tule, dz, axz, keys, list;
    
    // merges all dictionaries into a single list, where each related key
    // is merged with all it's other values as a sublist
    // basing off of args[1] (this should be the longest list (i know, bad Lynx).
    dict = .merge(@args);
    list = [];
    for z in (dict_keys(dict))
        list = [@list, dict[z]];
    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 apply
    arg tdict, list;
    var x;
    
    // Apply a translation-dict to a list
    for x in [1 .. listlen(list)] {
        catch ~keynf {
            list = replace(list, x, tdict[list[x]]);
        }
    }
    return list;
.

method apply_to_keys
    arg tdict, dict;
    var x, newdict;
    
    // Apply a t-dict to the keys of a dict
    newdict = #[];
    for x in (dict) {
        catch ~keynf {
            x = replace(x, 1, tdict[x[1]]);
        }
        newdict = dict_add(newdict, @x);
    }
    return newdict;
.

method apply_to_values
    arg tdict, dict;
    var x, newdict;
    
    // Apply a t-dict to the values of a dict
    newdict = #[];
    for x in (dict) {
        catch ~keynf {
            x = replace(x, 2, tdict[x[2]]);
        }
        newdict = dict_add(newdict, @x);
    }
    return newdict;
.

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 " + ($data.unparse(key))) + " (") + ($data.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 " + ($data.unparse(key))) + " (") + ($data.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) <);
.