--- parser3/src/include/pa_array.h 2001/01/27 15:21:05 1.2 +++ parser3/src/include/pa_array.h 2012/03/16 09:24:08 1.81 @@ -1,107 +1,265 @@ -/* - $Id: pa_array.h,v 1.2 2001/01/27 15:21:05 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-2012 Art. Lebedev Studio (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -#include +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.81 2012/03/16 09:24:08 moko Exp $" + +// 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_types.h" + // allocated size + size_t fallocated; -class Pool; + // array size + size_t fused; -class Array { public: - typedef void *Item; + 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; + + return true; + } - enum { - CR_PREALLOCATED_COUNT=10, - CR_GROW_PERCENT=60 + }; -private: - friend Pool; + typedef T element_type; - // the pool I'm allocated on - Pool *pool; + inline Array(size_t initial=0): + fallocated(initial), + fused(0) + { + felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; + } - struct Chunk { - // the number of rows in chunk - int count; - union Row { - 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 - - // 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; - -private: - // last chank allocated count - int curr_chunk_rows; +#ifdef USE_DESTRUCTORS + inline ~Array(){ + if(felements) + pa_free(felements); + } +#endif - // array size - int fused_rows; + /// 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+fallocated/32 : 3); // 3 is PAF default, confirmed by tests - int cache_index; - Chunk::Row *cache_row; - int cache_countdown; - Chunk::Row *cache_link_row; - -private: - // new&constructors made private to enforce factory manufacturing at pool - void *operator new(size_t size, Pool *apool); + felements[fused++]=src; - void construct(Pool *apool, int initial_rows); - Array(Pool *apool) { - construct(apool, CR_PREALLOCATED_COUNT); + return *this; } - Array(Pool *apool, int initial_rows) { - construct(apool, initial_rows); + + /// 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(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 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