src/array_list.c

changeset 853
d4baf4dd55c3
parent 850
b2bc48c2b251
child 854
fe0d69d72bcd
equal deleted inserted replaced
852:16e2a3391e88 853:d4baf4dd55c3
271 ) { 271 ) {
272 return 1 != cx_arl_insert_array(list, index, element, 1); 272 return 1 != cx_arl_insert_array(list, index, element, 1);
273 } 273 }
274 274
275 static int cx_arl_insert_iter( 275 static int cx_arl_insert_iter(
276 struct cx_mut_iterator_s *iter, 276 struct cx_iterator_s *iter,
277 void const *elem, 277 void const *elem,
278 int prepend 278 int prepend
279 ) { 279 ) {
280 struct cx_list_s *list = iter->src_handle; 280 struct cx_list_s *list = iter->src_handle.m;
281 if (iter->index < list->size) { 281 if (iter->index < list->size) {
282 int result = cx_arl_insert_element( 282 int result = cx_arl_insert_element(
283 list, 283 list,
284 iter->index + 1 - prepend, 284 iter->index + 1 - prepend,
285 elem 285 elem
451 } 451 }
452 } 452 }
453 453
454 static bool cx_arl_iter_valid(void const *it) { 454 static bool cx_arl_iter_valid(void const *it) {
455 struct cx_iterator_s const *iter = it; 455 struct cx_iterator_s const *iter = it;
456 struct cx_list_s const *list = iter->src_handle; 456 struct cx_list_s const *list = iter->src_handle.c;
457 return iter->index < list->size; 457 return iter->index < list->size;
458 } 458 }
459 459
460 static void *cx_arl_iter_current(void const *it) { 460 static void *cx_arl_iter_current(void const *it) {
461 struct cx_iterator_s const *iter = it; 461 struct cx_iterator_s const *iter = it;
462 return iter->elem_handle; 462 return iter->elem_handle;
463 } 463 }
464 464
465 static void cx_arl_iter_next(void *it) { 465 static void cx_arl_iter_next(void *it) {
466 struct cx_iterator_base_s *itbase = it; 466 struct cx_iterator_s *iter = it;
467 if (itbase->remove) { 467 if (iter->remove) {
468 struct cx_mut_iterator_s *iter = it; 468 iter->remove = false;
469 itbase->remove = false; 469 cx_arl_remove(iter->src_handle.m, iter->index);
470 cx_arl_remove(iter->src_handle, iter->index); 470 } else {
471 } else {
472 struct cx_iterator_s *iter = it;
473 iter->index++; 471 iter->index++;
474 iter->elem_handle = 472 iter->elem_handle =
475 ((char *) iter->elem_handle) 473 ((char *) iter->elem_handle)
476 + ((struct cx_list_s const *) iter->src_handle)->item_size; 474 + ((struct cx_list_s const *) iter->src_handle.c)->item_size;
477 } 475 }
478 } 476 }
479 477
480 static void cx_arl_iter_prev(void *it) { 478 static void cx_arl_iter_prev(void *it) {
481 struct cx_iterator_base_s *itbase = it; 479 struct cx_iterator_s *iter = it;
482 struct cx_mut_iterator_s *iter = it; 480 cx_array_list const* list = iter->src_handle.c;
483 cx_array_list *const list = iter->src_handle; 481 if (iter->remove) {
484 if (itbase->remove) { 482 iter->remove = false;
485 itbase->remove = false; 483 cx_arl_remove(iter->src_handle.m, iter->index);
486 cx_arl_remove(iter->src_handle, iter->index);
487 } 484 }
488 iter->index--; 485 iter->index--;
489 if (iter->index < list->base.size) { 486 if (iter->index < list->base.size) {
490 iter->elem_handle = ((char *) list->data) 487 iter->elem_handle = ((char *) list->data)
491 + iter->index * list->base.item_size; 488 + iter->index * list->base.item_size;
499 bool backwards 496 bool backwards
500 ) { 497 ) {
501 struct cx_iterator_s iter; 498 struct cx_iterator_s iter;
502 499
503 iter.index = index; 500 iter.index = index;
504 iter.src_handle = list; 501 iter.src_handle.c = list;
505 iter.elem_handle = cx_arl_at(list, index); 502 iter.elem_handle = cx_arl_at(list, index);
506 iter.elem_size = list->item_size; 503 iter.elem_size = list->item_size;
507 iter.elem_count = list->size; 504 iter.elem_count = list->size;
508 iter.base.valid = cx_arl_iter_valid; 505 iter.valid = cx_arl_iter_valid;
509 iter.base.current = cx_arl_iter_current; 506 iter.current = cx_arl_iter_current;
510 iter.base.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next; 507 iter.next = backwards ? cx_arl_iter_prev : cx_arl_iter_next;
511 iter.base.remove = false; 508 iter.remove = false;
512 iter.base.mutating = false; 509 iter.mutating = false;
513 510
514 return iter; 511 return iter;
515 } 512 }
516 513
517 static cx_list_class cx_array_list_class = { 514 static cx_list_class cx_array_list_class = {

mercurial