Annotation of parser3/src/main/pa_table.C, revision 1.53.2.10.2.3

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class.
                      3: 
1.53.2.8  paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com)
1.44      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.48      paf         6: */
1.15      paf         7: 
1.53.2.10.2.3! (paf        8:: static const char* IDENT_TABLE_C="$Date: 2003/03/19 16:28:09 $";
1.2       paf         9: 
1.1       paf        10: #include "pa_table.h"
1.53.2.10.2.1  (paf       11:: 
1.25      paf        12: #include "pa_exception.h"
1.1       paf        13: 
1.53.2.10.2.2  (paf       14:: Table::Table(columns_type acolumns, int initial_rows):
1.53.2.1  paf        15:        Array<element_type>(initial_rows),
1.13      paf        16: 
1.5       paf        17:        fcurrent(0),
                     18:        fcolumns(acolumns), 
1.53.2.10.2.2  (paf       19::       name2number(new name2number_hash_class) {
1.1       paf        20: 
1.53.2.10.2.3! (paf       21::       if(fcolumns) {
        !            22::               size_t fcolumns_count=fcolumns->count();
        !            23::               for(size_t i=0; i<fcolumns_count; i++) {
        !            24::                       const String& name=*fcolumns->unchecked_get(i);
1.53.2.1  paf        25:                        name2number->put(name, i+1);
1.1       paf        26:                }
1.53.2.10.2.3! (paf       27::       }
1.42      paf        28: }
                     29: 
1.53.2.1  paf        30: Table::Table(const Table& src, int offset, int limit) :
                     31:        Array<element_type>(limit/*may be more than needed, no harm done*/),
1.42      paf        32: 
1.51      paf        33:        fcurrent(0),
1.53.2.1  paf        34:        fcolumns(src.fcolumns),
                     35:        name2number(src.name2number) {
1.42      paf        36: 
1.53.2.1  paf        37:        append(src, offset, limit);
1.2       paf        38: }
                     39: 
1.53.2.10.2.1  (paf       40:: int Table::column_name2index(const String& column_name, bool bark) const {
1.53.2.10.2.2  (paf       41::       if(fcolumns) {// named
                     42::               int result=name2number->get(column_name)-1; // -1 = column not found
1.29      paf        43:                if(bark && result<0)
1.45      paf        44:                        throw Exception("parser.runtime",
1.53.2.10.2.2  (paf       45::                               &column_name,
1.29      paf        46:                                "column not found");
                     47:                return result;
1.53.2.2  paf        48:        } else // nameless
1.53.2.10.2.2  (paf       49::               return column_name.as_int();
1.21      paf        50: }
                     51: 
1.53.2.10.2.3! (paf       52:: const String* Table::item(size_t column) {
1.21      paf        53:        if(valid(fcurrent)) {
1.53.2.6  paf        54:                element_type row=get(fcurrent);
1.53.2.2  paf        55:                if(column>=0 && column<row->count()) // proper index?
1.53.2.10.2.3! (paf       56::                       return row->unchecked_get(column);
1.21      paf        57:        }
1.53.2.10.2.1  (paf       58::       return 0; // it's OK we don't have row|column, just return nothing
1.21      paf        59: }
                     60: 
1.53.2.10.2.3! (paf       61:: bool Table::locate(size_t column, const String& value) {
        !            62::       size_t scurrent=fcurrent;
        !            63::       size_t lcount=count();
        !            64::       for(fcurrent=0; fcurrent<lcount; fcurrent++) {
1.53.2.10.2.2  (paf       65::               const String* item_value=item(column);
                     66::               if(*item_value == value)
1.21      paf        67:                        return true;
1.2       paf        68:        }
                     69: 
1.31      parser     70:        fcurrent=scurrent;
1.21      paf        71:        return false;
1.28      paf        72: }
                     73: 
1.53.2.10.2.1  (paf       74:: bool Table::locate(const String& column, const String& value) {
1.29      paf        75:        return locate(column_name2index(column, true), value);
1.22      paf        76: }
                     77: 
1.41      paf        78: void Table::offset(bool absolute, int offset) {
1.53.2.10.2.3! (paf       79::       if(size_t lcount=count())
        !            80::               fcurrent=((absolute?0:fcurrent)+offset+lcount)%lcount;
1.1       paf        81: }

E-mail: