--- parser3/src/main/Attic/pa_array.C 2001/01/27 15:45:24 1.3 +++ parser3/src/main/Attic/pa_array.C 2001/01/29 15:56:04 1.7 @@ -1,5 +1,5 @@ /* - $Id: pa_array.C,v 1.3 2001/01/27 15:45:24 paf Exp $ + $Id: pa_array.C,v 1.7 2001/01/29 15:56:04 paf Exp $ */ #include @@ -10,14 +10,13 @@ void *Array::operator new(size_t size, P return apool->malloc(size); } -void Array::construct(Pool *apool, int initial_rows) { - pool=apool; - curr_chunk_rows=initial_rows; - head=static_cast( - pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); - head->count=curr_chunk_rows; +Array::Array(Pool *apool, int initial_rows) : + pool(apool) { + head=tail=static_cast( + pool->malloc(sizeof(int)+sizeof(Chunk::Row)*initial_rows+sizeof(Chunk *))); + head->count=initial_rows; append_here=head->rows; - link_row=&head->rows[curr_chunk_rows]; + link_row=&head->rows[initial_rows]; link_row->link=0; fused_rows=0; @@ -25,21 +24,20 @@ void Array::construct(Pool *apool, int i cache_chunk=head; } -void Array::expand() { - curr_chunk_rows+=curr_chunk_rows*CR_GROW_PERCENT/100; - Chunk *chunk=static_cast( - pool->malloc(sizeof(int)+sizeof(Chunk::Row)*curr_chunk_rows+sizeof(Chunk *))); - chunk->count=curr_chunk_rows; +void Array::expand(int chunk_rows) { + Chunk *chunk=tail=static_cast( + pool->malloc(sizeof(int)+sizeof(Chunk::Row)*chunk_rows+sizeof(Chunk *))); + chunk->count=chunk_rows; link_row->link=chunk; append_here=chunk->rows; - link_row=&chunk->rows[curr_chunk_rows]; + link_row=&chunk->rows[chunk_rows]; link_row->link=0; } Array& Array::operator += (Item src) { if(chunk_is_full()) - expand(); + expand(tail->count*CR_GROW_PERCENT/100); append_here->item=src; append_here++; fused_rows++; @@ -47,37 +45,6 @@ Array& Array::operator += (Item src) { return *this; } -/* -char *Array::c_str() { - char *result=static_cast(pool->malloc(size()+1)); - - char *copy_here=result; - Chunk *chunk=&head; - do { - Chunk::Row *row=chunk->rows; - for(int i=0; icount; i++) { - if(row==append_here) - goto break2; - - memcpy(copy_here, row->item.ptr, row->item.size); - copy_here+=row->item.size; - row++; - } - chunk=row->link; - } while(chunk); -break2: - *copy_here=0; - return result; -} -*/ -/* -void Array::put(int index, Item item) { -} - -Array::Item Array::get(int index) { -} -*/ - Array::Item& Array::operator [] (int index) { if(!(index>=0 && indexrows[index-cache_chunk_base].item; -} \ No newline at end of file +} + +Array& Array::operator += (Array& src) { + int src_used_rows=src.fused_rows; + int last_chunk_rows_left=link_row-append_here; + + // our last chunk too small for src to fit? + if(src_used_rows>last_chunk_rows_left) { + // shrink last chunk to used rows + tail->count-=last_chunk_rows_left; + link_row=append_here; + + // append new src_used_rows-ed chunk + expand(src_used_rows); + } + + Chunk *src_chunk=src.head; + Chunk::Row *dest_rows=append_here; + int rows_left_to_copy=src_used_rows; + while(true) { + int src_count=src_chunk->count; + Chunk *next_chunk=src_chunk->rows[src_count].link; + if(next_chunk) { + // not last source chunk + // taking it all + memcpy(dest_rows, src_chunk->rows, sizeof(Chunk::Row)*src_count); + dest_rows+=src_count; + rows_left_to_copy-=src_count; + + src_chunk=next_chunk; + } else { + // the last source chunk + // taking only those rows of chunk that _left_to_copy + memcpy(dest_rows, src_chunk->rows, sizeof(Chunk::Row)*rows_left_to_copy); + break; + } + } + append_here+=src_used_rows; + fused_rows+=src_used_rows; + + return *this; +}