Annotation of parser3/src/include/pa_table.h, revision 1.48.2.11.2.8
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class decl.
3:
1.48.2.9 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.48.2.11.2.8! (paf 11:: static const char* IDENT_TABLE_H="$Date: 2003/04/11 15:00:05 $";
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.48.2.11.2.4 (paf 31:: class Table: public Array<ArrayString*> {
1.1 paf 32: public:
1.48.2.11.2.3 (paf 33:: typedef ArrayString* columns_type;
1.1 paf 34:
1.48.2.11.2.8! (paf 35:: struct Action_options {
! 36:: size_t offset;
! 37:: size_t limit; //< negative limit means 'all'. zero limit means 'nothing'
! 38:: bool reverse;
! 39:: bool defined;
! 40::
! 41:: Action_options():
! 42:: offset(0), limit(ARRAY_OPTION_LIMIT_ALL), reverse(false),
! 43:: defined(false) {}
! 44:: };
! 45::
1.48.2.1 paf 46: Table(
1.48.2.4 paf 47: columns_type acolumns,
1.48.2.11.2.8! (paf 48:: size_t initial_rows=3);
! 49:: Table(const Table& src, Action_options& options);
1.2 paf 50:
1.42 paf 51: /// gets column names
1.48.2.4 paf 52: columns_type columns() { return fcolumns; }
1.5 paf 53:
1.18 paf 54: /// moves @a current pointer
1.48.2.11.2.8! (paf 55:: void set_current(size_t acurrent); /// @return current pointer
! 56:: size_t current() const { return fcurrent; }
1.37 paf 57: void offset(bool absolute, int offset);
1.5 paf 58:
1.48.2.11.2.7 (paf 59:: /** @return column index from @a column_name. '<0' if no such column
1.48.2.1 paf 60: if no such - 'bark'
1.32 paf 61: */
1.48.2.11.2.1 (paf 62:: int column_name2index(const String& column, bool bark) const;
1.30 paf 63:
1.26 paf 64: /// @return item from @a column
1.48.2.11.2.6 (paf 65:: const String* item(size_t column);
1.30 paf 66:
67: /// @return item from @a column. '0' if no such column
1.48.2.11.2.1 (paf 68:: const String* item(const String& column) {
1.32 paf 69: int index=column_name2index(column, false);
1.48.2.11.2.1 (paf 70:: return index>=0?item(index):0;
1.24 paf 71: }
1.23 paf 72:
73: /// saves to text file
1.48.2.11.2.1 (paf 74:: void save(bool nameless_save, const String& file_spec);
1.2 paf 75:
1.48.2.11.2.8! (paf 76:: template<typename I>
! 77:: bool locate(bool (*func)(Table& self, I* info), void* info, Action_options& o) {
! 78:: size_t count=this->count();
! 79:: if(!count || !o.limit)
! 80:: return false;
! 81:: if(o.limit==ARRAY_OPTION_LIMIT_ALL)
! 82:: o.limit=count;
! 83:: size_t row=o.offset;
! 84::
! 85:: size_t saved_current=current();
! 86:: if(o.reverse) { // reverse
! 87:: size_t to=row>o.limit?row-o.limit:0;
! 88:: for(; row>=to; --row) {
! 89:: set_current(row);
! 90::
! 91:: if(func(*this, info))
! 92:: return true;
! 93:: }
! 94:: } else { // forward
! 95:: size_t to=min(row+o.limit, count);
! 96:: for(; row<to; row++) {
! 97:: set_current(row);
! 98::
! 99:: if(func(*this, info))
! 100:: return true;
! 101:: }
! 102:: }
! 103:: set_current(saved_current);
! 104::
! 105:: return false;
! 106:: }
! 107::
! 108:: bool locate(int column, const String& value, Action_options& options);
! 109:: bool locate(const String& column, const String& value, Action_options& options);
1.27 paf 110:
1.12 paf 111: private:
1.21 paf 112:
1.5 paf 113: // current row
1.48.2.11.2.6 (paf 114:: size_t fcurrent;
1.5 paf 115:
116: // columns
1.48.2.4 paf 117: columns_type fcolumns;
1.48.2.1 paf 118:
119: // column names are already referenced in fcolumns, no need to doublecheck references here
120: // column name->number lookup table
1.48.2.11.2.5 (paf 121:: typedef Hash<const StringBody, int> name2number_hash_class;
1.48.2.11.2.1 (paf 122:: name2number_hash_class* name2number;
1.48.2.1 paf 123:
124: // we own columns?
1.5 paf 125:
1.48.2.11.2.6 (paf 126:: bool valid(size_t index) const { return index>=0 && index<count(); }
1.2 paf 127:
1.1 paf 128: };
129:
130: #endif
E-mail: