Skip to content

Instantly share code, notes, and snippets.

@alts
Created May 30, 2012 08:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alts/2834497 to your computer and use it in GitHub Desktop.
Save alts/2834497 to your computer and use it in GitHub Desktop.
investigate how mutable dictionaries grow.
//
// main.m
// nsmutabledict
//
// Created by Stephen Altamirano on 5/29/12.
//
#import <Foundation/Foundation.h>
NSMutableArray* dict_keys(NSMutableDictionary* d)
{
NSMutableArray* a = [NSMutableArray arrayWithCapacity:[d count]];
for (NSString* key in d)
{
[a addObject:key];
}
return a;
}
void compare_keys(NSArray* arr1, NSArray* arr2)
{
if (!arr1)
{
return;
}
NSUInteger total_keys = [arr2 count];
int skipped = 0;
for (int i=0; i < total_keys - 1; i++)
{
if ([arr2 objectAtIndex:i] != [arr1 objectAtIndex:i - skipped])
{
if (skipped == 0)
{
skipped += 1;
}
else
{
NSLog(@"rehashed at insertion %ld", total_keys);
return;
}
}
}
}
void run()
{
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSMutableArray* last_keys = nil;
NSMutableArray* keys = nil;
for (int i = 0; i < 10000; i++)
{
keys = dict_keys(dict);
compare_keys(last_keys, keys);
last_keys = keys;
[dict setObject:[NSNumber numberWithInt:i]
forKey:[NSString stringWithFormat:@"%d", i]];
}
}
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
run();
[pool drain];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment