-
Notifications
You must be signed in to change notification settings - Fork 41
/
NSDictionary+SimpleMutations.m
108 lines (95 loc) · 2.99 KB
/
NSDictionary+SimpleMutations.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
#import "NSDictionary+SimpleMutations.h"
@implementation NSDictionary (SimpleMutations)
- (NSDictionary*)dictionaryBySettingValue:(id)value
forKey:(id)key {
if (!key) {
return [NSDictionary dictionaryWithDictionary:self] ;
}
NSMutableDictionary* mutant = [self mutableCopy] ;
[mutant setValue:value
forKey:key] ;
NSDictionary* newDic = [NSDictionary dictionaryWithDictionary:mutant] ;
#if !__has_feature(objc_arc)
[mutant release] ;
#endif
return newDic ;
}
- (NSDictionary*)dictionaryByRemovingObjectForKey:(id)key {
return [self dictionaryBySettingValue:nil
forKey:key] ;
}
- (NSDictionary*)dictionaryByAddingEntriesFromDictionary:(NSDictionary*)otherDic {
NSMutableDictionary* mutant = [self mutableCopy] ;
if (otherDic) {
[mutant addEntriesFromDictionary:otherDic] ;
}
NSDictionary* newDic = [NSDictionary dictionaryWithDictionary:mutant] ;
#if !__has_feature(objc_arc)
[mutant release] ;
#endif
return newDic ;
}
- (NSDictionary*)dictionaryByAppendingEntriesFromDictionary:(NSDictionary*)otherDic {
NSMutableDictionary* mutant = [self mutableCopy] ;
for (id key in otherDic) {
if ([self objectForKey:key] == nil) {
[mutant setObject:[otherDic objectForKey:key]
forKey:key] ;
}
}
NSDictionary* newDic = [NSDictionary dictionaryWithDictionary:mutant] ;
#if !__has_feature(objc_arc)
[mutant release] ;
#endif
return newDic ;
}
+ (void)mutateAdditions:(NSMutableDictionary*)additions
deletions:(NSMutableSet*)deletions
newAdditions:(NSMutableDictionary*)newAdditions
newDeletions:(NSMutableSet*)newDeletions {
NSSet* immuterator ;
// Remove from newAdditions and newDeletions any members
// in these new inputs which cancel one another out
immuterator = [[NSSet alloc] initWithArray:[newAdditions allKeys]] ;
for (id key in immuterator) {
id member = [newDeletions member:key] ;
if (member) {
[newAdditions removeObjectForKey:key] ;
[newDeletions removeObject:member] ;
}
}
#if !__has_feature(objc_arc)
[immuterator release] ;
#endif
// Remove from newAdditions any which cancel out existing deletions,
// and do the cancellation
immuterator = [[NSSet alloc] initWithArray:[newAdditions allKeys]] ;
for (id key in immuterator) {
id member = [deletions member:key] ;
if (member) {
[newAdditions removeObjectForKey:key] ;
[deletions removeObject:member] ;
}
}
#if !__has_feature(objc_arc)
[immuterator release] ;
#endif
// Add surviving new additions to existing additions
[additions addEntriesFromDictionary:newAdditions] ;
// Remove from newDeletions any which cancel out existing additions,
// and do the cancellation
immuterator = [newDeletions copy] ;
for (id key in immuterator) {
id object = [additions objectForKey:key] ;
if (object) {
[newDeletions removeObject:key] ;
[additions removeObjectForKey:key] ;
}
}
#if !__has_feature(objc_arc)
[immuterator release] ;
#endif
// Add surviving new deletions to existing deletions
[deletions unionSet:newDeletions] ;
}
@end