-
Notifications
You must be signed in to change notification settings - Fork 1
/
FLazyArray.m
174 lines (154 loc) · 5.29 KB
/
FLazyArray.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
#import "FLazyArray.h"
#import <objc/runtime.h>
@interface FLazyArray () {
@protected
FLazyArrayResolver _resolver;
NSPointerArray *_array;
NSMutableIndexSet *_resolvedIndexes;
}
@end
@implementation FLazyArray
+ (instancetype)lazyArrayWithCount:(NSUInteger const)aCount resolver:(FLazyArrayResolver const)aResolver
{
FLazyArray * const array = [self new];
array->_resolver = aResolver;
array->_array = [NSPointerArray strongObjectsPointerArray];
array->_array.count = aCount;
return array;
}
- (instancetype)init
{
if((self = [super init]))
_resolvedIndexes = [NSMutableIndexSet new];
return self;
}
- (void)_resolveIndexes:(NSIndexSet * const)aIndexes
{
NSIndexSet *indexes = aIndexes;
if([indexes count] < _batchSize) {
indexes = [aIndexes mutableCopy];
NSRange const validRange = { 0, [self count] };
[aIndexes enumerateIndexesWithOptions:NSEnumerationReverse
usingBlock:^(NSUInteger idx, BOOL *stop) {
[(id)indexes addIndexesInRange:NSIntersectionRange(validRange,
(NSRange) { idx, _batchSize - [indexes count] + 1})];
if([indexes count] >= _batchSize)
*stop = YES;
}];
if([indexes count] < _batchSize && [aIndexes firstIndex] > 0) {
NSUInteger const firstIndex = [aIndexes firstIndex];
NSUInteger const remaining = _batchSize - [indexes count];
[(id)indexes addIndexesInRange:(NSRange) {
firstIndex > remaining ? firstIndex - remaining : 0,
MIN([self count], remaining)
}];
}
}
NSIndexSet * const unresolvedIndexes = [indexes indexesPassingTest:^BOOL(NSUInteger idx, BOOL *_) {
return ![_resolvedIndexes containsIndex:idx];
}];
if([unresolvedIndexes count] == 0)
return;
__strong id * const newlyResolved = (__strong id *)calloc([unresolvedIndexes count], sizeof(id));
_resolver(unresolvedIndexes, newlyResolved);
__block __strong id *head = newlyResolved;
[unresolvedIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
NSAssert(*head, @"Object resolved to nil!");
[_array replacePointerAtIndex:idx withPointer:(__bridge void *)*head++];
}];
[_resolvedIndexes addIndexes:unresolvedIndexes];
free(newlyResolved);
}
- (id)objectAtIndex:(NSUInteger const)aIdx
{
id object = (id)[_array pointerAtIndex:aIdx];
if(__builtin_expect(object != nil, 1))
return object;
else {
[self _resolveIndexes:[NSIndexSet indexSetWithIndex:aIdx]];
return (id)[_array pointerAtIndex:aIdx];
}
}
- (id)objectAtIndexedSubscript:(NSUInteger const)aIdx
{
return [self objectAtIndex:aIdx];
}
- (NSArray *)objectsAtIndexes:(NSIndexSet * const)aIndexes
{
if(![_resolvedIndexes containsIndexes:aIndexes])
[self _resolveIndexes:aIndexes];
NSMutableArray * const array = [NSMutableArray arrayWithCapacity:[aIndexes count]];
[aIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[array addObject:self[idx]];
}];
return array;
}
- (NSArray *)objectsInRange:(NSRange const)aRange
{
return [self objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:aRange]];
}
- (NSUInteger)count
{
return [_array count];
}
- (void)forgetObjectAtIndex:(NSUInteger)aIdx
{
[_resolvedIndexes removeIndex:aIdx];
[_array replacePointerAtIndex:aIdx withPointer:NULL];
}
- (void)forgetObjectsAtIndexes:(NSIndexSet * const)aIndexes
{
[_resolvedIndexes removeIndexes:aIndexes];
[aIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL *stop) {
[_array replacePointerAtIndex:idx withPointer:NULL];
}];
}
- (void)forgetObjectsInRange:(NSRange const)aRange
{
[self forgetObjectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:aRange]];
}
- (void)forgetAllObjects
{
[_resolvedIndexes removeAllIndexes];
NSUInteger const count = [self count];
_array.count = 0;
_array.count = count;
}
- (NSIndexSet *)resolvedIndexes
{
return [_resolvedIndexes copy];
}
- (NSString *)description
{
NSMutableString * const desc = [NSMutableString stringWithFormat:@"<%@: %p\n", [self class], self];
for(NSUInteger i = 0; i < [_array count]; ++i) {
[desc appendFormat:@" %@,\n", [_resolvedIndexes containsIndex:i]
? (id)[_array pointerAtIndex:i]
: @"(unresolved)"];
}
[desc appendString:@">"];
return desc;
}
@end
@implementation FMutableLazyArray
- (void)insertObjectAtIndex:(NSUInteger const)aIdx
{
[_resolvedIndexes shiftIndexesStartingAtIndex:aIdx by:1];
[_array insertPointer:NULL atIndex:aIdx];
}
- (void)removeObjectAtIndex:(NSUInteger)aIdx
{
[_resolvedIndexes removeIndex:aIdx];
[_resolvedIndexes shiftIndexesStartingAtIndex:aIdx+1 by:-1];
[_array removePointerAtIndex:aIdx];
}
- (void)removeObjectsAtAtIndexes:(NSIndexSet *)aIndexes
{
[_resolvedIndexes removeIndexes:aIndexes];
[aIndexes enumerateIndexesWithOptions:NSEnumerationReverse
usingBlock:^(NSUInteger idx, BOOL *stop) {
[_resolvedIndexes shiftIndexesStartingAtIndex:idx+1 by:-1];
[_array removePointerAtIndex:idx];
}];
}
@end