Annotation of parser3/src/include/pa_table.h, revision 1.66
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.66 ! moko 11: #define IDENT_PA_TABLE_H "$Id: pa_table.h,v 1.65 2015/07/28 14:42:44 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.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:
1.65 moko 61: /// sets @a column value
62: void put_item(size_t column, const String*);
63:
1.66 ! moko 64: /// removes current row
! 65: void remove_current();
! 66:
1.30 paf 67: /// @return item from @a column. '0' if no such column
1.52 paf 68: const String* item(const String& column) {
1.32 paf 69: int index=column_name2index(column, false);
1.30 paf 70: return index>=0?item(index):0;
1.24 paf 71: }
1.23 paf 72:
73: /// saves to text file
74: void save(bool nameless_save, const String& file_spec);
1.2 paf 75:
1.52 paf 76: template<typename I>
77: void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
78: if(!o.adjust(count()))
79: return;
80:
81: size_t saved_current=current();
82: size_t row=o.offset;
83: if(o.reverse) { // reverse
1.62 misha 84: for(size_t i=0; i<o.limit; i++) {
85: set_current(row-i);
1.52 paf 86: func(*this, info);
87: }
88: } else { // forward
89: for(size_t to=row+o.limit; row<to; row++) {
90: set_current(row);
91: func(*this, info);
92: }
93: }
94: set_current(saved_current);
95: }
96:
97: template<typename I>
1.54 paf 98: bool table_first_that(bool (*func)(Table& self, I info), I info, Action_options& o) {
1.52 paf 99: if(!o.adjust(count()))
100: return false;
101:
102: size_t saved_current=current();
103: size_t row=o.offset;
104: if(o.reverse) { // reverse
1.62 misha 105: for(size_t i=0; i<o.limit; i++) {
106: set_current(row-i);
1.52 paf 107:
108: if(func(*this, info))
109: return true;
110: }
111: } else { // forward
112: for(size_t to=row+o.limit; row<to; row++) {
113: set_current(row);
114:
115: if(func(*this, info))
116: return true;
117: }
118: }
119: set_current(saved_current);
120:
121: return false;
122: }
123:
124:
1.49 paf 125: bool locate(int column, const String& value, Action_options& options);
126: bool locate(const String& column, const String& value, Action_options& options);
1.24 paf 127:
1.12 paf 128: private:
1.21 paf 129:
1.52 paf 130: /// current row
131: size_t fcurrent;
1.1 paf 132:
1.52 paf 133: /// columns
134: columns_type fcolumns;
1.5 paf 135:
1.52 paf 136: /// column name->number lookup table
1.63 misha 137: typedef HashString<int> name2number_hash_class;
1.52 paf 138: name2number_hash_class* name2number;
1.5 paf 139:
1.52 paf 140: /// is that @c index falid?
1.55 paf 141: bool valid(size_t index) const { return index<count(); }
1.2 paf 142:
1.1 paf 143: };
144:
145: #endif
E-mail: