-
Notifications
You must be signed in to change notification settings - Fork 1
/
MutableOrderedDictionary.m
166 lines (138 loc) · 4.63 KB
/
MutableOrderedDictionary.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//
// MutableOrderedDictionary.m
//
// Created by Heitor Ferreira on 9/21/12.
//
//
#import "MutableOrderedDictionary.h"
@interface MutableOrderedDictionary ()
@property(nonatomic,strong) NSMutableArray *orderedKeys;
@property(nonatomic,strong) NSMutableDictionary *store;
@end
@implementation MutableOrderedDictionary
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Initialization
////////////////////////////////////////////////////////////////////////////////////////////////////
+ (MutableOrderedDictionary *)dictionaryWithCapacity:(NSUInteger)capacity
{
return [[MutableOrderedDictionary alloc] initWithCapacity:capacity];
}
- (id)init
{
return [self initWithCapacity:0];
}
- (id)initWithCapacity:(NSUInteger)capacity
{
if(self = [super init]) {
_orderedKeys = [NSMutableArray arrayWithCapacity:capacity];
_store = [NSMutableDictionary dictionaryWithCapacity:capacity];
}
return self;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Access
////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSUInteger)count
{
return [self.orderedKeys count];
}
- (BOOL)containsKey:(id<NSCopying>)aKey
{
return ([self.store objectForKey:aKey] != nil);
}
- (NSArray *)allValues
{
return [self.store allValues];
}
- (NSArray *)orderedValues
{
NSMutableArray *values = [NSMutableArray arrayWithCapacity:self.store.count];
[self.orderedKeys enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[values addObject:[self.store objectForKey:obj]];
}];
return [values copy];
}
- (NSDictionary*)dictionary
{
return [self.store copy];
}
- (NSEnumerator *)keyEnumerator
{
return self.orderedKeys.objectEnumerator;
}
- (NSSet *)keySet
{
return [NSSet setWithArray:self.orderedKeys];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)objectForKey:(id)aKey
{
return [self.store objectForKey:aKey];
}
- (id)objectForKeyedSubscript:(id)aKey
{
return [self.store objectForKey:aKey];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)objectAtIndex:(NSUInteger)index
{
return [self.store objectForKey:[self.orderedKeys objectAtIndex:index]];
}
- (id)objectAtIndexedSubscript:(NSInteger)index
{
return [self objectAtIndex:index];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark - Mutation
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey
{
if([self.store objectForKey:aKey]) {
[self.orderedKeys removeObject:aKey];
}
[self.store setObject:anObject forKey:aKey];
[self.orderedKeys addObject:aKey];
}
- (void)setObject:(id)anObject forKeyedSubscript:(id<NSCopying>)aKey
{
[self setObject:anObject forKey:aKey];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)removeObjectForKey:(id<NSCopying>)aKey
{
if([self.store objectForKey:aKey] != nil) {
[self.store removeObjectForKey:aKey];
[self.orderedKeys removeObject:aKey];
}
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
id key = [self.orderedKeys objectAtIndex:index];
if(key) {
[self.orderedKeys removeObjectAtIndex:index];
[self.store removeObjectForKey:key];
}
}
- (void)removeAllObjects
{
[self.store removeAllObjects];
[self.orderedKeys removeAllObjects];
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark NSMutableCopying
////////////////////////////////////////////////////////////////////////////////////////////////////
- (id)mutableCopyWithZone:(NSZone *)zone
{
MutableOrderedDictionary *copy = [[MutableOrderedDictionary allocWithZone:zone] init];
copy.store = [self.store mutableCopyWithZone:zone];
copy.orderedKeys = [self.orderedKeys mutableCopyWithZone:zone];
return copy;
}
////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark NSFastEnumeration
////////////////////////////////////////////////////////////////////////////////////////////////////
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len
{
return [self.orderedKeys countByEnumeratingWithState:state objects:buffer count:len];
}
@end