Differences From Artifact [9bd99c7ed0]:
- File src/OFArray.m — part of check-in [c5d65603d8] at 2010-08-28 18:54:35 on branch trunk — Initial support for blocks. (user: js, size: 6843) [annotate] [blame] [check-ins using]
To Artifact [1e2bb24ba2]:
- File
src/OFArray.m
— part of check-in
[e2b06423e0]
at
2010-09-05 23:19:08
on branch trunk
— Replace OFObject* with id in many places.
We assume now that every object understands retain, release, etc. so
that we can weaken the type from OFObject* to id. This makes it
possible to use different root object classes. (user: js, size: 6865) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
21 22 23 24 25 26 27 | @implementation OFArray + array { return [[[self alloc] init] autorelease]; } | | | | | | < | | | | | 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | @implementation OFArray + array { return [[[self alloc] init] autorelease]; } + arrayWithObject: (id)obj { return [[[self alloc] initWithObject: obj] autorelease]; } + arrayWithObjects: (id)first, ... { id ret; va_list args; va_start(args, first); ret = [[[self alloc] initWithObject: first argList: args] autorelease]; va_end(args); return ret; } + arrayWithCArray: (id*)objs { return [[[self alloc] initWithCArray: objs] autorelease]; } + arrayWithCArray: (id*)objs length: (size_t)len { return [[[self alloc] initWithCArray: objs length: len] autorelease]; } - init { self = [super init]; @try { array = [[OFDataArray alloc] initWithItemSize: sizeof(id)]; } @catch (OFException *e) { /* * We can't use [super dealloc] on OS X here. Compiler bug? * [self dealloc] will do here as we check for nil in dealloc. */ [self dealloc]; @throw e; } return self; } - initWithObject: (id)obj { self = [self init]; @try { [array addItem: &obj]; } @catch (OFException *e) { [self dealloc]; @throw e; } [obj retain]; return self; } - initWithObjects: (id)first, ... { id ret; va_list args; va_start(args, first); ret = [self initWithObject: first argList: args]; va_end(args); return ret; } - initWithObject: (id)first argList: (va_list)args { id obj; self = [self init]; @try { [array addItem: &first]; while ((obj = va_arg(args, id)) != nil) { [array addItem: &obj]; [obj retain]; } } @catch (OFException *e) { [self dealloc]; @throw e; } return self; } - initWithCArray: (id*)objs { id *obj; size_t count; self = [self init]; count = 0; |
︙ | ︙ | |||
148 149 150 151 152 153 154 | [self dealloc]; @throw e; } return self; } | | | 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 | [self dealloc]; @throw e; } return self; } - initWithCArray: (id*)objs length: (size_t)len { size_t i; self = [self init]; for (i = 0; i < len; i++) |
︙ | ︙ | |||
190 191 192 193 194 195 196 | { return [self retain]; } - mutableCopy { OFArray *new = [[OFMutableArray alloc] init]; | | | | | 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | { return [self retain]; } - mutableCopy { OFArray *new = [[OFMutableArray alloc] init]; id *objs; size_t count, i; objs = [array cArray]; count = [array count]; [new->array addNItems: count fromCArray: objs]; for (i = 0; i < count; i++) [objs[i] retain]; return new; } - (id)objectAtIndex: (size_t)index { return *((id*)[array itemAtIndex: index]); } - (size_t)indexOfObject: (id)obj { id *objs = [array cArray]; size_t i, count = [array count]; if (objs == NULL) return SIZE_MAX; for (i = 0; i < count; i++) if ([objs[i] isEqual: obj]) return i; return SIZE_MAX; } - (size_t)indexOfObjectIdenticalTo: (id)obj { id *objs = [array cArray]; size_t i, count = [array count]; if (objs == NULL) return SIZE_MAX; |
︙ | ︙ | |||
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | } - (OFString*)componentsJoinedByString: (OFString*)separator { OFString *str; OFString **objs = [array cArray]; size_t i, count = [array count]; IMP append; if (count == 0) return @""; if (count == 1) return [objs[0] retain]; str = [OFMutableString string]; append = [str methodForSelector: @selector(appendString:)]; for (i = 0; i < count - 1; i++) { append(str, @selector(appendString:), objs[i]); append(str, @selector(appendString:), separator); } append(str, @selector(appendString:), objs[i]); return str; } | > > > > > > | | | 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 | } - (OFString*)componentsJoinedByString: (OFString*)separator { OFString *str; OFString **objs = [array cArray]; size_t i, count = [array count]; Class cls; IMP append; if (count == 0) return @""; if (count == 1) return [objs[0] retain]; str = [OFMutableString string]; cls = [OFString class]; append = [str methodForSelector: @selector(appendString:)]; for (i = 0; i < count - 1; i++) { if (![objs[i] isKindOfClass: cls]) @throw [OFInvalidArgumentException newWithClass: isa selector: _cmd]; append(str, @selector(appendString:), objs[i]); append(str, @selector(appendString:), separator); } append(str, @selector(appendString:), objs[i]); return str; } - (BOOL)isEqual: (id)obj { id *objs, *objs2; size_t i, count, count2; if (![obj isKindOfClass: [OFArray class]]) return NO; count = [array count]; count2 = [(OFArray*)obj count]; |
︙ | ︙ | |||
304 305 306 307 308 309 310 | return NO; return YES; } - (uint32_t)hash { | | | 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | return NO; return YES; } - (uint32_t)hash { id *objs = [array cArray]; size_t i, count = [array count]; uint32_t hash; OF_HASH_INIT(hash); for (i = 0; i < count; i++) { uint32_t h = [objs[i] hash]; |
︙ | ︙ | |||
350 351 352 353 354 355 356 | initWithDataArray: array mutationsPointer: NULL] autorelease]; } #ifdef OF_HAVE_BLOCKS - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block { | | | | 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | initWithDataArray: array mutationsPointer: NULL] autorelease]; } #ifdef OF_HAVE_BLOCKS - (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block { id *objs = [array cArray]; size_t i, count = [array count]; BOOL stop = NO; for (i = 0; i < count && !stop; i++) block(objs[i], i, &stop); } #endif - (void)dealloc { id *objs = [array cArray]; size_t i, count = [array count]; for (i = 0; i < count; i++) [objs[i] release]; [array release]; |
︙ | ︙ | |||
394 395 396 397 398 399 400 | - (id)nextObject { if (mutationsPtr != NULL && *mutationsPtr != mutations) @throw [OFEnumerationMutationException newWithClass: isa]; if (pos < count) | | | 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 | - (id)nextObject { if (mutationsPtr != NULL && *mutationsPtr != mutations) @throw [OFEnumerationMutationException newWithClass: isa]; if (pos < count) return *(id*)[array itemAtIndex: pos++]; return nil; } - (void)reset { if (mutationsPtr != NULL && *mutationsPtr != mutations) @throw [OFEnumerationMutationException newWithClass: isa]; pos = 0; } @end /// \endcond |