|
|
| version 1.90, 2024/09/10 19:09:56 | version 1.92, 2024/09/14 22:58:50 |
|---|---|
| Line 76 public: | Line 76 public: |
| return true; | return true; |
| } | } |
| }; | }; |
| typedef T element_type; | typedef T element_type; |
| Line 89 public: | Line 87 public: |
| felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; | felements=fallocated?(T *)pa_malloc(fallocated*sizeof(T)):0; |
| } | } |
| #ifdef USE_DESTRUCTORS | #ifdef USE_DESTRUCTORS |
| inline ~Array(){ | inline ~Array(){ |
| if(felements) | if(felements) |
| pa_free(felements); | pa_free(felements); |
| Line 98 public: | Line 96 public: |
| /// how many items are in Array | /// how many items are in Array |
| inline size_t count() const { return fused; } | inline size_t count() const { return fused; } |
| /// append to array | /// append to array |
| inline Array& operator+=(T src) { | inline Array& operator+=(T src) { |
| if(is_full()) | if(is_full()) |
| Line 217 public: | Line 216 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() { |
| Line 255 private: //disabled | Line 240 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)=(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)=(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 274 public: | Line 296 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.fused; |
| } | } |
| /// there are still elements | /// there are still elements |
| Line 298 public: | Line 320 public: |
| } | } |
| inline char *key(){ | inline char *key(){ |
| char local_buf[MAX_NUMBER]; | return pa_uitoa(index()); |
| size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index()); | |
| return pa_strdup(local_buf, length); | |
| } | } |
| }; | }; |
| Line 313 template<typename T> class Array_reverse | Line 333 template<typename T> class Array_reverse |
| public: | public: |
| Array_reverse_iterator(const Array<T>& aarray): farray(aarray) { | Array_reverse_iterator(const Array<T>& aarray): farray(aarray) { |
| fcurrent=farray.felements+farray.count(); | fcurrent=farray.felements + farray.fused; |
| } | } |
| /// there are still elements | /// there are still elements |
| Line 332 public: | Line 352 public: |
| } | } |
| inline char *key(){ | inline char *key(){ |
| char local_buf[MAX_NUMBER]; | return pa_uitoa(index()); |
| size_t length=snprintf(local_buf, MAX_NUMBER, "%zu", index()); | |
| return pa_strdup(local_buf, length); | |
| } | } |
| }; | }; |