ObjFW  Diff

Differences From Artifact [9ddf5a7d27]:

To Artifact [79fa6ef127]:


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
55
56
57
58
59
60
61







62
63
64
65
66
67
68







-
-
-
-
-
-
-








static bool
defaultEqual(void *object1, void *object2)
{
	return (object1 == object2);
}

OF_DIRECT_MEMBERS
@interface OFMapTable ()
- (void)of_setObject: (void *)object
	      forKey: (void *)key
		hash: (unsigned long)hash;
@end

OF_DIRECT_MEMBERS
@interface OFMapTableEnumerator ()
- (instancetype)of_initWithMapTable: (OFMapTable *)mapTable
			    buckets: (struct of_map_table_bucket **)buckets
			   capacity: (unsigned long)capacity
		   mutationsPointer: (unsigned long *)mutationsPtr
    OF_METHOD_FAMILY(init);
240
241
242
243
244
245
246
247
248
249



250
251
252
253
254
255
256
257
233
234
235
236
237
238
239



240
241
242

243
244
245
246
247
248
249







-
-
-
+
+
+
-







	    initWithKeyFunctions: _keyFunctions
		 objectFunctions: _objectFunctions
			capacity: _capacity];

	@try {
		for (unsigned long i = 0; i < _capacity; i++)
			if (_buckets[i] != NULL && _buckets[i] != &deleted)
				[copy of_setObject: _buckets[i]->object
					    forKey: _buckets[i]->key
					      hash: OF_ROR(_buckets[i]->hash,
				setObject(copy, _buckets[i]->key,
				    _buckets[i]->object,
				    OF_ROR(_buckets[i]->hash, _rotate));
							_rotate)];
	} @catch (id e) {
		[copy release];
		@throw e;
	}

	return copy;
}
292
293
294
295
296
297
298

299

300
301
302
303
304


305
306
307

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


394
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
284
285
286
287
288
289
290
291

292
293
294
295
296

297
298
299
300

301
302
303

304
305
306

307
308

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

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







+
-
+




-
+
+


-
+


-
+


-
+

-
+







+
-
+




-
-
+
+
+




-
+




-
+








-
+



-
-
-
+
+
+


-
-
-
+
+
+







-
-
+
+

+
-
-
+
+


-
+





-
+

-
-
+
+


+
-
+





+
-
-
+
+


-
+

-
-
+
+

-
-
+
+
+



-
+

-
-
+
+








-
+






-
+

-
+






-
-
+
+




-
-
-
+
+
+




-
+







		if (_keyFunctions.equal(_buckets[i]->key, key))
			return _buckets[i]->object;
	}

	return NULL;
}

static void
- (void)of_resizeForCount: (unsigned long)count OF_DIRECT
resizeForCount(OFMapTable *self, unsigned long count)
{
	unsigned long fullness, capacity;
	struct of_map_table_bucket **buckets;

	if (count > ULONG_MAX / sizeof(*_buckets) || count > ULONG_MAX / 8)
	if (count > ULONG_MAX / sizeof(*self->_buckets) ||
	    count > ULONG_MAX / 8)
		@throw [OFOutOfRangeException exception];

	fullness = count * 8 / _capacity;
	fullness = count * 8 / self->_capacity;

	if (fullness >= 6) {
		if (_capacity > ULONG_MAX / 2)
		if (self->_capacity > ULONG_MAX / 2)
			return;

		capacity = _capacity * 2;
		capacity = self->_capacity * 2;
	} else if (fullness <= 1)
		capacity = _capacity / 2;
		capacity = self->_capacity / 2;
	else
		return;

	/*
	 * Don't downsize if we have an initial capacity or if we would fall
	 * below the minimum capacity.
	 */
	if ((capacity < self->_capacity && count > self->_count) ||
	if ((capacity < _capacity && count > _count) || capacity < MIN_CAPACITY)
	    capacity < MIN_CAPACITY)
		return;

	buckets = of_alloc_zeroed(capacity, sizeof(*buckets));

	for (unsigned long i = 0; i < _capacity; i++) {
		if (_buckets[i] != NULL && _buckets[i] != &deleted) {
	for (unsigned long i = 0; i < self->_capacity; i++) {
		if (self->_buckets[i] != NULL &&
		    self->_buckets[i] != &deleted) {
			unsigned long j, last;

			last = capacity;

			for (j = _buckets[i]->hash & (capacity - 1);
			for (j = self->_buckets[i]->hash & (capacity - 1);
			    j < last && buckets[j] != NULL; j++);

			/* In case the last bucket is already used */
			if (j >= last) {
				last = _buckets[i]->hash & (capacity - 1);
				last = self->_buckets[i]->hash & (capacity - 1);

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

			if (j >= last)
				@throw [OFOutOfRangeException exception];

			buckets[j] = _buckets[i];
			buckets[j] = self->_buckets[i];
		}
	}

	free(_buckets);
	_buckets = buckets;
	_capacity = capacity;
	free(self->_buckets);
	self->_buckets = buckets;
	self->_capacity = capacity;
}

- (void)of_setObject: (void *)object
	      forKey: (void *)key
		hash: (unsigned long)hash
static void
setObject(OFMapTable *restrict self, void *key, void *object,
    unsigned long hash)
{
	unsigned long i, last;
	void *old;

	if (key == NULL || object == NULL)
		@throw [OFInvalidArgumentException exception];

	hash = OF_ROL(hash, _rotate);
	last = _capacity;
	hash = OF_ROL(hash, self->_rotate);
	last = self->_capacity;

	for (i = hash & (self->_capacity - 1);
	for (i = hash & (_capacity - 1); i < last && _buckets[i] != NULL; i++) {
		if (_buckets[i] == &deleted)
	    i < last && self->_buckets[i] != NULL; i++) {
		if (self->_buckets[i] == &deleted)
			continue;

		if (_keyFunctions.equal(_buckets[i]->key, key))
		if (self->_keyFunctions.equal(self->_buckets[i]->key, key))
			break;
	}

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

		for (i = 0; i < last && _buckets[i] != NULL; i++) {
			if (_buckets[i] == &deleted)
		for (i = 0; i < last && self->_buckets[i] != NULL; i++) {
			if (self->_buckets[i] == &deleted)
				continue;

			if (self->_keyFunctions.equal(
			if (_keyFunctions.equal(_buckets[i]->key, key))
			    self->_buckets[i]->key, key))
				break;
		}
	}

	/* Key not in map table */
	if (i >= last || self->_buckets[i] == NULL ||
	if (i >= last || _buckets[i] == NULL || _buckets[i] == &deleted ||
	    !_keyFunctions.equal(_buckets[i]->key, key)) {
	    self->_buckets[i] == &deleted ||
	    !self->_keyFunctions.equal(self->_buckets[i]->key, key)) {
		struct of_map_table_bucket *bucket;

		[self of_resizeForCount: _count + 1];
		resizeForCount(self, self->_count + 1);

		_mutations++;
		last = _capacity;
		self->_mutations++;
		last = self->_capacity;

		for (i = hash & (_capacity - 1); i < last &&
		    _buckets[i] != NULL && _buckets[i] != &deleted; i++);
		for (i = hash & (self->_capacity - 1); i < last &&
		    self->_buckets[i] != NULL && self->_buckets[i] != &deleted;
		    i++);

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

			for (i = 0; i < last && _buckets[i] != NULL &&
			    _buckets[i] != &deleted; i++);
			for (i = 0; i < last && self->_buckets[i] != NULL &&
			    self->_buckets[i] != &deleted; i++);
		}

		if (i >= last)
			@throw [OFOutOfRangeException exception];

		bucket = of_alloc(1, sizeof(*bucket));

		@try {
			bucket->key = _keyFunctions.retain(key);
			bucket->key = self->_keyFunctions.retain(key);
		} @catch (id e) {
			free(bucket);
			@throw e;
		}

		@try {
			bucket->object = _objectFunctions.retain(object);
			bucket->object = self->_objectFunctions.retain(object);
		} @catch (id e) {
			_keyFunctions.release(bucket->key);
			self->_keyFunctions.release(bucket->key);
			free(bucket);
			@throw e;
		}

		bucket->hash = hash;

		_buckets[i] = bucket;
		_count++;
		self->_buckets[i] = bucket;
		self->_count++;

		return;
	}

	old = _buckets[i]->object;
	_buckets[i]->object = _objectFunctions.retain(object);
	_objectFunctions.release(old);
	old = self->_buckets[i]->object;
	self->_buckets[i]->object = self->_objectFunctions.retain(object);
	self->_objectFunctions.release(old);
}

- (void)setObject: (void *)object forKey: (void *)key
{
	[self of_setObject: object forKey: key hash: _keyFunctions.hash(key)];
	setObject(self, key, object,_keyFunctions.hash(key));
}

- (void)removeObjectForKey: (void *)key
{
	unsigned long i, hash, last;

	if (key == NULL)
467
468
469
470
471
472
473
474

475
476
477
478
479
480
481
467
468
469
470
471
472
473

474
475
476
477
478
479
480
481







-
+







			_keyFunctions.release(_buckets[i]->key);
			_objectFunctions.release(_buckets[i]->object);

			free(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			[self of_resizeForCount: _count];
			resizeForCount(self, _count);

			return;
		}
	}

	if (i < last)
		return;
492
493
494
495
496
497
498
499

500
501
502
503
504
505
506
492
493
494
495
496
497
498

499
500
501
502
503
504
505
506







-
+







			_objectFunctions.release(_buckets[i]->object);

			free(_buckets[i]);
			_buckets[i] = &deleted;

			_count--;
			_mutations++;
			[self of_resizeForCount: _count];
			resizeForCount(self, _count);

			return;
		}
	}
}

- (void)removeAllObjects