-
Notifications
You must be signed in to change notification settings - Fork 1
/
EBUseTracker.m
275 lines (224 loc) · 8.14 KB
/
EBUseTracker.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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#if __has_feature(objc_arc)
#error ARC required to be disabled (-fno-objc-arc)
#endif
#import "EBUseTracker.h"
#import <EBFoundation/EBFoundation.h>
typedef struct EBUseRecord
{
struct EBUseRecord *leftRecord;
struct EBUseRecord *rightRecord;
id <NSObject> object;
} EBUseRecord;
@interface EBUseEnumerator : NSObject <NSFastEnumeration>
- (id)initWithStartRecord: (EBUseRecord *)startRecord forward: (BOOL)forward mutationIndicator: (unsigned long *)mutationIndicator;
@end
@implementation EBUseTracker
{
/* The left record of the head record is the tail record */
EBUseRecord *_headRecord;
/* Maps object -> EBUseRecord */
CFMutableDictionaryRef _object2RecordMap;
/* For NSFastEnumeration */
unsigned long _mutationIndicator;
}
#pragma mark - Creation -
- (id)init
{
if (!(self = [super init]))
return nil;
_object2RecordMap = CFDictionaryCreateMutable(nil, 10, &kCFTypeDictionaryKeyCallBacks, nil);
EBAssertOrRecover(_object2RecordMap, goto failed);
return self;
failed:
{
[self release];
return nil;
}
}
- (void)dealloc
{
/* Free all the records and referenced objects stored in the map */
for (id <NSObject> currentObject in (NSDictionary *)_object2RecordMap)
{
EBUseRecord *record = (EBUseRecord *)CFDictionaryGetValue(_object2RecordMap, currentObject);
/* Grave error if any record doesn't exist or is nil for the given object. */
EBAssertOrBail(record);
free(record),
record = nil;
}
if (_object2RecordMap)
CFRelease(_object2RecordMap),
_object2RecordMap = nil;
[super dealloc];
}
#pragma mark - Methods -
- (void)usedObject: (id <NSObject>)object
{
[self insertRecordAtHeadForObject: object];
}
- (void)removeObject: (id <NSObject>)object
{
[self removeRecordForObject: object];
}
- (id <NSObject>)mostRecentlyUsedObject
{
return (_headRecord ? _headRecord->object : nil);
}
- (id <NSObject>)leastRecentlyUsedObject
{
return (_headRecord ? _headRecord->leftRecord->object : nil);
}
- (id <NSObject>)popMostRecentlyUsedObject
{
EBConfirmOrPerform(_headRecord, return nil);
id <NSObject> object = [[_headRecord->object retain] autorelease];
[self removeObject: object];
return object;
}
- (id <NSObject>)popLeastRecentlyUsedObject
{
EBConfirmOrPerform(_headRecord, return nil);
id <NSObject> object = [[_headRecord->leftRecord->object retain] autorelease];
[self removeObject: object];
return object;
}
- (id <NSFastEnumeration>)mostRecentlyUsedObjectEnumerator
{
return [[[EBUseEnumerator alloc] initWithStartRecord: _headRecord forward: YES
mutationIndicator: &_mutationIndicator] autorelease];
}
- (id <NSFastEnumeration>)leastRecentlyUsedObjectEnumerator
{
return [[[EBUseEnumerator alloc] initWithStartRecord: (_headRecord ? _headRecord->leftRecord : nil) forward: NO
mutationIndicator: &_mutationIndicator] autorelease];
}
#pragma mark - Private Methods -
/* If a record has neighbors, detaches a record from them. */
static void detachRecord(EBUseRecord *record)
{
NSCParameterAssert(record);
if (record->leftRecord)
record->leftRecord->rightRecord = record->rightRecord;
if (record->rightRecord)
record->rightRecord->leftRecord = record->leftRecord;
}
/* Inserts/moves 'newRecord' to the left of 'neighborRecord', or if 'neighborRecord' == nil, makes 'newRecord' reference itself. */
static void insertRecord(EBUseRecord *neighborRecord, EBUseRecord *newRecord)
{
NSCParameterAssert(newRecord);
/* Attempting to move a record before itself has no effect. */
EBConfirmOrPerform(neighborRecord != newRecord, return);
/* First detach the record */
detachRecord(newRecord);
/* If we have neighbors: */
if (neighborRecord)
{
/* Update newRecord's fields */
newRecord->leftRecord = neighborRecord->leftRecord;
newRecord->rightRecord = neighborRecord;
/* Update the neighbors' fields */
newRecord->leftRecord->rightRecord = newRecord;
newRecord->rightRecord->leftRecord = newRecord;
}
/* If 'newRecord' is the only record: */
else
{
/* Set newRecord's neighbors to itself */
newRecord->leftRecord = newRecord;
newRecord->rightRecord = newRecord;
}
}
- (void)insertRecordAtHeadForObject: (id <NSObject>)object
{
NSParameterAssert(object);
/* Get the object's record, creating it if it doesn't exist */
EBUseRecord *record = (EBUseRecord *)CFDictionaryGetValue(_object2RecordMap, object);
if (!record)
{
/* Using calloc so bytes are zeroed */
record = calloc(1, sizeof(*record));
EBAssertOrRecover(record, return);
/* We don't need to retain 'object' because the _object2RecordMap retains it for us! */
record->object = object;
/* Store the object-record pair in the map */
CFDictionarySetValue(_object2RecordMap, object, record);
/* Update our counter */
_count++;
}
/* Insert the record at the head */
insertRecord(_headRecord, record);
/* Update _headRecord */
_headRecord = record;
/* Update our mutation indicator */
_mutationIndicator++;
}
- (void)removeRecordForObject: (id <NSObject>)object
{
NSParameterAssert(object);
/* Get the record for the object, and if it doesn't exist, ignore the request to remove the object. */
EBUseRecord *record = (EBUseRecord *)CFDictionaryGetValue(_object2RecordMap, object);
if (record)
{
/* Grave error if we have a record but we have a 0 count! */
EBAssertOrBail(_count);
/* Doing our cleanup here in the reverse order of -insertObjectAtHead. */
/* Update _headRecord */
if (_count == 1) _headRecord = nil;
else if (_headRecord == record) _headRecord = record->rightRecord;
/* Detach the record from the linked list */
detachRecord(record);
/* Update our count */
_count--;
/* Remove the object-record pair from the map */
CFDictionaryRemoveValue(_object2RecordMap, object);
/* Finally free the record */
free(record),
record = nil;
/* Update our mutation indicator */
_mutationIndicator++;
}
}
@end
@implementation EBUseEnumerator
{
EBUseRecord *_startRecord;
EBUseRecord *_currentRecord;
BOOL _forward;
unsigned long *_mutationIndicator;
}
- (id)initWithStartRecord: (EBUseRecord *)startRecord forward: (BOOL)forward mutationIndicator: (unsigned long *)mutationIndicator
{
/* We don't require a 'startRecord'! If it's nil, we just return 0 objects. */
NSParameterAssert(mutationIndicator);
if (!(self = [super init]))
return nil;
_startRecord = startRecord;
_currentRecord = startRecord;
_forward = forward;
_mutationIndicator = mutationIndicator;
return self;
}
- (NSUInteger)countByEnumeratingWithState: (NSFastEnumerationState *)state objects: (id *)requestedObjects count: (NSUInteger)requestedObjectsCount
{
NSParameterAssert(state);
NSParameterAssert(requestedObjects || !requestedObjectsCount);
/* Set up 'state' at the beginning of our enumeration */
if (!state->state)
{
state->mutationsPtr = _mutationIndicator;
state->state = YES;
}
/* itemsPtr has to be set to where we're putting our objects, which in our case is the supplied stack-based 'requestedObjects' */
state->itemsPtr = requestedObjects;
NSUInteger returnedObjectsCount = 0;
for (; _currentRecord && returnedObjectsCount < requestedObjectsCount; returnedObjectsCount++)
{
requestedObjects[returnedObjectsCount] = _currentRecord->object;
/* Get the next record, resetting _currentRecord if we've reached the end of our enumeration */
_currentRecord = (_forward ? _currentRecord->rightRecord : _currentRecord->leftRecord);
if (_currentRecord == _startRecord)
_currentRecord = nil;
}
return returnedObjectsCount;
}
@end