-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCDZDictionaryArray.m
More file actions
196 lines (180 loc) · 5.18 KB
/
CDZDictionaryArray.m
File metadata and controls
196 lines (180 loc) · 5.18 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// CDZDictionaryArray.m
//
//
// Created by zhengchen2 on 14-9-5.
// Copyright (c) 2014年 zhengchen2 All rights reserved.
//
#import "CDZDictionaryArray.h"
@implementation CDZDictionaryArray
-(id)init{
self = [super init];
if(self){
_array = [[NSMutableArray alloc]init];
_keyArray = [[NSMutableArray alloc]init];
_dictionary = [[NSMutableDictionary alloc]init];
}
return self;
}
-(id)initWithCapacity:(NSUInteger)numItems{
self = [super init];
if(self){
_array = [[NSMutableArray alloc]initWithCapacity:numItems];
_keyArray = [[NSMutableArray alloc]initWithCapacity:numItems];
_dictionary = [[NSMutableDictionary alloc]initWithCapacity:numItems];
}
return self;
}
-(NSMutableArray*)mutableArray{
return [NSMutableArray arrayWithArray:_array];
}
-(NSMutableDictionary*)mutableDictionary{
return [NSMutableDictionary dictionaryWithDictionary:_dictionary];
}
-(void)addObject:(id)anObject forKey:(id<NSCopying>)aKey{
if(aKey == nil || anObject == nil){
return;
}
id object = [_dictionary objectForKey:aKey];
if(object){
NSUInteger index = [_array indexOfObject:object];
[_array removeObjectAtIndex:index];
[_keyArray removeObjectAtIndex:index];
}
[_array addObject:anObject];
[_keyArray addObject:aKey];
[_dictionary setObject:anObject forKey:aKey];
}
-(void)insertObject:(id)anObject atIndex:(NSUInteger)index forKey:(id<NSCopying>)aKey{
if(aKey == nil || anObject == nil){
return;
}
id object = [_dictionary objectForKey:aKey];
if(object){
NSInteger index = [_array indexOfObject:object];
[_array removeObjectAtIndex:index];
[_keyArray removeObjectAtIndex:index];
}
[_array insertObject:anObject atIndex:index];
[_keyArray insertObject:aKey atIndex:index];
[_dictionary setObject:anObject forKey:aKey];
}
-(BOOL)containsObject:(id)anObject{
if(anObject == nil){
return NO;
}
return [_array containsObject:anObject];
}
-(BOOL)containsObjectForKey:(id)aKey{
id object = [_dictionary objectForKey:aKey];
return (object != nil);
}
// 存在aKey,则进行替换,不存在则进行简单添加
-(void)replaceObjectForKey:(id)aKey withObject:(id)anObject{
if(aKey == nil || anObject == nil){
return;
}
id object = [_dictionary objectForKey:aKey];
if(object){
NSInteger index = [_array indexOfObject:object];
[_array replaceObjectAtIndex:index withObject:anObject];
[_keyArray replaceObjectAtIndex:index withObject:aKey];
}
else{
[_array addObject:anObject];
[_keyArray addObject:aKey];
}
[_dictionary setObject:anObject forKey:aKey];
}
-(void)removeObject:(id)anObject{
if(anObject == nil){
return;
}
NSString* aKey = [self keyOfObject:anObject];
if(aKey){
[_dictionary removeObjectForKey:aKey];
}
NSInteger index = [_array indexOfObject:anObject];
[_array removeObjectAtIndex:index];
[_keyArray removeObjectAtIndex:index];
}
-(void)removeAllObjects{
[_dictionary removeAllObjects];
[_array removeAllObjects];
[_keyArray removeAllObjects];
}
-(void)removeObjectForKey:(id)aKey{
if(aKey == nil){
return;
}
id object = [_dictionary objectForKey:aKey];
NSInteger index = [_array indexOfObject:object];
[_array removeObjectAtIndex:index];
[_keyArray removeObjectAtIndex:index];
[_dictionary removeObjectForKey:aKey];
}
-(void)removeLastObject{
id lastObject = [_array lastObject];
NSString* aKey = [self keyOfObject:lastObject];
if(aKey){
[_dictionary removeObjectForKey:aKey];
}
[_array removeLastObject];
[_keyArray removeLastObject];
}
-(id)firstObject{
return [_array firstObject];
}
-(id)lastObject{
return [_array lastObject];
}
-(id)objectAtIndex:(NSUInteger)index{
return [_array objectAtIndex:index];
}
-(id)objectForKey:(id)aKey{
if(aKey == nil){
return nil;
}
id object = [_dictionary objectForKey:aKey];
return object;
}
-(NSInteger)count{
return [_array count];
}
-(NSInteger)indexOfObject:(id)anObject{
if(anObject == nil){
return -1;
}
return [_array indexOfObject:anObject];
}
-(NSInteger)indexForKey:(id)aKey{
id object = [_dictionary objectForKey:aKey];
if(object){
return [_array indexOfObject:object];
}
else{
return -1;
}
}
// 获取 anObject 的 key
-(id)keyOfObject:(id)anObject{
NSUInteger index = [_array indexOfObject:anObject];
if(index < _keyArray.count){
return [_keyArray objectAtIndex:index];
}
else{
return nil;
}
}
- (void)enumerateUsingBlock:(void (^)(id key, id obj, NSUInteger idx,BOOL *stop))block{
[_keyArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
id key = obj;
id object = [_array objectAtIndex:idx];
block(key, object, idx, stop);
}];
}
#pragma mark - NSFastEnumeration
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id __unsafe_unretained [])buffer count:(NSUInteger)len{
return [_array countByEnumeratingWithState:state objects:buffer count:len];
}
@end