ObjFW  Diff

Differences From Artifact [bf50d5443a]:

To Artifact [f2f7830f20]:


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







-
-
+
+












-
+







}

+ dictionaryWithDictionary: (OFDictionary*)dict
{
	return [[[self alloc] initWithDictionary: dict] autorelease];
}

+ dictionaryWithObject: (OFObject*)obj
		forKey: (OFObject <OFCopying>*)key
+ dictionaryWithObject: (id)obj
		forKey: (id <OFCopying>)key
{
	return [[[self alloc] initWithObject: obj
				      forKey: key] autorelease];
}

+ dictionaryWithObjects: (OFArray*)objs
		forKeys: (OFArray*)keys
{
	return [[[self alloc] initWithObjects: objs
				      forKeys: keys] autorelease];
}

+ dictionaryWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
+ dictionaryWithKeysAndObjects: (id <OFCopying>)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);
	ret = [[[self alloc] initWithKey: first
				 argList: args] autorelease];
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
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







-
+
















-
+













-
-
+
+







		@throw e;
	}

	size = dict->size;
	count = dict->count;

	for (i = 0; i < size; i++) {
		OFObject <OFCopying> *key;
		id <OFCopying> key;
		BUCKET *b;

		if (dict->data[i] == NULL || dict->data[i] == DELETED)
			continue;

		@try {
			b = [self allocMemoryWithSize: sizeof(BUCKET)];
			key = [dict->data[i]->key copy];
		} @catch (OFException *e) {
			[self dealloc];
			@throw e;
		}

		@try {
			[dict->data[i]->object retain];
		} @catch (OFException *e) {
			[key release];
			[(id)key release];
			[self dealloc];
			@throw e;
		}

		b->key = key;
		b->object = dict->data[i]->object;
		b->hash = dict->data[i]->hash;
		data[i] = b;
	}

	return self;
}

- initWithObject: (OFObject*)obj
	  forKey: (OFObject <OFCopying>*)key
- initWithObject: (id)obj
	  forKey: (id <OFCopying>)key
{
	uint32_t i;
	BUCKET *b;

	self = [self init];

	@try {
171
172
173
174
175
176
177
178

179
180
181
182
183
184
185
186
187
188
189
190
191

192
193
194
195
196
197
198

199
200
201
202
203
204
205
171
172
173
174
175
176
177

178
179
180
181
182
183
184
185
186
187
188
189
190

191
192
193
194
195
196
197

198
199
200
201
202
203
204
205







-
+












-
+






-
+







		 * Anyway, we didn't do anything yet anyway, so [self dealloc]
		 * works.
		 */
		[self dealloc];
		@throw e;
	}

	i = [key hash] & 1;
	i = [(id)key hash] & 1;

	@try {
		b = [self allocMemoryWithSize: sizeof(BUCKET)];
		key = [key copy];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	@try {
		[obj retain];
	} @catch (OFException *e) {
		[key release];
		[(id)key release];
		[self dealloc];
		@throw e;
	}

	b->key = key;
	b->object = obj;
	b->hash = [key hash];
	b->hash = [(id)key hash];
	data[i] = b;
	count = 1;

	return self;
}

- initWithObjects: (OFArray*)objs
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
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







-
+







-
+





-
+

-
+







	for (i = 0; i < count; i++) {
		uint32_t j, hash, last;

		hash = [keys_carray[i] hash];
		last = size;

		for (j = hash & (size - 1); j < last && data[j] != NULL; j++)
			if ([data[j]->key isEqual: keys_carray[i]])
			if ([(id)data[j]->key isEqual: keys_carray[i]])
				break;

		/* In case the last bucket is already used */
		if (j >= last) {
			last = hash & (size - 1);

			for (j = 0; j < last && data[j] != NULL; j++)
				if ([data[j]->key isEqual: keys_carray[i]])
				if ([(id)data[j]->key isEqual: keys_carray[i]])
					break;
		}

		/* Key not in dictionary */
		if (j >= last || data[j] == NULL ||
		    ![data[j]->key isEqual: keys_carray[i]]) {
		    ![(id)data[j]->key isEqual: keys_carray[i]]) {
			BUCKET *b;
			OFObject <OFCopying> *key;
			id <OFCopying> key;

			last = size;

			j = hash & (size - 1);
			for (; j < last && data[j] != NULL; j++);

			/* In case the last bucket is already used */
290
291
292
293
294
295
296
297

298
299
300
301
302
303
304
290
291
292
293
294
295
296

297
298
299
300
301
302
303
304







-
+







				[self dealloc];
				@throw e;
			}

			@try {
				[objs_carray[i] retain];
			} @catch (OFException *e) {
				[key release];
				[(id)key release];
				[self dealloc];
				@throw e;
			}

			b->key = key;
			b->object = objs_carray[i];
			b->hash = hash;
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
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







-
+












-
+



-
+







-
+








		data[j]->object = objs_carray[i];
	}

	return self;
}

- initWithKeysAndObjects: (OFObject <OFCopying>*)first, ...
- initWithKeysAndObjects: (id <OFCopying>)first, ...
{
	id ret;
	va_list args;

	va_start(args, first);
	ret = [self initWithKey: first
			argList: args];
	va_end(args);

	return ret;
}

