Annotation of parser3/src/include/pa_array.h, revision 1.23
1.1 paf 1: /*
1.21 paf 2: Parser
3: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.22 paf 4: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
1.21 paf 5:
1.23 ! paf 6: $Id: pa_array.h,v 1.22 2001/03/11 08:16:32 paf Exp $
1.1 paf 7: */
8:
9: /*
10:
11: Array Chunk0
12: ====== ========
13: head--------------->[ptr]
14: append_here-------->[ptr]
15: link_row ........
16: . .
17: . [ptr]
18: ...........>[link to the next chunk]
19:
20: */
21:
22: #ifndef PA_ARRAY_H
23: #define PA_ARRAY_H
24:
25: #include <stddef.h>
26:
1.13 paf 27: #include "pa_pool.h"
1.1 paf 28: #include "pa_types.h"
1.11 paf 29: #include "pa_string.h"
1.1 paf 30:
1.13 paf 31: class Array : public Pooled {
1.1 paf 32: public:
1.7 paf 33:
1.11 paf 34: typedef void Item;
1.10 paf 35:
1.1 paf 36: enum {
1.7 paf 37: CR_INITIAL_ROWS_DEFAULT=10,
1.1 paf 38: CR_GROW_PERCENT=60
39: };
40:
1.6 paf 41: public:
42:
1.11 paf 43: Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT);
1.7 paf 44:
1.18 paf 45: int size() const {
46: // for get and quick_get
47: cache_chunk_base=0;
48: cache_chunk=head;
49: return fused_rows;
50: }
1.15 paf 51: Array& operator += (Item *src);
1.20 paf 52: // Array<const Item*> replacement
53: Array& operator += (const Item *src) { return *this+=const_cast<Item*>(src); }
1.19 paf 54: Array& append_array(const Array& src, int offset=0);
1.16 paf 55: Item *quick_get(int index) const {
1.14 paf 56: // considering these true:
57: // index increments from 0 to size()-1
58: // index>=0 && index<size()
59: // index>=cache_chunk_base
60:
61: // next chunk will be with "index" row
62: if(!(index<cache_chunk_base+cache_chunk->count)) {
63: int count=cache_chunk->count;
64: cache_chunk_base+=count;
65: cache_chunk=cache_chunk->rows[count].link;
66: }
67:
68: return cache_chunk->rows[index-cache_chunk_base].item;
69: }
70:
1.15 paf 71: Item *get(int index) const;
1.17 paf 72: void put(int index, Item *item);
1.12 paf 73: const String *get_string(int index) const {
74: return static_cast<const String *>(get(index));
75: }
1.1 paf 76:
1.7 paf 77: private:
78:
1.1 paf 79: struct Chunk {
80: // the number of rows in chunk
81: int count;
82: union Row {
1.15 paf 83: Item *item;
1.1 paf 84: Chunk *link; // link to the next chunk in chain
85: } rows[1];
86: // next rows are here
87: }
88: *head; // the head chunk of the chunk chain
89:
1.5 paf 90: // last allocated chunk
91: // helps appending Arrays
92: Chunk *tail;
93:
1.1 paf 94: // next append would write to this record
95: Chunk::Row *append_here;
96:
97: // the address of place where lies address
98: // of the link to the next chunk to allocate
99: Chunk::Row *link_row;
100:
101: private:
1.7 paf 102:
1.1 paf 103: // array size
104: int fused_rows;
105:
1.12 paf 106: mutable int cache_chunk_base;
107: mutable Chunk *cache_chunk;
1.2 paf 108:
1.1 paf 109: private:
110:
111: bool chunk_is_full() {
112: return append_here == link_row;
113: }
1.5 paf 114: void expand(int chunk_rows);
1.2 paf 115:
1.1 paf 116: private: //disabled
117:
1.11 paf 118: //Array(Array&) { }
1.12 paf 119: Array& operator = (const Array&) { return *this; }
1.1 paf 120: };
121:
122: #endif
E-mail: