forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHTMLOrderedDictionary.m
167 lines (136 loc) · 4.58 KB
/
HTMLOrderedDictionary.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
167
// HTMLOrderedDictionary.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import "HTMLOrderedDictionary.h"
NS_ASSUME_NONNULL_BEGIN
@implementation HTMLOrderedDictionary
{
CFMutableDictionaryRef _map;
NSMutableArray *_keys;
}
- (instancetype)initWithCapacity:(NSUInteger)numItems
{
if ((self = [super init])) {
_map = CFDictionaryCreateMutable(nil, numItems, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
_keys = [NSMutableArray arrayWithCapacity:numItems];
}
return self;
}
// Diagnostic needs ignoring on iOS 5.
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmismatched-parameter-types"
- (instancetype)initWithObjects:(const id __nonnull [])objects forKeys:(const id<NSCopying> __nonnull [])keys count:(NSUInteger)count
#pragma clang diagnostic pop
{
if ((self = [self initWithCapacity:count])) {
for (NSUInteger i = 0; i < count; i++) {
id object = objects[i];
id key = keys[i];
if (!object) [NSException raise:NSInvalidArgumentException format:@"%@ object at %@ cannot be nil", NSStringFromSelector(_cmd), @(i)];
if (!key) [NSException raise:NSInvalidArgumentException format:@"%@ key at %@ cannot be nil", NSStringFromSelector(_cmd), @(i)];
self[keys[i]] = objects[i];
}
}
return self;
}
- (id)init
{
return [self initWithCapacity:0];
}
- (id __nullable)initWithCoder:(NSCoder *)coder
{
NSDictionary *map = [coder decodeObjectForKey:@"map"];
NSArray *keys = [coder decodeObjectForKey:@"keys"];
HTMLOrderedDictionary *dictionary = [self initWithCapacity:keys.count];
for (id key in keys) {
dictionary[key] = map[key];
}
return dictionary;
}
- (void)dealloc
{
CFRelease(_map);
}
- (Class __nullable)classForKeyedArchiver
{
return [self class];
}
- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:(__bridge NSDictionary *)_map forKey:@"map"];
[coder encodeObject:_keys forKey:@"keys"];
}
- (id)copyWithZone:(NSZone * __nullable)zone
{
HTMLOrderedDictionary *copy = [[[self class] allocWithZone:zone] initWithCapacity:self.count];
[copy addEntriesFromDictionary:self];
return copy;
}
- (id)mutableCopyWithZone:(NSZone * __nullable)zone
{
return [self copyWithZone:zone];
}
- (NSUInteger)count
{
return _keys.count;
}
- (id __nullable)objectForKey:(id)key
{
NSParameterAssert(key);
return (__bridge id)CFDictionaryGetValue(_map, (__bridge const void *)key);
}
- (NSUInteger)indexOfKey:(id)key
{
if ([self objectForKey:key]) {
return [_keys indexOfObject:key];
} else {
return NSNotFound;
}
}
- (id __nullable)firstKey
{
return _keys.firstObject;
}
- (id __nullable)lastKey
{
return _keys.lastObject;
}
- (void)setObject:(id)object forKey:(id<NSCopying>)key
{
if (!object) [NSException raise:NSInvalidArgumentException format:@"%@ object cannot be nil", NSStringFromSelector(_cmd)];
if (!key) [NSException raise:NSInvalidArgumentException format:@"%@ key cannot be nil", NSStringFromSelector(_cmd)];
[self insertObject:object forKey:key atIndex:self.count];
}
- (void)removeObjectForKey:(id<NSCopying>)key
{
if (!key) [NSException raise:NSInvalidArgumentException format:@"%@ key cannot be nil", NSStringFromSelector(_cmd)];
if ([self objectForKey:key]) {
CFDictionaryRemoveValue(_map, (__bridge const void *)key);
[_keys removeObject:key];
}
}
- (void)insertObject:(id)object forKey:(id<NSCopying>)key atIndex:(NSUInteger)index
{
if (!object) [NSException raise:NSInvalidArgumentException format:@"%@ object cannot be nil", NSStringFromSelector(_cmd)];
if (!key) [NSException raise:NSInvalidArgumentException format:@"%@ key cannot be nil", NSStringFromSelector(_cmd)];
if (index > self.count) [NSException raise:NSRangeException format:@"%@ index %@ beyond count %@ of array", NSStringFromSelector(_cmd), @(index), @(self.count)];
if (![self objectForKey:key]) {
key = [key copyWithZone:nil];
[_keys insertObject:key atIndex:index];
}
CFDictionarySetValue(_map, (__bridge const void *)key, (__bridge const void *)object);
}
- (NSEnumerator *)keyEnumerator
{
return _keys.objectEnumerator;
}
- (id)objectAtIndexedSubscript:(NSUInteger)index
{
return _keys[index];
}
- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(__unsafe_unretained id __nonnull [])buffer count:(NSUInteger)len
{
return [_keys countByEnumeratingWithState:state objects:buffer count:len];
}
@end
NS_ASSUME_NONNULL_END