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