- initWithKey: (OFObject <OFCopying>*)key
- initWithKey: (id <OFCopying>)key
      argList: (va_list)args
{
	BUCKET *b;
	OFObject *obj;
	id obj;
	size_t i;
	uint32_t j, hash;
	va_list args2;

	self = [super init];

	count = 1;
	for (va_copy(args2, args); va_arg(args2, OFObject*) != nil; count++);
	for (va_copy(args2, args); va_arg(args2, id) != nil; count++);
	count >>= 1;

	if (count > UINT32_MAX) {
		Class c = isa;
		[self dealloc];
		@throw [OFOutOfRangeException newWithClass: c];
	}
395
396
397
398
399
400
401
402

403
404
405
406
407
408
409
410

411
412
413
414
415
416
417
418
419
420
421
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
453
454
455
456
457
458
459

460
461
462
463
464
465

466
467
468
469
470
471
472
395
396
397
398
399
400
401

402
403
404
405
406
407
408
409

410
411
412
413
414
415
416
417
418
419
420
421
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
453
454
455
456
457
458

459
460
461
462
463
464

465
466
467
468
469
470
471
472







-
+







-
+













-
+












-
-
+
+








-
+



-
+







-
+





-
+







		Class c = isa;
		size = 0;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	if ((obj = va_arg(args, OFObject*)) == nil) {
	if ((obj = va_arg(args, id)) == nil) {
		Class c = isa;
		[self dealloc];
		@throw [OFInvalidArgumentException newWithClass: c
						       selector: _cmd];
	}

	/* Add first key / object pair */
	hash = [key hash];
	hash = [(id)key hash];
	j = hash & (size - 1);

	@try {
		b = [self allocMemoryWithSize: sizeof(BUCKET)];
		key = [key copy];
	} @catch (OFException *e) {
		[self dealloc];
		@throw e;
	}

	@try {
		[obj retain];
	} @catch (OFException *e) {
		[key release];
		[(id)key release];
		[self dealloc];
		@throw e;
	}

	b->key = key;
	b->object = obj;
	b->hash = hash;
	data[j] = b;

	for (i = 1; i < count; i++) {
		uint32_t last;

		key = va_arg(args, OFObject <OFCopying>*);
		obj = va_arg(args, OFObject*);
		key = va_arg(args, id <OFCopying>);
		obj = va_arg(args, id);

		if (key == nil || obj == nil) {
			Class c = isa;
			[self dealloc];
			@throw [OFInvalidArgumentException newWithClass: c
							       selector: _cmd];
		}

		hash = [key hash];
		hash = [(id)key hash];
		last = size;

		for (j = hash & (size - 1); j < last && data[j] != NULL; j++)
			if ([data[j]->key isEqual: key])
			if ([(id)data[j]->key isEqual: key])
				break;

		/* In case the last bucket is already used */
		if (j >= last) {
			last = hash & (size - 1);

			for (j = 0; j < last && data[j] != NULL; j++)
				if ([data[j]->key isEqual: key])
				if ([(id)data[j]->key isEqual: key])
					break;
		}

		/* Key not in dictionary */
		if (j >= last || data[j] == NULL ||
		    ![data[j]->key isEqual: key]) {
		    ![(id)data[j]->key isEqual: key]) {
			last = size;

			j = hash & (size - 1);
			for (; j < last && data[j] != NULL; j++);

			/* In case the last bucket is already used */
			if (j >= last) {
488
489
490
491
492
493
494
495

496
497
498
499
500
501
502
488
489
490
491
492
493
494

495
496
497
498
499
500
501
502







-
+







				[self dealloc];
				@throw e;
			}

			@try {
				[obj retain];
			} @catch (OFException *e) {
				[key release];
				[(id)key release];
				[self dealloc];
				@throw e;
			}

			b->key = key;
			b->object = obj;
			b->hash = hash;
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
567
568
569
570
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
567
568
569
570







-
+














-
+













-
+








		data[j]->object = obj;
	}

	return self;
}

- (id)objectForKey: (OFObject <OFCopying>*)key
- (id)objectForKey: (id)key
{
	uint32_t i, hash, last;

	if (key == nil)
		@throw [OFInvalidArgumentException newWithClass: isa
						       selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([data[i]->key isEqual: key])
		if ([(id)data[i]->key isEqual: key])
			return data[i]->object;
	}

	if (i < last)
		return nil;

	/* In case the last bucket is already used */
	last = hash & (size - 1);

	for (i = 0; i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;

		if ([data[i]->key isEqual: key])
		if ([(id)data[i]->key isEqual: key])
			return data[i]->object;
	}

	return nil;
}

- (size_t)count
652
653
654
655
656
657
658
659

660
661
662
663
664
665
666
652
653
654
655
656
657
658

659
660
661
662
663
664
665
666







-
+








- (void)dealloc
{
	uint32_t i;

	for (i = 0; i < size; i++) {
		if (data[i] != NULL && data[i] != DELETED) {
			[data[i]->key release];
			[(id)data[i]->key release];
			[data[i]->object release];
		}
	}

	[super dealloc];
}