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.26 2003/04/07 17:19:41 nilsson Exp $ + // $Id: Table.pmod,v 1.27 2004/01/11 00:38:16 nilsson Exp $      #pike __REAL_VERSION__   #define TABLE_ERR(msg) error("(Table) "+msg+"\n")      //! ADT.Table is a generic module for manipulating tables.   //!   //! Each table contains one or several columns.   //! Each column is associated with a name, the column name.   //! Optionally, one can provide a column type. The Table module can do a number   //! of operations on a given table, like computing the sum of a column,
pike.git/lib/modules/ADT.pmod/Table.pmod:28:    if(!ap) cs = ({ cs });    foreach(cs, string|int f)    if(zero_type(intp(f)?f:fieldmap[lower_case(f)])) {    if(!forgive)    TABLE_ERR("Unknown field '"+f+"'");    } else    v += ({ intp(f)?f:fieldmap[lower_case(f)] });    return ap?v:v[0];    }    -  object copy(array|void tab, array|void fie, array|void typ) +  this_program copy(array|void tab, array|void fie, array|void typ)    { -  return object_program(this_object())(tab||table,fie||fields,typ||types); +  return this_program(tab||table,fie||fields,typ||types);    }       //! This method returns a binary string representation of the table. It is    //! useful when one wants to store a the table, for example in a file.    string encode()    {    return encode_value(([ "table":table,"fields":fields,"types":types ]));    }       //! This method returns a table object from a binary string
pike.git/lib/modules/ADT.pmod/Table.pmod:54:    mapping m = decode_value(s);    return copy(m->table, m->fields, m->types);    }       mixed cast(string type)    {    switch(type) {    case "array":    return copy_value(table);    case "string": -  return ASCII->encode(this_object()); +  return ASCII->encode(this);    }    }       //! This method returns the column names for the table. The case used when    //! the table was created will be returned.    array(string) _indices()    {    return copy_value(fields);    }   
pike.git/lib/modules/ADT.pmod/Table.pmod:202:    columns = remap(arrayp(columns)?columns:({ columns }));    foreach(table, mixed row)    if(f(@rows(row, columns), @args))    t += ({ row });    return copy(t, fields, types);    }       //! This method calls the function @[f] for each column each time a    //! non uniqe row will be joined. The table will be grouped by the    //! columns not listed. The result will be returned as a new table object. -  object group(mapping(int|string:function)|function f, mixed ... args) +  this_program group(mapping(int|string:function)|function f, mixed ... args)    {    if(!sizeof(table)) -  return this_object(); +  return this;       if(functionp(f)) {    if(!arrayp(args[0]))    args[0] = ({ args[0] });    f = mkmapping(args[0], allocate(sizeof(args[0]), f));    args = args[1..];    }       mapping m = ([]);    array cs = remap(indices(f));
pike.git/lib/modules/ADT.pmod/Table.pmod:231:    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);    }       //! This method sums all equal rows. The table will be grouped by the    //! columns not listed. The result will be returned as a new table object. -  object sum(int|string ... columns) +  this_program sum(int|string ... columns)    {    return group(`+, columns);    }       //! This method groups by the given columns and returns a table with only    //! unique rows. When no columns are given, all rows will be unique. A new    //! table object will be returned. -  object distinct(int|string ... columns) +  this_program distinct(int|string ... columns)    {    if(!sizeof(columns))    return sum();    array f = remap(fields) - remap(columns);    mapping m = mkmapping(f, Array.map(f, lambda(mixed unused)    { return lambda(mixed x1,    mixed x2)    { return x1; }; }));    return group(m);    }
pike.git/lib/modules/ADT.pmod/Table.pmod:273:    if(arrayp(v))    for(int i = 0; i < sizeof(v); i++)    t[r][columns[i]] = v[i];    else    t[r][columns[0]] = v;    }    }    return copy(t, fields, types);    }    -  static private object _sort(int is_reversed, int|string ... cs) +  static private this_program _sort(int is_reversed, int|string ... cs)    {    if(!sizeof(cs)) -  return this_object(); +  return this;    int c;    array t = copy_value(table);    if(!catch(c = remap(cs[-1])))    {    mapping m = ([]);    for(int r = 0; r < sizeof(t); r++)    {    mixed d;    if(!m[d = t[r][c]])    m[d] = ({ t[r] });