26 #import "OrderedDictionary.h"
28 NSString *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];
54 return [
self initWithCapacity:0];
57 - (id)initWithCapacity:(NSUInteger)capacity
62 dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
63 array = [[NSMutableArray alloc] initWithCapacity:capacity];
77 return [
self mutableCopy];
80 - (void)setObject:(
id)anObject forKey:(
id)aKey
82 if (![dictionary objectForKey:aKey])
84 [array addObject:aKey];
86 [dictionary setObject:anObject forKey:aKey];
89 - (void)removeObjectForKey:(
id)aKey
91 [dictionary removeObjectForKey:aKey];
92 [array removeObject:aKey];
97 return [dictionary count];
100 - (id)objectForKey:(
id)aKey
102 return [dictionary objectForKey:aKey];
105 - (NSEnumerator *)keyEnumerator
107 return [array objectEnumerator];
110 - (NSEnumerator *)reverseKeyEnumerator
112 return [array reverseObjectEnumerator];
115 - (void)insertObject:(
id)anObject forKey:(
id)aKey atIndex:(NSUInteger)anIndex
117 if ([dictionary objectForKey:aKey])
119 [
self removeObjectForKey:aKey];
121 [array insertObject:aKey atIndex:anIndex];
122 [dictionary setObject:anObject forKey:aKey];
125 - (id)keyAtIndex:(NSUInteger)anIndex
127 return [array objectAtIndex:anIndex];
130 - (NSUInteger)indexForKey:(
id)key
132 return [array indexOfObject:key];
135 - (NSUInteger)indexForValue:(
id)value
137 NSArray *keys = [
self allKeysForObject:value];
138 if ([keys count] > 0)
140 return [
self indexForKey:[keys objectAtIndex:0]];
146 - (NSString *)descriptionWithLocale:(
id)locale indent:(NSUInteger)level
148 NSMutableString *indentString = [NSMutableString string];
149 NSUInteger count = level;
150 for (NSUInteger i = 0; i < count; i++)
152 [indentString appendFormat:@" "];
155 NSMutableString *description = [NSMutableString string];
156 [description appendFormat:@"%@{\n", indentString];
157 for (NSObject *key in
self)
159 [description appendFormat:@"%@ %@ = %@;\n", indentString,
160 DescriptionForObject(key, locale, level),
161 DescriptionForObject([
self objectForKey:key], locale, level)];
163 [description appendFormat:@"%@}\n", indentString];