Annotation of parser3/src/include/pa_table.h, revision 1.78
1.14 paf 1: /** @file
1.15 paf 2: Parser: table class decl.
3:
1.74 moko 4: Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com)
1.73 moko 5: Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
1.1 paf 6: */
7:
8: #ifndef PA_TABLE_H
9: #define PA_TABLE_H
1.45 paf 10:
1.78 ! moko 11: #define IDENT_PA_TABLE_H "$Id: pa_table.h,v 1.77 2024/12/08 01:16:42 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.75 moko 17: class Temp_current;
18:
1.16 paf 19: /**
1.19 paf 20: VTable backend.
1.16 paf 21:
22: holds:
1.14 paf 23: - column names[if any]
24: - data rows
25: - current row pointer
26:
27: uses String for column names and data items
28:
1.28 paf 29: hence most of tables are "named", no need to uptimize nameless onces.
30: rows and strings stored are read-only. once stored they can be removed,
31: but not altered. that's handy for quick copying & co. see table:join
1.14 paf 32: */
1.52 paf 33: class Table: public Array<ArrayString*> {
1.1 paf 34: public:
1.52 paf 35: typedef ArrayString* columns_type;
1.2 paf 36:
1.72 moko 37: Table(columns_type acolumns, size_t initial_rows=3);
1.52 paf 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.78 ! moko 43: /// @return current pointer
! 44: size_t current() const { return fcurrent; }
! 45:
! 46: /// sets @a current pointer, can be out of range when restoring current in modified table
1.52 paf 47: void set_current(size_t acurrent) {
1.75 moko 48: fcurrent=acurrent<count() ? acurrent : count()>0 ? count()-1 : 0;
49: }
1.5 paf 50:
1.78 ! moko 51: /// sets or offsets @a current pointer, wrapping within the table
1.37 paf 52: void offset(bool absolute, int offset);
1.5 paf 53:
1.78 ! moko 54: /// is that @c index falid?
! 55: bool valid(size_t index) const { return index<count(); }
! 56:
1.67 moko 57: /// @return checks all rows to find maximum cells number
1.68 moko 58: size_t max_cells() const;
1.67 moko 59:
60: /** @return column index from @a column_name. '<0' if no such column
1.52 paf 61: if no such - 'bark'
1.32 paf 62: */
63: int column_name2index(const String& column, bool bark) const;
1.30 paf 64:
1.72 moko 65: void column_names_init();
66:
1.26 paf 67: /// @return item from @a column
1.52 paf 68: const String* item(size_t column);
1.30 paf 69:
1.65 moko 70: /// sets @a column value
71: void put_item(size_t column, const String*);
72:
1.66 moko 73: /// removes current row
74: void remove_current();
75:
1.30 paf 76: /// @return item from @a column. '0' if no such column
1.52 paf 77: const String* item(const String& column) {
1.32 paf 78: int index=column_name2index(column, false);
1.30 paf 79: return index>=0?item(index):0;
1.24 paf 80: }
1.23 paf 81:
82: /// saves to text file
83: void save(bool nameless_save, const String& file_spec);
1.2 paf 84:
1.52 paf 85: template<typename I>
1.77 moko 86: void table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o);
1.52 paf 87:
88: template<typename I>
1.54 paf 89: bool table_first_that(bool (*func)(Table& self, I info), I info, Action_options& o) {
1.52 paf 90: if(!o.adjust(count()))
91: return false;
92:
1.76 moko 93: size_t saved_current=current();
1.52 paf 94: size_t row=o.offset;
95: if(o.reverse) { // reverse
1.62 misha 96: for(size_t i=0; i<o.limit; i++) {
97: set_current(row-i);
1.52 paf 98:
99: if(func(*this, info))
100: return true;
101: }
102: } else { // forward
103: for(size_t to=row+o.limit; row<to; row++) {
104: set_current(row);
105:
106: if(func(*this, info))
107: return true;
108: }
109: }
110:
1.76 moko 111: set_current(saved_current);
1.52 paf 112: return false;
113: }
114:
1.49 paf 115: bool locate(int column, const String& value, Action_options& options);
116: bool locate(const String& column, const String& value, Action_options& options);
1.24 paf 117:
1.12 paf 118: private:
1.21 paf 119:
1.52 paf 120: /// current row
121: size_t fcurrent;
1.1 paf 122:
1.52 paf 123: /// columns
124: columns_type fcolumns;
1.5 paf 125:
1.52 paf 126: /// column name->number lookup table
1.63 misha 127: typedef HashString<int> name2number_hash_class;
1.52 paf 128: name2number_hash_class* name2number;
1.5 paf 129:
1.2 paf 130:
1.1 paf 131: };
132:
1.75 moko 133: /// Auto-object that temporarily saves and restores current
134: class Temp_current {
135: Table& ftable;
136: size_t fcurrent;
137: public:
138: Temp_current(Table& atable) : ftable(atable), fcurrent(atable.current()){}
139: ~Temp_current(){
140: ftable.set_current(fcurrent);
141: }
142: };
143:
1.77 moko 144: template<typename I> void Table::table_for_each(void (*func)(Table& self, I* info), I* info, Action_options& o) {
145: if(!o.adjust(count()))
146: return;
147:
148: Temp_current tc(*this);
149: size_t row=o.offset;
150: if(o.reverse) { // reverse
151: for(size_t i=0; i<o.limit; i++) {
152: set_current(row-i);
153: func(*this, info);
154: }
155: } else { // forward
156: for(size_t to=row+o.limit; row<to; row++) {
157: set_current(row);
158: func(*this, info);
159: }
160: }
161: }
162:
1.1 paf 163: #endif
E-mail: