Navigation Menu

Skip to content

Commit

Permalink
simplify setObject:forKey: by using removeObjectForKey: to remove any…
Browse files Browse the repository at this point in the history
… old mapping rather than trying to be clever by re-using it
  • Loading branch information
Michael Ash authored and Michael Ash committed Feb 26, 2012
1 parent 0993f6b commit 61b39bd
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions MAMutableDictionary.m
Expand Up @@ -140,20 +140,13 @@ - (void)removeObjectForKey: (id)key

- (void)setObject: (id)obj forKey: (id)key
{
NSUInteger bucketIndex = [key hash] % _size;
_MAMutableDictionaryBucket *bucket = _array[bucketIndex];
while(bucket)
{
if([[bucket key] isEqual: key])
{
[bucket setObj: obj];
return;
}
bucket = [bucket next];
}
_MAMutableDictionaryBucket *newBucket = [[_MAMutableDictionaryBucket alloc] init];
[newBucket setKey: key];
[newBucket setObj: obj];

[self removeObjectForKey: key];

NSUInteger bucketIndex = [key hash] % _size;
[newBucket setNext: _array[bucketIndex]];
[_array[bucketIndex] release];
_array[bucketIndex] = newBucket;
Expand Down

0 comments on commit 61b39bd

Please sign in to comment.