ObjFW  Diff

Differences From Artifact [57851d31d6]:

To Artifact [b8875e845f]:


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
134
135

136
137
138
139


140
141
142
143
144

145
146

147
148
149


150
151
152
153
154
155
156
157
158
159


160
161
162
163
164
165
166
167


168
169
170


171
172
173
174
175


176
177
178
179
180
181
182
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
134

135
136
137


138
139
140
141
142
143

144
145

146
147


148
149
150
151
152
153
154
155
156
157


158
159
160
161
162
163
164
165


166
167
168


169
170
171
172
173


174
175
176
177
178
179
180
181
182







-
+

-
+


-
+


-
+

-
-
-
-
+
+
+
+




-
+

-
+


-
-
+
+

-
-
+
+
















-
+




-
-
+
+








-
+


-
+

-
-
-
-
+
+
+
+




-
-
+
+




-
+

-
-
-
-
+
+
+
+









-
+




-
+


-
-
+
+




-
+

-
+

-
-
+
+








-
-
+
+






-
-
+
+

-
-
+
+



-
-
+
+








@implementation OFArray
+ array
{
	return [[[self alloc] init] autorelease];
}

+ arrayWithObject: (id)obj
+ arrayWithObject: (id)object
{
	return [[[self alloc] initWithObject: obj] autorelease];
	return [[[self alloc] initWithObject: object] autorelease];
}

+ arrayWithObjects: (id)first, ...
+ arrayWithObjects: (id)firstObject, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [[[self alloc] initWithObject: first
				    argList: args] autorelease];
	va_end(args);
	va_start(arguments, firstObject);
	ret = [[[self alloc] initWithObject: firstObject
			       argumentList: arguments] autorelease];
	va_end(arguments);

	return ret;
}

+ arrayWithCArray: (id*)objs
+ arrayWithCArray: (id*)objects
{
	return [[[self alloc] initWithCArray: objs] autorelease];
	return [[[self alloc] initWithCArray: objects] autorelease];
}

+ arrayWithCArray: (id*)objs
	   length: (size_t)len
+ arrayWithCArray: (id*)objects
	   length: (size_t)length
{
	return [[[self alloc] initWithCArray: objs
				      length: len] autorelease];
	return [[[self alloc] initWithCArray: objects
				      length: length] autorelease];
}

- init
{
	self = [super init];

	@try {
		array = [[OFDataArray alloc] initWithItemSize: sizeof(id)];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObject: (id)obj
- initWithObject: (id)object
{
	self = [self init];

	@try {
		[array addItem: &obj];
		[obj retain];
		[array addItem: &object];
		[object retain];
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithObjects: (id)first, ...
- initWithObjects: (id)firstObject, ...
{
	id ret;
	va_list args;
	va_list arguments;

	va_start(args, first);
	ret = [self initWithObject: first
			   argList: args];
	va_end(args);
	va_start(arguments, firstObject);
	ret = [self initWithObject: firstObject
		      argumentList: arguments];
	va_end(arguments);

	return ret;
}

- initWithObject: (id)first
	 argList: (va_list)args
- initWithObject: (id)firstObject
    argumentList: (va_list)arguments
{
	self = [self init];

	@try {
		id obj;
		id object;

		[array addItem: &first];
		while ((obj = va_arg(args, id)) != nil) {
			[array addItem: &obj];
			[obj retain];
		[array addItem: &firstObject];
		while ((object = va_arg(arguments, id)) != nil) {
			[array addItem: &object];
			[object retain];
		}
	} @catch (id e) {
		[self release];
		@throw e;
	}

	return self;
}

- initWithCArray: (id*)objs
- initWithCArray: (id*)objects
{
	self = [self init];

	@try {
		id *obj;
		id *object;
		size_t count = 0;

		for (obj = objs; *obj != nil; obj++) {
			[*obj retain];
		for (object = objects; *object != nil; object++) {
			[*object retain];
			count++;
		}

		[array addNItems: count
		      fromCArray: objs];
		      fromCArray: objects];
	} @catch (id e) {
		id *obj;
		id *object;

		for (obj = objs; *obj != nil; obj++)
			[*obj release];
		for (object = objects; *object != nil; object++)
			[*object release];

		[self release];
		@throw e;
	}

	return self;
}

- initWithCArray: (id*)objs
	  length: (size_t)len
- initWithCArray: (id*)objects
	  length: (size_t)length
{
	self = [self init];

	@try {
		size_t i;

		for (i = 0; i < len; i++)
			[objs[i] retain];
		for (i = 0; i < length; i++)
			[objects[i] retain];

		[array addNItems: len
		      fromCArray: objs];
		[array addNItems: length
		      fromCArray: objects];
	} @catch (id e) {
		size_t i;

		for (i = 0; i < len; i++)
			[objs[i] release];
		for (i = 0; i < length; i++)
			[objects[i] release];

		[self release];
		@throw e;
	}

	return self;
}
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
239

240
241
242

243
244
245
246

247
248
249
250
251
252

253
254

255
256
257

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
300
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

239
240
241

242
243
244
245

246
247
248
249
250
251

252
253

254
255
256

257
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
300







-
-
+
+


-
+


-
-
+
+


-
+

-
+







-
+

-
+


-
+



-
+





-
+

-
+


-
+



-
+





-
+

-
+


-
+



-
+





-
+

-
+


-
+



-
+







-
+

-
+




-
+

-
+







- copy
{
	return [self retain];
}

- mutableCopy
{
	OFArray *new = [[OFMutableArray alloc] init];
	id *objs;
	OFArray *mutableCopy = [[OFMutableArray alloc] init];
	id *cArray;
	size_t count, i;

	objs = [array cArray];
	cArray = [array cArray];
	count = [array count];

	[new->array addNItems: count
		   fromCArray: objs];
	[mutableCopy->array addNItems: count
			   fromCArray: cArray];

	for (i = 0; i < count; i++)
		[objs[i] retain];
		[cArray[i] retain];

	return new;
	return mutableCopy;
}

- (id)objectAtIndex: (size_t)index
{
	return *((id*)[array itemAtIndex: index]);
}

- (size_t)indexOfObject: (id)obj
- (size_t)indexOfObject: (id)object
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
	if (cArray == NULL)
		return OF_INVALID_INDEX;

	for (i = 0; i < count; i++)
		if ([objs[i] isEqual: obj])
		if ([cArray[i] isEqual: object])
			return i;

	return OF_INVALID_INDEX;
}

- (size_t)indexOfObjectIdenticalTo: (id)obj
- (size_t)indexOfObjectIdenticalTo: (id)object
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
	if (cArray == NULL)
		return OF_INVALID_INDEX;

	for (i = 0; i < count; i++)
		if (objs[i] == obj)
		if (cArray[i] == object)
			return i;

	return OF_INVALID_INDEX;
}

- (BOOL)containsObject: (id)obj
- (BOOL)containsObject: (id)object
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
	if (cArray == NULL)
		return NO;

	for (i = 0; i < count; i++)
		if ([objs[i] isEqual: obj])
		if ([cArray[i] isEqual: object])
			return YES;

	return NO;
}

- (BOOL)containsObjectIdenticalTo: (id)obj
- (BOOL)containsObjectIdenticalTo: (id)object
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	if (objs == NULL)
	if (cArray == NULL)
		return NO;

	for (i = 0; i < count; i++)
		if (objs[i] == obj)
		if (cArray[i] == object)
			return YES;

	return NO;
}

- (id)firstObject
{
	id *first = [array firstItem];
	id *firstObject = [array firstItem];

	return (first != NULL ? *first : nil);
	return (firstObject != NULL ? *firstObject : nil);
}

- (id)lastObject
{
	id *last = [array lastItem];
	id *lastObject = [array lastItem];

	return (last != NULL ? *last : nil);
	return (lastObject != NULL ? *lastObject : nil);
}

- (OFArray*)objectsFromIndex: (size_t)start
		     toIndex: (size_t)end
{
	size_t count = [array count];

310
311
312
313
314
315
316
317
318


319
320
321
322
323
324
325

326
327
328


329
330
331
332
333
334


335
336
337
338

339
340
341
342
343
344
345
346
347
348


349
350
351

352

353
354


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
381
382
383
384

385
386
387
388
389
390
391
310
311
312
313
314
315
316


317
318
319
320
321
322
323
324

325
326


327
328
329
330
331
332


333
334
335
336
337

338
339
340
341
342
343
344
345
346


347
348
349
350

351
352
353


354
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
381
382
383
384
385

386
387
388
389
390
391
392
393







-
-
+
+






-
+

-
-
+
+




-
-
+
+



-
+








-
-
+
+


-
+

+
-
-
+
+

-
+


+
+

-

-
+


-
-
+
+


-
+







-
+






-
+







	return [self objectsFromIndex: range.start
			      toIndex: range.start + range.length];
}

- (OFString*)componentsJoinedByString: (OFString*)separator
{
	OFAutoreleasePool *pool;
	OFString *str;
	OFObject **objs = [array cArray];
	OFString *ret;
	OFObject **cArray = [array cArray];
	size_t i, count = [array count];
	IMP append;

	if (count == 0)
		return @"";
	if (count == 1)
		return [objs[0] description];
		return [cArray[0] description];

	str = [OFMutableString string];
	append = [str methodForSelector: @selector(appendString:)];
	ret = [OFMutableString string];
	append = [ret methodForSelector: @selector(appendString:)];

	pool = [[OFAutoreleasePool alloc] init];

	for (i = 0; i < count - 1; i++) {
		append(str, @selector(appendString:), [objs[i] description]);
		append(str, @selector(appendString:), separator);
		append(ret, @selector(appendString:), [cArray[i] description]);
		append(ret, @selector(appendString:), separator);

		[pool releaseObjects];
	}
	append(str, @selector(appendString:), [objs[i] description]);
	append(ret, @selector(appendString:), [cArray[i] description]);

	[pool release];

	/*
	 * Class swizzle the string to be immutable. We declared the return type
	 * to be OFString*, so it can't be modified anyway. But not swizzling it
	 * would create a real copy each time -[copy] is called.
	 */
	str->isa = [OFString class];
	return str;
	ret->isa = [OFString class];
	return ret;
}

- (BOOL)isEqual: (id)obj
- (BOOL)isEqual: (id)object
{
	OFArray *otherArray;
	id *objs, *objs2;
	size_t i, count, count2;
	id *cArray, *otherCArray;
	size_t i, count;

	if (![obj isKindOfClass: [OFArray class]])
	if (![object isKindOfClass: [OFArray class]])
		return NO;

	otherArray = (OFArray*)object;

	count = [array count];
	count2 = [(OFArray*)obj count];

	if (count != count2)
	if (count != [otherArray count])
		return NO;

	objs = [array cArray];
	objs2 = [(OFArray*)obj cArray];
	cArray = [array cArray];
	otherCArray = [otherArray cArray];

	for (i = 0; i < count; i++)
		if (![objs[i] isEqual: objs2[i]])
		if (![cArray[i] isEqual: otherCArray[i]])
			return NO;

	return YES;
}

- (uint32_t)hash
{
	id *objs = [array cArray];
	id *cArray = [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];
		uint32_t h = [cArray[i] hash];

		OF_HASH_ADD(hash, h >> 24);
		OF_HASH_ADD(hash, (h >> 16) & 0xFF);
		OF_HASH_ADD(hash, (h >> 8) & 0xFF);
		OF_HASH_ADD(hash, h & 0xFF);
	}

422
423
424
425
426
427
428
429

430
431
432
433
434


435
436
437
438

439
440

441
442
443
444
445


446
447
448
449
450
451
452
424
425
426
427
428
429
430

431
432
433
434


435
436
437
438
439

440
441

442
443
444
445


446
447
448
449
450
451
452
453
454







-
+



-
-
+
+



-
+

-
+



-
-
+
+







	 */
	ret->isa = [OFString class];
	return ret;
}

