1. Juri Pakaste
  2. cocoa-scalararrays

Commits

Juri Pakaste  committed 4e57d4b

added sort and sortWithBlock: and one test

  • Participants
  • Parent commits 05266ce
  • Branches feature-sort

Comments (0)

Files changed (3)

File ScalarArray/ScalarArray.h

View file
 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; \

File ScalarArray/ScalarArray.m

View file
 
 #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);
 

File Tests/ScalarArrayTest.m

View file
 @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");
+}
+
 
 @end