X-Git-Url: https://git.saurik.com/cydia.git/blobdiff_plain/6fa0bb60da6b661c2aafc03f11b842ee7136a9bb..367a112d938db5c80814b42c428b517e4a65efd9:/Menes/radixSortWithSelector.mm diff --git a/Menes/radixSortWithSelector.mm b/Menes/radixSortWithSelector.mm index 23dd6240..0c38a334 100644 --- a/Menes/radixSortWithSelector.mm +++ b/Menes/radixSortWithSelector.mm @@ -1,5 +1,5 @@ /* Cydia - iPhone UIKit Front-End for Debian APT - * Copyright (C) 2008-2014 Jay Freeman (saurik) + * Copyright (C) 2008-2015 Jay Freeman (saurik) */ /* GNU General Public License, Version 3 {{{ */ @@ -30,20 +30,7 @@ struct RadixItem_ { uint32_t key; }; -@implementation NSMutableArray (MenesRadixSortWithSelector) - -- (void) radixSortUsingFunction:(MenesRadixSortFunction)function withContext:(void *)argument { - size_t count([self count]); - struct RadixItem_ *swap(new RadixItem_[count * 2]); - - for (size_t i(0); i != count; ++i) { - RadixItem_ &item(swap[i]); - item.index = i; - - id object([self objectAtIndex:i]); - item.key = function(object, argument); - } - +static RadixItem_ *CYRadixSort(struct RadixItem_ *swap, size_t count) { struct RadixItem_ *lhs(swap), *rhs(swap + count); static const size_t width = 32; @@ -83,6 +70,42 @@ struct RadixItem_ { } delete [] hist; + return lhs; +} + +void CYRadixSortUsingFunction(id *self, size_t count, MenesRadixSortFunction function, void *argument) { + struct RadixItem_ *swap(new RadixItem_[count * 2]); + + for (size_t i(0); i != count; ++i) { + RadixItem_ &item(swap[i]); + item.index = i; + item.key = function(self[i], argument); + } + + auto lhs(CYRadixSort(swap, count)); + + const void **values(new const void *[count]); + for (size_t i(0); i != count; ++i) + values[i] = self[lhs[i].index]; + memcpy(self, values, count * sizeof(id)); + delete [] values; + + delete [] swap; +} + +@implementation NSMutableArray (MenesRadixSortWithSelector) + +- (void) radixSortUsingFunction:(MenesRadixSortFunction)function withContext:(void *)argument { + size_t count([self count]); + struct RadixItem_ *swap(new RadixItem_[count * 2]); + + for (size_t i(0); i != count; ++i) { + RadixItem_ &item(swap[i]); + item.index = i; + item.key = function([self objectAtIndex:i], argument); + } + + auto lhs(CYRadixSort(swap, count)); const void **values(new const void *[count]); for (size_t i(0); i != count; ++i)