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