|
|
| version 1.18, 2001/02/22 08:25:51 | version 1.90, 2024/09/10 19:09:56 |
|---|---|
| Line 1 | Line 1 |
| /* | /** @file |
| $Id$ | Parser: Array & Array_iterator classes decls. |
| */ | |
| /* | |
| Array Chunk0 | |
| ====== ======== | |
| head--------------->[ptr] | |
| append_here-------->[ptr] | |
| link_row ........ | |
| . . | |
| . [ptr] | |
| ...........>[link to the next chunk] | |
| Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com) | |
| 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 |
| #include <stddef.h> | #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 : public Pooled { | // forwards |
| public: | |
| typedef void Item; | template<typename T> class Array_iterator; |
| template<typename T> class Array_reverse_iterator; | |
| enum { | // defines |
| CR_INITIAL_ROWS_DEFAULT=10, | |
| CR_GROW_PERCENT=60 | #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) |
| }; | |
| /// Simple Array | |
| template<typename T> class Array: public PA_Object { | |
| friend class Array_iterator<T>; | |
| friend class Array_reverse_iterator<T>; | |
| protected: | |
| /// elements[growing size] here | |
| T *felements; | |
| // allocated size | |
| size_t fallocated; | |
| // array size | |
| size_t fused; | |
| public: | public: |
| typedef Array_iterator<T> Iterator; | |
| typedef Array_reverse_iterator<T> ReverseIterator; | |
| Array(Pool& apool, int initial_rows=CR_INITIAL_ROWS_DEFAULT); | 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; | |
| int size() const { | return true; |
| // for get and quick_get | |
| cache_chunk_base=0; | |
| cache_chunk=head; | |
| return fused_rows; | |
| } | |
| Array& operator += (Item *src); | |
| Array& append_array(const Array& src); | |
| Item *quick_get(int index) const { | |
| // considering these true: | |
| // index increments from 0 to size()-1 | |
| // index>=0 && index<size() | |
| // index>=cache_chunk_base | |
| // next chunk will be with "index" row | |
| if(!(index<cache_chunk_base+cache_chunk->count)) { | |
| int count=cache_chunk->count; | |
| cache_chunk_base+=count; | |
| cache_chunk=cache_chunk->rows[count].link; | |
| } | } |
| 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; | |
| } | |
| #ifdef USE_DESTRUCTORS | |
| inline ~Array(){ | |
| if(felements) | |
| pa_free(felements); | |
| } | |
| #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; | |
| } | } |
| Item *get(int index) const; | /// append other Array portion to this one. starting from offset |
| void put(int index, Item *item); | Array& append(const Array& src, |
| const char *get_cstr(int index) const { | size_t offset=0, |
| return static_cast<const char *>(get(index)); | 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; from<from_end; from++) | |
| *to++=*from; | |
| fused+=limit; | |
| return *this; | |
| } | } |
| const String *get_string(int index) const { | |
| return static_cast<const String *>(get(index)); | /// get index-element |
| inline T get(size_t index) const { | |
| assert(index<count()); | |
| return felements[index]; | |
| } | } |
| private: | /// ref version of get |
| inline T& get_ref(size_t index) const { | |
| assert(index<count()); | |
| return felements[index]; | |
| } | |
| struct Chunk { | /// put index-element |
| // the number of rows in chunk | inline void put(size_t index, T element) { |
| int count; | assert(index<count()); |
| union Row { | felements[index]=element; |
| 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 | /// insert index-element |
| // helps appending Arrays | inline void insert(size_t index, T element) { |
| Chunk *tail; | assert(index<=count()); |
| // next append would write to this record | if(is_full()) |
| Chunk::Row *append_here; | expand(fallocated>0? 2+fallocated/32 : 3); // 3 is PAF default, confirmed by tests |
| // the address of place where lies address | |
| // of the link to the next chunk to allocate | |
| Chunk::Row *link_row; | |
| private: | memmove(felements+index+1, felements+index, (fused-index) * sizeof(T)); |
| // array size | felements[index]=element; |
| int fused_rows; | 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 void clear() { | |
| if(fused) | |
| memset(felements, 0, fused * sizeof(T)); | |
| fused=0; | |
| } | |
| /// iterate over all elements | |
| template<typename I> void for_each(void (*callback)(T, I), I info) const { | |
| T *last=felements+fused; | |
| for(T *current=felements; current<last; current++) | |
| callback(*current, info); | |
| } | |
| /// iterate over all elements | |
| template<typename I> void for_each(bool (*callback)(T, I), I info) const { | |
| T *last=felements+fused; | |
| for(T *current=felements; current<last; current++) | |
| if(callback(*current, info)) | |
| return; | |
| } | |
| /// iterate over all elements | |
| template<typename I> void for_each_ref(void (*callback)(T&, I), I info) { | |
| T *last=felements+fused; | |
| for(T *current=felements; current<last; current++) | |
| callback(*current, info); | |
| } | |
| /// iterate over all elements until condition becomes true, return that element | |
| template<typename I> T first_that(bool (*callback)(T, I), I info) const { | |
| T *last=felements+fused; | |
| for(T *current=felements; current<last; current++) | |
| if(callback(*current, info)) | |
| return *current; | |
| return T(0); | |
| } | |
| mutable int cache_chunk_base; | inline T* ptr(size_t index){ |
| mutable Chunk *cache_chunk; | return felements + index; |
| } | |
| private: | |
| 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: | |
| bool chunk_is_full() { | bool is_full() { |
| return append_here == link_row; | return fused == fallocated; |
| } | |
| void expand(size_t delta) { | |
| if(fallocated){ | |
| size_t new_allocated=fallocated+delta; | |
| felements=(T *)pa_realloc(felements, new_allocated*sizeof(T)); | |
| fallocated=new_allocated; | |
| } else { | |
| fallocated=delta; | |
| felements=(T *)pa_malloc(fallocated*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; } |
| }; | }; |
| /** 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: | |
| Array_iterator(const Array<T>& aarray): farray(aarray) { | |
| fcurrent=farray.felements; | |
| flast=farray.felements + farray.count(); | |
| } | |
| /// there are still elements | |
| inline operator bool () { | |
| return fcurrent < flast; | |
| } | |
| /// returns the current element and advances the iterator | |
| inline T next() { | |
| return *(fcurrent++); | |
| } | |
| /// returns the current element | |
| inline T value() { | |
| return *(fcurrent); | |
| } | |
| // returns the current index of the iterator | |
| inline size_t index() { | |
| return fcurrent - farray.felements; | |
| } | |
| inline char *key(){ | |
| char local_buf[MAX_NUMBER]; | |
| size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index()); | |
| return pa_strdup(local_buf, length); | |
| } | |
| }; | |
| 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.count(); | |
| } | |
| /// 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(){ | |
| char local_buf[MAX_NUMBER]; | |
| size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index()); | |
| return pa_strdup(local_buf, length); | |
| } | |
| }; | |
| #endif | #endif |