Commits

Juri Pakaste committed c2e28f6 Merge

merged

Comments (0)

Files changed (3)

ScalarArray/ScalarArray.h

 typedef t (^ Scalar ## ct ## ValueFromObject)(id object); \
 typedef BOOL (^ Scalar ## ct ## TestEqual)(t v0, t v1); \
 typedef NSUInteger (^ Scalar ## ct ## ValueHash)(t value); \
+typedef int (^ Scalar ## ct ## ComparisonBlock)(const void *v0, const void *v1); \
 @interface Scalar ## ct ## Array : NSObject \
 -(void)addValue:(t)val; \
 -(void)addValuesFromArray:(NSArray*)array withBlock:(Scalar ## ct ## ValueFromObject)block; \
 -(void)removeValuesInRange:(NSRange)range; \
 -(void)replaceValueAtIndex:(NSUInteger)index withValue:(t)value; \
 -(void)reverse; \
+-(void)sort; \
+-(void)sortWithBlock:(Scalar ## ct ## ComparisonBlock)block; \
 -(t)valueAtIndex:(NSUInteger)index; \
 @end
 
     return NSMakeRange(i * tsize, tsize);
 }
 
-#define SCALAR_ARRAY_IMPLEMENTATION_TYPE(t, ct, defaultValue, defaultEquals, valueHash) \
+#define SCALAR_ARRAY_IMPLEMENTATION_TYPE(t, ct, defaultValue, defaultEquals, valueHash, defaultSort) \
 @interface Scalar ## ct ## Array () \
 @property (nonatomic, readonly, retain) NSMutableData *data; \
 @property (nonatomic, readonly, assign) NSUInteger count; \
         bytes[j] = temp; \
     }; \
 } \
+-(void)sort { \
+    if (defaultSort != nil) \
+        [self sortWithBlock:defaultSort]; \
+} \
+-(void)sortWithBlock:(Scalar ## ct ## ComparisonBlock)block { \
+    t *bytes = (t*)[data mutableBytes]; \
+    qsort_b(bytes, count, sizeof(t), block); \
+} \
 -(void)dealloc { \
     [data release]; data = nil; \
     count = 0; \

ScalarArray/ScalarArray.m

 
 #import "ScalarArray.h"
 
-SCALAR_ARRAY_IMPLEMENTATION_TYPE(double, Double, 0.0, ^(double v0, double v1) { return (BOOL)(v0 == v1); }, ^(double v) { return (NSUInteger)v ^ 71; });
-SCALAR_ARRAY_IMPLEMENTATION_TYPE(NSInteger, NSInteger, 0, nil, nil);
-SCALAR_ARRAY_IMPLEMENTATION_TYPE(CGFloat, CGFloat, 0.0, nil, nil);
-SCALAR_ARRAY_IMPLEMENTATION_TYPE(CGRect, CGRect,  ((CGRect){ (CGPoint){0, 0}, (CGSize){ 0, 0 } }), nil, nil);
+SCALAR_ARRAY_IMPLEMENTATION_TYPE(double,
+                                 Double,
+                                 0.0, 
+                                 ^(double v0, double v1) { return (BOOL)(v0 == v1); },
+                                 ^(double v) { return (NSUInteger)v ^ 71; },
+                                 ^(const void *v0, const void *v1) {
+                                     const double dv0 = *(const double*)v0;
+                                     const double dv1 = *(const double*)v1;
+                                     return dv0 < dv1 ? -1 : ((dv0 > dv1) ? 1 : 0);
+                                 });
+SCALAR_ARRAY_IMPLEMENTATION_TYPE(NSInteger, NSInteger, 0, nil, nil, nil);
+SCALAR_ARRAY_IMPLEMENTATION_TYPE(CGFloat, CGFloat, 0.0, nil, nil, nil);
+SCALAR_ARRAY_IMPLEMENTATION_TYPE(CGRect, CGRect,  ((CGRect){ (CGPoint){0, 0}, (CGSize){ 0, 0 } }), nil, nil, nil);
 

Tests/ScalarArrayTest.m

 @end
 
 SCALAR_ARRAY_INTERFACE_TYPE(double, NoEqualsDouble);
-SCALAR_ARRAY_IMPLEMENTATION_TYPE(double, NoEqualsDouble, 0.0, nil, nil);
+SCALAR_ARRAY_IMPLEMENTATION_TYPE(double, NoEqualsDouble, 0.0, nil, nil, nil);
 
 
 
 }
 
 
+-(void)testDefaultSort
+{
+    ScalarDoubleArray *da0 = [[[ScalarDoubleArray alloc] init] autorelease];
+    [da0 addValue:32.0];
+    [da0 addValue:10.0];
+    [da0 addValue:49.0];
+    [da0 sort];
+    GHAssertEquals([da0 valueAtIndex:0], 10.0, @"Wrong value 0");
+    GHAssertEquals([da0 valueAtIndex:1], 32.0, @"Wrong value 1");
+    GHAssertEquals([da0 valueAtIndex:2], 49.0, @"Wrong value 2");
+}
+
+
+-(void)testSortEmpty
+{
+    ScalarDoubleArray *da0 = [[[ScalarDoubleArray alloc] init] autorelease];
+    [da0 sort];
+    GHAssertEquals([da0 count], 0UL, @"Expected 0 items after sort");
+}
+
+
+-(void)testSortOne
+{
+    ScalarDoubleArray *da0 = [[[ScalarDoubleArray alloc] init] autorelease];
+    [da0 addValue:54.0];
+    [da0 sort];
+    GHAssertEquals([da0 count], 1UL, @"Expected 0 items after sort");
+    GHAssertEquals([da0 valueAtIndex:0], 54.0, @"Wrong value 0");
+}
+
+
+-(void)testInverseSort
+{
+    ScalarDoubleArray *da0 = [[[ScalarDoubleArray alloc] init] autorelease];
+    [da0 addValue:32.0];
+    [da0 addValue:10.5];
+    [da0 addValue:49.0];
+    [da0 sortWithBlock:^int(const void *v0, const void *v1) {
+        double dv0 = *(const double*)v0;
+        double dv1 = *(const double*)v1;
+        return (dv0 < dv1) ? 1 : ((dv0 > dv1) ? -1 : 0);
+    }];
+    GHAssertEquals([da0 valueAtIndex:0], 49.0, @"Wrong value 0");
+    GHAssertEquals([da0 valueAtIndex:1], 32.0, @"Wrong value 1");
+    GHAssertEquals([da0 valueAtIndex:2], 10.5, @"Wrong value 2");
+}
+
 
 @end
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.