Changeset 733 for cpp/frams/util/list.h
 Timestamp:
 02/15/18 00:43:07 (6 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

cpp/frams/util/list.h
r286 r733 16 16 class SListStats 17 17 { 18 19 int objects, allocations, copied, totalmem, usedmem;20 SListStats():objects(0),allocations(0),copied(0),totalmem(0),usedmem() {}21 ~SListStats()18 public: 19 int objects, allocations, copied, totalmem, usedmem; 20 SListStats():objects(0),allocations(0),copied(0),totalmem(0),usedmem() {} 21 ~SListStats() 22 22 { 23 printf("\n"24 25 26 27 28 29 30 31 23 printf("\n" 24 " SListStats:\n" 25 " objects = %ld\n" 26 " allocations = %ld\n" 27 " copied = %ld\n" 28 " total mem = %ld\n" 29 " usage = %ld %%\n" 30 "\n", 31 objects,allocations,copied,totalmem,(usedmem*100)/totalmem); 32 32 } 33 33 }; … … 38 38 { 39 39 protected: 40 int have,used,pos; // ile,zaj,poz41 T *mem;42 void resize(int x)40 int have, used, pos; // ile,zaj,poz 41 T *mem; 42 void resize(int x) 43 43 { 44 if (mem  x)44 if (mem  x) 45 45 { 46 mem=(T*)realloc(mem,x*sizeof(T));46 mem = (T*)realloc(mem, x*sizeof(T)); 47 47 #ifdef SLISTSTATS 48 SListStats::stats.allocations++;49 SListStats::stats.copied+=sizeof(T)*min(x,have);48 SListStats::stats.allocations++; 49 SListStats::stats.copied+=sizeof(T)*min(x,have); 50 50 #endif 51 51 } 52 have=x;52 have = x; 53 53 } 54 54 55 55 public: 56 56 57 SListTempl(const SListTempl<T>& src):have(0),used(0),pos(0),mem(0) 58 {(*this)=src;} 59 SListTempl():have(0),used(0),pos(0),mem(0) 57 SListTempl(const SListTempl<T>& src) :have(0), used(0), pos(0), mem(0) 58 { 59 (*this) = src; 60 } 61 SListTempl() :have(0), used(0), pos(0), mem(0) 60 62 {} 61 ~SListTempl()63 ~SListTempl() 62 64 { 63 65 #ifdef SLISTSTATS 64 SListStats::stats.objects++;65 SListStats::stats.totalmem+=sizeof(T)*have;66 SListStats::stats.usedmem+=sizeof(T)*used;66 SListStats::stats.objects++; 67 SListStats::stats.totalmem+=sizeof(T)*have; 68 SListStats::stats.usedmem+=sizeof(T)*used; 67 69 #endif 68 hardclear();70 hardclear(); 69 71 } 70 72 71 void needSize(int s) 72 { if (s>have) resize(s+3+s/8); } 73 void operator+=(const T& e) ///< append one element 74 {append(e);} 75 void operator=(const T& e) ///< remove one element 76 {int i; if ((i=find(e))>=0) remove(i);} 77 void remove(int i,int n=1) ///< remove n elements from position i 73 void needSize(int s) 78 74 { 79 if ((i>=size())(i<0)) return; 80 if (i>(size()n)) n=size()i; 81 if (pos>=i) pos=n; 82 memmove(mem+i,mem+i+n,sizeof(T)*(usedni)); 83 used=n; 75 if (s > have) resize(s + 3 + s / 8); 84 76 } 85 void operator=(int i) {remove(i);} ///< remove element from position i 86 T& operator()(int i) const ///< return element at position i 87 {return mem[i];} 88 bool hasMore() {return pos<size();} 89 int operator==(const SListTempl<T>& l) const ///< compare list 90 { 91 if (size() != l.size()) return 0; 92 for (int i=0;i<size();i++) if (l.mem[i] != mem[i]) return 0; 93 return 1; 94 } 95 int find(const T& e) const ///< return position of element, or 1 if not found 96 { for (int i=0;i<size();i++) if (mem[i]==e) return i; return 1; } 97 void append(const T& data) ///< add 1 element 98 {needSize(size()+1); mem[used++]=data;} 99 void append(const T* data,int n) ///< add n elements 77 void operator+=(const T& e) ///< append one element 100 78 { 101 needSize(size()+n); 102 memcpy(mem+used,data,sizeof(T)*n); 103 used+=n; 79 append(e); 104 80 } 105 void insert(int p,T* data,int n) ///< insert n elements at position p 81 void operator=(const T& e) ///< remove one element 106 82 { 107 if (p>size()) p=size(); 108 needSize(size()+n); 109 memmove(mem+p+n,mem+p,sizeof(T)*(size()p)); 110 memcpy(mem+p,data,sizeof(T)*n); 111 if (pos>p) pos+=n; 83 int i; if ((i = find(e)) >= 0) remove(i); 112 84 } 113 void insert(int p,const T& data) ///< insert 1 element at position p 85 void remove(int i, int n = 1) ///< remove n elements from position i 114 86 { 115 if (p>size()) p=size(); 116 needSize(size()+1); 117 memmove(mem+p+1,mem+p,sizeof(T)*(size()p)); 118 if (pos>p) pos++; 119 mem[p]=data; 120 used++; 87 if ((i >= size())  (i<0)) return; 88 if (i>(size()  n)) n = size()  i; 89 if (pos >= i) pos = n; 90 memmove(mem + i, mem + i + n, sizeof(T)*(used  n  i)); 91 used = n; 121 92 } 122 void set(int p,const T& d) 123 { needSize(p+1); mem[p]=d; if (used<(p+1)) used=p+1;} 124 void setSize(int s) 125 { needSize(s); used=s;} 126 T& get(int i) const {return operator()(i);} 127 void clear() {used=0;} ///< remove all elements 128 void hardclear() {resize(0); used=0;} ///< remove all elements and free mem 129 int size() const {return used;} ///< list size 130 void operator=(const SListTempl<T>&src) ///duplicate 131 { 132 setSize(src.size()); 133 memcpy(mem, src.mem, src.size()*sizeof(T)); 134 } 135 void operator+=(const SListTempl<T>&src) ///< append src contents 136 { 137 needSize(size()+src.size()); 138 memcpy(mem+used, src.mem, src.size()*sizeof(T)); 139 used+=src.used; 140 } 141 void trim(int newsiz) 142 {if (newsiz<used) used=newsiz;} 93 void operator=(int i) { remove(i); } ///< remove element from position i 94 T& operator()(int i) const ///< return element at position i 95 { 96 return mem[i]; 97 } 98 bool hasMore() { return pos < size(); } 99 int operator==(const SListTempl<T>& l) const ///< compare list 100 { 101 if (size() != l.size()) return 0; 102 for (int i = 0; i<size(); i++) if (l.mem[i] != mem[i]) return 0; 103 return 1; 104 } 105 int find(const T& e) const ///< return position of element, or 1 if not found 106 { 107 for (int i = 0; i<size(); i++) if (mem[i] == e) return i; return 1; 108 } 109 void append(const T& data) ///< add 1 element 110 { 111 needSize(size() + 1); mem[used++] = data; 112 } 113 void append(const T* data, int n) ///< add n elements 114 { 115 needSize(size() + n); 116 memcpy(mem + used, data, sizeof(T)*n); 117 used += n; 118 } 119 void insert(int p, T* data, int n) ///< insert n elements at position p 120 { 121 if (p>size()) p = size(); 122 needSize(size() + n); 123 memmove(mem + p + n, mem + p, sizeof(T)*(size()  p)); 124 memcpy(mem + p, data, sizeof(T)*n); 125 if (pos > p) pos += n; 126 } 127 void insert(int p, const T& data) ///< insert 1 element at position p 128 { 129 if (p > size()) p = size(); 130 needSize(size() + 1); 131 memmove(mem + p + 1, mem + p, sizeof(T)*(size()  p)); 132 if (pos > p) pos++; 133 mem[p] = data; 134 used++; 135 } 136 void set(int p, const T& d) 137 { 138 needSize(p + 1); mem[p] = d; if (used < (p + 1)) used = p + 1; 139 } 140 void setSize(int s) 141 { 142 needSize(s); used = s; 143 } 144 T& get(int i) const { return operator()(i); } 145 void clear() { used = 0; } ///< remove all elements 146 void hardclear() { resize(0); used = 0; } ///< remove all elements and free mem 147 int size() const { return used; } ///< list size 148 void operator=(const SListTempl<T>&src) ///duplicate 149 { 150 setSize(src.size()); 151 memcpy(mem, src.mem, src.size()*sizeof(T)); 152 } 153 void operator+=(const SListTempl<T>&src) ///< append src contents 154 { 155 needSize(size() + src.size()); 156 memcpy(mem + used, src.mem, src.size()*sizeof(T)); 157 used += src.used; 158 } 159 void trim(int newsiz) 160 { 161 if (newsiz < used) used = newsiz; 162 } 143 163 }; 144 164 … … 148 168 // usage example: FOREACH(char*,t,thelist) printf("t=%s\n",t); 149 169 150 template<class T> class PtrListTempl : public SListTempl<T>170 template<class T> class PtrListTempl : public SListTempl < T > 151 171 { 152 public: 153 T operator()(int i) const ///< return element at position i 154 {if (i>=this>size()) return 0; else return this>mem[i];} 172 public: 173 T operator()(int i) const ///< return element at position i 174 { 175 if (i >= this>size()) return 0; else return this>mem[i]; 176 } 155 177 156 T get(int i) const {return operator()(i);}157 T& getref(int i) const {return SListTempl<T>::get(i);}178 T get(int i) const { return operator()(i); } 179 T& getref(int i) const { return SListTempl<T>::get(i); } 158 180 }; 159 181 160 182 161 typedef PtrListTempl<void*> SList;183 typedef PtrListTempl<void*> SList; 162 184 163 185 #endif
Note: See TracChangeset
for help on using the changeset viewer.