summaryrefslogtreecommitdiff
path: root/Menes
diff options
context:
space:
mode:
authorJay Freeman (saurik) <saurik@saurik.com>2017-02-15 12:57:45 -0800
committerJay Freeman (saurik) <saurik@saurik.com>2017-02-15 12:57:45 -0800
commit5ddde60fb69b9b9d2466044e208977981d2e1fcb (patch)
treebf4be0e7b52c81eefff55f67460a75fd7d776569 /Menes
parent8f246508d03eb6540bf07f6478cf40cf656a5e1c (diff)
Avoid sorting packages_ (it is now Foundation :/).
Diffstat (limited to 'Menes')
-rw-r--r--Menes/radixSortWithSelector.h1
-rw-r--r--Menes/radixSortWithSelector.mm51
2 files changed, 38 insertions, 14 deletions
diff --git a/Menes/radixSortWithSelector.h b/Menes/radixSortWithSelector.h
index b57caf0..6c7f25e 100644
--- a/Menes/radixSortWithSelector.h
+++ b/Menes/radixSortWithSelector.h
@@ -25,6 +25,7 @@
#include <Foundation/Foundation.h>
typedef uint32_t (*MenesRadixSortFunction)(id, void *);
+void CYRadixSortUsingFunction(id *self, size_t count, MenesRadixSortFunction function, void *argument);
@interface NSMutableArray (MenesRadixSortWithSelector)
- (void) radixSortUsingFunction:(MenesRadixSortFunction)function withContext:(void *)argument;
diff --git a/Menes/radixSortWithSelector.mm b/Menes/radixSortWithSelector.mm
index 5523e3c..0c38a33 100644
--- a/Menes/radixSortWithSelector.mm
+++ b/Menes/radixSortWithSelector.mm
@@ -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)