Annotation of parser3/src/include/pa_table.h, revision 1.48.2.11.2.9
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.9! (paf 11:: static const char* IDENT_TABLE_H="$Date: 2003/04/11 16:05:43 $";
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.9! (paf 55:: void set_current(size_t acurrent) {
! 56:: assert(acurrent<count());
! 57::
! 58:: fcurrent=acurrent;
! 59:: }
! 60:: /// @return current pointer
1.48.2.11.2.8 (paf 61:: size_t current() const { return fcurrent; }
1.37 paf 62: void offset(bool absolute, int offset);
1.5 paf 63:
1.48.2.11.2.7 (paf 64:: /** @return column index from @a column_name. '<0' if no such column
1.48.2.1 paf 65: if no such - 'bark'
1.32 paf 66: */
1.48.2.11.2.1 (paf 67:: int column_name2index(const String& column, bool bark) const;
1.30 paf 68:
1.26 paf 69: /// @return item from @a column
1.48.2.11.2.6 (paf 70:: const String* item(size_t column);
1.30 paf 71:
72: /// @return item from @a column. '0' if no such column
1.48.2.11.2.1 (paf 73:: const String* item(const String& column) {
1.32 paf 74: int index=column_name2index(column, false);
1.48.2.11.2.1 (paf 75:: return index>=0?item(index):0;
1.24 paf 76: }
1.23 paf 77:
78: /// saves to text file
1.48.2.11.2.1 (paf 79:: void save(bool nameless_save, const String& file_spec);
1.2 paf 80:
1.48.2.11.2.8 (paf 81:: template<typename I>
82:: bool locate(bool (*func)(Table& self, I* info), void* info, Action_options& o) {
83:: size_t count=this->count();
84:: if(!count || !o.limit)
85:: return false;
86:: size_t row=o.offset;
1.48.2.11.2.9! (paf 87:: if(row>=count)
! 88:: return false;
! 89:: // max(limit)
! 90:: size_t m=o.reverse?
! 91:: o.offset
! 92:: :count-o.offset;
! 93:: if(!m)
! 94:: return false;
! 95:: // fix limit
! 96:: if(o.limit==ARRAY_OPTION_LIMIT_ALL || o.limit>m)
! 97:: o.limit=m;
1.48.2.11.2.8 (paf 98::
99:: size_t saved_current=current();
100:: if(o.reverse) { // reverse
1.48.2.11.2.9! (paf 101:: for(size_t to=row-o.limit; row>=to; --row) {
1.48.2.11.2.8 (paf 102:: set_current(row);
103::
104:: if(func(*this, info))
105:: return true;
106:: }
107:: } else { // forward
1.48.2.11.2.9! (paf 108:: for(size_t to=row+o.limit; row<to; row++) {
1.48.2.11.2.8 (paf 109:: set_current(row);
110::
111:: if(func(*this, info))
112:: return true;
113:: }
114:: }
115:: set_current(saved_current);
116::
117:: return false;
118:: }
119::
120:: bool locate(int column, const String& value, Action_options& options);
121:: bool locate(const String& column, const String& value, Action_options& options);
1.27 paf 122:
1.12 paf 123: private:
1.21 paf 124:
1.5 paf 125: // current row
1.48.2.11.2.6 (paf 126:: size_t fcurrent;
1.5 paf 127:
128: // columns
1.48.2.4 paf 129: columns_type fcolumns;
1.48.2.1 paf 130:
131: // column names are already referenced in fcolumns, no need to doublecheck references here
132: // column name->number lookup table
1.48.2.11.2.5 (paf 133:: typedef Hash<const StringBody, int> name2number_hash_class;
1.48.2.11.2.1 (paf 134:: name2number_hash_class* name2number;
1.48.2.1 paf 135:
136: // we own columns?
1.5 paf 137:
1.48.2.11.2.6 (paf 138:: bool valid(size_t index) const { return index>=0 && index<count(); }
1.2 paf 139:
1.1 paf 140: };
141:
142: #endif
E-mail: