|
|
| version 1.60, 2003/11/07 13:59:21 | version 1.69, 2006/11/03 17:32:30 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Array & Array_iterator classes decls. | Parser: Array & Array_iterator classes decls. |
| Copyright (c) 2001-2003 ArtLebedev Group (http://www.artlebedev.com) | Copyright (c) 2001-2005 ArtLebedev Group (http://www.artlebedev.com) |
| Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru) | 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 |
| static const char* IDENT_ARRAY_Y="$Date$"; | static const char * const IDENT_ARRAY_Y="$Date$"; |
| // includes | // includes |
| Line 61 public: | Line 61 public: |
| return false; | return false; |
| // max(limit) | // max(limit) |
| size_t m=reverse? | size_t m=reverse? |
| offset | offset+1 |
| :count-offset; | :count-offset; |
| if(!m) | if(!m) |
| return false; | return false; |
| Line 89 public: | Line 89 public: |
| /// append to array | /// append to array |
| Array& operator += (T src) { | Array& operator += (T src) { |
| if(is_full()) | if(is_full()) |
| expand(2); | expand(+2); |
| felements[fused++]=src; | felements[fused++]=src; |
| Line 139 public: | Line 139 public: |
| /// get index-element | /// get index-element |
| T get(size_t index) const { | T get(size_t index) const { |
| assert(index>=0 && index<count()); | assert(index<count()); |
| return felements[index]; | return felements[index]; |
| } | } |
| /// ref version of get | /// ref version of get |
| T& get_ref(size_t index) const { | T& get_ref(size_t index) const { |
| assert(index>=0 && index<count()); | assert(index<count()); |
| return felements[index]; | return felements[index]; |
| } | } |
| /// put index-element | /// put index-element |
| void put(size_t index, T element) { | void put(size_t index, T element) { |
| assert(index>=0 && index<count()); | assert(index<count()); |
| felements[index]=element; | felements[index]=element; |
| } | } |
| Line 165 public: | Line 165 public: |
| } | } |
| /// iterate over all elements | /// 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) { | template<typename I> void for_each_ref(void (*callback)(T&, I), I info) { |
| T *last=felements+fused; | T *last=felements+fused; |
| for(T *current=felements; current<last; current++) | for(T *current=felements; current<last; current++) |
| Line 228 public: | Line 236 public: |
| } | } |
| /// quickly extracts next Array element | /// quickly extracts next Array element |
| const T next() { | T next() { |
| return *(fcurrent++); | |
| } | |
| }; | |
| /* | |
| /** Nonconst 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_modifing_iterator { | |
| Array<T>& farray; | |
| T *fcurrent; | |
| T *flast; | |
| public: | |
| Array_modifing_iterator(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++); | return *(fcurrent++); |
| } | } |
| }; | }; |
| */ | |
| #endif | #endif |