Working on the condes feature

git-svn-id: svn://svn.cc65.org/cc65/trunk@451 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2000-11-20 15:22:57 +00:00
parent 9c35f5278a
commit b9970cb7da
35 changed files with 1029 additions and 388 deletions

View File

@@ -225,14 +225,64 @@ void CollReplace (Collection* C, void* Item, unsigned Index)
void CollSort (Collection* C, int (*Compare) (const void*, const void*))
/* Sort the collection using the given compare function.
* BEWARE: The function uses qsort internally, so the Compare function does
* actually get pointers to the object pointers, not just object pointers!
*/
static void QuickSort (Collection* C, int Lo, int Hi,
int (*Compare) (void*, const void*, const void*),
void* Data)
/* Internal recursive sort function. */
{
/* Use qsort */
qsort (C->Items, C->Count, sizeof (void*), Compare);
/* Get a pointer to the items */
void** Items = C->Items;
/* Quicksort */
while (Hi > Lo) {
int I = Lo + 1;
int J = Hi;
while (I <= J) {
while (I <= J && Compare (Data, Items[Lo], Items[I]) >= 0) {
++I;
}
while (I <= J && Compare (Data, Items[Lo], Items[J]) < 0) {
--J;
}
if (I <= J) {
/* Swap I and J */
void* Tmp = Items[I];
Items[I] = Items[J];
Items[J] = Tmp;
++I;
--J;
}
}
if (J != Lo) {
/* Swap J and Lo */
void* Tmp = Items[J];
Items[J] = Items[Lo];
Items[Lo] = Tmp;
}
if (J > (Hi + Lo) / 2) {
QuickSort (C, J + 1, Hi, Compare, Data);
Hi = J - 1;
} else {
QuickSort (C, Lo, J - 1, Compare, Data);
Lo = J + 1;
}
}
}
void CollSort (Collection* C,
int (*Compare) (void*, const void*, const void*),
void* Data)
/* Sort the collection using the given compare function. The data pointer is
* passed as *first* element to the compare function, it's not used by the
* sort function itself. The other two pointer passed to the Compare function
* are pointers to objects.
*/
{
if (C->Count > 1) {
QuickSort (C, 0, C->Count-1, Compare, Data);
}
}