1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2014 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
22 #include "CyteKit/UCPlatform.h"
24 #include "Menes/radixSortWithSelector.h"
26 #include <objc/runtime.h>
33 @implementation NSMutableArray (MenesRadixSortWithSelector)
35 - (void) radixSortUsingFunction:(MenesRadixSortFunction)function withContext:(void *)argument {
36 size_t count([self count]);
37 struct RadixItem_ *swap(new RadixItem_[count * 2]);
39 for (size_t i(0); i != count; ++i) {
40 RadixItem_ &item(swap[i]);
43 id object([self objectAtIndex:i]);
44 item.key = function(object, argument);
47 struct RadixItem_ *lhs(swap), *rhs(swap + count);
49 static const size_t width = 32;
50 static const size_t bits = 11;
51 static const size_t slots = 1 << bits;
52 static const size_t passes = (width + (bits - 1)) / bits;
54 size_t *hist(new size_t[slots]);
56 for (size_t pass(0); pass != passes; ++pass) {
57 memset(hist, 0, sizeof(size_t) * slots);
59 for (size_t i(0); i != count; ++i) {
60 uint32_t key(lhs[i].key);
62 key &= _not(uint32_t) >> width - bits;
67 for (size_t i(0); i != slots; ++i) {
73 for (size_t i(0); i != count; ++i) {
74 uint32_t key(lhs[i].key);
76 key &= _not(uint32_t) >> width - bits;
77 rhs[hist[key]++] = lhs[i];
87 const void **values(new const void *[count]);
88 for (size_t i(0); i != count; ++i)
89 values[i] = [self objectAtIndex:lhs[i].index];
90 CFArrayReplaceValues((CFMutableArrayRef) self, CFRangeMake(0, count), values, count);
96 - (void) radixSortUsingSelector:(SEL)selector {
97 if ([self count] == 0)
100 IMP imp(class_getMethodImplementation([[self lastObject] class], selector));
101 [self radixSortUsingFunction:reinterpret_cast<MenesRadixSortFunction>(imp) withContext:selector];