ObjFW  Diff

Differences From Artifact [81e5d7fd45]:

To Artifact [64cc71dd61]:


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

	@try {
		uint32_t i;
		OFDictionary_hashtable *hashtable;

		if (dictionary == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		if (![dictionary isKindOfClass:
		    [OFDictionary_hashtable class]] &&
		    ![dictionary isKindOfClass:
		    [OFMutableDictionary_hashtable class]])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		hashtable = (OFDictionary_hashtable*)dictionary;

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: hashtable->size];








|







|







64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

	@try {
		uint32_t i;
		OFDictionary_hashtable *hashtable;

		if (dictionary == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		if (![dictionary isKindOfClass:
		    [OFDictionary_hashtable class]] &&
		    ![dictionary isKindOfClass:
		    [OFMutableDictionary_hashtable class]])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		hashtable = (OFDictionary_hashtable*)dictionary;

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: hashtable->size];

128
129
130
131
132
133
134
135

136
137
138
139
140
141
142

143
144
145
146
147
148
149
		OFEnumerator *enumerator;
		id key;
		uint32_t i, newSize;

		count = [dictionary count];

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (i = 0; i < newSize; i++)
			data[i] = NULL;








|
>






|
>







128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
		OFEnumerator *enumerator;
		id key;
		uint32_t i, newSize;

		count = [dictionary count];

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (i = 0; i < newSize; i++)
			data[i] = NULL;

168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
				last = hash & (size - 1);

				for (i = 0; i < last && data[i] != NULL; i++);
			}

			if (data[i] != NULL)
				@throw [OFOutOfRangeException
				    exceptionWithClass: isa];

			bucket = [self allocMemoryWithSize: sizeof(*bucket)];

			object = [dictionary objectForKey: key];

			bucket->key = [key copy];
			bucket->object = [object retain];







|







170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
				last = hash & (size - 1);

				for (i = 0; i < last && data[i] != NULL; i++);
			}

			if (data[i] != NULL)
				@throw [OFOutOfRangeException
				    exceptionWithClass: [self class]];

			bucket = [self allocMemoryWithSize: sizeof(*bucket)];

			object = [dictionary objectForKey: key];

			bucket->key = [key copy];
			bucket->object = [object retain];
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215

	@try {
		uint32_t i;
		struct of_dictionary_hashtable_bucket *bucket;

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

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: 2];

		size = 2;
		for (i = 0; i < size; i++)







|







