src/array_list.c

changeset 630
ac5e7f789048
parent 629
6c81ee4f11ad
child 638
eafb45eefc51
equal deleted inserted replaced
629:6c81ee4f11ad 630:ac5e7f789048
241 return 0; 241 return 0;
242 } 242 }
243 } 243 }
244 244
245 static int cx_arl_insert_iter( 245 static int cx_arl_insert_iter(
246 struct cx_iterator_s *iter, 246 struct cx_mut_iterator_s *iter,
247 void const *elem, 247 void const *elem,
248 int prepend 248 int prepend
249 ) { 249 ) {
250 struct cx_list_s *list = iter->src_handle; 250 struct cx_list_s *list = iter->src_handle;
251 if (iter->index < list->size) { 251 if (iter->index < list->size) {
365 for (size_t i = 0; i < half; i++) { 365 for (size_t i = 0; i < half; i++) {
366 cx_array_swap(data, list->itemsize, i, list->size - 1 - i); 366 cx_array_swap(data, list->itemsize, i, list->size - 1 - i);
367 } 367 }
368 } 368 }
369 369
370 static bool cx_arl_iter_valid(struct cx_iterator_s const *iter) { 370 static bool cx_arl_iter_valid(void const *it) {
371 struct cx_iterator_s const *iter = it;
371 struct cx_list_s const *list = iter->src_handle; 372 struct cx_list_s const *list = iter->src_handle;
372 return iter->index < list->size; 373 return iter->index < list->size;
373 } 374 }
374 375
375 static void *cx_arl_iter_current(struct cx_iterator_s const *iter) { 376 static void *cx_arl_iter_current(void const *it) {
377 struct cx_iterator_s const *iter = it;
376 return iter->elem_handle; 378 return iter->elem_handle;
377 } 379 }
378 380
379 static void cx_arl_iter_next(struct cx_iterator_s *iter) { 381 static void cx_arl_iter_next(void *it) {
380 if (iter->remove) { 382 struct cx_iterator_base_s *itbase = it;
381 iter->remove = false; 383 if (itbase->remove) {
384 struct cx_mut_iterator_s *iter = it;
385 itbase->remove = false;
382 cx_arl_remove(iter->src_handle, iter->index); 386 cx_arl_remove(iter->src_handle, iter->index);
383 } else { 387 } else {
388 struct cx_iterator_s *iter = it;
384 iter->index++; 389 iter->index++;
385 iter->elem_handle = 390 iter->elem_handle =
386 ((char *) iter->elem_handle) 391 ((char *) iter->elem_handle)
387 + ((struct cx_list_s const *) iter->src_handle)->itemsize; 392 + ((struct cx_list_s const *) iter->src_handle)->itemsize;
388 } 393 }
389 } 394 }
390 395
396 static bool cx_arl_iter_flag_rm(void *it) {
397 struct cx_iterator_base_s *iter = it;
398 if (iter->mutating) {
399 iter->remove = true;
400 return true;
401 } else {
402 return false;
403 }
404 }
405
391 static struct cx_iterator_s cx_arl_iterator( 406 static struct cx_iterator_s cx_arl_iterator(
392 struct cx_list_s *list, 407 struct cx_list_s const *list,
393 size_t index 408 size_t index
394 ) { 409 ) {
395 struct cx_iterator_s iter; 410 struct cx_iterator_s iter;
396 411
397 iter.index = index; 412 iter.index = index;
398 iter.src_handle = list; 413 iter.src_handle = list;
399 iter.elem_handle = cx_arl_at(list, index); 414 iter.elem_handle = cx_arl_at(list, index);
400 iter.valid = cx_arl_iter_valid; 415 iter.base.valid = cx_arl_iter_valid;
401 iter.current = cx_arl_iter_current; 416 iter.base.current = cx_arl_iter_current;
402 iter.next = cx_arl_iter_next; 417 iter.base.next = cx_arl_iter_next;
403 iter.remove = false; 418 iter.base.flag_removal = cx_arl_iter_flag_rm;
404 419 iter.base.remove = false;
420 iter.base.mutating = false;
421
422 return iter;
423 }
424
425 static struct cx_mut_iterator_s cx_arl_mut_iterator(
426 struct cx_list_s *list,
427 size_t index
428 ) {
429 CxIterator it = cx_arl_iterator(list, index);
430 it.base.mutating = true;
431
432 // we know the iterators share the same memory layout
433 CxMutIterator iter;
434 memcpy(&iter, &it, sizeof(CxMutIterator));
405 return iter; 435 return iter;
406 } 436 }
407 437
408 static cx_list_class cx_array_list_class = { 438 static cx_list_class cx_array_list_class = {
409 cx_arl_destructor, 439 cx_arl_destructor,
416 cx_arl_find, 446 cx_arl_find,
417 cx_arl_sort, 447 cx_arl_sort,
418 cx_arl_compare, 448 cx_arl_compare,
419 cx_arl_reverse, 449 cx_arl_reverse,
420 cx_arl_iterator, 450 cx_arl_iterator,
451 cx_arl_mut_iterator,
421 }; 452 };
422 453
423 CxList *cxArrayListCreate( 454 CxList *cxArrayListCreate(
424 CxAllocator const *allocator, 455 CxAllocator const *allocator,
425 CxListComparator comparator, 456 CxListComparator comparator,

mercurial