1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/CollectionUtils.m Sun Apr 06 19:13:27 2008 -0700
1.3 @@ -0,0 +1,192 @@
1.4 +//
1.5 +// CollectionUtils.m
1.6 +// MYUtilities
1.7 +//
1.8 +// Created by Jens Alfke on 1/5/08.
1.9 +// Copyright 2008 Jens Alfke. All rights reserved.
1.10 +//
1.11 +
1.12 +#import "CollectionUtils.h"
1.13 +#import "Test.h"
1.14 +
1.15 +
1.16 +NSDictionary* _dictof(const struct _dictpair* pairs, size_t count)
1.17 +{
1.18 + CAssert(count<10000);
1.19 + id objects[count], keys[count];
1.20 + size_t n = 0;
1.21 + for( size_t i=0; i<count; i++,pairs++ ) {
1.22 + if( pairs->value ) {
1.23 + objects[n] = pairs->value;
1.24 + keys[n] = pairs->key;
1.25 + n++;
1.26 + }
1.27 + }
1.28 + return [NSDictionary dictionaryWithObjects: objects forKeys: keys count: n];
1.29 +}
1.30 +
1.31 +
1.32 +NSMutableDictionary* _mdictof(const struct _dictpair* pairs, size_t count)
1.33 +{
1.34 + CAssert(count<10000);
1.35 + id objects[count], keys[count];
1.36 + size_t n = 0;
1.37 + for( size_t i=0; i<count; i++,pairs++ ) {
1.38 + if( pairs->value ) {
1.39 + objects[n] = pairs->value;
1.40 + keys[n] = pairs->key;
1.41 + n++;
1.42 + }
1.43 + }
1.44 + return [NSMutableDictionary dictionaryWithObjects: objects forKeys: keys count: n];
1.45 +}
1.46 +
1.47 +
1.48 +BOOL $equal(id obj1, id obj2) // Like -isEqual: but works even if either/both are nil
1.49 +{
1.50 + if( obj1 )
1.51 + return obj2 && [obj1 isEqual: obj2];
1.52 + else
1.53 + return obj2==nil;
1.54 +}
1.55 +
1.56 +
1.57 +NSValue* _box(const void *value, const char *encoding)
1.58 +{
1.59 + // file:///Developer/Documentation/DocSets/com.apple.ADC_Reference_Library.DeveloperTools.docset/Contents/Resources/Documents/documentation/DeveloperTools/gcc-4.0.1/gcc/Type-encoding.html
1.60 + char e = encoding[0];
1.61 + if( e=='r' ) // ignore 'const' modifier
1.62 + e = encoding[1];
1.63 + switch( e ) {
1.64 + case 'c': return [NSNumber numberWithChar: *(char*)value];
1.65 + case 'C': return [NSNumber numberWithUnsignedChar: *(char*)value];
1.66 + case 's': return [NSNumber numberWithShort: *(short*)value];
1.67 + case 'S': return [NSNumber numberWithUnsignedShort: *(unsigned short*)value];
1.68 + case 'i': return [NSNumber numberWithInt: *(int*)value];
1.69 + case 'I': return [NSNumber numberWithUnsignedInt: *(unsigned int*)value];
1.70 + case 'l': return [NSNumber numberWithLong: *(long*)value];
1.71 + case 'L': return [NSNumber numberWithUnsignedLong: *(unsigned long*)value];
1.72 + case 'q': return [NSNumber numberWithLongLong: *(long long*)value];
1.73 + case 'Q': return [NSNumber numberWithUnsignedLongLong: *(unsigned long long*)value];
1.74 + case 'f': return [NSNumber numberWithFloat: *(float*)value];
1.75 + case 'd': return [NSNumber numberWithDouble: *(double*)value];
1.76 + case '*': return [NSString stringWithUTF8String: *(char**)value];
1.77 + case '@': return *(id*)value;
1.78 + default: return [NSValue value: value withObjCType: encoding];
1.79 + }
1.80 +}
1.81 +
1.82 +
1.83 +id _cast( Class requiredClass, id object )
1.84 +{
1.85 + if( object && ! [object isKindOfClass: requiredClass] )
1.86 + [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
1.87 + requiredClass,[object class],object];
1.88 + return object;
1.89 +}
1.90 +
1.91 +id _castNotNil( Class requiredClass, id object )
1.92 +{
1.93 + if( ! [object isKindOfClass: requiredClass] )
1.94 + [NSException raise: NSInvalidArgumentException format: @"%@ required, but got %@ %p",
1.95 + requiredClass,[object class],object];
1.96 + return object;
1.97 +}
1.98 +
1.99 +id _castIf( Class requiredClass, id object )
1.100 +{
1.101 + if( object && ! [object isKindOfClass: requiredClass] )
1.102 + object = nil;
1.103 + return object;
1.104 +}
1.105 +
1.106 +NSArray* _castArrayOf(Class itemClass, NSArray *a)
1.107 +{
1.108 + id item;
1.109 + foreach( item, $cast(NSArray,a) )
1.110 + _cast(itemClass,item);
1.111 + return a;
1.112 +}
1.113 +
1.114 +
1.115 +void setObj( id *var, id value )
1.116 +{
1.117 + if( value != *var ) {
1.118 + [*var release];
1.119 + *var = [value retain];
1.120 + }
1.121 +}
1.122 +
1.123 +BOOL ifSetObj( id *var, id value )
1.124 +{
1.125 + if( value != *var && ![value isEqual: *var] ) {
1.126 + [*var release];
1.127 + *var = [value retain];
1.128 + return YES;
1.129 + } else {
1.130 + return NO;
1.131 + }
1.132 +}
1.133 +
1.134 +
1.135 +void setString( NSString **var, NSString *value )
1.136 +{
1.137 + if( value != *var ) {
1.138 + [*var release];
1.139 + *var = [value copy];
1.140 + }
1.141 +}
1.142 +
1.143 +
1.144 +BOOL ifSetString( NSString **var, NSString *value )
1.145 +{
1.146 + if( value != *var && ![value isEqualToString: *var] ) {
1.147 + [*var release];
1.148 + *var = [value copy];
1.149 + return YES;
1.150 + } else {
1.151 + return NO;
1.152 + }
1.153 +}
1.154 +
1.155 +
1.156 +@implementation NSArray (MYUtils)
1.157 +
1.158 +- (BOOL) my_containsObjectIdenticalTo: (id)object
1.159 +{
1.160 + return [self indexOfObjectIdenticalTo: object] != NSNotFound;
1.161 +}
1.162 +
1.163 +@end
1.164 +
1.165 +
1.166 +#import "Test.h"
1.167 +
1.168 +TestCase(CollectionUtils) {
1.169 + NSArray *a = $array(@"foo",@"bar",@"baz");
1.170 + //Log(@"a = %@",a);
1.171 + NSArray *aa = [NSArray arrayWithObjects: @"foo",@"bar",@"baz",nil];
1.172 + CAssertEqual(a,aa);
1.173 +
1.174 + const char *cstr = "a C string";
1.175 + id o = $object(cstr);
1.176 + //Log(@"o = %@",o);
1.177 + CAssertEqual(o,@"a C string");
1.178 +
1.179 + NSDictionary *d = $dict({@"int", $object(1)},
1.180 + {@"double", $object(-1.1)},
1.181 + {@"char", $object('x')},
1.182 + {@"ulong", $object(1234567UL)},
1.183 + {@"longlong",$object(987654321LL)},
1.184 + {@"cstr", $object(cstr)});
1.185 + //Log(@"d = %@",d);
1.186 + NSDictionary *dd = [NSDictionary dictionaryWithObjectsAndKeys:
1.187 + [NSNumber numberWithInt: 1], @"int",
1.188 + [NSNumber numberWithDouble: -1.1], @"double",
1.189 + [NSNumber numberWithChar: 'x'], @"char",
1.190 + [NSNumber numberWithUnsignedLong: 1234567UL], @"ulong",
1.191 + [NSNumber numberWithDouble: 987654321LL], @"longlong",
1.192 + @"a C string", @"cstr",
1.193 + nil];
1.194 + CAssertEqual(d,dd);
1.195 +}