src/array_list.c

changeset 1026
ca20f9ffcb62
parent 1022
2911c1f4a570
equal deleted inserted replaced
1025:439407fc29e1 1026:ca20f9ffcb62
106 106
107 // determine size and capacity 107 // determine size and capacity
108 size_t oldcap; 108 size_t oldcap;
109 size_t oldsize; 109 size_t oldsize;
110 size_t max_size; 110 size_t max_size;
111 if (width == 0 || width == __WORDSIZE) { 111 if (width == 0 || width == CX_WORDSIZE) {
112 oldcap = *(size_t*) capacity; 112 oldcap = *(size_t*) capacity;
113 oldsize = *(size_t*) size; 113 oldsize = *(size_t*) size;
114 max_size = SIZE_MAX; 114 max_size = SIZE_MAX;
115 } else if (width == 16) { 115 } else if (width == 16) {
116 oldcap = *(uint16_t*) capacity; 116 oldcap = *(uint16_t*) capacity;
119 } else if (width == 8) { 119 } else if (width == 8) {
120 oldcap = *(uint8_t*) capacity; 120 oldcap = *(uint8_t*) capacity;
121 oldsize = *(uint8_t*) size; 121 oldsize = *(uint8_t*) size;
122 max_size = UINT8_MAX; 122 max_size = UINT8_MAX;
123 } 123 }
124 #if __WORDSIZE == 64 124 #if CX_WORDSIZE == 64
125 else if (width == 32) { 125 else if (width == 32) {
126 oldcap = *(uint32_t*) capacity; 126 oldcap = *(uint32_t*) capacity;
127 oldsize = *(uint32_t*) size; 127 oldsize = *(uint32_t*) size;
128 max_size = UINT32_MAX; 128 max_size = UINT32_MAX;
129 } 129 }
160 160
161 // store new pointer 161 // store new pointer
162 *array = newmem; 162 *array = newmem;
163 163
164 // store new capacity 164 // store new capacity
165 if (width == 0 || width == __WORDSIZE) { 165 if (width == 0 || width == CX_WORDSIZE) {
166 *(size_t*) capacity = newcap; 166 *(size_t*) capacity = newcap;
167 } else if (width == 16) { 167 } else if (width == 16) {
168 *(uint16_t*) capacity = newcap; 168 *(uint16_t*) capacity = (uint16_t) newcap;
169 } else if (width == 8) { 169 } else if (width == 8) {
170 *(uint8_t*) capacity = newcap; 170 *(uint8_t*) capacity = (uint8_t) newcap;
171 } 171 }
172 #if __WORDSIZE == 64 172 #if CX_WORDSIZE == 64
173 else if (width == 32) { 173 else if (width == 32) {
174 *(uint32_t*) capacity = newcap; 174 *(uint32_t*) capacity = (uint32_t) newcap;
175 } 175 }
176 #endif 176 #endif
177 } 177 }
178 178
179 return 0; 179 return 0;
199 199
200 // determine size and capacity 200 // determine size and capacity
201 size_t oldcap; 201 size_t oldcap;
202 size_t oldsize; 202 size_t oldsize;
203 size_t max_size; 203 size_t max_size;
204 if (width == 0 || width == __WORDSIZE) { 204 if (width == 0 || width == CX_WORDSIZE) {
205 oldcap = *(size_t*) capacity; 205 oldcap = *(size_t*) capacity;
206 oldsize = *(size_t*) size; 206 oldsize = *(size_t*) size;
207 max_size = SIZE_MAX; 207 max_size = SIZE_MAX;
208 } else if (width == 16) { 208 } else if (width == 16) {
209 oldcap = *(uint16_t*) capacity; 209 oldcap = *(uint16_t*) capacity;
212 } else if (width == 8) { 212 } else if (width == 8) {
213 oldcap = *(uint8_t*) capacity; 213 oldcap = *(uint8_t*) capacity;
214 oldsize = *(uint8_t*) size; 214 oldsize = *(uint8_t*) size;
215 max_size = UINT8_MAX; 215 max_size = UINT8_MAX;
216 } 216 }
217 #if __WORDSIZE == 64 217 #if CX_WORDSIZE == 64
218 else if (width == 32) { 218 else if (width == 32) {
219 oldcap = *(uint32_t*) capacity; 219 oldcap = *(uint32_t*) capacity;
220 oldsize = *(uint32_t*) size; 220 oldsize = *(uint32_t*) size;
221 max_size = UINT32_MAX; 221 max_size = UINT32_MAX;
222 } 222 }
276 // copy elements and set new size 276 // copy elements and set new size
277 memmove(start, src, elem_count * elem_size); 277 memmove(start, src, elem_count * elem_size);
278 278
279 // if any of size or capacity changed, store them back 279 // if any of size or capacity changed, store them back
280 if (newsize != oldsize || newcap != oldcap) { 280 if (newsize != oldsize || newcap != oldcap) {
281 if (width == 0 || width == __WORDSIZE) { 281 if (width == 0 || width == CX_WORDSIZE) {
282 *(size_t*) capacity = newcap; 282 *(size_t*) capacity = newcap;
283 *(size_t*) size = newsize; 283 *(size_t*) size = newsize;
284 } else if (width == 16) { 284 } else if (width == 16) {
285 *(uint16_t*) capacity = newcap; 285 *(uint16_t*) capacity = (uint16_t) newcap;
286 *(uint16_t*) size = newsize; 286 *(uint16_t*) size = (uint16_t) newsize;
287 } else if (width == 8) { 287 } else if (width == 8) {
288 *(uint8_t*) capacity = newcap; 288 *(uint8_t*) capacity = (uint8_t) newcap;
289 *(uint8_t*) size = newsize; 289 *(uint8_t*) size = (uint8_t) newsize;
290 } 290 }
291 #if __WORDSIZE == 64 291 #if CX_WORDSIZE == 64
292 else if (width == 32) { 292 else if (width == 32) {
293 *(uint32_t*) capacity = newcap; 293 *(uint32_t*) capacity = (uint32_t) newcap;
294 *(uint32_t*) size = newsize; 294 *(uint32_t*) size = (uint32_t) newsize;
295 } 295 }
296 #endif 296 #endif
297 } 297 }
298 298
299 // return successfully 299 // return successfully
432 if (result < 0) { 432 if (result < 0) {
433 return size; 433 return size;
434 } else if (result == 0) { 434 } else if (result == 0) {
435 return 0; 435 return 0;
436 } 436 }
437
438 // special case: there is only one element and that is smaller
439 if (size == 1) return 0;
437 440
438 // check the last array element 441 // check the last array element
439 result = cmp_func(elem, array + elem_size * (size - 1)); 442 result = cmp_func(elem, array + elem_size * (size - 1));
440 if (result >= 0) { 443 if (result >= 0) {
441 return size - 1; 444 return size - 1;

mercurial