jens@0
|
1 |
//
|
jens@0
|
2 |
// CollectionUtils.h
|
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@11
|
9 |
#import <Foundation/Foundation.h>
|
jens@16
|
10 |
#define _MYUTILITIES_COLLECTIONUTILS_ 1
|
jens@0
|
11 |
|
jens@0
|
12 |
// Collection creation conveniences:
|
jens@0
|
13 |
|
jens@0
|
14 |
#define $array(OBJS...) ({id objs[]={OBJS}; \
|
jens@0
|
15 |
[NSArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
|
jens@0
|
16 |
#define $marray(OBJS...) ({id objs[]={OBJS}; \
|
jens@0
|
17 |
[NSMutableArray arrayWithObjects: objs count: sizeof(objs)/sizeof(id)];})
|
jens@0
|
18 |
|
jens@0
|
19 |
#define $dict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \
|
jens@0
|
20 |
_dictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
|
jens@0
|
21 |
#define $mdict(PAIRS...) ({struct _dictpair pairs[]={PAIRS}; \
|
jens@0
|
22 |
_mdictof(pairs,sizeof(pairs)/sizeof(struct _dictpair));})
|
jens@0
|
23 |
|
jens@0
|
24 |
#define $object(VAL) ({__typeof(VAL) v=(VAL); _box(&v,@encode(__typeof(v)));})
|
jens@0
|
25 |
|
jens@0
|
26 |
|
jens@8
|
27 |
// Apply a selector to each array element, returning an array of the results:
|
jens@8
|
28 |
NSArray* $apply( NSArray *src, SEL selector, id defaultValue );
|
jens@8
|
29 |
NSArray* $applyKeyPath( NSArray *src, NSString *keyPath, id defaultValue );
|
jens@8
|
30 |
|
jens@8
|
31 |
|
jens@0
|
32 |
// Object conveniences:
|
jens@0
|
33 |
|
jens@0
|
34 |
BOOL $equal(id obj1, id obj2); // Like -isEqual: but works even if either/both are nil
|
jens@0
|
35 |
|
jens@9
|
36 |
NSString* $string( const char *utf8Str );
|
jens@9
|
37 |
|
jens@0
|
38 |
#define $sprintf(FORMAT, ARGS... ) [NSString stringWithFormat: (FORMAT), ARGS]
|
jens@0
|
39 |
|
jens@4
|
40 |
#define $cast(CLASSNAME,OBJ) ((CLASSNAME*)(_cast([CLASSNAME class],(OBJ))))
|
jens@4
|
41 |
#define $castNotNil(CLASSNAME,OBJ) ((CLASSNAME*)(_castNotNil([CLASSNAME class],(OBJ))))
|
jens@4
|
42 |
#define $castIf(CLASSNAME,OBJ) ((CLASSNAME*)(_castIf([CLASSNAME class],(OBJ))))
|
jens@0
|
43 |
#define $castArrayOf(ITEMCLASSNAME,OBJ) _castArrayOf([ITEMCLASSNAME class],(OBJ)))
|
jens@0
|
44 |
|
jens@0
|
45 |
void setObj( id *var, id value );
|
jens@0
|
46 |
BOOL ifSetObj( id *var, id value );
|
jens@0
|
47 |
void setString( NSString **var, NSString *value );
|
jens@0
|
48 |
BOOL ifSetString( NSString **var, NSString *value );
|
jens@0
|
49 |
|
jens@0
|
50 |
|
jens@8
|
51 |
#define $true ((NSNumber*)kCFBooleanTrue)
|
jens@8
|
52 |
#define $false ((NSNumber*)kCFBooleanFalse)
|
jens@8
|
53 |
|
jens@8
|
54 |
|
jens@0
|
55 |
@interface NSArray (MYUtils)
|
jens@0
|
56 |
- (BOOL) my_containsObjectIdenticalTo: (id)object;
|
jens@0
|
57 |
@end
|
jens@0
|
58 |
|
jens@0
|
59 |
|
jens@5
|
60 |
@interface NSSet (MYUtils)
|
jens@5
|
61 |
+ (NSSet*) my_unionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
|
jens@7
|
62 |
+ (NSSet*) my_intersectionOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
|
jens@5
|
63 |
+ (NSSet*) my_differenceOfSet: (NSSet*)set1 andSet: (NSSet*)set2;
|
jens@5
|
64 |
@end
|
jens@5
|
65 |
|
jens@5
|
66 |
|
jens@0
|
67 |
#pragma mark -
|
jens@0
|
68 |
#pragma mark FOREACH:
|
jens@0
|
69 |
|
jens@0
|
70 |
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_5
|
jens@0
|
71 |
#define foreach(VAR,ARR) for(VAR in ARR)
|
jens@0
|
72 |
|
jens@0
|
73 |
#else
|
jens@0
|
74 |
struct foreachstate {NSArray *array; unsigned n, i;};
|
jens@0
|
75 |
static inline struct foreachstate _initforeach( NSArray *arr ) {
|
jens@0
|
76 |
struct foreachstate s;
|
jens@0
|
77 |
s.array = arr;
|
jens@0
|
78 |
s.n = [arr count];
|
jens@0
|
79 |
s.i = 0;
|
jens@0
|
80 |
return s;
|
jens@0
|
81 |
}
|
jens@0
|
82 |
#define foreach(VAR,ARR) for( struct foreachstate _s = _initforeach((ARR)); \
|
jens@0
|
83 |
_s.i<_s.n && ((VAR)=[_s.array objectAtIndex: _s.i], YES); \
|
jens@0
|
84 |
_s.i++ )
|
jens@0
|
85 |
#endif
|
jens@0
|
86 |
|
jens@0
|
87 |
|
jens@0
|
88 |
// Internals (don't use directly)
|
jens@0
|
89 |
struct _dictpair { id key; id value; };
|
jens@0
|
90 |
NSDictionary* _dictof(const struct _dictpair*, size_t count);
|
jens@0
|
91 |
NSMutableDictionary* _mdictof(const struct _dictpair*, size_t count);
|
jens@0
|
92 |
NSValue* _box(const void *value, const char *encoding);
|
jens@0
|
93 |
id _cast(Class,id);
|
jens@0
|
94 |
id _castNotNil(Class,id);
|
jens@0
|
95 |
id _castIf(Class,id);
|
jens@0
|
96 |
NSArray* _castArrayOf(Class,NSArray*);
|