26#import "OrderedDictionary.h"
28NSString *DescriptionForObject(NSObject *
object,
id locale, NSUInteger indent)
30 NSString *objectString;
31 if ([
object isKindOfClass:[NSString class]])
33 objectString = (NSString *)[[
object retain] autorelease];
35 else if ([
object respondsToSelector:@selector(descriptionWithLocale:indent:)])
37 objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent];
39 else if ([
object respondsToSelector:@selector(descriptionWithLocale:)])
41 objectString = [(NSSet *)object descriptionWithLocale:locale];
45 objectString = [object description];
52- (void)initStorageWithCapacity:(NSUInteger)capacity
54 dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
55 array = [[NSMutableArray alloc] initWithCapacity:capacity];
62 [
self initStorageWithCapacity:0];
66- (id)initWithCapacity:(NSUInteger)capacity
70 [
self initStorageWithCapacity:capacity];
83 return [
self mutableCopy];
86- (void)setObject:(
id)anObject forKey:(
id)aKey
88 if (![dictionary objectForKey:aKey])
90 [array addObject:aKey];
92 [dictionary setObject:anObject forKey:aKey];
95- (void)removeObjectForKey:(
id)aKey
97 [dictionary removeObjectForKey:aKey];
98 [array removeObject:aKey];
103 return [dictionary count];
106- (id)objectForKey:(
id)aKey
108 return [dictionary objectForKey:aKey];
111- (NSEnumerator *)keyEnumerator
113 return [array objectEnumerator];
116- (NSEnumerator *)reverseKeyEnumerator
118 return [array reverseObjectEnumerator];
121- (void)insertObject:(
id)anObject forKey:(
id)aKey atIndex:(NSUInteger)anIndex
123 if ([dictionary objectForKey:aKey])
125 [
self removeObjectForKey:aKey];
127 [array insertObject:aKey atIndex:anIndex];
128 [dictionary setObject:anObject forKey:aKey];
131- (id)keyAtIndex:(NSUInteger)anIndex
133 return [array objectAtIndex:anIndex];
136- (NSUInteger)indexForKey:(
id)key
138 return [array indexOfObject:key];
141- (NSUInteger)indexForValue:(
id)value
143 NSArray *keys = [
self allKeysForObject:value];
144 if ([keys count] > 0)
146 return [
self indexForKey:[keys objectAtIndex:0]];
152- (NSString *)descriptionWithLocale:(
id)locale indent:(NSUInteger)level
154 NSMutableString *indentString = [NSMutableString string];
155 NSUInteger count = level;
156 for (NSUInteger i = 0; i < count; i++)
158 [indentString appendFormat:@" "];
161 NSMutableString *description = [NSMutableString string];
162 [description appendFormat:@"%@{\n", indentString];
163 for (NSObject *key in
self)
165 [description appendFormat:@"%@ %@ = %@;\n", indentString,
166 DescriptionForObject(key, locale, level),
167 DescriptionForObject([
self objectForKey:key], locale, level)];
169 [description appendFormat:@"%@}\n", indentString];