|
|
| version 1.102, 2024/12/23 16:59:17 | version 1.108, 2026/04/25 13:38:46 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: Array & Array_iterator classes decls. | Parser: Array & Array_iterator classes decls. |
| Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| Line 227 protected: | Line 227 protected: |
| void resize(size_t asize) { | void resize(size_t asize) { |
| if(fallocated){ | if(fallocated){ |
| felements=(T *)pa_realloc(felements, asize*sizeof(T)); | felements=(T *)pa_realloc(felements, asize*sizeof(T)); |
| #ifdef PA_DEBUG_DISABLE_GC | |
| // non-gc realloc doesn't zero; manually zero expanded region | |
| if(asize > fallocated) | |
| memset((void *)(felements+fallocated), 0, (asize-fallocated) * sizeof(T)); | |
| #endif | |
| fallocated=asize; | fallocated=asize; |
| } else { | } else { |
| fallocated=asize; | fallocated=asize; |
| Line 241 private: //disabled | Line 246 private: //disabled |
| }; | }; |
| /// Commonly used, templated to work with any integer type | |
| 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: | /** Array iterator, usage: |
| @code | @code |
| // Array<T> a; | // Array<T> a; |
| Line 318 public: | Line 286 public: |
| inline size_t index() { | inline size_t index() { |
| return fcurrent - farray.felements; | return fcurrent - farray.felements; |
| } | } |
| // returns the current index string value of the iterator | |
| inline char *key() { | |
| return pa_uitoa(index()); | |
| } | |
| }; | }; |
| // Slower array iterator for arrays that can be modified during iteration | // Slower array iterator for arrays that can be modified during iteration |
| Line 349 public: | Line 312 public: |
| inline size_t index() { | inline size_t index() { |
| return findex; | return findex; |
| } | } |
| inline char* key() { | |
| return pa_uitoa(findex); | |
| } | |
| }; | }; |
| // Robust as used for arrays that can be modified during iteration | // Robust as used for arrays that can be modified during iteration |
| Line 375 public: | Line 334 public: |
| inline size_t index() { | inline size_t index() { |
| return findex; | return findex; |
| } | } |
| inline char *key() { | |
| return pa_uitoa(index()); | |
| } | |
| }; | }; |
| #endif | #endif |