Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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