Sunday, June 16, 2013

NSCountedSet initWithCapacity example in Objective C (iOS).


NSCountedSet initWithCapacity

Returns a counted set object initialized with enough memory to hold a given number of objects.

- (id)initWithCapacity:(NSUInteger)numItems

Parameters
numItems
The initial capacity of the new counted set.

Return Value
A counted set object initialized with enough memory to hold numItems objects

Discussion of [NSCountedSet initWithCapacity]
The method is the designated initializer for NSCountedSet.

Note that the capacity is simply a hint to help initial memory allocation—the initial count of the object is 0, and the set still grows and shrinks as you add and remove objects. The hint is typically useful if the set will become large.

NSCountedSet initWithCapacity example.
NSDictionary * dict1 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:1], @"foo",
                        [NSNumber numberWithInt:2], @"bar", nil];
NSDictionary * dict2 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:1], @"foo",
                        [NSNumber numberWithInt:3], @"bar", nil];
NSDictionary * dict3 = [[NSDictionary alloc] initWithObjectsAndKeys:
                        [NSNumber numberWithInt:2], @"foo",
                        [NSNumber numberWithInt:1], @"bar", nil];
NSArray * arrayOfDictionaries = [[NSArray alloc] initWithObjects:
                                 dict1, dict2, dict3, nil];

// count all keys in an array of dictionaries (arrayOfDictionaries):

NSMutableDictionary * countKeys = [[NSMutableDictionary alloc] initWithCapacity:0];
NSCountedSet * counts = [[NSCountedSet alloc] initWithCapacity:0];

NSArray * keys;
NSString * pairString;
NSString * countKey;
for (NSDictionary * dictionary in arrayOfDictionaries)
{
    keys = [dictionary allKeys];
    for (NSString * key in keys)
    {
        pairString = [NSString stringWithFormat:@"%@->%@", key, [dictionary valueForKey:key]];
        if ([countKeys valueForKey:pairString] == nil)
        {
            [countKeys setValue:[NSString stringWithString:pairString] forKey:pairString];
        }
        countKey = [countKeys valueForKey:pairString];
        { [counts addObject:countKey]; }
    }
}

NSLog(@"%@", counts);

[counts release];
[countKeys release];

[arrayOfDictionaries release];
[dict1 release];
[dict2 release];
[dict3 release];

Example of [NSCountedSet initWithCapacity].
UIImage        *image;
int             randomIndex;

NSCountedSet   *set    = [[NSCountedSet alloc] initWithCapacity:50];
NSMutableArray *arrays = [[NSMutableArray alloc] initWithCapacity:5];

for ( int i = 0; i < 5; i++ ) {
    while ( [set count] < 50 ) {
        randomIndex = arc4random() % 6;
        image = [images objectAtIndex:randomIndex];

        switch ( [set count] ) {
            case 48:
            case 49:
                if ( [set countForObject:image] < 9 ) {
                    [set addObject:image];
                }
                break;

            default:
                if ( [set countForObject:image] < 8 ) {
                    [set addObject:image];
                }
        }
    }

    [arrays addObject:[set allObjects]];
    [set removeAllObjects];
}

[set release];

NSCountedSet initWithCapacity example.
@implementation NSString(permutation)
- (BOOL)isPermutation:(NSString*)other
{
    if( [self length] != [other length] ) return NO;
    if( [self isEqualToString:other] )    return YES;
    NSUInteger length = [self length];
    NSCountedSet* set1 = [[[NSCountedSet alloc] initWithCapacity:length] autorelease];
    NSCountedSet* set2 = [[[NSCountedSet alloc] initWithCapacity:length] autorelease];
    for( int i = 0; i < length; i++ ) {
        NSRange range = NSMakeRange(i, 1);
        [set1 addObject:[self substringWithRange:range]];
        [set2 addObject:[self substringWithRange:range]];
    }
    return [set1 isEqualTo:set2];
}
@end

End of NSCountedSet initWithCapacity example article.