--- parser3/src/main/Attic/pa_array.C 2001/01/29 09:57:22 1.5 +++ parser3/src/main/Attic/pa_array.C 2001/03/11 08:16:34 1.22 @@ -1,24 +1,24 @@ /* - $Id: pa_array.C,v 1.5 2001/01/29 09:57:22 paf Exp $ + Parser + Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexander Petrosyan (http://design.ru/paf) + + $Id: pa_array.C,v 1.22 2001/03/11 08:16:34 paf Exp $ */ #include #include "pa_pool.h" +#include "pa_array.h" +#include "pa_exception.h" -void *Array::operator new(size_t size, Pool *apool) { - return apool->malloc(size); -} - -void Array::construct(Pool *apool, int initial_rows) { - tail - 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) : + Pooled(apool) { + head=tail=static_cast( + 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; @@ -26,22 +26,23 @@ void Array::construct(Pool *apool, int i cache_chunk=head; } -void Array::expand() { - tail - 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) { + if(chunk_rows( + 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) { +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++; @@ -49,11 +50,11 @@ Array& Array::operator += (Item src) { return *this; } -Array::Item& Array::operator [] (int index) { +Array::Item *Array::get(int index) const { if(!(index>=0 && indexrows[index-cache_chunk_base].item; } -Array& Array::operator += (Array& src) { - int src_size=src.size(); +void Array::put(int index, Item *item) { + if(!(index>=0 && index=cache_chunk_base && indexcount)) { + int count=cache_chunk->count; + cache_chunk_base+=count; + cache_chunk=cache_chunk->rows[count].link; + } + + cache_chunk->rows[index-cache_chunk_base].item=item; +} + +Array& Array::append_array(const Array& src, int offset) { + int src_rows_to_copy=src.fused_rows-offset; int last_chunk_rows_left=link_row-append_here; // our last chunk too small for src to fit? - if(src_size>last_chunk_rows_left) { + if(src_rows_to_copy>last_chunk_rows_left) { // shrink last chunk to used rows tail->count-=last_chunk_rows_left; link_row=append_here; - // append new src_size-ed chunk - Chunk *chunk=static_cast( - pool->malloc(sizeof(int)+sizeof(Chunk::Row)*src_size+sizeof(Chunk *))); - chunk->count=src_size; - tail=link_row->link=chunk; - append_here=chunk->rows; - link_row=&chunk->rows[curr_chunk_rows]; - link_row->link=0; - } - - Chunk *src_chunk=src.head; - Chunk::Row *dest_rows=append_here; - int rows_left_to_copy=src_size; - 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 new src_used_rows-ed chunk + expand(src_rows_to_copy); + } + + Chunk *src_chunk=src.head; + Chunk::Row *dest_rows=append_here; + int rows_left_to_copy=src.fused_rows; + int rows_left_to_skip=offset; + 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 + int rows_to_copy_now=src_count-rows_left_to_skip; + if(rows_to_copy_now>0) + memcpy(dest_rows, src_chunk->rows+rows_left_to_skip, + sizeof(Chunk::Row)*rows_to_copy_now); + else + rows_left_to_skip-=src_count; + + dest_rows+=rows_to_copy_now; + 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 + int rows_to_copy_now=rows_left_to_copy-rows_left_to_skip; + if(rows_to_copy_now>0) + memcpy(dest_rows, src_chunk->rows+rows_left_to_skip, + sizeof(Chunk::Row)*rows_to_copy_now); + break; } - } else { } + append_here+=src_rows_to_copy; + fused_rows+=src_rows_to_copy; return *this; } - -void Array::remove(int index, int count=1) { -}