--- parser3/src/include/pa_array.h 2001/03/10 16:34:34 1.21 +++ parser3/src/include/pa_array.h 2020/12/15 17:10:30 1.86 @@ -1,125 +1,268 @@ -/* - Parser - Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexander Petrosyan +/** @file + Parser: Array & Array_iterator classes decls. - $Id: pa_array.h,v 1.21 2001/03/10 16:34:34 paf Exp $ + Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com) + Author: Alexandr Petrosian (http://paf.design.ru) */ -/* +#ifndef PA_ARRAY_H +#define PA_ARRAY_H - Array Chunk0 - ====== ======== - head--------------->[ptr] - append_here-------->[ptr] - link_row ........ - . . - . [ptr] - ...........>[link to the next chunk] +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.86 2020/12/15 17:10:30 moko Exp $" -*/ +// includes -#ifndef PA_ARRAY_H -#define PA_ARRAY_H +#include "pa_memory.h" +#include "pa_exception.h" -#include +// forwards -#include "pa_pool.h" -#include "pa_types.h" -#include "pa_string.h" +template class Array_iterator; -class Array : public Pooled { -public: +// defines - typedef void Item; +#define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) - enum { - CR_INITIAL_ROWS_DEFAULT=10, - CR_GROW_PERCENT=60 - }; +/// Simple Array +template class Array: public PA_Object { -public: + friend class Array_iterator; - Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT); +protected: - int size() const { - // for get and quick_get - cache_chunk_base=0; - cache_chunk=head; - return fused_rows; - } - Array& operator += (Item *src); - // Array replacement - Array& operator += (const Item *src) { return *this+=const_cast(src); } - Array& append_array(const Array& src, int offset=0); - Item *quick_get(int index) const { - // considering these true: - // index increments from 0 to size()-1 - // index>=0 && index=cache_chunk_base - - // 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; + /// 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; + + return true; } + - return cache_chunk->rows[index-cache_chunk_base].item; + }; + + typedef T element_type; + + inline Array(size_t initial=0): + fallocated(initial), + fused(0) + { + felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; } - Item *get(int index) const; - void put(int index, Item *item); - const char *get_cstr(int index) const { - return static_cast(get(index)); +#ifdef USE_DESTRUCTORS + inline ~Array(){ + if(felements) + pa_free(felements); } - const String *get_string(int index) const { - return static_cast(get(index)); +#endif + + /// 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 + + felements[fused++]=src; + + return *this; } -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' + + size_t src_count=src.count(); + // skip tivials + if(!src_count || !limit || offset>=src_count) + return *this; + // max(limit) + size_t m=src_count-offset; + // fix limit + if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) + limit=m; + + ssize_t delta=limit-(fallocated-fused); + if(delta>0) + expand(delta); + + T* from=&src.felements[offset]; + T* to=&felements[fused]; + for(T* from_end=from+limit; from0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests - mutable int cache_chunk_base; - mutable Chunk *cache_chunk; - -private: + memmove(felements+index+1, felements+index, (fused-index) * sizeof(T)); - bool chunk_is_full() { - return append_here == link_row; + felements[index]=element; + fused++; + } + + /// remove index-element + inline void remove(size_t index) { + 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 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