@@ -26,17 +26,17 @@ #import "OFEnumerationMutationException.h" #import "OFInvalidArgumentException.h" #import "OFOutOfRangeException.h" -#define MIN_CAPACITY 16 +static const unsigned long minCapacity = 16; struct OFMapTableBucket { void *key, *object; unsigned long hash; }; -static struct OFMapTableBucket deleted = { 0 }; +static struct OFMapTableBucket deletedBucket = { 0 }; static void * defaultRetain(void *object) { return object; @@ -147,12 +147,12 @@ if (capacity * 8 / _capacity >= 6) if (_capacity <= ULONG_MAX / 2) _capacity *= 2; - if (_capacity < MIN_CAPACITY) - _capacity = MIN_CAPACITY; + if (_capacity < minCapacity) + _capacity = minCapacity; _buckets = OFAllocZeroedMemory(_capacity, sizeof(*_buckets)); if (OFHashSeed != 0) _rotate = OFRandom16() & 31; @@ -165,11 +165,11 @@ } - (void)dealloc { for (unsigned long i = 0; i < _capacity; i++) { - if (_buckets[i] != NULL && _buckets[i] != &deleted) { + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) { _keyFunctions.release(_buckets[i]->key); _objectFunctions.release(_buckets[i]->object); OFFreeMemory(_buckets[i]); } @@ -205,18 +205,18 @@ /* * 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) || - capacity < MIN_CAPACITY) + capacity < minCapacity) return; buckets = OFAllocZeroedMemory(capacity, sizeof(*buckets)); for (unsigned long i = 0; i < self->_capacity; i++) { if (self->_buckets[i] != NULL && - self->_buckets[i] != &deleted) { + self->_buckets[i] != &deletedBucket) { unsigned long j, last; last = capacity; for (j = self->_buckets[i]->hash & (capacity - 1); @@ -255,11 +255,11 @@ hash = OFRotateLeft(hash, self->_rotate); last = self->_capacity; for (i = hash & (self->_capacity - 1); i < last && self->_buckets[i] != NULL; i++) { - if (self->_buckets[i] == &deleted) + if (self->_buckets[i] == &deletedBucket) continue; if (self->_keyFunctions.equal(self->_buckets[i]->key, key)) break; } @@ -267,11 +267,11 @@ /* In case the last bucket is already used */ if (i >= last) { last = hash & (self->_capacity - 1); for (i = 0; i < last && self->_buckets[i] != NULL; i++) { - if (self->_buckets[i] == &deleted) + if (self->_buckets[i] == &deletedBucket) continue; if (self->_keyFunctions.equal( self->_buckets[i]->key, key)) break; @@ -278,29 +278,29 @@ } } /* Key not in map table */ if (i >= last || self->_buckets[i] == NULL || - self->_buckets[i] == &deleted || + self->_buckets[i] == &deletedBucket || !self->_keyFunctions.equal(self->_buckets[i]->key, key)) { struct OFMapTableBucket *bucket; resizeForCount(self, self->_count + 1); self->_mutations++; last = self->_capacity; for (i = hash & (self->_capacity - 1); i < last && - self->_buckets[i] != NULL && self->_buckets[i] != &deleted; - i++); + self->_buckets[i] != NULL && + self->_buckets[i] != &deletedBucket; i++); /* In case the last bucket is already used */ if (i >= last) { last = hash & (self->_capacity - 1); for (i = 0; i < last && self->_buckets[i] != NULL && - self->_buckets[i] != &deleted; i++); + self->_buckets[i] != &deletedBucket; i++); } if (i >= last) @throw [OFOutOfRangeException exception]; @@ -350,11 +350,11 @@ mapTable->_keyFunctions.equal != _keyFunctions.equal || mapTable->_objectFunctions.equal != _objectFunctions.equal) return false; for (unsigned long i = 0; i < _capacity; i++) { - if (_buckets[i] != NULL && _buckets[i] != &deleted) { + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) { void *objectIter = [mapTable objectForKey: _buckets[i]->key]; if (!_objectFunctions.equal(objectIter, _buckets[i]->object)) @@ -368,11 +368,11 @@ - (unsigned long)hash { unsigned long hash = 0; for (unsigned long i = 0; i < _capacity; i++) { - if (_buckets[i] != NULL && _buckets[i] != &deleted) { + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) { hash ^= OFRotateRight(_buckets[i]->hash, _rotate); hash ^= _objectFunctions.hash(_buckets[i]->object); } } @@ -386,11 +386,12 @@ objectFunctions: _objectFunctions capacity: _capacity]; @try { for (unsigned long i = 0; i < _capacity; i++) - if (_buckets[i] != NULL && _buckets[i] != &deleted) + if (_buckets[i] != NULL && + _buckets[i] != &deletedBucket) setObject(copy, _buckets[i]->key, _buckets[i]->object, OFRotateRight(_buckets[i]->hash, _rotate)); } @catch (id e) { [copy release]; @@ -414,11 +415,11 @@ hash = OFRotateLeft(_keyFunctions.hash(key), _rotate); last = _capacity; for (i = hash & (_capacity - 1); i < last && _buckets[i] != NULL; i++) { - if (_buckets[i] == &deleted) + if (_buckets[i] == &deletedBucket) continue; if (_keyFunctions.equal(_buckets[i]->key, key)) return _buckets[i]->object; } @@ -428,11 +429,11 @@ /* In case the last bucket is already used */ last = hash & (_capacity - 1); for (i = 0; i < last && _buckets[i] != NULL; i++) { - if (_buckets[i] == &deleted) + if (_buckets[i] == &deletedBucket) continue; if (_keyFunctions.equal(_buckets[i]->key, key)) return _buckets[i]->object; } @@ -454,21 +455,21 @@ hash = OFRotateLeft(_keyFunctions.hash(key), _rotate); last = _capacity; for (i = hash & (_capacity - 1); i < last && _buckets[i] != NULL; i++) { - if (_buckets[i] == &deleted) + if (_buckets[i] == &deletedBucket) continue; if (_keyFunctions.equal(_buckets[i]->key, key)) { _mutations++; _keyFunctions.release(_buckets[i]->key); _objectFunctions.release(_buckets[i]->object); OFFreeMemory(_buckets[i]); - _buckets[i] = &deleted; + _buckets[i] = &deletedBucket; _count--; resizeForCount(self, _count); return; @@ -480,19 +481,19 @@ /* In case the last bucket is already used */ last = hash & (_capacity - 1); for (i = 0; i < last && _buckets[i] != NULL; i++) { - if (_buckets[i] == &deleted) + if (_buckets[i] == &deletedBucket) continue; if (_keyFunctions.equal(_buckets[i]->key, key)) { _keyFunctions.release(_buckets[i]->key); _objectFunctions.release(_buckets[i]->object); OFFreeMemory(_buckets[i]); - _buckets[i] = &deleted; + _buckets[i] = &deletedBucket; _count--; _mutations++; resizeForCount(self, _count); @@ -503,11 +504,11 @@ - (void)removeAllObjects { for (unsigned long i = 0; i < _capacity; i++) { if (_buckets[i] != NULL) { - if (_buckets[i] == &deleted) { + if (_buckets[i] == &deletedBucket) { _buckets[i] = NULL; continue; } _keyFunctions.release(_buckets[i]->key); @@ -517,11 +518,11 @@ _buckets[i] = NULL; } } _count = 0; - _capacity = MIN_CAPACITY; + _capacity = minCapacity; _buckets = OFResizeMemory(_buckets, _capacity, sizeof(*_buckets)); /* * Get a new random value for _rotate, so that it is not less secure * than creating a new hash map. @@ -534,11 +535,11 @@ { if (object == NULL || _count == 0) return false; for (unsigned long i = 0; i < _capacity; i++) - if (_buckets[i] != NULL && _buckets[i] != &deleted) + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) if (_objectFunctions.equal(_buckets[i]->object, object)) return true; return false; } @@ -547,11 +548,11 @@ { if (object == NULL || _count == 0) return false; for (unsigned long i = 0; i < _capacity; i++) - if (_buckets[i] != NULL && _buckets[i] != &deleted) + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) if (_buckets[i]->object == object) return true; return false; } @@ -581,11 +582,11 @@ unsigned long j = state->state; int i; for (i = 0; i < count; i++) { for (; j < _capacity && (_buckets[j] == NULL || - _buckets[j] == &deleted); j++); + _buckets[j] == &deletedBucket); j++); if (j < _capacity) { objects[i] = _buckets[j]->key; j++; } else @@ -608,11 +609,11 @@ for (size_t i = 0; i < _capacity && !stop; i++) { if (_mutations != mutations) @throw [OFEnumerationMutationException exceptionWithObject: self]; - if (_buckets[i] != NULL && _buckets[i] != &deleted) + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) block(_buckets[i]->key, _buckets[i]->object, &stop); } } - (void)replaceObjectsUsingBlock: (OFMapTableReplaceBlock)block @@ -622,11 +623,11 @@ for (size_t i = 0; i < _capacity; i++) { if (_mutations != mutations) @throw [OFEnumerationMutationException exceptionWithObject: self]; - if (_buckets[i] != NULL && _buckets[i] != &deleted) { + if (_buckets[i] != NULL && _buckets[i] != &deletedBucket) { void *new; new = block(_buckets[i]->key, _buckets[i]->object); if (new == NULL) @throw [OFInvalidArgumentException exception]; @@ -683,11 +684,11 @@ if (*_mutationsPtr != _mutations) @throw [OFEnumerationMutationException exceptionWithObject: _mapTable]; for (; _position < _capacity && (_buckets[_position] == NULL || - _buckets[_position] == &deleted); _position++); + _buckets[_position] == &deletedBucket); _position++); if (_position < _capacity) return &_buckets[_position++]->key; else return NULL; @@ -700,11 +701,11 @@ if (*_mutationsPtr != _mutations) @throw [OFEnumerationMutationException exceptionWithObject: _mapTable]; for (; _position < _capacity && (_buckets[_position] == NULL || - _buckets[_position] == &deleted); _position++); + _buckets[_position] == &deletedBucket); _position++); if (_position < _capacity) return &_buckets[_position++]->object; else return NULL;