* Insert an element into a sorted list. * The comparison function receives two list elements. */
| 767 | * The comparison function receives two list elements. |
| 768 | */ |
| 769 | void dList_insert_sorted (Dlist *lp, void *data, dCompareFunc func) |
| 770 | { |
| 771 | int i, st, min, max; |
| 772 | |
| 773 | if (lp) { |
| 774 | min = st = i = 0; |
| 775 | max = lp->len - 1; |
| 776 | while (min <= max) { |
| 777 | i = (min + max) / 2; |
| 778 | st = func(lp->list[i], data); |
| 779 | if (st < 0) { |
| 780 | min = i + 1; |
| 781 | } else if (st > 0) { |
| 782 | max = i - 1; |
| 783 | } else { |
| 784 | break; |
| 785 | } |
| 786 | } |
| 787 | dList_insert_pos(lp, data, (st >= 0) ? i : i+1); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
| 792 | * Search a sorted list. |
no test coverage detected