--- parser3/src/include/pa_array.h 2001/02/11 11:27:24 1.13 +++ parser3/src/include/pa_array.h 2005/08/09 08:14:49 1.65 @@ -1,97 +1,236 @@ -/* - $Id: pa_array.h,v 1.13 2001/02/11 11:27:24 paf Exp $ -*/ - -/* - - Array Chunk0 - ====== ======== - head--------------->[ptr] - append_here-------->[ptr] - link_row ........ - . . - . [ptr] - ...........>[link to the next chunk] +/** @file + Parser: Array & Array_iterator classes decls. + Copyright (c) 2001-2005 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: 2005/08/09 08:14:49 $"; + +// includes + +#include "pa_memory.h" +#include "pa_exception.h" + +// forwards + +template class Array_iterator; + +// defines + +#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; -#include "pa_pool.h" -#include "pa_types.h" -#include "pa_string.h" + // allocated size + size_t fallocated; + + // array size + size_t fused; -class Array : public Pooled { 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; + size_t row=offset; + if(row>=count) + return false; + // max(limit) + size_t m=reverse? + offset + :count-offset; + if(!m) + return false; + // fix limit + if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) + limit=m; - typedef void Item; + return true; + } - enum { - CR_INITIAL_ROWS_DEFAULT=10, - CR_GROW_PERCENT=60 + }; -public: + typedef T element_type; - Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT); + Array(size_t initial=3): + fallocated(initial>3?initial:3), + fused(0) + { + felements=static_cast(malloc(fallocated*sizeof(T))); + } - int size() const { return fused_rows; } - Array& operator += (const Item *src); - Array& append_array(const Array& src); - const Item *get(int index) const; - const char *get_cstr(int index) const { - return static_cast(get(index)); - } - const String *get_string(int index) const { - return static_cast(get(index)); - } - -private: - - struct Chunk { - // the number of rows in chunk - int count; - union Row { - const Item *item; - Chunk *link; // link to the next chunk in chain - } rows[1]; - // next rows are here - } - *head; // the head chunk of the chunk chain - - // last allocated chunk - // helps appending Arrays - Chunk *tail; - - // next append would write to this record - Chunk::Row *append_here; - - // the address of place where lies address - // of the link to the next chunk to allocate - Chunk::Row *link_row; + /// how many items are in Array + size_t count() const { return fused; } + /// append to array + Array& operator += (T src) { + if(is_full()) + expand(2); -private: + felements[fused++]=src; - // array size - int fused_rows; + return *this; + } - mutable int cache_chunk_base; - mutable Chunk *cache_chunk; - -private: + /// 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; from void for_each(void (*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 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