--- parser3/src/include/pa_array.h 2003/01/24 11:33:02 1.57.2.7 +++ parser3/src/include/pa_array.h 2003/03/08 12:22:06 1.57.2.27 @@ -1,52 +1,67 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) + Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_Y="$Date: 2003/01/24 11:33:02 $"; +static const char* IDENT_ARRAY_H="$Date: 2003/03/08 12:22:06 $"; -#include "pa_pool.h" +// includes + +#include "pa_memory.h" #include "pa_exception.h" -template class Array_iterator; +// forwards -/** - Simple Array. +template class Array_iterator; -*/ +/// Simple Array template class Array: public PA_Object { - friend class Array_iterator; + friend class Array_iterator; + +protected: + + // default expand delta size + int fdelta; + + /// elements[growing size] here + T *felements; // allocated size int fallocated; + // array size + int fused; + public: typedef T element_type; Array(int initial=3, int delta=1): - fallocated(initial?initial:3), + fallocated(initial>3?initial:3), fdelta(delta), fused(0) { - if(fallocated<=0 || fdelta<1) + if(fdelta<1) throw Exception(0, Exception::undefined_source, - "Array::Array(%d, %d) too small", initial, delta); + "Array::Array(delta=%d) too small", delta); - felements=new T[fallocated]; + // we can't use new(0) here[like we did in Hash] because we need to use realloc + // on MSVC new returns NOT pointer learned from pa_malloc, so we can't do new+realloc + felements=static_cast(calloc(fallocated*sizeof(T))); } override ~Array() { + // see comment in ctor T *last=felements+fused; for(T *current=felements; current~T(); // manually invoking destructors - delete felements; + free(felements); } /// how many items are in Array @@ -63,11 +78,11 @@ public: /// append other Array portion to this one. starting from offset Array& append(const Array& src, int offset=0, int limit=0) { - if(!(offset>=0 && offset0) - expand(needed); - - memcpy(&felements[fused+=limit], &src.felements[offset], limit*sizeof(T)); + int delta=limit-(fallocated-fused); + if(delta>0) + expand(delta); + + T* from=&src.felements[offset]; + T* to=&felements[fused]; + T* from_end=from+limit; + while(from=0 && index=0 && index=0 && index CharPtr; - -class Pool: public Array { -public: - char *malloc(size_t size) { - CharPtr result=CharPtr(new char[size]); - *this += result; - return result.get(); - } -}; - -void *operator new(size_t size, Pool& pool) { - return pool.malloc(size); -} /// handy array iterator template class Array_iterator { - const Array& farray; + Array& farray; T *fcurrent; T *flast; public: - Array_iterator(const Array& aarray) : farray(aarray) { - fcurrent(farray.felements); - flast=farray+farray.count(); + Array_iterator(Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements+farray.count(); } /// there are still elements @@ -186,8 +187,8 @@ public: } /// quickly extracts next Array element - T next() { - return *(furrent++); + T& next() { + return *(fcurrent++); } };