|
|
| version 1.78, 2009/04/30 04:40:30 | version 1.89, 2024/09/07 16:30:26 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Array & Array_iterator classes decls. | Parser: Array & Array_iterator classes decls. |
| Copyright (c) 2001-2009 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| #ifndef PA_ARRAY_H | #ifndef PA_ARRAY_H |
| #define PA_ARRAY_H | #define PA_ARRAY_H |
| static const char * const IDENT_ARRAY_Y="$Date$"; | #define IDENT_PA_ARRAY_H "$Id$" |
| // includes | // includes |
| Line 40 protected: | Line 40 protected: |
| size_t fused; | size_t fused; |
| public: | public: |
| typedef Array_iterator<T> Iterator; | |
| struct Action_options { | struct Action_options { |
| size_t offset; | size_t offset; |
| size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing' | size_t limit; //< ARRAY_OPTION_LIMIT_ALL means 'all'. zero limit means 'nothing' |
| Line 95 public: | Line 97 public: |
| /// 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:3); // 3 is PAF default, confirmed by tests | expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests |
| felements[fused++]=src; | felements[fused++]=src; |
| Line 105 public: | Line 107 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) { //< negative limit means 'all'. zero limit means 'nothing' |
| bool reverse=false) { | |
| size_t src_count=src.count(); | size_t src_count=src.count(); |
| // skip tivials | // skip tivials |
| if(!src_count || !limit || offset>=src_count) | if(!src_count || !limit || offset>=src_count) |
| return *this; | return *this; |
| // max(limit) | // max(limit) |
| size_t m=reverse? | size_t m=src_count-offset; |
| 1+offset | |
| :src_count-offset; | |
| if(!m) | |
| return *this; | |
| // fix limit | // fix limit |
| if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) | if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) |
| limit=m; | limit=m; |
| ssize_t delta=reverse? | ssize_t delta=limit-(fallocated-fused); |
| (ssize_t)limit | |
| :limit-(fallocated-fused); | |
| if(delta>0) | if(delta>0) |
| expand(delta); | expand(delta); |
| T* from=&src.felements[offset]; | T* from=&src.felements[offset]; |
| T* to=&felements[fused]; | T* to=&felements[fused]; |
| if(reverse) { // reverse | for(T* from_end=from+limit; from<from_end; from++) |
| for(T* from_end=from-limit; from>from_end; --from) | *to++=*from; |
| *to++=*from; | |
| } else { // forward | |
| for(T* from_end=from+limit; from<from_end; from++) | |
| *to++=*from; | |
| } | |
| fused+=limit; | fused+=limit; |
| return *this; | return *this; |
| } | } |
| Line 161 public: | Line 149 public: |
| felements[index]=element; | felements[index]=element; |
| } | } |
| /// insert index-element | |
| inline void insert(size_t index, T element) { | |
| assert(index<=count()); | |
| if(is_full()) | |
| expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests | |
| memmove(felements+index+1, felements+index, (fused-index) * sizeof(T)); | |
| felements[index]=element; | |
| fused++; | |
| } | |
| /// remove index-element | |
| inline void remove(size_t index) { | |
| assert(index<count()); | |
| if (index<--fused) | |
| memmove(felements+index, felements+index+1, (fused-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() { | |
| fused=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+fused; |
| Line 199 public: | Line 211 public: |
| return felements + index; | return felements + index; |
| } | } |
| void fit(size_t index, T element){ | |
| if(index >= fallocated){ | |
| size_t new_allocated=fallocated>0 ? fallocated : 3; | |
| while(new_allocated <= index){ | |
| new_allocated+=2 + new_allocated/32; | |
| } | |
| expand(new_allocated - fallocated); | |
| } | |
| felements[index]=element; | |
| if(index >= fused){ | |
| fused=index+1; | |
| } | |
| } | |
| protected: | protected: |
| bool is_full() { | bool is_full() { |
| return fused == fallocated; | return fused == fallocated; |
| } | } |
| void expand(size_t delta) { | void expand(size_t delta) { |
| if(fallocated){ | if(fallocated){ |
| size_t new_allocated=fallocated+delta; | size_t new_allocated=fallocated+delta; |
| Line 225 private: //disabled | Line 252 private: //disabled |
| /** Array iterator, usage: | /** Array iterator, usage: |
| @code | @code |
| // Array<T> a; | // Array<T> a; |
| for(Array_iterator<T> i(a); i.has_next(); ) { | for(Array_iterator<T> i(a); i; ) { |
| T& element=i.next(); | T& element=i.next(); |
| ... | ... |
| } | } |
| Line 245 public: | Line 272 public: |
| } | } |
| /// there are still elements | /// there are still elements |
| bool has_next() { | operator bool () { |
| return fcurrent<flast; | return fcurrent<flast; |
| } | } |
| /// quickly extracts next Array element | /// returns the current element and advances the iterator |
| T next() { | T next() { |
| return *(fcurrent++); | return *(fcurrent++); |
| } | } |
| /// returns the current element | |
| T value() { | |
| return *(fcurrent); | |
| } | |
| // returns the current index of the iterator | |
| size_t index() { | |
| return fcurrent - farray.felements; | |
| } | |
| }; | }; |
| #endif | #endif |