Annotation of parser3/src/include/pa_table.h, revision 1.67

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class decl.
                      3: 
1.64      moko        4:        Copyright (c) 2001-2012 Art. Lebedev Studio (http://www.artlebedev.com)
1.41      paf         5:        Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.1       paf         6: */
                      7: 
                      8: #ifndef PA_TABLE_H
                      9: #define PA_TABLE_H
1.45      paf        10: 
1.67    ! moko       11: #define IDENT_PA_TABLE_H "$Id: pa_table.h,v 1.66 2015/07/28 21:23:38 moko Exp $"
1.1       paf        12: 
                     13: #include "pa_types.h"
                     14: #include "pa_hash.h"
1.2       paf        15: #include "pa_string.h"
1.1       paf        16: 
1.16      paf        17: /** 
1.19      paf        18:        VTable backend.
1.16      paf        19: 
                     20:        holds:
1.14      paf        21:        - column names[if any]
                     22:        - data rows
                     23:        - current row pointer
                     24: 
                     25:        uses String for column names and data items
                     26: 
1.28      paf        27:        hence most of tables are "named", no need to uptimize nameless onces.
                     28:        rows and strings stored are read-only. once stored they can be removed,
                     29:        but not altered. that's handy for quick copying & co. see table:join
1.14      paf        30: */
1.52      paf        31: class Table: public Array<ArrayString*> {
1.1       paf        32: public:
1.52      paf        33:        typedef ArrayString* columns_type;
1.2       paf        34: 
1.52      paf        35:        Table(
                     36:                columns_type acolumns,
                     37:                size_t initial_rows=3);
                     38:        Table(const Table& src, Action_options& options);
1.21      paf        39: 
1.42      paf        40:        /// gets column names
1.52      paf        41:        columns_type columns() { return fcolumns; }
1.42      paf        42: 
1.52      paf        43:        /// moves @a current pointer
                     44:        void set_current(size_t acurrent) {
                     45:                assert(acurrent==0 || acurrent<count());
1.5       paf        46: 
1.52      paf        47:                fcurrent=acurrent;
                     48:        }
1.50      paf        49:        /// @return current pointer
1.52      paf        50:        size_t current() const { return fcurrent; }
1.37      paf        51:        void offset(bool absolute, int offset);
1.5       paf        52: 
1.67    ! moko       53:        /// @return checks all rows to find maximum cells number
        !            54:        int max_cells() const;
        !            55: 
        !            56:        /** @return column index from @a column_name. '<0' if no such column
1.52      paf        57:                if no such - 'bark'
1.32      paf        58:        */
                     59:        int column_name2index(const String& column, bool bark) const;
1.30      paf        60: 
1.26      paf        61:        /// @return item from @a column
1.52      paf        62:        const String* item(size_t column);
1.30      paf        63: 
1.65      moko       64:        /// sets @a column value
                     65:        void put_item(size_t column, const String*);
                     66: 
1.66      moko       67:        /// removes current row
                     68:        void remove_current();
                     69: 
1.30      paf        70:        /// @return item from @a column. '0' if no such column
1.52      paf        71:        const String* item(const String& column) {
1.32      paf        72:                int index=column_name2index(column, false);
1.30      paf        73:                return index>=0?item(index):0;
1.24      paf        74:        }
1.23      paf        75: 
                     76:        /// saves to text file
                     77:        void save(bool nameless_save, const String& file_spec);
1.2       paf        78: 
1.52      paf        79:        template<typename I>
                     80:        void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
                     81:                if(!o.adjust(count()))
                     82:                        return;
                     83: 
                     84:                size_t saved_current=current();
                     85:                size_t row=o.offset;
                     86:                if(o.reverse) { // reverse
1.62      misha      87:                        for(size_t i=0; i<o.limit; i++) {
                     88:                                set_current(row-i);
1.52      paf        89:                                func(*this, info);
                     90:                        }
                     91:                } else { // forward
                     92:                        for(size_t to=row+o.limit; row<to; row++) {
                     93:                                set_current(row);
                     94:                                func(*this, info);
                     95:                        }
                     96:                }
                     97:                set_current(saved_current);
                     98:        }
                     99: 
                    100:        template<typename I>
1.54      paf       101:        bool table_first_that(bool (*func)(Table& self, I info), I info, Action_options& o) {
1.52      paf       102:                if(!o.adjust(count()))
                    103:                        return false;
                    104: 
                    105:                size_t saved_current=current();
                    106:                size_t row=o.offset;
                    107:                if(o.reverse) { // reverse
1.62      misha     108:                        for(size_t i=0; i<o.limit; i++) {
                    109:                                set_current(row-i);
1.52      paf       110: 
                    111:                                if(func(*this, info))
                    112:                                        return true;
                    113:                        }
                    114:                } else { // forward
                    115:                        for(size_t to=row+o.limit; row<to; row++) {
                    116:                                set_current(row);
                    117: 
                    118:                                if(func(*this, info))
                    119:                                        return true;
                    120:                        }
                    121:                }
                    122:                set_current(saved_current);
                    123: 
                    124:                return false;
                    125:        }
                    126: 
                    127: 
1.49      paf       128:        bool locate(int column, const String& value, Action_options& options);
                    129:        bool locate(const String& column, const String& value, Action_options& options);
1.24      paf       130: 
1.12      paf       131: private:
1.21      paf       132:        
1.52      paf       133:        /// current row
                    134:        size_t fcurrent;
1.1       paf       135: 
1.52      paf       136:        /// columns
                    137:        columns_type fcolumns;
1.5       paf       138: 
1.52      paf       139:        /// column name->number lookup table
1.63      misha     140:        typedef HashString<int> name2number_hash_class;
1.52      paf       141:        name2number_hash_class* name2number;
1.5       paf       142: 
1.52      paf       143:        /// is that @c index falid?
1.55      paf       144:        bool valid(size_t index) const { return index<count(); }
1.2       paf       145: 
1.1       paf       146: };
                    147: 
                    148: #endif

E-mail: