|
|
| version 1.89, 2024/09/07 16:30:26 | version 1.97, 2024/10/07 23:46:24 |
|---|---|
| Line 13 | Line 13 |
| // includes | // includes |
| #include "pa_memory.h" | #include "pa_memory.h" |
| #include "pa_types.h" | |
| #include "pa_exception.h" | #include "pa_exception.h" |
| // forwards | // forwards |
| template<typename T> class Array_iterator; | template<typename T> class Array_iterator; |
| template<typename T> class Array_reverse_iterator; | |
| // defines | // defines |
| Line 27 template<typename T> class Array_iterato | Line 29 template<typename T> class Array_iterato |
| template<typename T> class Array: public PA_Object { | template<typename T> class Array: public PA_Object { |
| friend class Array_iterator<T>; | friend class Array_iterator<T>; |
| friend class Array_reverse_iterator<T>; | |
| protected: | protected: |
| Line 37 protected: | Line 40 protected: |
| size_t fallocated; | size_t fallocated; |
| // array size | // array size |
| size_t fused; | size_t fsize; |
| public: | public: |
| typedef Array_iterator<T> Iterator; | typedef Array_iterator<T> Iterator; |
| typedef Array_reverse_iterator<T> ReverseIterator; | |
| struct Action_options { | struct Action_options { |
| size_t offset; | size_t offset; |
| Line 49 public: | Line 53 public: |
| bool defined; | bool defined; |
| Action_options( | Action_options( |
| size_t aoffset=0, | size_t aoffset=0, |
| size_t alimit=ARRAY_OPTION_LIMIT_ALL, | size_t alimit=ARRAY_OPTION_LIMIT_ALL, |
| bool areverse=false): | bool areverse=false): |
| offset(aoffset), limit(alimit), reverse(areverse), | offset(aoffset), limit(alimit), reverse(areverse), |
| defined(false) {} | defined(false) {} |
| Line 67 public: | Line 71 public: |
| if(!m) | if(!m) |
| return false; | return false; |
| // fix limit | // fix limit |
| if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) | if(limit>m) |
| limit=m; | limit=m; |
| return true; | return true; |
| } | } |
| }; | }; |
| typedef T element_type; | typedef T element_type; |
| inline Array(size_t initial=0): | inline Array(size_t initial=0): |
| fallocated(initial), | fallocated(initial), |
| fused(0) | fsize(0) |
| { | { |
| felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; | felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; |
| } | } |
| #ifdef USE_DESTRUCTORS | #ifdef USE_DESTRUCTORS |
| inline ~Array(){ | inline ~Array(){ |
| if(felements) | if(felements) |
| pa_free(felements); | pa_free(felements); |
| Line 93 public: | Line 95 public: |
| #endif | #endif |
| /// how many items are in Array | /// how many items are in Array |
| inline size_t count() const { return fused; } | inline size_t count() const { return fsize; } |
| /// append to array | /// append to array |
| inline Array& operator+=(T src) { | inline Array& operator+=(T src) { |
| if(is_full()) | if(is_full()) |
| expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests | expand(); |
| felements[fused++]=src; | felements[fsize++]=src; |
| return *this; | return *this; |
| } | } |
| Line 107 public: | Line 110 public: |
| /// append other Array portion to this one. starting from offset | /// append other Array portion to this one. starting from offset |
| Array& append(const Array& src, | Array& append(const Array& src, |
| size_t offset=0, | size_t offset=0, |
| size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< negative limit means 'all'. zero limit means 'nothing' | size_t limit=ARRAY_OPTION_LIMIT_ALL) { //< zero limit means 'nothing' |
| size_t src_count=src.count(); | size_t src_count=src.count(); |
| // skip tivials | // skip tivials |
| Line 116 public: | Line 119 public: |
| // max(limit) | // max(limit) |
| size_t m=src_count-offset; | size_t m=src_count-offset; |
| // fix limit | // fix limit |
| if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) | if(limit>m) |
| limit=m; | limit=m; |
| ssize_t delta=limit-(fallocated-fused); | fit(fsize-1+limit); |
| if(delta>0) | |
| expand(delta); | |
| T* from=&src.felements[offset]; | T* from=&src.felements[offset]; |
| T* to=&felements[fused]; | T* to=&felements[fsize]; |
| for(T* from_end=from+limit; from<from_end; from++) | for(T* from_end=from+limit; from<from_end;) |
| *to++=*from; | *to++=*from++; |
| fused+=limit; | fsize+=limit; |
| return *this; | return *this; |
| } | } |
| Line 154 public: | Line 155 public: |
| assert(index<=count()); | assert(index<=count()); |
| if(is_full()) | if(is_full()) |
| expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests | expand(); |
| memmove(felements+index+1, felements+index, (fused-index) * sizeof(T)); | memmove(felements+index+1, felements+index, (fsize-index) * sizeof(T)); |
| felements[index]=element; | felements[index]=element; |
| fused++; | fsize++; |
| } | } |
| /// remove index-element | /// remove index-element |
| inline void remove(size_t index) { | inline void remove(size_t index) { |
| assert(index<count()); | assert(index<count()); |
| if (index<--fused) | if (index<--fsize) |
| memmove(felements+index, felements+index+1, (fused-index) * sizeof(T)); | memmove(felements+index, felements+index+1, (fsize-index) * sizeof(T)); |
| } | } |
| inline T operator [](size_t index) const { return get(index); } | inline T operator [](size_t index) const { return get(index); } |
| inline void clear() { | inline void clear() { |
| fused=0; | if(fsize) |
| memset(felements, 0, fsize * sizeof(T)); | |
| fsize=0; | |
| } | } |
| /// iterate over all elements | /// iterate over all elements |
| template<typename I> void for_each(void (*callback)(T, I), I info) const { | template<typename I> void for_each(void (*callback)(T, I), I info) const { |
| T *last=felements+fused; | T *last=felements+fsize; |
| for(T *current=felements; current<last; current++) | for(T *current=felements; current<last; current++) |
| callback(*current, info); | callback(*current, info); |
| } | } |
| /// iterate over all elements | /// iterate over all elements |
| template<typename I> void for_each(bool (*callback)(T, I), I info) const { | template<typename I> void for_each(bool (*callback)(T, I), I info) const { |
| T *last=felements+fused; | T *last=felements+fsize; |
| for(T *current=felements; current<last; current++) | for(T *current=felements; current<last; current++) |
| if(callback(*current, info)) | if(callback(*current, info)) |
| return; | return; |
| Line 192 public: | Line 195 public: |
| /// iterate over all elements | /// iterate over all elements |
| template<typename I> void for_each_ref(void (*callback)(T&, I), I info) { | template<typename I> void for_each_ref(void (*callback)(T&, I), I info) { |
| T *last=felements+fused; | T *last=felements+fsize; |
| for(T *current=felements; current<last; current++) | for(T *current=felements; current<last; current++) |
| callback(*current, info); | callback(*current, info); |
| } | } |
| /// iterate over all elements until condition becomes true, return that element | /// iterate over all elements until condition becomes true, return that element |
| template<typename I> T first_that(bool (*callback)(T, I), I info) const { | template<typename I> T first_that(bool (*callback)(T, I), I info) const { |
| T *last=felements+fused; | T *last=felements+fsize; |
| for(T *current=felements; current<last; current++) | for(T *current=felements; current<last; current++) |
| if(callback(*current, info)) | if(callback(*current, info)) |
| return *current; | return *current; |
| Line 211 public: | Line 214 public: |
| return felements + index; | return felements + index; |
| } | } |
| void fit(size_t index, T element){ | protected: |
| if(index >= fallocated){ | |
| size_t new_allocated=fallocated>0 ? fallocated : 3; | inline bool is_full() { |
| while(new_allocated <= index){ | return fsize == fallocated; |
| new_allocated+=2 + new_allocated/32; | |
| } | |
| expand(new_allocated - fallocated); | |
| } | |
| felements[index]=element; | |
| if(index >= fused){ | |
| fused=index+1; | |
| } | |
| } | } |
| protected: | inline void expand() { |
| resize(fallocated>0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests | |
| } | |
| bool is_full() { | inline void fit(size_t index){ |
| return fused == fallocated; | if(index >= fallocated) |
| resize(max(this->fallocated + this->fallocated/4, index+1)); | |
| } | } |
| void expand(size_t delta) { | void resize(size_t asize) { |
| if(fallocated){ | if(fallocated){ |
| size_t new_allocated=fallocated+delta; | felements=(T *)pa_realloc(felements, asize*sizeof(T)); |
| felements=(T *)pa_realloc(felements, new_allocated*sizeof(T)); | fallocated=asize; |
| fallocated=new_allocated; | |
| } else { | } else { |
| fallocated=delta; | fallocated=asize; |
| felements=(T *)pa_malloc(fallocated*sizeof(T)); | felements=(T *)pa_malloc(asize*sizeof(T)); |
| } | } |
| } | } |
| Line 249 private: //disabled | Line 246 private: //disabled |
| }; | }; |
| /// Commonly used, templated to work with any integer type | |
| template<typename T> 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)=(char)(n % base) + '0'; | |
| n/=base; | |
| } while (n > 0); | |
| if (negative) { | |
| *(--pos) = '-'; | |
| } | |
| return pa_strdup(pos, buf + MAX_NUMBER - pos); | |
| } | |
| template<typename T> char* pa_uitoa(T n, T base=10){ | |
| char buf[MAX_NUMBER + 1]; | |
| char* pos=buf + MAX_NUMBER; | |
| *pos='\0'; | |
| do { | |
| *(--pos)=(char)(n % base) + '0'; | |
| n/=base; | |
| } while (n > 0); | |
| return pa_strdup(pos, buf + MAX_NUMBER - pos); | |
| } | |
| /** Array iterator, usage: | /** Array iterator, usage: |
| @code | @code |
| // Array<T> a; | // Array<T> a; |
| Line 268 public: | Line 302 public: |
| Array_iterator(const Array<T>& aarray): farray(aarray) { | Array_iterator(const Array<T>& aarray): farray(aarray) { |
| fcurrent=farray.felements; | fcurrent=farray.felements; |
| flast=farray.felements+farray.count(); | flast=farray.felements + farray.fsize; |
| } | } |
| /// there are still elements | /// there are still elements |
| operator bool () { | inline operator bool () { |
| return fcurrent<flast; | return fcurrent < flast; |
| } | } |
| /// returns the current element and advances the iterator | /// returns the current element and advances the iterator |
| T next() { | inline T next() { |
| return *(fcurrent++); | return *(fcurrent++); |
| } | } |
| /// returns the current element | /// returns the current element |
| T value() { | inline T value() { |
| return *(fcurrent); | return *(fcurrent); |
| } | } |
| // returns the current index of the iterator | // returns the current index of the iterator |
| size_t index() { | inline size_t index() { |
| return fcurrent - farray.felements; | return fcurrent - farray.felements; |
| } | } |
| inline char *key(){ | |
| return pa_uitoa(index()); | |
| } | |
| }; | |
| template<typename T> class Array_reverse_iterator { | |
| const Array<T>& farray; | |
| T *fcurrent; | |
| public: | |
| Array_reverse_iterator(const Array<T>& 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 | #endif |