- (void)makeObjectsPerformSelector: (SEL)selector
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		((void(*)(id, SEL))[objs[i]
		    methodForSelector: selector])(objs[i], selector);
		((void(*)(id, SEL))[cArray[i]
		    methodForSelector: selector])(cArray[i], selector);
}

- (void)makeObjectsPerformSelector: (SEL)selector
			withObject: (id)obj
			withObject: (id)object
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		((void(*)(id, SEL, id))[objs[i]
		    methodForSelector: selector])(objs[i], selector, obj);
		((void(*)(id, SEL, id))[cArray[i]
		    methodForSelector: selector])(cArray[i], selector, object);
}

- (int)countByEnumeratingWithState: (of_fast_enumeration_state_t*)state
			   objects: (id*)objects
			     count: (int)count_
{
	size_t count = [array count];
471
472
473
474
475
476
477
478

479
480
481
482
483

484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

500
501
502
503

504
505
506
507
508
509
510
473
474
475
476
477
478
479

480
481
482
483
484

485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500

501
502
503
504

505
506
507
508
509
510
511
512







-
+




-
+















-
+



-
+







					mutationsPointer: NULL] autorelease];
}

#ifdef OF_HAVE_BLOCKS
- (void)enumerateObjectsUsingBlock: (of_array_enumeration_block_t)block
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];
	BOOL stop = NO;

	for (i = 0; i < count && !stop; i++) {
		block(objs[i], i, &stop);
		block(cArray[i], i, &stop);
		[pool releaseObjects];
	}

	[pool release];
}

