pike.git / lib / modules / ADT.pmod / Table.pmod

version» Context lines:

pike.git/lib/modules/ADT.pmod/Table.pmod:1:   // Table.pmod by Fredrik Noring, 1998 - // $Id: Table.pmod,v 1.3 1998/03/25 16:59:16 noring Exp $ + // $Id: Table.pmod,v 1.4 1998/05/09 21:30:22 noring Exp $      #define TABLE_ERR(msg) throw(({ "(Table) "+msg+"\n", backtrace() }))      class table {    static private mapping fieldmap;    static private array table, fields, types;       static private array|int remap(array|string|int cs, int|void forgive)    {    array v = ({});
pike.git/lib/modules/ADT.pmod/Table.pmod:156:    object filter(function f, array(int|string)|int|string cs, mixed ... args)    {    array t = ({});    cs = remap(arrayp(cs)?cs:({ cs }));    foreach(table, mixed row)    if(f(@rows(row, cs), @args))    t += ({ row });    return copy(t, fields, types);    }    -  object map(mapping(int|string:function) f, mixed ... args) +  object group(mapping(int|string:function)|function f, mixed ... args)    {    if(!sizeof(table))    return this_object();    -  +  if(functionp(f)) { +  f = mkmapping(args[0], allocate(sizeof(args[0]), f)); +  args = args[1..]; +  } +     mapping m = ([]);    array cs = remap(indices(f));    f = mkmapping(cs, values(f));    array(int) keys = indices(fields) - cs;    foreach(table, array row) {    string key = encode_value(rows(row, keys));    if(array a = m[key])    foreach(cs, int c)    a[c] = f[c](a[c], row[c], @args);    else    m[key] = copy_value(row);    }    return copy(values(m), fields, types);    }       object sum(int|string ... cs)    { -  array v = ({}); -  for(int i = 0; i < sizeof(cs); i++) -  v += ({ `+ }); -  return map(mkmapping(cs, v)); +  return group(`+, cs);    }       object distinct(int|string ... cs)    {    if(!sizeof(cs))    return sum();    array f = remap(fields) - remap(cs);    mapping m = mkmapping(f, Array.map(f, lambda()    { return lambda(mixed x1,    mixed x2)    { return x1; }; })); -  return map(m); +  return group(m);    }    -  object map_col(function f,array(int|string)|int|string cs,mixed ... args) +  object map(function f, array(int|string)|int|string cs, mixed ... args)    {    int ap = arrayp(cs);    array t = copy_value(table);    if(!catch(cs = remap(ap?cs:({ cs })))) {    for(int r = 0; r < sizeof(t); r++) {    mixed v = f(@rows(t[r], cs), @args);    if(arrayp(v))    for(int i = 0; i < sizeof(v); i++)    t[r][cs[i]] = v[i];    else
pike.git/lib/modules/ADT.pmod/Table.pmod:240:    t = v*({});    }    return copy(t, fields, types)->sort(@cs[0..(sizeof(cs)-2)]);    }       object rsort(int|string ... cs)    {    return sort(@cs)->reverse();    }    -  object truncate(int n) +  object limit(int n)    {    return copy(table[0..(n-1)], fields, types);    }    -  object rename(string from, string to) +  object rename(string|int from, string to)    { -  return copy(table, replace(fields, fields[remap(from)], to), types); +  array a = copy_value(fields); +  a[remap(from)] = to; +  return copy(table, a, types);    }       mapping type(int|string c, void|mapping m)    {    if(query_num_arg() == 2)    types[remap(c)] = copy_value(m);    return copy_value(types[remap(c)]);    }       void create(array(array) tab, array(string) fie, array|void typ)
pike.git/lib/modules/ADT.pmod/Table.pmod:322:       string encode(object t, void|mapping options)    {    options = options || ([]);    mapping sizes = ([]);    array fields = indices(t);    string indent = String.strmult(" ", options->indent);       t = t->copy(({ fields }) + values(t));    foreach(indices(fields), string field) -  t = (t->map_col(lambda(mixed m, string field, mapping sizes) +  t = (t->map(lambda(mixed m, string field, mapping sizes)    { m = (string)m;    sizes[field] = max(sizeof(m), sizes[field]);    return m; }, field, field, sizes)-> -  map_col(lambda(string s, string size, int num) +  map(lambda(string s, string size, int num)    { return sprintf("%"+(num?"":"-")+size+"s", s); },    field, (string)sizes[field],    (t->type(field)||([]))->type == "num"));       string l = (indent+"-"+    Array.map(values(sizes),    lambda(int n)    { return String.strmult("-", n); })*"---"+"-");    array table = values(t);    return (indent+" "+table[0]*" "+"\n"+l+"\n"+    Array.map(table[1..], lambda(array row, string indent)    { return indent+" "+row*" "; }, -  indent)*"\n"+"\n"+l+"\n"); +  indent)*"\n"+(sizeof(table)>1?"\n":"")+l+"\n");    }   }();      // Experimental   object SQL = class {    object decode(array t, void|mapping options)    {    // Yet to be done    return 0;    }