|
|
| version 1.2, 2001/01/27 15:21:05 | version 1.78, 2009/04/30 04:40:30 |
|---|---|
| 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-2009 ArtLebedev Group (http://www.artlebedev.com) | |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | |
| */ | */ |
| #ifndef PA_ARRAY_H | #ifndef PA_ARRAY_H |
| #define PA_ARRAY_H | #define PA_ARRAY_H |
| #include <stddef.h> | static const char * const IDENT_ARRAY_Y="$Date$"; |
| // includes | |
| #include "pa_memory.h" | |
| #include "pa_exception.h" | |
| // forwards | |
| template<typename T> class Array_iterator; | |
| #include "pa_types.h" | // defines |
| class Pool; | #define ARRAY_OPTION_LIMIT_ALL ((size_t)-1) |
| /// Simple Array | |
| template<typename T> class Array: public PA_Object { | |
| friend class Array_iterator<T>; | |
| protected: | |
| /// elements[growing size] here | |
| T *felements; | |
| // allocated size | |
| size_t fallocated; | |
| // array size | |
| size_t fused; | |
| class Array { | |
| public: | public: |
| typedef void *Item; | 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; | |
| return true; | |
| } | |
| enum { | |
| CR_PREALLOCATED_COUNT=10, | |
| CR_GROW_PERCENT=60 | |
| }; | }; |
| private: | typedef T element_type; |
| friend Pool; | |
| // the pool I'm allocated on | inline Array(size_t initial=0): |
| Pool *pool; | fallocated(initial), |
| fused(0) | |
| { | |
| felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; | |
| } | |
| struct Chunk { | #ifdef USE_DESTRUCTORS |
| // the number of rows in chunk | inline ~Array(){ |
| int count; | if(felements) |
| union Row { | pa_free(felements); |
| Item item; | } |
| Chunk *link; // link to the next chunk in chain | #endif |
| } rows[1]; | |
| // next rows are here | |
| } | |
| *head; // the head chunk of the chunk chain | |
| // 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: | |
| // last chank allocated count | |
| int curr_chunk_rows; | |
| // array size | /// how many items are in Array |
| int fused_rows; | inline size_t count() const { return fused; } |
| /// append to array | |
| inline Array& operator+=(T src) { | |
| if(is_full()) | |
| expand(fallocated>0?2:3); // 3 is PAF default, confirmed by tests | |
| int cache_index; | felements[fused++]=src; |
| Chunk::Row *cache_row; | |
| int cache_countdown; | |
| Chunk::Row *cache_link_row; | |
| private: | |
| // new&constructors made private to enforce factory manufacturing at pool | |
| void *operator new(size_t size, Pool *apool); | |
| void construct(Pool *apool, int initial_rows); | return *this; |
| Array(Pool *apool) { | |
| construct(apool, CR_PREALLOCATED_COUNT); | |
| } | } |
| Array(Pool *apool, int initial_rows) { | |
| construct(apool, initial_rows); | /// append other Array portion to this one. starting from offset |
| Array& append(const Array& src, | |
| size_t offset=0, | |
| size_t limit=ARRAY_OPTION_LIMIT_ALL, //< negative limit means 'all'. zero limit means 'nothing' | |
| bool reverse=false) { | |
| size_t src_count=src.count(); | |
| // skip tivials | |
| if(!src_count || !limit || offset>=src_count) | |
| return *this; | |
| // max(limit) | |
| size_t m=reverse? | |
| 1+offset | |
| :src_count-offset; | |
| if(!m) | |
| return *this; | |
| // fix limit | |
| if(limit==ARRAY_OPTION_LIMIT_ALL || limit>m) | |
| limit=m; | |
| ssize_t delta=reverse? | |
| (ssize_t)limit | |
| :limit-(fallocated-fused); | |
| if(delta>0) | |
| expand(delta); | |
| T* from=&src.felements[offset]; | |
| T* to=&felements[fused]; | |
| if(reverse) { // reverse | |
| for(T* from_end=from-limit; from>from_end; --from) | |
| *to++=*from; | |
| } else { // forward | |
| for(T* from_end=from+limit; from<from_end; from++) | |
| *to++=*from; | |
| } | |
| fused+=limit; | |
| return *this; | |
| } | } |
| /// get index-element | |
| inline T get(size_t index) const { | |
| assert(index<count()); | |
| return felements[index]; | |
| } | |
| bool chunk_is_full() { | /// ref version of get |
| return append_here == link_row; | inline T& get_ref(size_t index) const { |
| assert(index<count()); | |
| return felements[index]; | |
| } | } |
| void expand(); | |
| public: | /// put index-element |
| inline void put(size_t index, T element) { | |
| assert(index<count()); | |
| felements[index]=element; | |
| } | |
| inline T operator [](size_t index) const { return get(index); } | |
| /// 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; | |
| } | |
| int size() { return fused_rows; } | /// iterate over all elements |
| Array& operator += (Item src); | 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 |
| void put(int index, Item item); | template<typename I> T first_that(bool (*callback)(T, I), I info) const { |
| Item get(int index); | T *last=felements+fused; |
| */ | for(T *current=felements; current<last; current++) |
| Item& operator [] (int index); | if(callback(*current, info)) |
| return *current; | |
| return T(0); | |
| } | |
| inline T* ptr(size_t index){ | |
| return felements + index; | |
| } | |
| protected: | |
| bool is_full() { | |
| 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)); | |
| } | |
| } | |
| private: //disabled | private: //disabled |
| Array& operator = (Array& src) { return *this; } | Array(const Array&) {} |
| Array(Array& src) {} | Array& operator = (const Array&) { return *this; } |
| }; | }; |
| /** Array iterator, usage: | |
| @code | |
| // Array<T> a; | |
| for(Array_iterator<T> i(a); i.has_next(); ) { | |
| 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 | |
| bool has_next() { | |
| return fcurrent<flast; | |
| } | |
| /// quickly extracts next Array element | |
| T next() { | |
| return *(fcurrent++); | |
| } | |
| }; | |
| #endif | #endif |