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