203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

	@try {
		uint32_t i;
		struct of_dictionary_hashtable_bucket *bucket;

		if (key == nil || object == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: 2];

		size = 2;
		for (i = 0; i < size; i++)
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
	  forKeys: (OFArray*)keys
{
	id ret;

	@try {
		if ([objects count] != [keys count])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa];

		ret = [self initWithObjects: [objects objects]
				    forKeys: [keys objects]
				      count: [objects count]];
	} @catch (id e) {
		[self release];
		@throw e;







|







238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
	  forKeys: (OFArray*)keys
{
	id ret;

	@try {
		if ([objects count] != [keys count])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]];

		ret = [self initWithObjects: [objects objects]
				    forKeys: [keys objects]
				      count: [objects count]];
	} @catch (id e) {
		[self release];
		@throw e;
261
262
263
264
265
266
267
268

269
270
271
272
273
274
275

276
277
278
279
280
281
282

	@try {
		uint32_t i, j, newSize;

		count = count_;

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (j = 0; j < newSize; j++)
			data[j] = NULL;








|
>






|
>







263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

	@try {
		uint32_t i, j, newSize;

		count = count_;

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (j = 0; j < newSize; j++)
			data[j] = NULL;

319
320
321
322
323
324
325
326
327
328
329
330
331
332
333

					for (j = 0; j < last && data[j] != NULL;
					    j++);
				}

				if (j >= last)
					@throw [OFOutOfRangeException
					    exceptionWithClass: isa];

				bucket =
				    [self allocMemoryWithSize: sizeof(*bucket)];
				key = [keys[i] copy];

				bucket->key = key;
				bucket->object = [objects[i] retain];







|







323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

					for (j = 0; j < last && data[j] != NULL;
					    j++);
				}

				if (j >= last)
					@throw [OFOutOfRangeException
					    exceptionWithClass: [self class]];

				bucket =
				    [self allocMemoryWithSize: sizeof(*bucket)];
				key = [keys[i] copy];

				bucket->key = key;
				bucket->object = [objects[i] retain];
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
394
395
396

397
398
399
400
401
402
403
		va_list argumentsCopy;
		struct of_dictionary_hashtable_bucket *bucket;

		va_copy(argumentsCopy, arguments);

		if (firstKey == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		key = firstKey;

		if ((object = va_arg(arguments, id)) == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		count = 1;
		for (; va_arg(argumentsCopy, id) != nil; count++);
		count >>= 1;

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException exceptionWithClass: isa];


		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (j = 0; j < newSize; j++)
			data[j] = NULL;








|






|







|
>






|
>







371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
		va_list argumentsCopy;
		struct of_dictionary_hashtable_bucket *bucket;

		va_copy(argumentsCopy, arguments);

		if (firstKey == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		key = firstKey;

		if ((object = va_arg(arguments, id)) == nil)
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		count = 1;
		for (; va_arg(argumentsCopy, id) != nil; count++);
		count >>= 1;

		if (count > UINT32_MAX)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		for (newSize = 1; newSize < count; newSize <<= 1);
		if (count * 4 / newSize >= 3)
			newSize <<= 1;

		if (newSize == 0)
			@throw [OFOutOfRangeException
			    exceptionWithClass: [self class]];

		data = [self allocMemoryWithSize: sizeof(*data)
					   count: newSize];

		for (j = 0; j < newSize; j++)
			data[j] = NULL;

418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
			uint32_t last;

			key = va_arg(arguments, id);
			object = va_arg(arguments, id);

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

			hash = [key hash];
			last = size;

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







|







424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
			uint32_t last;

			key = va_arg(arguments, id);
			object = va_arg(arguments, id);

			if (key == nil || object == nil)
				@throw [OFInvalidArgumentException
				    exceptionWithClass: [self class]
					      selector: _cmd];

			hash = [key hash];
			last = size;

			for (j = hash & (size - 1); j < last && data[j] != NULL;
			    j++)
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470

					for (j = 0; j < last && data[j] != NULL;
					    j++);
				}

				if (j >= last)
					@throw [OFOutOfRangeException
					    exceptionWithClass: isa];

				bucket =
				    [self allocMemoryWithSize: sizeof(*bucket)];
				bucket->key = [key copy];
				bucket->object = [object retain];
				bucket->hash = hash;








|







462
463
464
465
466
467
468
469
470
471
472
473
474
475
476

					for (j = 0; j < last && data[j] != NULL;
					    j++);
				}

				if (j >= last)
					@throw [OFOutOfRangeException
					    exceptionWithClass: [self class]];

				bucket =
				    [self allocMemoryWithSize: sizeof(*bucket)];
				bucket->key = [key copy];
				bucket->object = [object retain];
				bucket->hash = hash;

502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFXMLElement *keyElement, *objectElement;

		if ((![[element name] isEqual: @"OFDictionary"] &&
		    ![[element name] isEqual: @"OFMutableDictionary"]) ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: isa
				      selector: _cmd];

		dictionary = [OFMutableDictionary dictionary];

		keys = [element elementsForName: @"key"
				      namespace: OF_SERIALIZATION_NS];
		objects = [element elementsForName: @"object"
					 namespace: OF_SERIALIZATION_NS];

		if ([keys count] != [objects count])
			@throw [OFInvalidFormatException
			    exceptionWithClass: isa];

		keyEnumerator = [keys objectEnumerator];
		objectEnumerator = [objects objectEnumerator];
		pool2 = [[OFAutoreleasePool alloc] init];

		while ((keyElement = [keyEnumerator nextObject]) != nil &&
		    (objectElement = [objectEnumerator nextObject]) != nil) {
			OFXMLElement *key, *object;

			key = [[keyElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];
			object = [[objectElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];

			if (key == nil || object == nil)
				@throw [OFInvalidFormatException
				    exceptionWithClass: isa];

			[dictionary setObject: [object objectByDeserializing]
				       forKey: [key objectByDeserializing]];

			[pool2 releaseObjects];
		}








|











|
















|







508
509
510
511
512
513
514
515
516
517
518
519
520
521
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
		OFEnumerator *keyEnumerator, *objectEnumerator;
		OFXMLElement *keyElement, *objectElement;

		if ((![[element name] isEqual: @"OFDictionary"] &&
		    ![[element name] isEqual: @"OFMutableDictionary"]) ||
		    ![[element namespace] isEqual: OF_SERIALIZATION_NS])
			@throw [OFInvalidArgumentException
			    exceptionWithClass: [self class]
				      selector: _cmd];

		dictionary = [OFMutableDictionary dictionary];

		keys = [element elementsForName: @"key"
				      namespace: OF_SERIALIZATION_NS];
		objects = [element elementsForName: @"object"
					 namespace: OF_SERIALIZATION_NS];

		if ([keys count] != [objects count])
			@throw [OFInvalidFormatException
			    exceptionWithClass: [self class]];

		keyEnumerator = [keys objectEnumerator];
		objectEnumerator = [objects objectEnumerator];
		pool2 = [[OFAutoreleasePool alloc] init];

		while ((keyElement = [keyEnumerator nextObject]) != nil &&
		    (objectElement = [objectEnumerator nextObject]) != nil) {
			OFXMLElement *key, *object;

			key = [[keyElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];
			object = [[objectElement elementsForNamespace:
			    OF_SERIALIZATION_NS] firstObject];

			if (key == nil || object == nil)
				@throw [OFInvalidFormatException
				    exceptionWithClass: [self class]];

			[dictionary setObject: [object objectByDeserializing]
				       forKey: [key objectByDeserializing]];

			[pool2 releaseObjects];
		}

555
556
557
558
559
560
561
562

563
564
565
566
567
568
569
570
}

- (id)objectForKey: (id)key
{
	uint32_t i, hash, last;

	if (key == nil)
		@throw [OFInvalidArgumentException exceptionWithClass: isa

							     selector: _cmd];

	hash = [key hash];
	last = size;

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







|
>
|







561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
}

- (id)objectForKey: (id)key
{
	uint32_t i, hash, last;

	if (key == nil)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	hash = [key hash];
	last = size;

	for (i = hash & (size - 1); i < last && data[i] != NULL; i++) {
		if (data[i] == DELETED)
			continue;
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
	[super dealloc];
}

- (void)reset
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: isa
				object: dictionary];

	pos = 0;
}
@end

@implementation OFDictionaryObjectEnumerator_hashtable
- (id)nextObject
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: isa
				object: dictionary];

	for (; pos < size && (data[pos] == NULL ||
	    data[pos] == DELETED); pos++);

	if (pos < size)
		return data[pos++]->object;
	else
		return nil;
}
@end

@implementation OFDictionaryKeyEnumerator_hashtable
- (id)nextObject
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: isa
				object: dictionary];

	for (; pos < size && (data[pos] == NULL ||
	    data[pos] == DELETED); pos++);

	if (pos < size)
		return data[pos++]->key;
	else
		return nil;
}
@end







|











|

















|











805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
	[super dealloc];
}

- (void)reset
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [self class]
				object: dictionary];

	pos = 0;
}
@end

@implementation OFDictionaryObjectEnumerator_hashtable
- (id)nextObject
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [self class]
				object: dictionary];

	for (; pos < size && (data[pos] == NULL ||
	    data[pos] == DELETED); pos++);

	if (pos < size)
		return data[pos++]->object;
	else
		return nil;
}
@end

@implementation OFDictionaryKeyEnumerator_hashtable
- (id)nextObject
{
	if (mutationsPtr != NULL && *mutationsPtr != mutations)
		@throw [OFEnumerationMutationException
		    exceptionWithClass: [self class]
				object: dictionary];

	for (; pos < size && (data[pos] == NULL ||
	    data[pos] == DELETED); pos++);

	if (pos < size)
		return data[pos++]->key;
	else
		return nil;
}
@end