|
|
| version 1.57.2.1, 2003/01/22 15:39:07 | version 1.97, 2024/10/07 23:46:24 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Array & Array_iter classes decls. | Parser: Array & Array_iterator classes decls. |
| Copyright (c) 2001, 2003 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* IDENT_ARRAY_Y="$Date$"; | #define IDENT_PA_ARRAY_H "$Id$" |
| #include "pa_pool.h" | // includes |
| #include "pa_memory.h" | |
| #include "pa_types.h" | #include "pa_types.h" |
| #include "pa_string.h" | #include "pa_exception.h" |
| class Array_iter; | // forwards |
| /** | template<typename T> class Array_iterator; |
| Pooled Array. | template<typename T> class Array_reverse_iterator; |
| Internal structure: | // defines |
| @verbatim | |
| Array Chunk0 | #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) |
| ====== ======== | |
| head--------------->[ptr] | /// Simple Array |
| append_here-------->[ptr] | template<typename T> class Array: public PA_Object { |
| link_row ........ | |
| . . | friend class Array_iterator<T>; |
| . [ptr] | friend class Array_reverse_iterator<T>; |
| ...........>[link to the next chunk] | |
| @endverbatim | protected: |
| */ | |
| /// elements[growing size] here | |
| T *felements; | |
| // allocated size | |
| size_t fallocated; | |
| // array size | |
| size_t fsize; | |
| class Array: public PA_Object { | |
| friend class Array_iter; | |
| public: | public: |
| typedef Array_iterator<T> Iterator; | |
| typedef Array_reverse_iterator<T> ReverseIterator; | |
| /// Array item type | struct Action_options { |
| typedef void Item; | 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; | |
| /*/// for_each iterator function type, const info | return true; |
| typedef void (*For_each_func_const)(Item *value, const void *info); | } |
| */ | |
| /// for_each iterator function type | |
| typedef void (*For_each_func)(Item *value, void *info); | |
| /* | |
| /// for_each iterator function type, passing item storage address | |
| typedef void (*For_each_func_storage)(Item ** value, void *info); | |
| */ | |
| /// first_that iterator function type, const info | |
| typedef void *(*Item_that_func_const)(Item *value, const void *info); | |
| /// first_that iterator function type | |
| typedef void *(*Item_that_func)(Item *value, void *info); | |
| enum { | |
| CR_INITIAL_ROWS_DEFAULT=3, ///< default preallocated row count | |
| CR_GROW_COUNT=3 ///< each time the Array chunk_is_full() array expanded() | |
| }; | }; |
| public: | typedef T element_type; |
| inline Array(size_t initial=0): | |
| fallocated(initial), | |
| fsize(0) | |
| { | |
| felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; | |
| } | |
| #ifdef USE_DESTRUCTORS | |
| inline ~Array(){ | |
| if(felements) | |
| pa_free(felements); | |
| } | |
| #endif | |
| Array(int initial_rows=CR_INITIAL_ROWS_DEFAULT); | /// how many items are in Array |
| inline size_t count() const { return fsize; } | |
| /// size Array. how many items are in it | /// append to array |
| int size() const { return fused_rows; } | inline Array& operator+=(T src) { |
| /// append Item to array | if(is_full()) |
| Array& operator += (Item *src); | expand(); |
| /// append int value to array | |
| Array& operator += (int value) { return *this+=reinterpret_cast<Item *>(value); } | |
| /// dirty hack to allow constant items storage. I long for Array<const Item*> | felements[fsize++]=src; |
| Array& operator += (const Item *src) { return *this+=const_cast<Item *>(src); } | |
| return *this; | |
| } | |
| /// append other Array portion to this one. starting from offset | /// append other Array portion to this one. starting from offset |
| Array& append_array(const Array& src, int offset=0, int limit=0); | Array& append(const Array& src, |
| size_t offset=0, | |
| size_t limit=ARRAY_OPTION_LIMIT_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>m) | |
| limit=m; | |
| fit(fsize-1+limit); | |
| T* from=&src.felements[offset]; | |
| T* to=&felements[fsize]; | |
| for(T* from_end=from+limit; from<from_end;) | |
| *to++=*from++; | |
| fsize+=limit; | |
| return *this; | |
| } | |
| Item *get(int index) const; | /// get index-element |
| int get_int(int index) const { return reinterpret_cast<int>(get(index)); } | inline T get(size_t index) const { |
| assert(index<count()); | |
| return felements[index]; | |
| } | |
| /// ref version of get | |
| inline T& get_ref(size_t index) const { | |
| assert(index<count()); | |
| return felements[index]; | |
| } | |
| /// put index-element | |
| inline void put(size_t index, T element) { | |
| assert(index<count()); | |
| felements[index]=element; | |
| } | |
| /// insert index-element | |
| inline void insert(size_t index, T element) { | |
| assert(index<=count()); | |
| if(is_full()) | |
| expand(); | |
| memmove(felements+index+1, felements+index, (fsize-index) * sizeof(T)); | |
| felements[index]=element; | |
| fsize++; | |
| } | |
| void put(int index, Item *item); | /// remove index-element |
| void put_int(int index, int value) { put(index, reinterpret_cast<Item *>(value)); } | inline void remove(size_t index) { |
| /// convinient way to get strings from Array. I long for Array<const String *> | assert(index<count()); |
| const String *get_string(int index) const { | if (index<--fsize) |
| return const_cast<const String *>(static_cast<String *>(get(index))); | memmove(felements+index, felements+index+1, (fsize-index) * sizeof(T)); |
| } | } |
| /*/// iterate over all elements, const info | inline T operator [](size_t index) const { return get(index); } |
| void for_each(For_each_func_const func, const void *info=0) const; | |
| */ | inline void clear() { |
| if(fsize) | |
| memset(felements, 0, fsize * sizeof(T)); | |
| fsize=0; | |
| } | |
| /// iterate over all elements | /// iterate over all elements |
| void for_each(For_each_func func, void *info=0) const; | template<typename I> void for_each(void (*callback)(T, I), I info) const { |
| T *last=felements+fsize; | |
| for(T *current=felements; current<last; current++) | |
| callback(*current, info); | |
| } | |
| /*/// iterate over all elements, passing address of item storage | /// iterate over all elements |
| void for_each(For_each_func_storage func, void *info=0); | template<typename I> void for_each(bool (*callback)(T, I), I info) const { |
| */ | T *last=felements+fsize; |
| for(T *current=felements; current<last; current++) | |
| /// iterate over all elements until condition, const info | if(callback(*current, info)) |
| void* first_that(Item_that_func_const func, const void *info=0) const; | return; |
| } | |
| /// iterate over all elements until condition | |
| void* first_that(Item_that_func func, void *info=0) const; | |
| private: | |
| /// several record elements | |
| struct Chunk { | |
| int count; ///< the number of rows in chunk | |
| /// item or a link to next chunk union | |
| 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 | |
| /** last allocated chunk | |
| helps appending Arrays | |
| */ | |
| Chunk *tail; | |
| /// 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: | /// iterate over all elements |
| template<typename I> void for_each_ref(void (*callback)(T&, I), I info) { | |
| T *last=felements+fsize; | |
| for(T *current=felements; current<last; current++) | |
| callback(*current, info); | |
| } | |
| // array size | /// iterate over all elements until condition becomes true, return that element |
| int fused_rows; | template<typename I> T first_that(bool (*callback)(T, I), I info) const { |
| T *last=felements+fsize; | |
| for(T *current=felements; current<last; current++) | |
| if(callback(*current, info)) | |
| return *current; | |
| return T(0); | |
| } | |
| inline T* ptr(size_t index){ | |
| return felements + index; | |
| } | |
| protected: | |
| inline bool is_full() { | |
| return fsize == fallocated; | |
| } | |
| private: | inline void expand() { |
| resize(fallocated>0 ? fallocated+fallocated/2+2 : 3); // 3 is PAF default, confirmed by tests | |
| } | |
| bool chunk_is_full() { | inline void fit(size_t index){ |
| return append_here == link_row; | if(index >= fallocated) |
| resize(max(this->fallocated + this->fallocated/4, index+1)); | |
| } | |
| 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)); | |
| } | |
| } | } |
| void expand(int chunk_rows); | |
| private: //disabled | private: //disabled |
| //Array(Array&) { } | Array(const Array&) {} |
| Array& operator = (const Array&) { return *this; } | Array& operator = (const Array&) { return *this; } |
| }; | }; |
| /// handy array iterator | /// Commonly used, templated to work with any integer type |
| class Array_iter { | |
| 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: | |
| @code | |
| // Array<T> a; | |
| for(Array_iterator<T> i(a); i; ) { | |
| T& element=i.next(); | |
| ... | |
| } | |
| @endcode | |
| */ | |
| template<typename T> class Array_iterator { | |
| const Array<T>& farray; | |
| T *fcurrent; | |
| T *flast; | |
| public: | public: |
| Array_iter(const Array& aarray) : array(aarray), | Array_iterator(const Array<T>& aarray): farray(aarray) { |
| chunk(aarray.head), | fcurrent=farray.felements; |
| row(chunk->rows), | flast=farray.felements + farray.fsize; |
| countdown(chunk->count) { | |
| } | } |
| /// there are still elements | /// there are still elements |
| bool has_next() { | inline operator bool () { |
| return !(chunk==array.tail && row==array.append_here); | return fcurrent < flast; |
| } | } |
| /// quickly extracts next Array::Item | /// returns the current element and advances the iterator |
| Array::Item *next() { | inline T next() { |
| // assuming: never called after has_next()! | return *(fcurrent++); |
| if(countdown==0) { // end of chunk? | |
| chunk=row->link; | |
| row=chunk->rows; | |
| countdown=chunk->count; | |
| } | |
| Array::Item *result=row->item; | |
| row++; countdown--; | |
| return result; | |
| } | } |
| /// quickly extracts next Array::Item as const String | /// returns the current element |
| const String *next_string() { | inline T value() { |
| return const_cast<const String *>(static_cast<String *>(next())); | return *(fcurrent); |
| } | } |
| private: | // returns the current index of the iterator |
| const Array& array; | inline size_t index() { |
| const Array::Chunk *chunk; | return fcurrent - farray.felements; |
| const Array::Chunk::Row *row; | } |
| int countdown; | |
| 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 |