- (OFArray*)mappedArrayUsingBlock: (of_array_map_block_t)block
{
	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFArray *ret;
	size_t count = [array count];
	id *tmp = [self allocMemoryForNItems: count
				    withSize: sizeof(id)];

	@try {
		id *objs = [array cArray];
		id *cArray = [array cArray];
		size_t i;

		for (i = 0; i < count; i++)
			tmp[i] = block(objs[i], i);
			tmp[i] = block(cArray[i], i);

		ret = [[OFArray alloc] initWithCArray: tmp
					       length: count];

		@try {
			[pool release];
		} @finally {
522
523
524
525
526
527
528
529

530
531
532
533
534


535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553

554
555
556
557

558
559
560
561
562
563
564
524
525
526
527
528
529
530

531
532
533
534


535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554

555
556
557
558

559
560
561
562
563
564
565
566







-
+



-
-
+
+


















-
+



-
+







	OFAutoreleasePool *pool = [[OFAutoreleasePool alloc] init];
	OFArray *ret;
	size_t count = [array count];
	id *tmp = [self allocMemoryForNItems: count
				    withSize: sizeof(id)];

	@try {
		id *objs = [array cArray];
		id *cArray = [array cArray];
		size_t i, j = 0;

		for (i = 0; i < count; i++) {
			if (block(objs[i], i))
				tmp[j++] = objs[i];
			if (block(cArray[i], i))
				tmp[j++] = cArray[i];

			[pool releaseObjects];
		}

		[pool release];

		ret = [OFArray arrayWithCArray: tmp
					length: j];
	} @finally {
		[self freeMemory: tmp];
	}

	return ret;
}
#endif

- (void)dealloc
{
	id *objs = [array cArray];
	id *cArray = [array cArray];
	size_t i, count = [array count];

	for (i = 0; i < count; i++)
		[objs[i] release];
		[cArray[i] release];

	[array release];

	[super dealloc];
}
@end