|
|
| version 1.85, 2017/02/07 22:00:31 | version 1.90, 2024/09/10 19:09:56 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Array & Array_iterator classes decls. | Parser: Array & Array_iterator classes decls. |
| Copyright (c) 2001-2017 Art. Lebedev Studio (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 |
| 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 40 protected: | Line 43 protected: |
| size_t fused; | size_t fused; |
| public: | public: |
| typedef Array_iterator<T> Iterator; | |
| typedef Array_reverse_iterator<T> ReverseIterator; | |
| 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 170 public: | Line 176 public: |
| 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() { |
| if(fused) | |
| memset(felements, 0, fused * sizeof(T)); | |
| fused=0; | fused=0; |
| } | } |
| Line 209 public: | Line 217 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 235 private: //disabled | Line 258 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 251 public: | Line 274 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.count(); |
| } | } |
| /// there are still elements | /// there are still elements |
| bool has_next() { | inline operator bool () { |
| return fcurrent<flast; | return fcurrent < flast; |
| } | } |
| /// quickly extracts next Array element | /// returns the current element and advances the iterator |
| T next() { | inline T next() { |
| return *(fcurrent++); | 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 |