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

version» Context lines:

pike.git/lib/modules/ADT.pmod/Table.pmod:10:   //! 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,   //! grouping, sorting etc.   //!   //! All column references are case insensitive. A column can be referred to by   //! its position (starting from zero). All operations are non-destructive. That   //! means that a new table object will be returned after, for example, a sort.      //! The table base-class.   class table { -  protected private mapping fieldmap; -  protected private array table, fields, types; +  protected mapping fieldmap; +  protected array table, fields, types;    -  protected private array|int remap(array|string|int cs, int|void forgive) +  protected array|int remap(array|string|int cs, int|void forgive)    {    array v = ({});    int ap = arrayp(cs);    if(!ap) cs = ({ cs });    foreach(cs, string|int f) -  if(zero_type(intp(f)?f:fieldmap[lower_case(f)])) { +  if(undefinedp(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];    }       this_program copy(array|void tab, array|void fie, array|void typ)    {    return this_program(tab||table,fie||fields,typ||types);
pike.git/lib/modules/ADT.pmod/Table.pmod:47:    }       //! This method returns a table object from a binary string    //! representation of a table, as returned by @[encode()].    object decode(string s)    {    mapping m = decode_value(s);    return copy(m->table, m->fields, m->types);    }    -  mixed cast(string type) +  protected mixed cast(string type)    {    switch(type) {    case "array":    return copy_value(table);    case "string":    return ASCII->encode(this);    } -  +  return UNDEFINED;    }       //! This method returns the column names for the table. The case used when    //! the table was created will be returned. -  array(string) _indices() +  protected array(string) _indices()    {    return copy_value(fields);    }       //! This method returns the contents of a table as a two dimensional array.    //! The format is an array of rows. Each row is an array of columns. -  array(array) _values() +  protected array(array) _values()    {    return copy_value(table);    }       //! This method returns the number of rows in the table. -  int _sizeof() +  protected int _sizeof()    {    return sizeof(table);    }       //! This method reverses the rows of the table and returns a    //! new table object. -  object reverse() +  protected this_program reverse()    {    return copy(predef::reverse(table), fields, types);    }       //! This method returns the contents of a given column as an array.    array col(int|string column)    {    return copy_value(predef::column(table, remap(column)));    }       //! This method returns the contents of a given row as an array.    array row(int row_number)    {    return copy_value(table[row_number]);    }       //! Same as @[col()]. -  array `[](int|string column) +  protected array `[](int|string column)    {    return col(column);    }       //! This method compares two tables. They are equal if the contents    //! of the tables and the column names are equal. The column name    //! comparison is case insensitive. -  int `==(object table) +  protected int(0..1) `==(object table)    {    return (equal(Array.map(fields, lower_case),    Array.map(indices(table), lower_case)) && -  equal(this_program::table, values(table))); +  equal(this::table, values(table)));    }       //! This method appends two tables. The table given as an argument will be    //! added at the bottom of the current table. Note, the column names must    //! be equal. The column name comparison is case insensitive. -  object append_bottom(object table) +  this_program append_bottom(object table)    {    if(!equal(Array.map(indices(table), lower_case),    Array.map(fields, lower_case)))    TABLE_ERR("Table fields are not equal."); -  return copy(this_program::table+values(table), fields, types); +  return copy(this::table+values(table), fields, types);    }       //! This method appends two tables. The table given as an argument will be    //! added on the right side of the current table. Note that the number of    //! rows in both tables must be equal. -  object append_right(object table) +  this_program append_right(object table)    { -  if(sizeof(table) != sizeof(this_program::table)) +  if(sizeof(table) != sizeof(this::table))    TABLE_ERR("Table sizes are not equal.");    array v = values(table); -  for(int r = 0; r < sizeof(this_program::table); r++) -  v[r] = this_program::table[r] + v[r]; +  for(int r = 0; r < sizeof(this::table); r++) +  v[r] = this::table[r] + v[r];    return copy(v, fields+indices(table), types+table->all_types());    }    -  protected private mixed op_col(function f, int|string c, mixed ... args) +  protected mixed op_col(function f, int|string c, mixed ... args)    {    c = remap(c);    mixed x = table[0][c];    for(int r = 1; r < sizeof(table); r++)    f(x, table[r][c], @args);    return x;    }       mixed sum_col(int|string c)    {
pike.git/lib/modules/ADT.pmod/Table.pmod:166:    {    return op_col(min, c);    }       mixed max_col(int|string c)    {    return op_col(max, c);    }       //! This method returns a new table object with the selected columns only. -  object select(int|string ... columns) +  this_program select(int|string ... columns)    {    array t = ({});    columns = remap(columns);    for(int r = 0; r < sizeof(table); r++)    t += ({ rows(table[r], columns) });    return copy(t, rows(fields, columns), rows(types, columns));    }       //! Like @[select()], but the given @[columns] will not be in the    //! resulting table. -  object remove(int|string ... columns) +  this_program remove(int|string ... columns)    {    return select(@remap(fields) - remap(columns, 1));    }       //! This method calls the function for each row. If the function    //! returns zero, the row will be thrown away. If the function    //! returns something non-zero, the row will be kept. The result    //! will be returned as a new table object. -  object where(array(int|string)|int|string columns, function f, +  this_program where(array(int|string)|int|string columns, function f,    mixed ... args)    {    array t = ({});    f = f || lambda(mixed x) { return x; };    columns = remap(arrayp(columns)?columns:({ columns }));    foreach(table, mixed row)    if(f(@rows(row, columns), @args))    t += ({ row });    return copy(t, fields, types);    }
pike.git/lib/modules/ADT.pmod/Table.pmod:272:    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);    }    -  protected private this_program _sort(int is_reversed, int|string ... cs) +  protected this_program _sort(int is_reversed, int|string ... cs)    {    if(!sizeof(cs))    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++)    {
pike.git/lib/modules/ADT.pmod/Table.pmod:307:    copy(t, fields, types)->sort(@cs[0..(sizeof(cs)-2)]);    }       //! This method sorts the table in ascendent order on one or several columns    //! and returns a new table object. The left most column is sorted last. Note    //! that the sort is stable.    //!    //! @seealso    //! @[rsort()]    //! -  object sort(int|string ... columns) +  this_program sort(int|string ... columns)    {    return _sort(0, @columns);    }       //! Like @[sort()], but in descending order.    object rsort(int|string ... columns)    {    return _sort(1, @columns);    }       //! This method truncates the table to the first @[n] rows and returns    //! a new object. -  object limit(int n) +  this_program limit(int n)    {    return copy(table[0..(n-1)], fields, types);    }       //! This method renames the column named @[from] to @[to] and    //! returns a new table object. Note that @[from] can be the column    //! position. -  object rename(string|int from, string to) +  this_program rename(string|int from, string to)    {    array a = copy_value(fields);    a[remap(from)] = to;    return copy(table, a, types);    }       //! This method gives the type for the given @[column].    //!    //! If a second argument is given, the old type will be replaced    //! with @[type]. The column type is only used when the table is displayed.
pike.git/lib/modules/ADT.pmod/Table.pmod:380:    //! @expr{"text"@} or @expr{"num"@} (numerical). Text columns are left    //! adjusted, whereas numerical columns are right adjusted. If a mapping    //! in the array is 0 (zero), it will be assumed to be a text column.    //! If @[column_types] is omitted, all columns will displayed as text.    //!    //! See @[ADT.Table.ASCII.encode()] on how to display a table.    //!    //! @seealso    //! @[ADT.Table.ASCII.encode()]    //! -  void create(array(array) table, array(string) column_names, +  protected void create(array(array) table, array(string) column_names,    array(mapping(string:string))|void column_types)    {    if(!arrayp(table))    TABLE_ERR("Table not array");    if(!arrayp(column_names))    TABLE_ERR("Fields not array");    if(sizeof(table) && sizeof(table[0]) != sizeof(column_names))    TABLE_ERR("Table and field sizes differ");    foreach(column_names, string s)    if(!stringp(s))    TABLE_ERR("Field name not string");    -  this_program::table = copy_value(table); +  this::table = copy_value(table);    fields = copy_value(column_names);    types = allocate(sizeof(column_names));       if(column_types)    for(int i = 0; i < sizeof(fields); i++)    if(!column_types[i] || mappingp(column_types[i]))    types[i] = copy_value(column_types[i]);    else    TABLE_ERR("Field type not mapping");       array(int) a = indices(allocate(sizeof(fields)));    fieldmap = mkmapping(Array.map(fields, lower_case), a);    }   }      object Separated = class { -  protected private string _string(mixed x) { return (string)x; } +  protected string _string(mixed x) { return (string)x; }       object decode(string s, void|mapping options)    {    string rowsep = options->rowsep||"\n";    string colsep = options->colsep||"\t";    array t = Array.map(s/rowsep, `/, colsep);    return table(t[1..], t[0], options->types);    }       mixed encode(object t, void|mapping options)