Annotation of parser3/src/include/pa_table.h, revision 1.48.2.11.2.11
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.1 (paf 11:): static const char* IDENT_TABLE_H="$Date: 2003/04/14 14:03:11 $";
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.1 paf 35: Table(
1.48.2.4 paf 36: columns_type acolumns,
1.48.2.11.2.8 (paf 37:: size_t initial_rows=3);
38:: Table(const Table& src, Action_options& options);
1.2 paf 39:
1.42 paf 40: /// gets column names
1.48.2.4 paf 41: columns_type columns() { return fcolumns; }
1.5 paf 42:
1.18 paf 43: /// moves @a current pointer
1.48.2.11.2.9 (paf 44:: void set_current(size_t acurrent) {
1.48.2.11.2.1 (paf 45:): assert(acurrent==0 || acurrent<count());
1.48.2.11.2.9 (paf 46::
47:: fcurrent=acurrent;
48:: }
49:: /// @return current pointer
1.48.2.11.2.8 (paf 50:: size_t current() const { return fcurrent; }
1.37 paf 51: void offset(bool absolute, int offset);
1.5 paf 52:
1.48.2.11.2.7 (paf 53:: /** @return column index from @a column_name. '<0' if no such column
1.48.2.1 paf 54: if no such - 'bark'
1.32 paf 55: */
1.48.2.11.2.1 (paf 56:: int column_name2index(const String& column, bool bark) const;
1.30 paf 57:
1.26 paf 58: /// @return item from @a column
1.48.2.11.2.6 (paf 59:: const String* item(size_t column);
1.30 paf 60:
61: /// @return item from @a column. '0' if no such column
1.48.2.11.2.1 (paf 62:: const String* item(const String& column) {
1.32 paf 63: int index=column_name2index(column, false);
1.48.2.11.2.1 (paf 64:: return index>=0?item(index):0;
1.24 paf 65: }
1.23 paf 66:
67: /// saves to text file
1.48.2.11.2.1 (paf 68:: void save(bool nameless_save, const String& file_spec);
1.2 paf 69:
1.48.2.11.2.8 (paf 70:: template<typename I>
1.48.2.11.2.1 (paf 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();
1.48.2.11.2.8 (paf 76:: size_t row=o.offset;
1.48.2.11.2.1 (paf 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()))
1.48.2.11.2.9 (paf 94:: return false;
1.48.2.11.2.8 (paf 95::
96:: size_t saved_current=current();
1.48.2.11.2.1 (paf 97:): size_t row=o.offset;
1.48.2.11.2.8 (paf 98:: if(o.reverse) { // reverse
1.48.2.11.2.9 (paf 99:: for(size_t to=row-o.limit; row>=to; --row) {
1.48.2.11.2.8 (paf 100:: set_current(row);
101::
102:: if(func(*this, info))
103:: return true;
104:: }
105:: } else { // forward
1.48.2.11.2.9 (paf 106:: for(size_t to=row+o.limit; row<to; row++) {
1.48.2.11.2.8 (paf 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:: }
1.48.2.11.2.1 (paf 117:):
1.48.2.11.2.8 (paf 118::
119:: bool locate(int column, const String& value, Action_options& options);
120:: bool locate(const String& column, const String& value, Action_options& options);
1.27 paf 121:
1.12 paf 122: private:
1.21 paf 123:
1.48.2.11.2.1 (paf 124:): /// current row
1.48.2.11.2.6 (paf 125:: size_t fcurrent;
1.5 paf 126:
1.48.2.11.2.1 (paf 127:): /// columns
1.48.2.4 paf 128: columns_type fcolumns;
1.48.2.1 paf 129:
1.48.2.11.2.1 (paf 130:): /// column name->number lookup table
1.48.2.11.2.5 (paf 131:: typedef Hash<const StringBody, int> name2number_hash_class;
1.48.2.11.2.1 (paf 132:: name2number_hash_class* name2number;
1.48.2.1 paf 133:
1.48.2.11.2.1 (paf 134:): /// is that @c index falid?
1.48.2.11.2.6 (paf 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: