-
-
Notifications
You must be signed in to change notification settings - Fork 579
Use libc qsort() for Vector sorting #1784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ in the source distribution for its full text. | |
| #include "XUtils.h" | ||
|
|
||
|
|
||
| static Object_Compare vectorCompareFn; | ||
|
|
||
| Vector* Vector_new(const ObjectClass* type, bool owner, int size) { | ||
| Vector* this; | ||
|
|
||
|
|
@@ -95,38 +97,15 @@ void Vector_prune(Vector* this) { | |
|
|
||
| //static int comparisons = 0; | ||
|
|
||
| /* | ||
| static void swap(Object** array, int indexA, int indexB) { | ||
| assert(indexA >= 0); | ||
| assert(indexB >= 0); | ||
| Object* tmp = array[indexA]; | ||
| array[indexA] = array[indexB]; | ||
| array[indexB] = tmp; | ||
| } | ||
|
|
||
| static int partition(Object** array, int left, int right, int pivotIndex, Object_Compare compare) { | ||
| const Object* pivotValue = array[pivotIndex]; | ||
| swap(array, pivotIndex, right); | ||
| int storeIndex = left; | ||
| for (int i = left; i < right; i++) { | ||
| //comparisons++; | ||
| if (compare(array[i], pivotValue) <= 0) { | ||
| swap(array, i, storeIndex); | ||
| storeIndex++; | ||
| } | ||
| } | ||
| swap(array, storeIndex, right); | ||
| return storeIndex; | ||
| } | ||
|
|
||
| static void quickSort(Object** array, int left, int right, Object_Compare compare) { | ||
| if (left >= right) | ||
| return; | ||
|
|
||
| int pivotIndex = (left + right) / 2; | ||
| int pivotNewIndex = partition(array, left, right, pivotIndex, compare); | ||
| quickSort(array, left, pivotNewIndex - 1, compare); | ||
| quickSort(array, pivotNewIndex + 1, right, compare); | ||
| } | ||
| */ | ||
|
|
||
| // If I were to use only one sorting algorithm for both cases, it would probably be this one: | ||
| /* | ||
|
|
@@ -167,10 +146,15 @@ static void insertionSort(Object** array, int left, int right, Object_Compare co | |
| } | ||
| } | ||
|
|
||
| static int Vector_compareObjects(const void* v1, const void* v2) { | ||
| return vectorCompareFn(*(const Object* const*)v1, *(const Object* const*)v2); | ||
| } | ||
|
|
||
| void Vector_quickSortCustomCompare(Vector* this, Object_Compare compare) { | ||
| assert(compare); | ||
| assert(Vector_isConsistent(this)); | ||
| quickSort(this->array, 0, this->items - 1, compare); | ||
| vectorCompareFn = compare; | ||
| qsort(this->array, this->items, sizeof(Object*), Vector_compareObjects); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it weren't for FWIW: If we wanna be fancy, we could detect a compatible
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wish to keep the changes small and leave the POSIX standardised the GNU version of
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. Exactly because Which leaves the one NULL check I mentioned above as an item TBD …
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @BenBE Yes. I plan to add an
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not what I was referring to. And also won't actually help except as a canary. |
||
| assert(Vector_isConsistent(this)); | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't keep commented-out code around. That's what a VCS is for …
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then I would have to delete the commented-out
combSortcode as well...Would @hishamhm comment on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm happy to provide historical commentary, but I defer to the current maintainers on all things code review!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
combSortvs.insertionSortplace is a different part of the code.But as sorting has currently two implementations, this is some other cleanup task (#1785) anyway …