CollectionUtils.m
author Jens Alfke <jens@mooseyard.com>
Thu Mar 20 09:05:58 2008 -0700 (2008-03-20)
changeset 1 e55a17cdabd2
child 5 c9f2e0c7359a
permissions -rw-r--r--
Configurable logging (LogTo).
Added "my_" prefix to category method names.
Added MurmurHash.
Added UniqueWindowController.
Bug fixes.
     1 //
     2 //  CollectionUtils.m
     3 //  MYUtilities
     4 //
     5 //  Created by Jens Alfke on 1/5/08.
     6 //  Copyright 2008 Jens Alfke. All rights reserved.
     7 //
     8 
     9 #import "CollectionUtils.h"
    10 #import "Test.h"
    11 
    12 
    13 NSDictionary* _dictof(const struct _dictpair* pairs, size_t count)
    14 {
    15     CAssert(count<10000);
    16     id objects[count], keys[count];
    17     size_t n = 0;
    18     for( size_t i=0; i<count; i++,pairs++ ) {
    19         if( pairs->value ) {
    20             objects[n] = pairs->value;
    21             keys[n] = pairs->key;
    22             n++;
    23         }
    24     }
    25     return [NSDictionary dictionaryWithObjects: objects forKeys: keys count: n];
    26 }
    27 
    28 
    29 NSMutableDictionary* _mdictof(const struct _dictpair* pairs, size_t count)
    30 {
    31     CAssert(count<10000);
    32     id objects[count], keys[count];
    33     size_t n = 0;
    34     for( size_t i=0; i<count; i++,pairs++ ) {
    35         if( pairs->value ) {
    36             objects[n] = pairs->value;
    37             keys[n] = pairs->key;
    38             n++;
    39         }
    40     }
    41     return [NSMutableDictionary dictionaryWithObjects: objects forKeys: keys count: n];
    42 }
    43 
    44 
    45 BOOL $equal(id obj1, id obj2)      // Like -isEqual: but works even if either/both are nil
    46 {
    47     if( obj1 )
    48         return obj2 && [obj1 isEqual: obj2];
    49     else
    50         return obj2==nil;
    51 }
    52 
    53 
    54 NSValue* _box(const void *value, const char *encoding)
    55 {
    56     // file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
    57     char e = encoding[0];
    58     if( e=='r' )                // ignore 'const' modifier
    59         e = encoding[1];
    60     switch( e ) {
    61         case 'c':   return [NSNumber numberWithChar: *(char*)value];
    62         case 'C':   return [NSNumber numberWithUnsignedChar: *(char*)value];
    63         case 's':   return [NSNumber numberWithShort: *(short*)value];
    64         case 'S':   return [NSNumber numberWithUnsignedShort: *(unsigned short*)value];
    65         case 'i':   return [NSNumber numberWithInt: *(int*)value];
    66         case 'I':   return [NSNumber numberWithUnsignedInt: *(unsigned int*)value];
    67         case 'l':   return [NSNumber numberWithLong: *(long*)value];
    68         case 'L':   return [NSNumber numberWithUnsignedLong: *(unsigned long*)value];
    69         case 'q':   return [NSNumber numberWithLongLong: *(long long*)value];
    70         case 'Q':   return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value];
    71         case 'f':   return [NSNumber numberWithFloat: *(float*)value];
    72         case 'd':   return [NSNumber numberWithDouble: *(double*)value];
    73         case '*':   return [NSString stringWithUTF8String: *(char**)value];
    74         case '@':   return *(id*)value;
    75         default:    return [NSValue value: value withObjCType: encoding];
    76     }
    77 }
    78 
    79 
    80 id _cast( Class requiredClass, id object )
    81 {
    82     if( object && ! [object isKindOfClass: requiredClass] )
    83         [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
    84          requiredClass,[object class],object];
    85     return object;
    86 }
    87 
    88 id _castNotNil( Class requiredClass, id object )
    89 {
    90     if( ! [object isKindOfClass: requiredClass] )
    91         [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
    92          requiredClass,[object class],object];
    93     return object;
    94 }
    95 
    96 id _castIf( Class requiredClass, id object )
    97 {
    98     if( object && ! [object isKindOfClass: requiredClass] )
    99         object = nil;
   100     return object;
   101 }
   102 
   103 NSArray* _castArrayOf(Class itemClass, NSArray *a)
   104 {
   105     id item;
   106     foreach( item, $cast(NSArray,a) )
   107         _cast(itemClass,item);
   108     return a;
   109 }
   110 
   111 
   112 void setObj( id *var, id value )
   113 {
   114     if( value != *var ) {
   115         [*var release];
   116         *var = [value retain];
   117     }
   118 }
   119 
   120 BOOL ifSetObj( id *var, id value )
   121 {
   122     if( value != *var && ![value isEqual: *var] ) {
   123         [*var release];
   124         *var = [value retain];
   125         return YES;
   126     } else {
   127         return NO;
   128     }
   129 }
   130 
   131 
   132 void setString( NSString **var, NSString *value )
   133 {
   134     if( value != *var ) {
   135         [*var release];
   136         *var = [value copy];
   137     }
   138 }
   139 
   140 
   141 BOOL ifSetString( NSString **var, NSString *value )
   142 {
   143     if( value != *var && ![value isEqualToString: *var] ) {
   144         [*var release];
   145         *var = [value copy];
   146         return YES;
   147     } else {
   148         return NO;
   149     }
   150 }
   151 
   152 
   153 @implementation NSArray (MYUtils)
   154 
   155 - (BOOL) my_containsObjectIdenticalTo: (id)object
   156 {
   157     return [self indexOfObjectIdenticalTo: object] != NSNotFound;
   158 }
   159 
   160 @end
   161 
   162 
   163 #import "Test.h"
   164 
   165 TestCase(CollectionUtils) {
   166     NSArray *a = $array(@"foo",@"bar",@"baz");
   167     //Log(@"a = %@",a);
   168     NSArray *aa = [NSArray arrayWithObjects: @"foo",@"bar",@"baz",nil];
   169     CAssertEqual(a,aa);
   170     
   171     const char *cstr = "a C string";
   172     id o = $object(cstr);
   173     //Log(@"o = %@",o);
   174     CAssertEqual(o,@"a C string");
   175     
   176     NSDictionary *d = $dict({@"int",    $object(1)},
   177                             {@"double", $object(-1.1)},
   178                             {@"char",   $object('x')},
   179                             {@"ulong",  $object(1234567UL)},
   180                             {@"longlong",$object(987654321LL)},
   181                             {@"cstr",   $object(cstr)});
   182     //Log(@"d = %@",d);
   183     NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys:
   184                         [NSNumber numberWithInt: 1],                    @"int",
   185                         [NSNumber numberWithDouble: -1.1],              @"double",
   186                         [NSNumber numberWithChar: 'x'],                 @"char",
   187                         [NSNumber numberWithUnsignedLong: 1234567UL],   @"ulong",
   188                         [NSNumber numberWithDouble: 987654321LL],       @"longlong",
   189                         @"a C string",                                  @"cstr",
   190                         nil];
   191     CAssertEqual(d,dd);
   192 }