--- parser3/src/include/pa_array.h 2003/01/23 15:38:05 1.57.2.3 +++ parser3/src/include/pa_array.h 2024/10/04 05:12:05 1.96 @@ -1,153 +1,297 @@ /** @file Parser: Array & Array_iterator classes decls. - Copyright (c) 2001, 2003 ArtLebedev Group (http://www.artlebedev.com) - Author: Alexandr Petrosian (http://paf.design.ru) + Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) + Authors: Konstantin Morshnev , Alexandr Petrosian */ #ifndef PA_ARRAY_H #define PA_ARRAY_H -static const char* IDENT_ARRAY_Y="$Date: 2003/01/23 15:38:05 $"; +#define IDENT_PA_ARRAY_H "$Id: pa_array.h,v 1.96 2024/10/04 05:12:05 moko Exp $" -#include "pa_pool.h" -//#include "pa_types.h" +// includes + +#include "pa_memory.h" +#include "pa_types.h" #include "pa_exception.h" +// forwards + template class Array_iterator; +template class Array_reverse_iterator; -/** - Simple Array. +// defines -*/ +#define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) + +/// Simple Array template class Array: public PA_Object { - friend class Array_iterator; + friend class Array_iterator; + friend class Array_reverse_iterator; + +protected: + + /// elements[growing size] here + T *felements; // allocated size - int fallocated; + size_t fallocated; + + // array size + size_t fsize; public: + typedef Array_iterator Iterator; + typedef Array_reverse_iterator ReverseIterator; + + 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>m) + limit=m; + + return true; + } + }; + typedef T element_type; - Array(int initial=3, int delta=1): - fallocated(initial?initial:3), - fdelta(delta), - fused(0) + inline Array(size_t initial=0): + fallocated(initial), + fsize(0) { - if(fallocated<=0 || fdelta<1) - throw Exception(0, 0, - "Array::Array(%d, %d) too small", initial, delta); - - felements=new T[fallocated]; + felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; } - override ~Array() { - T *last=felements+fused; - for(T *current=felements; current=0 && offset=src_count) + return *this; + // max(limit) + size_t m=src_count-offset; // fix limit - { - int m=src.count()-offset; - if(!m || limit<0) - return *this; - if(!limit || limit>m) - limit=m; - } + if(limit>m) + limit=m; - int needed=limit-(fallocated-fused); - if(needed>0) - expand(needed); + fit(fsize-1+limit); - memcpy(&felements[fused+=limit], &src.felements[offset], limit*sizeof(T)); + T* from=&src.felements[offset]; + T* to=&felements[fsize]; + for(T* from_end=from+limit; from=0 && index=0 && index void for_each(void (*callback)(T, I), I info) const { + T *last=felements+fsize; + for(T *current=felements; current void for_each(bool (*callback)(T, I), I info) const { + T *last=felements+fsize; + for(T *current=felements; current void for_each(void callback(T, I), I info) const { - T *last=felements+fused; + template void for_each_ref(void (*callback)(T&, I), I info) { + T *last=felements+fsize; for(T *current=felements; current T *first_that(bool callback(T, I), I info) const { - T *last=felements+fused; + template T first_that(bool (*callback)(T, I), I info) const { + T *last=felements+fsize; for(T *current=felements; current0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests + } - bool is_full() { - return fused == fallocated; + inline void fit(size_t index){ + if(index >= fallocated) + resize(max(this->fallocated + this->fallocated/4, index+1)); } - void expand(int delta) { - fallocated+=delta; - felements = (T *)pa_realloc(felements, fallocated*sizeof(T)); + + void resize(size_t asize) { + if(fallocated){ + felements=(T *)pa_realloc(felements, asize*sizeof(T)); + fallocated=asize; + } else { + fallocated=asize; + felements=(T *)pa_malloc(asize*sizeof(T)); + } } private: //disabled + Array(const Array&) {} Array& operator = (const Array&) { return *this; } }; -/// handy array iterator + +/// Commonly used, templated to work with any integer type + +template char* pa_itoa(T n, T base=10){ + char buf[MAX_NUMBER + 1]; + char* pos=buf + MAX_NUMBER; + *pos='\0'; + + bool negative=n < 0; + if (n < 0){ + n=-n; + } + + do { + *(--pos)=(n % base) + '0'; + n/=base; + } while (n > 0); + + if (negative) { + *(--pos) = '-'; + } + return pa_strdup(pos, buf + MAX_NUMBER - pos); +} + +template char* pa_uitoa(T n, T base=10){ + char buf[MAX_NUMBER + 1]; + char* pos=buf + MAX_NUMBER; + *pos='\0'; + + do { + *(--pos)=(n % base) + '0'; + n/=base; + } while (n > 0); + + return pa_strdup(pos, buf + MAX_NUMBER - pos); +} + + +/** Array iterator, usage: + @code + // Array a; + for(Array_iterator i(a); i; ) { + T& element=i.next(); + ... + } + @endcode +*/ template class Array_iterator { const Array& farray; @@ -156,21 +300,66 @@ template class Array_iterato public: - Array_iterator(const Array& aarray) : farray(aarray) { - fcurrent(farray.felements); - flast=farray+farray.count(); + Array_iterator(const Array& aarray): farray(aarray) { + fcurrent=farray.felements; + flast=farray.felements + farray.fsize; } /// there are still elements - bool has_next() { - return fcurrent class Array_reverse_iterator { + + const Array& farray; + T *fcurrent; + +public: + + Array_reverse_iterator(const Array& aarray): farray(aarray) { + fcurrent=farray.felements + farray.fsize; + } + + /// there are still elements + inline operator bool () { + return fcurrent > farray.felements; + } + + /// returns the current element and advances the iterator + inline T prev() { + return *(--fcurrent); + } + + // returns the current index of the iterator + inline size_t index() { + return fcurrent - farray.felements; + } + + inline char *key(){ + return pa_uitoa(index()); + } + +}; #endif