Annotation of parser3/src/main/pa_array.C, revision 1.5
1.1 paf 1: /*
1.5 ! paf 2: $Id: pa_array.C,v 1.4 2001/01/29 09:38:33 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) {
1.5 ! paf 14: tail
1.1 paf 15: pool=apool;
16: curr_chunk_rows=initial_rows;
17: head=static_cast<Chunk *>(
18: pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
1.3 paf 19: head->count=curr_chunk_rows;
1.1 paf 20: append_here=head->rows;
21: link_row=&head->rows[curr_chunk_rows];
22: link_row->link=0;
23: fused_rows=0;
1.2 paf 24:
1.3 paf 25: cache_chunk_base=0;
26: cache_chunk=head;
1.1 paf 27: }
28:
29: void Array::expand() {
1.5 ! paf 30: tail
1.1 paf 31: curr_chunk_rows+=curr_chunk_rows*CR_GROW_PERCENT/100;
32: Chunk *chunk=static_cast<Chunk *>(
33: pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *)));
34: chunk->count=curr_chunk_rows;
35: link_row->link=chunk;
36: append_here=chunk->rows;
37: link_row=&chunk->rows[curr_chunk_rows];
38: link_row->link=0;
39: }
40:
41:
42: Array& Array::operator += (Item src) {
43: if(chunk_is_full())
44: expand();
45:
46: append_here->item=src;
47: append_here++; fused_rows++;
48:
49: return *this;
50: }
51:
1.2 paf 52: Array::Item& Array::operator [] (int index) {
1.3 paf 53: if(!(index>=0 && index<size())) {
54: // FIX: some sort of thread-global error
55: Item *result=0;
56: return *result;
1.2 paf 57: }
58:
1.3 paf 59: // if they ask index to the left of cached position, forget cache
60: if(index<cache_chunk_base) {
61: cache_chunk_base=0;
62: cache_chunk=head;
63: }
64:
65: // navigate to chunk with "index" row
66: while(!(index>=cache_chunk_base && index<cache_chunk_base+cache_chunk->count)) {
67: int count=cache_chunk->count;
68: cache_chunk_base+=count;
69: cache_chunk=cache_chunk->rows[count].link;
70: }
71:
72: return cache_chunk->rows[index-cache_chunk_base].item;
1.4 paf 73: }
74:
75: Array& Array::operator += (Array& src) {
76: int src_size=src.size();
77: int last_chunk_rows_left=link_row-append_here;
78:
1.5 ! paf 79: // our last chunk too small for src to fit?
! 80: if(src_size>last_chunk_rows_left) {
! 81: // shrink last chunk to used rows
! 82: tail->count-=last_chunk_rows_left;
! 83: link_row=append_here;
! 84:
! 85: // append new src_size-ed chunk
! 86: Chunk *chunk=static_cast<Chunk *>(
! 87: pool->malloc(sizeof(int)+sizeof(Chunk::Row)*src_size+sizeof(Chunk *)));
! 88: chunk->count=src_size;
! 89: tail=link_row->link=chunk;
! 90: append_here=chunk->rows;
! 91: link_row=&chunk->rows[curr_chunk_rows];
! 92: link_row->link=0;
! 93: }
! 94:
1.4 paf 95: Chunk *src_chunk=src.head;
96: Chunk::Row *dest_rows=append_here;
97: int rows_left_to_copy=src_size;
98: while(true) {
99: int src_count=src_chunk->count;
100: Chunk *next_chunk=src_chunk->rows[src_count].link;
101: if(next_chunk) {
102: // not last source chunk
103: // taking it all
104: memcpy(dest_rows, src_chunk->rows, sizeof(Chunk::Row)*src_count);
105: dest_rows+=src_count;
106: rows_left_to_copy-=src_count;
107:
108: src_chunk=next_chunk;
109: } else {
110: // the last source chunk
111: // taking only those rows of chunk that _left_to_copy
112: memcpy(dest_rows, src_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy);
113: break;
114: }
115: }
116: } else {
117: }
118:
119: return *this;
120: }
121:
122: void Array::remove(int index, int count=1) {
123: }
E-mail: