| 105 | **********************************************************************/ |
| 106 | |
| 107 | void |
| 108 | ELIST::sort ( //sort elements |
| 109 | int comparator ( //comparison routine |
| 110 | const void *, const void *)) { |
| 111 | ELIST_ITERATOR it(this); |
| 112 | inT32 count; |
| 113 | ELIST_LINK **base; //ptr array to sort |
| 114 | ELIST_LINK **current; |
| 115 | inT32 i; |
| 116 | |
| 117 | /* Allocate an array of pointers, one per list element */ |
| 118 | count = length (); |
| 119 | base = (ELIST_LINK **) malloc (count * sizeof (ELIST_LINK *)); |
| 120 | |
| 121 | /* Extract all elements, putting the pointers in the array */ |
| 122 | current = base; |
| 123 | for (it.mark_cycle_pt (); !it.cycled_list (); it.forward ()) { |
| 124 | *current = it.extract (); |
| 125 | current++; |
| 126 | } |
| 127 | |
| 128 | /* Sort the pointer array */ |
| 129 | qsort ((char *) base, count, sizeof (*base), comparator); |
| 130 | |
| 131 | /* Rebuild the list from the sorted pointers */ |
| 132 | current = base; |
| 133 | for (i = 0; i < count; i++) { |
| 134 | it.add_to_end (*current); |
| 135 | current++; |
| 136 | } |
| 137 | free(base); |
| 138 | } |
| 139 | |
| 140 | // Assuming list has been sorted already, insert new_link to |
| 141 | // keep the list sorted according to the same comparison function. |
nothing calls this directly
no test coverage detected