src/cx/array_list.h

changeset 1622
27e7a4bf1a39
parent 1621
c52a4c67e29e
child 1623
592aae491264
equal deleted inserted replaced
1621:c52a4c67e29e 1622:27e7a4bf1a39
395 * Internal function - do not use. 395 * Internal function - do not use.
396 * 396 *
397 * @param allocator the allocator to use for a possible reallocation 397 * @param allocator the allocator to use for a possible reallocation
398 * @param array a pointer to the array structure 398 * @param array a pointer to the array structure
399 * @param elem_size the size of one element 399 * @param elem_size the size of one element
400 * @param cmp_func
401 * @param sorted_data a pointer to an array of data that shall be inserted 400 * @param sorted_data a pointer to an array of data that shall be inserted
402 * @param n the number of elements that shall be inserted 401 * @param n the number of elements that shall be inserted
402 * @param cmp_func the compare function
403 * @param allow_duplicates @c false if duplicates shall be skipped during insertion 403 * @param allow_duplicates @c false if duplicates shall be skipped during insertion
404 * @retval zero success 404 * @retval zero success
405 * @retval non-zero a re-allocation was necessary but failed 405 * @retval non-zero a re-allocation was necessary but failed
406 */ 406 */
407 cx_attr_nonnull 407 cx_attr_nonnull
408 CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array, 408 CX_EXPORT int cx_array_insert_sorted_(const CxAllocator *allocator, CxArray *array,
409 size_t elem_size, cx_compare_func cmp_func, const void *sorted_data, size_t n, 409 size_t elem_size, const void *sorted_data, size_t n,
410 bool allow_duplicates); 410 cx_compare_func cmp_func, bool allow_duplicates);
411 411
412 /** 412 /**
413 * Inserts an element into a sorted array. 413 * Inserts an element into a sorted array.
414 * 414 *
415 * When the capacity is not enough to hold the new element, a re-allocation is attempted. 415 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
416 * 416 *
417 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. 417 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
418 * 418 *
419 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation 419 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
420 * @param array the name of the array where the elements shall be inserted 420 * @param array the name of the array where the elements shall be inserted
421 * @param element (@c void*) a pointer to element that shall be inserted
421 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order 422 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
423 * @retval zero success
424 * @retval non-zero a re-allocation was necessary but failed
425 */
426 #define cx_array_insert_sorted_a(allocator, array, element, cmp_func) \
427 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, true)
428
429 /**
430 * Inserts an element into a sorted array.
431 *
432 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
433 *
434 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
435 *
436 * @param array the name of the array where the elements shall be inserted
422 * @param element (@c void*) a pointer to element that shall be inserted 437 * @param element (@c void*) a pointer to element that shall be inserted
423 * @retval zero success
424 * @retval non-zero a re-allocation was necessary but failed
425 */
426 #define cx_array_insert_sorted_a(allocator, array, cmp_func, element) \
427 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, true)
428
429 /**
430 * Inserts an element into a sorted array.
431 *
432 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
433 *
434 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
435 *
436 * @param array the name of the array where the elements shall be inserted
437 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order 438 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
438 * @param element (@c void*) a pointer to element that shall be inserted 439 * @retval zero success
439 * @retval zero success 440 * @retval non-zero a re-allocation was necessary but failed
440 * @retval non-zero a re-allocation was necessary but failed 441 */
441 */ 442 #define cx_array_insert_sorted(array, element, cmp_func) \
442 #define cx_array_insert_sorted(array, cmp_func, element) \ 443 cx_array_insert_sorted_a(cxDefaultAllocator, array, element, cmp_func)
443 cx_array_insert_sorted_a(cxDefaultAllocator, array, cmp_func, element)
444 444
445 /** 445 /**
446 * Inserts sorted data into a sorted array. 446 * Inserts sorted data into a sorted array.
447 * 447 *
448 * When the capacity is not enough to hold the new elements, a re-allocation is attempted. 448 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
449 * 449 *
450 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. 450 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
451 * 451 *
452 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation 452 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
453 * @param array the name of the array where the elements shall be inserted 453 * @param array the name of the array where the elements shall be inserted
454 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
455 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted 454 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
456 * @param n (@c size_t) the number of elements that shall be inserted 455 * @param n (@c size_t) the number of elements that shall be inserted
457 * @retval zero success 456 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
458 * @retval non-zero a re-allocation was necessary but failed 457 * @retval zero success
459 */ 458 * @retval non-zero a re-allocation was necessary but failed
460 #define cx_array_insert_sorted_array_a(allocator, array, cmp_func, sorted_data, n) \ 459 */
461 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, true) 460 #define cx_array_insert_sorted_array_a(allocator, array, sorted_data, n, cmp_func) \
461 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, true)
462 462
463 /** 463 /**
464 * Inserts sorted data into a sorted array. 464 * Inserts sorted data into a sorted array.
465 * 465 *
466 * When the capacity is not enough to hold the new elements, a re-allocation is attempted. 466 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
467 * 467 *
468 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. 468 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
469 * 469 *
470 * @param array the name of the array where the elements shall be inserted 470 * @param array the name of the array where the elements shall be inserted
471 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
472 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted 471 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
473 * @param n (@c size_t) the number of elements that shall be inserted 472 * @param n (@c size_t) the number of elements that shall be inserted
474 * @retval zero success 473 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
475 * @retval non-zero a re-allocation was necessary but failed 474 * @retval zero success
476 */ 475 * @retval non-zero a re-allocation was necessary but failed
477 #define cx_array_insert_sorted_array(array, cmp_func, sorted_data, n) \ 476 */
478 cx_array_insert_sorted_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) 477 #define cx_array_insert_sorted_array(array, sorted_data, n, cmp_func) \
478 cx_array_insert_sorted_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func)
479 479
480 /** 480 /**
481 * Inserts an element into a sorted array if it is not already contained. 481 * Inserts an element into a sorted array if it is not already contained.
482 * 482 *
483 * When the capacity is not enough to hold the new element, a re-allocation is attempted. 483 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
484 * 484 *
485 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined. 485 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
486 * 486 *
487 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation 487 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
488 * @param array the name of the array where the elements shall be inserted 488 * @param array the name of the array where the elements shall be inserted
489 * @param element (@c void*) a pointer to element that shall be inserted
489 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order 490 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
491 * @retval zero success
492 * @retval non-zero a re-allocation was necessary but failed
493 */
494 #define cx_array_insert_unique_a(allocator, array, element, cmp_func) \
495 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, false)
496
497 /**
498 * Inserts an element into a sorted array if it is not already contained.
499 *
500 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
501 *
502 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
503 *
504 * @param array the name of the array where the elements shall be inserted
490 * @param element (@c void*) a pointer to element that shall be inserted 505 * @param element (@c void*) a pointer to element that shall be inserted
491 * @retval zero success
492 * @retval non-zero a re-allocation was necessary but failed
493 */
494 #define cx_array_insert_unique_a(allocator, array, cmp_func, element) \
495 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, element, 1, false)
496
497 /**
498 * Inserts an element into a sorted array if it is not already contained.
499 *
500 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
501 *
502 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
503 *
504 * @param array the name of the array where the elements shall be inserted
505 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order 506 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
506 * @param element (@c void*) a pointer to element that shall be inserted 507 * @retval zero success
507 * @retval zero success 508 * @retval non-zero a re-allocation was necessary but failed
508 * @retval non-zero a re-allocation was necessary but failed 509 */
509 */ 510 #define cx_array_insert_unique(array, element, cmp_func) \
510 #define cx_array_insert_unique(array, cmp_func, element) \ 511 cx_array_insert_unique_a(cxDefaultAllocator, array, element, cmp_func)
511 cx_array_insert_unique_a(cxDefaultAllocator, array, cmp_func, element)
512 512
513 /** 513 /**
514 * Inserts sorted data into a sorted array, skipping duplicates. 514 * Inserts sorted data into a sorted array, skipping duplicates.
515 * 515 *
516 * When the capacity is not enough to hold the new elements, a re-allocation is attempted. 516 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
517 * 517 *
518 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. 518 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
519 * 519 *
520 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation 520 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
521 * @param array the name of the array where the elements shall be inserted 521 * @param array the name of the array where the elements shall be inserted
522 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
523 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted 522 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
524 * @param n (@c size_t) the number of elements that shall be inserted 523 * @param n (@c size_t) the number of elements that shall be inserted
525 * @retval zero success 524 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
526 * @retval non-zero a re-allocation was necessary but failed 525 * @retval zero success
527 */ 526 * @retval non-zero a re-allocation was necessary but failed
528 #define cx_array_insert_unique_array_a(allocator, array, cmp_func, sorted_data, n) \ 527 */
529 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), cmp_func, sorted_data, n, false) 528 #define cx_array_insert_unique_array_a(allocator, array, sorted_data, n, cmp_func) \
529 cx_array_insert_sorted_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, false)
530 530
531 /** 531 /**
532 * Inserts sorted data into a sorted array, skipping duplicates. 532 * Inserts sorted data into a sorted array, skipping duplicates.
533 * 533 *
534 * When the capacity is not enough to hold the new elements, a re-allocation is attempted. 534 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
535 * 535 *
536 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined. 536 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
537 * 537 *
538 * @param array the name of the array where the elements shall be inserted 538 * @param array the name of the array where the elements shall be inserted
539 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
540 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted 539 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
541 * @param n (@c size_t) the number of elements that shall be inserted 540 * @param n (@c size_t) the number of elements that shall be inserted
542 * @retval zero success 541 * @param cmp_func (@c cx_compare_func) the compare function that establishes the order
543 * @retval non-zero a re-allocation was necessary but failed 542 * @retval zero success
544 */ 543 * @retval non-zero a re-allocation was necessary but failed
545 #define cx_array_insert_unique_array(array, cmp_func, sorted_data, n) \ 544 */
546 cx_array_insert_unique_array_a(cxDefaultAllocator, array, cmp_func, sorted_data, n) 545 #define cx_array_insert_unique_array(array, sorted_data, n, cmp_func) \
546 cx_array_insert_unique_array_a(cxDefaultAllocator, array, sorted_data, n, cmp_func)
547
548 /**
549 * Inserts sorted data into a sorted array.
550 *
551 * Internal function - do not use.
552 *
553 * @param allocator the allocator to use for a possible reallocation
554 * @param array a pointer to the array structure
555 * @param elem_size the size of one element
556 * @param sorted_data a pointer to an array of data that shall be inserted
557 * @param n the number of elements that shall be inserted
558 * @param cmp_func the compare function
559 * @param context additional context for the compare function
560 * @param allow_duplicates @c false if duplicates shall be skipped during insertion
561 * @retval zero success
562 * @retval non-zero a re-allocation was necessary but failed
563 */
564 cx_attr_nonnull
565 CX_EXPORT int cx_array_insert_sorted_c_(const CxAllocator *allocator, CxArray *array,
566 size_t elem_size, const void *sorted_data, size_t n,
567 cx_compare_func2 cmp_func, void *context, bool allow_duplicates);
568
569 /**
570 * Inserts an element into a sorted array.
571 *
572 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
573 *
574 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
575 *
576 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
577 * @param array the name of the array where the elements shall be inserted
578 * @param element (@c void*) a pointer to element that shall be inserted
579 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
580 * @param context (@c void*) additional context for the compare function
581 * @retval zero success
582 * @retval non-zero a re-allocation was necessary but failed
583 */
584 #define cx_array_insert_sorted_ca(allocator, array, element, cmp_func) \
585 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, true)
586
587 /**
588 * Inserts an element into a sorted array.
589 *
590 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
591 *
592 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
593 *
594 * @param array the name of the array where the elements shall be inserted
595 * @param element (@c void*) a pointer to element that shall be inserted
596 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
597 * @param context (@c void*) additional context for the compare function
598 * @retval zero success
599 * @retval non-zero a re-allocation was necessary but failed
600 */
601 #define cx_array_insert_sorted_c(array, element, cmp_func, context) \
602 cx_array_insert_sorted_ca(cxDefaultAllocator, array, element, cmp_func, context)
603
604 /**
605 * Inserts sorted data into a sorted array.
606 *
607 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
608 *
609 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
610 *
611 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
612 * @param array the name of the array where the elements shall be inserted
613 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
614 * @param n (@c size_t) the number of elements that shall be inserted
615 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
616 * @param context (@c void*) additional context for the compare function
617 * @retval zero success
618 * @retval non-zero a re-allocation was necessary but failed
619 */
620 #define cx_array_insert_sorted_array_ca(allocator, array, sorted_data, n, cmp_func, context) \
621 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, true)
622
623 /**
624 * Inserts sorted data into a sorted array.
625 *
626 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
627 *
628 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
629 *
630 * @param array the name of the array where the elements shall be inserted
631 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
632 * @param n (@c size_t) the number of elements that shall be inserted
633 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
634 * @param context (@c void*) additional context for the compare function
635 * @retval zero success
636 * @retval non-zero a re-allocation was necessary but failed
637 */
638 #define cx_array_insert_sorted_array_c(array, sorted_data, n, cmp_func, context) \
639 cx_array_insert_sorted_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context)
640
641 /**
642 * Inserts an element into a sorted array if it is not already contained.
643 *
644 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
645 *
646 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
647 *
648 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
649 * @param array the name of the array where the elements shall be inserted
650 * @param element (@c void*) a pointer to element that shall be inserted
651 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
652 * @param context (@c void*) additional context for the compare function
653 * @retval zero success
654 * @retval non-zero a re-allocation was necessary but failed
655 */
656 #define cx_array_insert_unique_ca(allocator, array, element, cmp_func, context) \
657 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), element, 1, cmp_func, context, false)
658
659 /**
660 * Inserts an element into a sorted array if it is not already contained.
661 *
662 * When the capacity is not enough to hold the new element, a re-allocation is attempted.
663 *
664 * @attention if the array is not sorted according to the specified @p cmp_func, the behavior is undefined.
665 *
666 * @param array the name of the array where the elements shall be inserted
667 * @param element (@c void*) a pointer to element that shall be inserted
668 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
669 * @param context (@c void*) additional context for the compare function
670 * @retval zero success
671 * @retval non-zero a re-allocation was necessary but failed
672 */
673 #define cx_array_insert_unique_c(array, element, cmp_func, context) \
674 cx_array_insert_unique_ca(cxDefaultAllocator, array, element, cmp_func, context)
675
676 /**
677 * Inserts sorted data into a sorted array, skipping duplicates.
678 *
679 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
680 *
681 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
682 *
683 * @param allocator (@c CxAllocator*) the allocator to use for a possible reallocation
684 * @param array the name of the array where the elements shall be inserted
685 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
686 * @param n (@c size_t) the number of elements that shall be inserted
687 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
688 * @param context (@c void*) additional context for the compare function
689 * @retval zero success
690 * @retval non-zero a re-allocation was necessary but failed
691 */
692 #define cx_array_insert_unique_array_ca(allocator, array, sorted_data, n, cmp_func, context) \
693 cx_array_insert_sorted_c_(allocator, (CxArray*)&(array), sizeof((array).data[0]), sorted_data, n, cmp_func, context, false)
694
695 /**
696 * Inserts sorted data into a sorted array, skipping duplicates.
697 *
698 * When the capacity is not enough to hold the new elements, a re-allocation is attempted.
699 *
700 * @attention if either array is not sorted according to the specified @p cmp_func, the behavior is undefined.
701 *
702 * @param array the name of the array where the elements shall be inserted
703 * @param sorted_data (@c void*) a pointer to an array of sorted data that shall be inserted
704 * @param n (@c size_t) the number of elements that shall be inserted
705 * @param cmp_func (@c cx_compare_func2) the compare function that establishes the order
706 * @param context (@c void*) additional context for the compare function
707 * @retval zero success
708 * @retval non-zero a re-allocation was necessary but failed
709 */
710 #define cx_array_insert_unique_array_c(array, sorted_data, n, cmp_func, context) \
711 cx_array_insert_unique_array_ca(cxDefaultAllocator, array, sorted_data, n, cmp_func, context)
547 712
548 /** 713 /**
549 * An alternative to qsort_r() when that is not available on your platform. 714 * An alternative to qsort_r() when that is not available on your platform.
550 * 715 *
551 * If it is available, qsort_r() is used directly. 716 * If it is available, qsort_r() is used directly.

mercurial