Created
April 29, 2011 06:17
-
-
Save zbowling/947931 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OrderedDictionary.h | |
// OrderedDictionary | |
// | |
// Created by Matt Gallagher on 19/12/08. | |
// Copyright 2008 Matt Gallagher. All rights reserved. | |
// | |
// Permission is given to use this source code file without charge in any | |
// project, commercial or otherwise, entirely at your risk, with the condition | |
// that any redistribution (in part or whole) of source code must retain | |
// this copyright and permission notice. Attribution in compiled projects is | |
// appreciated but not required. | |
// | |
// HEAVILY modified by Zac Bowling (2011) | |
// Copyright 2011 Zac Bowling | |
#import <Foundation/Foundation.h> | |
@interface MWOrderedDictionary : NSMutableDictionary | |
{ | |
NSMutableDictionary *dictionary; | |
NSMutableArray *array; | |
NSArray *cache; | |
} | |
- (NSArray *)asArray; | |
- (void)insertObject:(id)anObject forKey:(id)aKey; | |
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex; | |
- (id)keyAtIndex:(NSUInteger)anIndex; | |
- (NSEnumerator *)reverseKeyEnumerator; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// OrderedDictionary.m | |
// OrderedDictionary | |
// | |
// Created by Matt Gallagher on 19/12/08. | |
// Copyright 2008 Matt Gallagher. All rights reserved. | |
// | |
// Permission is given to use this source code file without charge in any | |
// project, commercial or otherwise, entirely at your risk, with the condition | |
// that any redistribution (in part or whole) of source code must retain | |
// this copyright and permission notice. Attribution in compiled projects is | |
// appreciated but not required. | |
// | |
// HEAVILY modified by Zac Bowling (2011) | |
// Copyright 2011 Zac Bowling | |
#import "MWOrderedDictionary.h" | |
NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent) | |
{ | |
NSString *objectString; | |
if ([object isKindOfClass:[NSString class]]) | |
{ | |
objectString = (NSString *)[[object retain] autorelease]; | |
} | |
else if ([object respondsToSelector:@selector(descriptionWithLocale:indent:)]) | |
{ | |
objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent]; | |
} | |
else if ([object respondsToSelector:@selector(descriptionWithLocale:)]) | |
{ | |
objectString = [(NSSet *)object descriptionWithLocale:locale]; | |
} | |
else | |
{ | |
objectString = [object description]; | |
} | |
return objectString; | |
} | |
@implementation MWOrderedDictionary | |
- (id)init | |
{ | |
return [self initWithCapacity:0]; | |
} | |
- (id)initWithCapacity:(NSUInteger)capacity | |
{ | |
self = [super init]; | |
if (self != nil) | |
{ | |
dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity]; | |
array = [[NSMutableArray alloc] initWithCapacity:capacity]; | |
cache = nil; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[cache release]; | |
[dictionary release]; | |
[array release]; | |
[super dealloc]; | |
} | |
- (id)copy | |
{ | |
return [self mutableCopy]; | |
} | |
- (void)setObject:(id)anObject forKey:(id)aKey | |
{ | |
if (cache) { | |
[cache release]; | |
cache = nil; | |
} | |
if (![dictionary objectForKey:aKey]) | |
{ | |
[array addObject:aKey]; | |
} | |
[dictionary setObject:anObject forKey:aKey]; | |
} | |
- (void)removeObjectForKey:(id)aKey | |
{ | |
if (cache) { | |
[cache release]; | |
cache = nil; | |
} | |
[dictionary removeObjectForKey:aKey]; | |
[array removeObject:aKey]; | |
} | |
- (NSUInteger)count | |
{ | |
return [dictionary count]; | |
} | |
- (id)objectForKey:(id)aKey | |
{ | |
return [dictionary objectForKey:aKey]; | |
} | |
- (NSEnumerator *)keyEnumerator | |
{ | |
return [array objectEnumerator]; | |
} | |
- (NSEnumerator *)reverseKeyEnumerator | |
{ | |
return [array reverseObjectEnumerator]; | |
} | |
- (void)insertObject:(id)anObject forKey:(id)aKey | |
{ | |
[self setObject:anObject forKey:aKey]; | |
} | |
- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex | |
{ | |
if (cache) { | |
[cache release]; | |
cache = nil; | |
} | |
if (![dictionary objectForKey:aKey]) | |
{ | |
[self removeObjectForKey:aKey]; | |
} | |
[array insertObject:aKey atIndex:anIndex]; | |
[dictionary setObject:anObject forKey:aKey]; | |
} | |
- (id)keyAtIndex:(NSUInteger)anIndex | |
{ | |
return [array objectAtIndex:anIndex]; | |
} | |
- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level | |
{ | |
NSMutableString *indentString = [NSMutableString string]; | |
NSUInteger i, count = level; | |
for (i = 0; i < count; i++) | |
{ | |
[indentString appendFormat:@" "]; | |
} | |
NSMutableString *description = [NSMutableString string]; | |
[description appendFormat:@"%@{\n", indentString]; | |
for (NSObject *key in self) | |
{ | |
[description appendFormat:@"%@ %@ = %@;\n", | |
indentString, | |
DescriptionForObject(key, locale, level), | |
DescriptionForObject([self objectForKey:key], locale, level)]; | |
} | |
[description appendFormat:@"%@}\n", indentString]; | |
return description; | |
} | |
- (NSArray *)asArray | |
{ | |
if (cache) | |
return cache; | |
NSMutableArray* arrayCopy = [NSMutableArray arrayWithCapacity:[self count]]; | |
for (id item in self) | |
{ | |
[arrayCopy addObject:[self objectForKey:item]]; | |
} | |
cache = [arrayCopy retain]; | |
return cache; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment