Annotation of parser3/src/main/pa_array.C, revision 1.3
1.1 paf 1: /*
1.3 ! paf 2: $Id: pa_array.C,v 1.2 2001/01/27 15:21:05 paf Exp $
1.1 paf 3: */
4:
5: #include <string.h>
6:
7: #include "pa_pool.h"
8:
9: void *Array::operator new(size_t size, Pool *apool) {
10: return apool->malloc(size);
11: }
12:
13: void Array::construct(Pool *apool, int initial_rows) {
14: pool=apool;
15: curr_chunk_rows=initial_rows;
16: head=static_cast<Chunk *>(
17: pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.3 ! paf 18: head->count=curr_chunk_rows;
1.1 paf 19: append_here=head->rows;
20: link_row=&head->rows[curr_chunk_rows];
21: link_row->link=0;
22: fused_rows=0;
1.2 paf 23:
1.3 ! paf 24: cache_chunk_base=0;
! 25: cache_chunk=head;
1.1 paf 26: }
27:
28: void Array::expand() {
29: curr_chunk_rows+=curr_chunk_rows*CR_GROW_PERCENT/100;
30: Chunk *chunk=static_cast<Chunk *>(
31: pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
32: chunk->count=curr_chunk_rows;
33: link_row->link=chunk;
34: append_here=chunk->rows;
35: link_row=&chunk->rows[curr_chunk_rows];
36: link_row->link=0;
37: }
38:
39:
40: Array& Array::operator += (Item src) {
41: if(chunk_is_full())
42: expand();
43:
44: append_here->item=src;
45: append_here++; fused_rows++;
46:
47: return *this;
48: }
49:
50: /*
51: char *Array::c_str() {
52: char *result=static_cast<char *>(pool->malloc(size()+1));
53:
54: char *copy_here=result;
55: Chunk *chunk=&head;
56: do {
57: Chunk::Row *row=chunk->rows;
58: for(int i=0; i<chunk->count; i++) {
59: if(row==append_here)
60: goto break2;
61:
62: memcpy(copy_here, row->item.ptr, row->item.size);
63: copy_here+=row->item.size;
64: row++;
65: }
66: chunk=row->link;
67: } while(chunk);
68: break2:
69: *copy_here=0;
70: return result;
71: }
1.2 paf 72: */
73: /*
74: void Array::put(int index, Item item) {
75: }
76:
77: Array::Item Array::get(int index) {
78: }
79: */
80:
81: Array::Item& Array::operator [] (int index) {
1.3 ! paf 82: if(!(index>=0 && index<size())) {
! 83: // FIX: some sort of thread-global error
! 84: Item *result=0;
! 85: return *result;
1.2 paf 86: }
87:
1.3 ! paf 88: // if they ask index to the left of cached position, forget cache
! 89: if(index<cache_chunk_base) {
! 90: cache_chunk_base=0;
! 91: cache_chunk=head;
! 92: }
! 93:
! 94: // navigate to chunk with "index" row
! 95: while(!(index>=cache_chunk_base && index<cache_chunk_base+cache_chunk->count)) {
! 96: int count=cache_chunk->count;
! 97: cache_chunk_base+=count;
! 98: cache_chunk=cache_chunk->rows[count].link;
! 99: }
! 100:
! 101: return cache_chunk->rows[index-cache_chunk_base].item;
1.2 paf 102: }
E-mail: