FreeRDP
Loading...
Searching...
No Matches
OrderedDictionary.m
1//
2// OrderedDictionary.m
3// OrderedDictionary
4//
5// Modified version (Added indexForKey/Value functions)
6//
7// Created by Matt Gallagher on 19/12/08.
8// Copyright 2008 Matt Gallagher. All rights reserved.
9//
10// This software is provided 'as-is', without any express or implied
11// warranty. In no event will the authors be held liable for any damages
12// arising from the use of this software. Permission is granted to anyone to
13// use this software for any purpose, including commercial applications, and to
14// alter it and redistribute it freely, subject to the following restrictions:
15//
16// 1. The origin of this software must not be misrepresented; you must not
17// claim that you wrote the original software. If you use this software
18// in a product, an acknowledgment in the product documentation would be
19// appreciated but is not required.
20// 2. Altered source versions must be plainly marked as such, and must not be
21// misrepresented as being the original software.
22// 3. This notice may not be removed or altered from any source
23// distribution.
24//
25
26#import "OrderedDictionary.h"
27
28NSString *DescriptionForObject(NSObject *object, id locale, NSUInteger indent)
29{
30 NSString *objectString;
31 if ([object isKindOfClass:[NSString class]])
32 {
33 objectString = (NSString *)[[object retain] autorelease];
34 }
35 else if ([object respondsToSelector:@selector(descriptionWithLocale:indent:)])
36 {
37 objectString = [(NSDictionary *)object descriptionWithLocale:locale indent:indent];
38 }
39 else if ([object respondsToSelector:@selector(descriptionWithLocale:)])
40 {
41 objectString = [(NSSet *)object descriptionWithLocale:locale];
42 }
43 else
44 {
45 objectString = [object description];
46 }
47 return objectString;
48}
49
50@implementation OrderedDictionary
51
52- (void)initStorageWithCapacity:(NSUInteger)capacity
53{
54 dictionary = [[NSMutableDictionary alloc] initWithCapacity:capacity];
55 array = [[NSMutableArray alloc] initWithCapacity:capacity];
56}
57
58- (id)init
59{
60 self = [super init];
61 if (self != nil)
62 [self initStorageWithCapacity:0];
63 return self;
64}
65
66- (id)initWithCapacity:(NSUInteger)capacity
67{
68 self = [super init];
69 if (self != nil)
70 [self initStorageWithCapacity:capacity];
71 return self;
72}
73
74- (void)dealloc
75{
76 [dictionary release];
77 [array release];
78 [super dealloc];
79}
80
81- (id)copy
82{
83 return [self mutableCopy];
84}
85
86- (void)setObject:(id)anObject forKey:(id)aKey
87{
88 if (![dictionary objectForKey:aKey])
89 {
90 [array addObject:aKey];
91 }
92 [dictionary setObject:anObject forKey:aKey];
93}
94
95- (void)removeObjectForKey:(id)aKey
96{
97 [dictionary removeObjectForKey:aKey];
98 [array removeObject:aKey];
99}
100
101- (NSUInteger)count
102{
103 return [dictionary count];
104}
105
106- (id)objectForKey:(id)aKey
107{
108 return [dictionary objectForKey:aKey];
109}
110
111- (NSEnumerator *)keyEnumerator
112{
113 return [array objectEnumerator];
114}
115
116- (NSEnumerator *)reverseKeyEnumerator
117{
118 return [array reverseObjectEnumerator];
119}
120
121- (void)insertObject:(id)anObject forKey:(id)aKey atIndex:(NSUInteger)anIndex
122{
123 if ([dictionary objectForKey:aKey])
124 {
125 [self removeObjectForKey:aKey];
126 }
127 [array insertObject:aKey atIndex:anIndex];
128 [dictionary setObject:anObject forKey:aKey];
129}
130
131- (id)keyAtIndex:(NSUInteger)anIndex
132{
133 return [array objectAtIndex:anIndex];
134}
135
136- (NSUInteger)indexForKey:(id)key
137{
138 return [array indexOfObject:key];
139}
140
141- (NSUInteger)indexForValue:(id)value
142{
143 NSArray *keys = [self allKeysForObject:value];
144 if ([keys count] > 0)
145 {
146 return [self indexForKey:[keys objectAtIndex:0]];
147 }
148
149 return NSNotFound;
150}
151
152- (NSString *)descriptionWithLocale:(id)locale indent:(NSUInteger)level
153{
154 NSMutableString *indentString = [NSMutableString string];
155 NSUInteger count = level;
156 for (NSUInteger i = 0; i < count; i++)
157 {
158 [indentString appendFormat:@" "];
159 }
160
161 NSMutableString *description = [NSMutableString string];
162 [description appendFormat:@"%@{\n", indentString];
163 for (NSObject *key in self)
164 {
165 [description appendFormat:@"%@ %@ = %@;\n", indentString,
166 DescriptionForObject(key, locale, level),
167 DescriptionForObject([self objectForKey:key], locale, level)];
168 }
169 [description appendFormat:@"%@}\n", indentString];
170 return description;
171}
172
173@end