--- parser3/src/include/pa_array.h 2001/09/26 10:32:25 1.44 +++ parser3/src/include/pa_array.h 2003/02/26 10:17:30 1.57.2.26 @@ -1,190 +1,196 @@ /** @file - Parser: Array & Array_iter classes decls. + Parser: Array & Array_iterator classes decls. - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan (http://design.ru/paf) - - $Id: pa_array.h,v 1.44 2001/09/26 10:32:25 parser Exp $ + 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 -#include "pa_config_includes.h" -#include "pa_pool.h" -#include "pa_types.h" -#include "pa_string.h" - -class Array_iter; - -/** - Pooled Array. - - Internal structure: - @verbatim - Array Chunk0 - ====== ======== - head--------------->[ptr] - append_here-------->[ptr] - link_row ........ - . . - . [ptr] - ...........>[link to the next chunk] - @endverbatim -*/ +static const char* IDENT_ARRAY_H="$Date: 2003/02/26 10:17:30 $"; -class Array : public Pooled { - friend Array_iter; -public: +// includes + +#include "pa_memory.h" +#include "pa_exception.h" + +// forwards + +template class Array_iterator; + +/// Simple Array +template class Array: public PA_Object { + + friend class Array_iterator; - /// Array item type - typedef void Item; +protected: - /*/// for_each iterator function type, const info - typedef void (*For_each_func_const)(Item *value, const void *info); - */ - - /// for_each iterator function type - typedef void (*For_each_func)(Item *value, void *info); - - /// first_that iterator function type, const info - typedef void *(*Item_that_func_const)(Item *value, const void *info); - - /// first_that iterator function type - typedef void *(*Item_that_func)(Item *value, void *info); - - enum { - CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count - CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded() - }; + // 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(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT); + Array(int initial=3, int delta=1): + fallocated(initial?initial:3), + fdelta(delta), + fused(0) + { + if(fallocated<=0 || fdelta<1) + throw Exception(0, + Exception::undefined_source, + "Array::Array(%d, %d) too small", initial, delta); + + // 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 + + free(felements); + } + + /// how many items are in Array + int count() const { return fused; } + /// append to array + Array& operator += (T src) { + if(is_full()) + expand(fdelta); - /// size Array. how many items are in it - int size() const { return fused_rows; } - /// append Item to array - Array& operator += (Item *src); - /// append int value to array - Array& operator += (int value) { return *this+=reinterpret_cast(value); } + felements[fused++]=src; - /// dirty hack to allow constant items storage. I long for Array - Array& operator += (const Item *src) { return *this+=const_cast(src); } + return *this; + } /// append other Array portion to this one. starting from offset - Array& append_array(const Array& src, int offset=0); + Array& append(const Array& src, int offset=0, int limit=0) { + if(offset<0) { + throw Exception(0, + Exception::undefined_source, + "Array::append(offset=%d) out <0", offset); + //return Nothing; // never + } + // fix limit + { + int m=src.count()-offset; + if(!m || limit<0) + return *this; + if(!limit || limit>m) + limit=m; + } - Item *get(int index) const; - int get_int(int index) const { return reinterpret_cast(get(index)); } + 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 - const String *get_string(int index) const { - return const_cast(static_cast(get(index))); + return felements[index]; } - /*/// iterate over all elements, const info - void for_each(For_each_func_const func, const void *info=0) const; - /*/ + T& operator [](int index) const { return get(index); } - /// iterate over all elements - void for_each(For_each_func func, void *info=0) const; - - /// iterate over all elements until condition, const info - void* first_that(Item_that_func_const func, const void *info=0) const; + /// put index-element + void put(int index, T& element) { + if(!(index>=0 && index void for_each(void (*callback)(T, I), I info) const { + T *last=felements+fused; + for(T *current=felements; current T first_that(bool (*callback)(T, I), I info) const { + T *last=felements+fused; + for(T *current=felements; current class Array_iterator { + + Array& farray; + T *fcurrent; + T *flast; + public: - Array_iter(const Array& aarray) : array(aarray), - chunk(aarray.head), - row(chunk->rows), - countdown(chunk->count) { + Array_iterator(Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements+farray.count(); } /// there are still elements bool has_next() { - return !(chunk==array.tail && row==array.append_here); + return fcurrentlink; - row=chunk->rows; - countdown=chunk->count; - } - Array::Item *result=row->item; - row++; countdown--; - return result; + /// quickly extracts next Array element + T& next() { + return *(fcurrent++); } - /// quickly extracts next Array::Item as const String - const String *next_string() { - return const_cast(static_cast(next())); - } - -private: - const Array& array; - const Array::Chunk *chunk; - const Array::Chunk::Row *row; - int countdown; - }; #endif