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

1.14      paf         1: /** @file
1.15      paf         2:        Parser: table class decl.
                      3: 
1.52    ! paf         4:        Copyright (c) 2001-2003 ArtLebedev Group (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.52    ! paf        11: static const char* IDENT_TABLE_H="$Date: 2003/04/24 10:16:09 $";
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.52    ! paf        53:        /** @return column index from @a column_name. '<0' if no such column
        !            54:                if no such - 'bark'
1.32      paf        55:        */
                     56:        int column_name2index(const String& column, bool bark) const;
1.30      paf        57: 
1.26      paf        58:        /// @return item from @a column
1.52    ! paf        59:        const String* item(size_t column);
1.30      paf        60: 
                     61:        /// @return item from @a column. '0' if no such column
1.52    ! paf        62:        const String* item(const String& column) {
1.32      paf        63:                int index=column_name2index(column, false);
1.30      paf        64:                return index>=0?item(index):0;
1.24      paf        65:        }
1.23      paf        66: 
                     67:        /// saves to text file
                     68:        void save(bool nameless_save, const String& file_spec);
1.2       paf        69: 
1.52    ! paf        70:        template<typename I>
        !            71:        void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
        !            72:                if(!o.adjust(count()))
        !            73:                        return;
        !            74: 
        !            75:                size_t saved_current=current();
        !            76:                size_t row=o.offset;
        !            77:                if(o.reverse) { // reverse
        !            78:                        for(size_t to=row-o.limit; row>=to; --row) {
        !            79:                                set_current(row);
        !            80:                                func(*this, info);
        !            81:                        }
        !            82:                } else { // forward
        !            83:                        for(size_t to=row+o.limit; row<to; row++) {
        !            84:                                set_current(row);
        !            85:                                func(*this, info);
        !            86:                        }
        !            87:                }
        !            88:                set_current(saved_current);
        !            89:        }
        !            90: 
        !            91:        template<typename I>
        !            92:        bool table_first_that(bool (*func)(Table& self, I* info), void* info, Action_options& o) {
        !            93:                if(!o.adjust(count()))
        !            94:                        return false;
        !            95: 
        !            96:                size_t saved_current=current();
        !            97:                size_t row=o.offset;
        !            98:                if(o.reverse) { // reverse
        !            99:                        for(size_t to=row-o.limit; row>=to; --row) {
        !           100:                                set_current(row);
        !           101: 
        !           102:                                if(func(*this, info))
        !           103:                                        return true;
        !           104:                        }
        !           105:                } else { // forward
        !           106:                        for(size_t to=row+o.limit; row<to; row++) {
        !           107:                                set_current(row);
        !           108: 
        !           109:                                if(func(*this, info))
        !           110:                                        return true;
        !           111:                        }
        !           112:                }
        !           113:                set_current(saved_current);
        !           114: 
        !           115:                return false;
        !           116:        }
        !           117: 
        !           118: 
1.49      paf       119:        bool locate(int column, const String& value, Action_options& options);
                    120:        bool locate(const String& column, const String& value, Action_options& options);
1.24      paf       121: 
1.12      paf       122: private:
1.21      paf       123:        
1.52    ! paf       124:        /// current row
        !           125:        size_t fcurrent;
1.1       paf       126: 
1.52    ! paf       127:        /// columns
        !           128:        columns_type fcolumns;
1.5       paf       129: 
1.52    ! paf       130:        /// column name->number lookup table
        !           131:        typedef Hash<const StringBody, int> name2number_hash_class;
        !           132:        name2number_hash_class* name2number;
1.5       paf       133: 
1.52    ! paf       134:        /// is that @c index falid?
        !           135:        bool valid(size_t index) const { return index>=0 && index<count(); }
1.2       paf       136: 
1.1       paf       137: };
                    138: 
                    139: #endif

E-mail: