src/cx/list.h

changeset 1163
68ff0839bc6a
parent 1162
e3bb67b72d33
equal deleted inserted replaced
1162:e3bb67b72d33 1163:68ff0839bc6a
360 cx_attr_nonnull 360 cx_attr_nonnull
361 static inline int cxListAdd( 361 static inline int cxListAdd(
362 CxList *list, 362 CxList *list,
363 const void *elem 363 const void *elem
364 ) { 364 ) {
365 list->collection.sorted = false;
365 return list->cl->insert_element(list, list->collection.size, elem); 366 return list->cl->insert_element(list, list->collection.size, elem);
366 } 367 }
367 368
368 /** 369 /**
369 * Adds multiple items to the end of the list. 370 * Adds multiple items to the end of the list.
385 static inline size_t cxListAddArray( 386 static inline size_t cxListAddArray(
386 CxList *list, 387 CxList *list,
387 const void *array, 388 const void *array,
388 size_t n 389 size_t n
389 ) { 390 ) {
391 list->collection.sorted = false;
390 return list->cl->insert_array(list, list->collection.size, array, n); 392 return list->cl->insert_array(list, list->collection.size, array, n);
391 } 393 }
392 394
393 /** 395 /**
394 * Inserts an item at the specified index. 396 * Inserts an item at the specified index.
407 static inline int cxListInsert( 409 static inline int cxListInsert(
408 CxList *list, 410 CxList *list,
409 size_t index, 411 size_t index,
410 const void *elem 412 const void *elem
411 ) { 413 ) {
414 list->collection.sorted = false;
412 return list->cl->insert_element(list, index, elem); 415 return list->cl->insert_element(list, index, elem);
413 } 416 }
414 417
415 /** 418 /**
416 * Inserts an item into a sorted list. 419 * Inserts an item into a sorted list.
420 *
421 * If the list is not sorted already, the behavior is undefined.
417 * 422 *
418 * @param list the list 423 * @param list the list
419 * @param elem a pointer to the element to add 424 * @param elem a pointer to the element to add
420 * @retval zero success 425 * @retval zero success
421 * @retval non-zero memory allocation failure 426 * @retval non-zero memory allocation failure
423 cx_attr_nonnull 428 cx_attr_nonnull
424 static inline int cxListInsertSorted( 429 static inline int cxListInsertSorted(
425 CxList *list, 430 CxList *list,
426 const void *elem 431 const void *elem
427 ) { 432 ) {
433 list->collection.sorted = true; // guaranteed by definition
428 const void *data = list->collection.store_pointer ? &elem : elem; 434 const void *data = list->collection.store_pointer ? &elem : elem;
429 return list->cl->insert_sorted(list, data, 1) == 0; 435 return list->cl->insert_sorted(list, data, 1) == 0;
430 } 436 }
431 437
432 /** 438 /**
453 CxList *list, 459 CxList *list,
454 size_t index, 460 size_t index,
455 const void *array, 461 const void *array,
456 size_t n 462 size_t n
457 ) { 463 ) {
464 list->collection.sorted = false;
458 return list->cl->insert_array(list, index, array, n); 465 return list->cl->insert_array(list, index, array, n);
459 } 466 }
460 467
461 /** 468 /**
462 * Inserts a sorted array into a sorted list. 469 * Inserts a sorted array into a sorted list.
467 * If there is not enough memory to add all elements, the returned value is 474 * If there is not enough memory to add all elements, the returned value is
468 * less than @p n. 475 * less than @p n.
469 * 476 *
470 * If this list is storing pointers instead of objects @p array is expected to 477 * If this list is storing pointers instead of objects @p array is expected to
471 * be an array of pointers. 478 * be an array of pointers.
479 *
480 * If the list is not sorted already, the behavior is undefined.
472 * 481 *
473 * @param list the list 482 * @param list the list
474 * @param array a pointer to the elements to add 483 * @param array a pointer to the elements to add
475 * @param n the number of elements to add 484 * @param n the number of elements to add
476 * @return the number of added elements 485 * @return the number of added elements
479 static inline size_t cxListInsertSortedArray( 488 static inline size_t cxListInsertSortedArray(
480 CxList *list, 489 CxList *list,
481 const void *array, 490 const void *array,
482 size_t n 491 size_t n
483 ) { 492 ) {
493 list->collection.sorted = true; // guaranteed by definition
484 return list->cl->insert_sorted(list, array, n); 494 return list->cl->insert_sorted(list, array, n);
485 } 495 }
486 496
487 /** 497 /**
488 * Inserts an element after the current location of the specified iterator. 498 * Inserts an element after the current location of the specified iterator.
503 cx_attr_nonnull 513 cx_attr_nonnull
504 static inline int cxListInsertAfter( 514 static inline int cxListInsertAfter(
505 CxIterator *iter, 515 CxIterator *iter,
506 const void *elem 516 const void *elem
507 ) { 517 ) {
508 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 0); 518 CxList* list = iter->src_handle.m;
519 list->collection.sorted = false;
520 return list->cl->insert_iter(iter, elem, 0);
509 } 521 }
510 522
511 /** 523 /**
512 * Inserts an element before the current location of the specified iterator. 524 * Inserts an element before the current location of the specified iterator.
513 * 525 *
527 cx_attr_nonnull 539 cx_attr_nonnull
528 static inline int cxListInsertBefore( 540 static inline int cxListInsertBefore(
529 CxIterator *iter, 541 CxIterator *iter,
530 const void *elem 542 const void *elem
531 ) { 543 ) {
532 return ((struct cx_list_s *) iter->src_handle.m)->cl->insert_iter(iter, elem, 1); 544 CxList* list = iter->src_handle.m;
545 list->collection.sorted = false;
546 return list->cl->insert_iter(iter, elem, 1);
533 } 547 }
534 548
535 /** 549 /**
536 * Removes the element at the specified index. 550 * Removes the element at the specified index.
537 * 551 *
628 * 642 *
629 * @param list the list 643 * @param list the list
630 */ 644 */
631 cx_attr_nonnull 645 cx_attr_nonnull
632 static inline void cxListClear(CxList *list) { 646 static inline void cxListClear(CxList *list) {
647 list->collection.sorted = true; // empty lists are always sorted
633 list->cl->clear(list); 648 list->cl->clear(list);
634 } 649 }
635 650
636 /** 651 /**
637 * Swaps two items in the list. 652 * Swaps two items in the list.
650 static inline int cxListSwap( 665 static inline int cxListSwap(
651 CxList *list, 666 CxList *list,
652 size_t i, 667 size_t i,
653 size_t j 668 size_t j
654 ) { 669 ) {
670 list->collection.sorted = false;
655 return list->cl->swap(list, i, j); 671 return list->cl->swap(list, i, j);
656 } 672 }
657 673
658 /** 674 /**
659 * Returns a pointer to the element at the specified index. 675 * Returns a pointer to the element at the specified index.
872 * @param list the list 888 * @param list the list
873 */ 889 */
874 cx_attr_nonnull 890 cx_attr_nonnull
875 static inline void cxListSort(CxList *list) { 891 static inline void cxListSort(CxList *list) {
876 list->cl->sort(list); 892 list->cl->sort(list);
893 list->collection.sorted = true;
877 } 894 }
878 895
879 /** 896 /**
880 * Reverses the order of the items. 897 * Reverses the order of the items.
881 * 898 *
882 * @param list the list 899 * @param list the list
883 */ 900 */
884 cx_attr_nonnull 901 cx_attr_nonnull
885 static inline void cxListReverse(CxList *list) { 902 static inline void cxListReverse(CxList *list) {
903 // still sorted, but not according to the cmp_func
904 list->collection.sorted = false;
886 list->cl->reverse(list); 905 list->cl->reverse(list);
887 } 906 }
888 907
889 /** 908 /**
890 * Compares a list to another list of the same type. 909 * Compares a list to another list of the same type.

mercurial