/////////////////////////////////////////////////////////////////////////////// version="version methods.lib 4.1.2.0 Feb_2019 "; //$Id$ category="Miscellaneous"; info=" LIBRARY: methods.lib installing methods in Singular AUTHORS: J. Boehm, boehm @ mathematik.uni-kl.de OVERVIEW: Methods select the function to execute by the types of the input tuple. The central function is installMethod, which takes a hashtable associating a tuple of input types to function names and creates a corresponding procedure. HashTables are lists with arbitrary index sets. They can be created by the command hashTable. Their size can be determined by the command size. Values can be extracted by selectKey or the * operator. HashTables can also be added using addHashTables or the + operator. Methods can be added with the + operator. TYPES: Method the class of all methods HashTable the class of all hash tables KEYWORDS: methods, hashtables PROCEDURES: installMethod install a method method make a method hashTable make a HashTable selectKey select a key in a HashTable, can also be used by * addHashTables add two HashTables, can also be used by + "; proc mod_init() { newstruct("HashTable","list keys, list values"); system("install","HashTable","print",printHashTable,1); system("install","HashTable","size",sizeHashTable,1); system("install","HashTable","*",selectKey,2); system("install","HashTable","+",addHashTables,2); newstruct("Method","HashTable functionClosures"); system("install","Method","print",printMethod,1); system("install","Method","+",addMethods,2); } proc hashTable(list k, list v) "USAGE: hashTable(k, v); k,v,lists RETURN: makes a HashTable with keys k and values v EXAMPLE: example hashTable, shows an example " { HashTable H; H.keys = k; H.values = v; return(H); } example { "EXAMPLE:"; echo=2; HashTable H = hashTable(list("a","b","c"),list("aa","bb","cc")); H; } proc printHashTable(HashTable H) { for (int j=1; j<=size(H); j++) { print(string((H.keys)[j])+" => "+string((H.values)[j])); } } proc sizeHashTable(HashTable H) { return(size(H.keys)); } proc selectKey(HashTable H, def k) "USAGE: selectKey(H, k); H HashTable, k key RETURN: returns the k-th value of H EXAMPLE: example selectKey, shows an example " { for (int j=1; j<=size(H); j++) { if ((H.keys)[j]==k){return((H.values)[j]);} } ERROR("key not found"); } example { "EXAMPLE:"; echo=2; HashTable H = hashTable(list("a","b","c"),list("aa","bb","cc")); selectKey(H,"b"); selectKey(H,"d"); H*"b"; } proc addHashTables(HashTable H, HashTable W) "USAGE: addHashTables(H, L); H HashTable, L HashTable RETURN: joins two hash tables with disjoint key sets THEORY: At the moment, the function does not check whether the key sets are disjoint. EXAMPLE: example addHashTables, shows an example " { HashTable D; D.keys = (H.keys)+(W.keys); D.values = (H.values)+(W.values); return(D); } example { "EXAMPLE:"; echo=2; HashTable H = hashTable(list("a","b","c"),list("aa","bb","cc")); HashTable W = hashTable(list("d","e","f"),list("aa","bb","cc")); addHashTables(H,W); H+W; } proc method(HashTable #) { Method M; //typeof(#); //if (typeof(#)=="HashTable"){ M.functionClosures = #; //} else //{ // HashTable H; // H.keys=list(#[1]); //H.values=list(#[2]); //M.functionClosures = H; //} return(M); } proc printMethod(Method f) { print(f.functionClosures); } proc addMethods(Method f, Method g) { Method M; HashTable H = (f.functionClosures)+(g.functionClosures); M.functionClosures = H; return(M); } proc installMethod(Method M,string fname) "USAGE: installMethod(M, st); M Method, st string RETURN: installs a method calling procedures according to M EXAMPLE: example installMethod, shows an example " { HashTable H = M.functionClosures; //string fname = nameof(M); //fname=fname[1..size(fname)-1]; string s = "proc "+fname+"() {"; string ff; list v = H.values; list k = H.keys; string ki; int i,j,l; for (i=1; i<=size(H); i++){ ff = v[i]; ki=""; for (j=1; j