--- parser3/src/include/pa_array.h 2001/03/26 09:53:42 1.31 +++ parser3/src/include/pa_array.h 2009/04/17 09:55:21 1.71 @@ -1,150 +1,248 @@ /** @file - Parser: array class decl. + 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.31 2001/03/26 09:53:42 paf Exp $ + Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -#include +static const char * const IDENT_ARRAY_Y="$Date: 2009/04/17 09:55:21 $"; -#include "pa_pool.h" -#include "pa_types.h" -#include "pa_string.h" - -/** - Pooled Array. - - Internal structure: - @verbatim - Array Chunk0 - ====== ======== - head--------------->[ptr] - append_here-------->[ptr] - link_row ........ - . . - . [ptr] - ...........>[link to the next chunk] - @endverbatim -*/ +// includes -class Array : public Pooled { -public: +#include "pa_memory.h" +#include "pa_exception.h" - /// Array item type - typedef void Item; +// forwards - /// for_each iterator function type - typedef void (*For_each_func)(Item *value, void *info); +template class Array_iterator; - /// first_that iterator function type - typedef bool (*First_that_func)(Item *value, const void *info); +// defines - enum { - CR_INITIAL_ROWS_DEFAULT=10, ///< default preallocated row count - CR_GROW_PERCENT=60 ///< each time the Array chunk_is_full() array expanded() - }; +#define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) + +/// Simple Array +template class Array: public PA_Object { + + friend class Array_iterator; + +protected: + + /// elements[growing size] here + T *felements; + + // allocated size + size_t fallocated; + + // array size + size_t fused; public: + struct Action_options { + size_t offset; + size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing' + bool reverse; + bool defined; + + Action_options( + size_t aoffset=0, + size_t alimit=ARRAY_OPTION_LIMIT_ALL, + bool areverse=false): + offset(aoffset), limit(alimit), reverse(areverse), + defined(false) {} + + bool adjust(size_t count) { + if(!count || !limit) + return false; + if(offset>=count) + return false; + // max(limit) + size_t m=reverse? + offset+1 + :count-offset; + if(!m) + return false; + // fix limit + if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) + limit=m; - Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT); + return true; + } + + + }; + + typedef T element_type; - /// size Array. how many items are in it - int size() const { - // for get and quick_get - cache_chunk_base=0; - cache_chunk=head; - return fused_rows; + inline Array(size_t initial=0): + fallocated(initial), + fused(0) + { + felements=fallocated?static_cast(malloc(fallocated*sizeof(T))):0; } - /// append Item to array - Array& operator += (Item *src); - /// dirty hack to allow constant items storage. I long for Array - Array& operator += (const Item *src) { return *this+=const_cast(src); } + /// how many items are in Array + inline size_t count() const { return fused; } + /// append to array + inline Array& operator+=(T src) { + if(is_full()) + expand(fallocated>0?2:3); // 3 is PAF default, confirmed by tests - /// append other Array portion to this one. starting from offset - Array& append_array(const Array& src, int offset=0); + felements[fused++]=src; - /** - quickly gets some item considering... + return *this; + } - these true: - - index increments from 0 to size()-1 - - index>=0 && index=cache_chunk_base - */ - Item *quick_get(int index) const { - // next chunk will be with "index" row - if(!(indexcount)) { - int count=cache_chunk->count; - cache_chunk_base+=count; - cache_chunk=cache_chunk->rows[count].link; + /// append other Array portion to this one. starting from offset + Array& append(const Array& src, + size_t offset=0, + size_t limit=ARRAY_OPTION_LIMIT_ALL, //< negative limit means 'all'. zero limit means 'nothing' + bool reverse=false) { + + size_t src_count=src.count(); + // skip tivials + if(!src_count || !limit || offset>=src_count) + return *this; + // max(limit) + size_t m=reverse? + 1+offset + :src_count-offset; + if(!m) + return *this; + // fix limit + if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) + limit=m; + + ssize_t delta=reverse? + (ssize_t)limit + :limit-(fallocated-fused); + if(delta>0) + expand(delta); + + T* from=&src.felements[offset]; + T* to=&felements[fused]; + if(reverse) { // reverse + for(T* from_end=from-limit; from>from_end; --from) + *to++=*from; + + } else { // forward + for(T* from_end=from+limit; fromrows[index-cache_chunk_base].item; + fused+=limit; + return *this; } - Item *get(int index) const; - void put(int index, Item *item); - /// convinient way to get strings from Array. I long for Array - const String *get_string(int index) const { - return const_cast(static_cast(get(index))); + /// get index-element + inline T get(size_t index) const { + assert(index void for_each(void (*callback)(T, I), I info) const { + T *last=felements+fused; + for(T *current=felements; current void for_each(bool (*callback)(T, I), I info) const { + T *last=felements+fused; + for(T *current=felements; current void for_each_ref(void (*callback)(T&, I), I info) { + 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(malloc(fallocated*sizeof(T))); + } } - void expand(int chunk_rows); private: //disabled - //Array(Array&) { } + Array(const Array&) {} Array& operator = (const Array&) { return *this; } }; + +/** Array iterator, usage: + @code + // Array a; + for(Array_iterator i(a); i.has_next(); ) { + T& element=i.next(); + ... + } + @endcode +*/ +template class Array_iterator { + + const Array& farray; + T *fcurrent; + T *flast; + +public: + + Array_iterator(const Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements+farray.count(); + } + + /// there are still elements + bool has_next() { + return fcurrent