forked from gobbledegook/creevey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDYRandomizableArray.m
206 lines (180 loc) · 6.65 KB
/
DYRandomizableArray.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
#import "DYRandomizableArray.h"
#import "NSMutableArray+DYMovable.h"
@implementation DYRandomizableArray
{
NSMutableArray *array, // working array
*orderedArray; // saved copy, only populated when array is randomized
NSMutableArray<NSNumber *> *randomToOrdered, // parallel array to the working array
*orderedToRandom; // inversion of randomToOrdered
}
- (instancetype)init
{
self = [super init];
if (self) {
array = [[NSMutableArray alloc] init];
orderedArray = [[NSMutableArray alloc] init];
randomToOrdered = [[NSMutableArray alloc] init];
orderedToRandom = [[NSMutableArray alloc] init];
}
return self;
}
// array stuff
- (NSUInteger)count {
return array.count;
}
- (id)objectAtIndex:(NSUInteger)index {
return array[index];
}
- (id)objectAtIndexedSubscript:(NSUInteger)index {
return array[index];
}
- (NSUInteger)indexOfObject:(id)anObject {
return [array indexOfObject:anObject];
}
- (NSUInteger)indexOfObject:(id)anObject usingComparator:(NSComparator)cmp {
NSArray *a = orderedArray.count ? orderedArray : array;
NSUInteger idx = [a indexOfObject:anObject inSortedRange:NSMakeRange(0, array.count) options:0 usingComparator:cmp];
if (idx == NSNotFound) return NSNotFound;
return orderedArray.count ? orderedToRandom[idx].unsignedIntegerValue : idx;
}
- (NSUInteger)indexOfObject:(id)anObject usingComparator:(NSComparator)cmp insertIndex:(NSUInteger *)insertIdx {
NSUInteger count = array.count;
NSArray *a = orderedArray.count ? orderedArray : array;
NSUInteger idx = [a indexOfObject:anObject inSortedRange:NSMakeRange(0, count) options:NSBinarySearchingInsertionIndex usingComparator:cmp];
if (idx < count && [a[idx] isEqual:anObject])
return orderedArray.count ? orderedToRandom[idx].unsignedIntegerValue : idx;
*insertIdx = idx;
return NSNotFound;
}
// mutable array stuff
- (void)setArray:(NSArray *)otherArray {
[array setArray:otherArray];
[orderedArray removeAllObjects];
[randomToOrdered removeAllObjects];
[orderedToRandom removeAllObjects];
}
- (void)removeAllObjects {
[array removeAllObjects];
[orderedArray removeAllObjects];
[randomToOrdered removeAllObjects];
[orderedToRandom removeAllObjects];
}
- (void)removeObjectAtIndex:(NSUInteger)index {
if (orderedArray.count) {
// remove the item from orderedArray
NSUInteger orderedIndex = randomToOrdered[index].unsignedIntegerValue;
[orderedArray removeObjectAtIndex:orderedIndex];
// adjust r2o
NSUInteger i, count = array.count;
for (i=0; i<count; ++i) {
NSUInteger n = (randomToOrdered[i]).unsignedIntegerValue;
if (n > orderedIndex)
randomToOrdered[i] = @(n-1);
}
[randomToOrdered removeObjectAtIndex:index];
// adjust o2r
for (i=0; i<count; ++i) {
NSUInteger n = (orderedToRandom[i]).unsignedIntegerValue;
if (n > index)
orderedToRandom[i] = @(n-1);
}
[orderedToRandom removeObjectAtIndex:orderedIndex];
}
[array removeObjectAtIndex:index];
}
- (NSUInteger)insertObject:(id)anObject usingComparator:(NSComparator)cmp atIndex:(NSUInteger)index {
NSUInteger count = array.count;
if (index > count) index = count;
NSMutableArray *a = orderedArray.count ? orderedArray : array;
NSUInteger oIdx = [a indexOfObject:anObject inSortedRange:NSMakeRange(0, count) options:NSBinarySearchingInsertionIndex usingComparator:cmp];
if (oIdx < count && [a[oIdx] isEqual:anObject])
return orderedArray.count ? orderedToRandom[oIdx].unsignedIntegerValue : oIdx;
return [self insertObject:anObject usingOrderedIndex:oIdx atIndex:index];
}
- (NSUInteger)insertObject:(id)anObject usingOrderedIndex:(NSUInteger)oIdx atIndex:(NSUInteger)index {
NSUInteger count = array.count;
if (orderedArray.count) {
[orderedArray insertObject:anObject atIndex:oIdx];
NSUInteger i;
for (i=0; i<count; ++i) {
NSUInteger n = (randomToOrdered[i]).unsignedIntegerValue;
if (n >= oIdx)
randomToOrdered[i] = @(n+1);
}
[randomToOrdered insertObject:@(oIdx) atIndex:index];
for (i=0; i<count; ++i) {
NSUInteger n = (orderedToRandom[i]).unsignedIntegerValue;
if (n >= index)
orderedToRandom[i] = @(n+1);
}
[orderedToRandom insertObject:@(index) atIndex:oIdx];
} else {
index = oIdx;
}
[array insertObject:anObject atIndex:index];
return index;
}
// randomizable stuff
- (void)derandomize {
[array setArray:orderedArray];
[orderedArray removeAllObjects];
[randomToOrdered removeAllObjects];
[orderedToRandom removeAllObjects];
}
- (void)randomize {
[self randomizeStartingWithObjectAtIndex:NSNotFound];
}
- (void)randomizeStartingWithObjectAtIndex:(NSUInteger)startIndex {
NSUInteger i, count = array.count;
// save a copy and initialize the other arrays if it's the first time randomizing
if (!orderedArray.count) {
[orderedArray setArray:array];
// initialize r2o array
[randomToOrdered removeAllObjects];
for (i=0; i<count; ++i) {
[randomToOrdered addObject:@(i)];
}
// and the o2r array
[orderedToRandom setArray:randomToOrdered]; // copy r2o just to make sure it has the right number of objects
}
// randomize
i = count;
if (startIndex != NSNotFound) {
// save selected object at the end
[array exchangeObjectAtIndex:startIndex withObjectAtIndex:--i];
[randomToOrdered exchangeObjectAtIndex:startIndex withObjectAtIndex:i];
}
while (--i) {
NSUInteger randomIndex = arc4random_uniform((uint32_t)i+1);
[array exchangeObjectAtIndex:i withObjectAtIndex:randomIndex];
[randomToOrdered exchangeObjectAtIndex:i withObjectAtIndex:randomIndex]; // simultaneously save r2o array (it's parallel)
orderedToRandom[(randomToOrdered[i]).unsignedIntegerValue] = @(i); // and save the inverse values to o2r array
}
if (startIndex != NSNotFound) {
// put selected object at the start
[array exchangeObjectAtIndex:0 withObjectAtIndex:count-1];
[randomToOrdered exchangeObjectAtIndex:0 withObjectAtIndex:count-1];
orderedToRandom[(randomToOrdered[count-1]).unsignedIntegerValue] = @(count-1);
}
// don't forget the item at index 0!
orderedToRandom[(randomToOrdered[0]).unsignedIntegerValue] = @0U;
}
- (NSUInteger)orderedIndexFromIndex:(NSUInteger)index {
return randomToOrdered[index].unsignedIntegerValue;
}
- (NSUInteger)orderedIndexOfObjectAfterIndex:(NSUInteger)index {
NSUInteger i = randomToOrdered[index].unsignedIntegerValue + 1;
if (i == array.count) return NSNotFound;
return (orderedToRandom[i]).unsignedIntegerValue;
}
- (NSUInteger)orderedIndexOfObjectBeforeIndex:(NSUInteger)index {
NSUInteger i = randomToOrdered[index].unsignedIntegerValue;
if (i == 0) return NSNotFound;
return (orderedToRandom[i-1]).unsignedIntegerValue;
}
- (void)changeBase:(NSString *)basePath toPath:(NSString *)newBase {
if (orderedArray.count)
[orderedArray changeBase:basePath toPath:newBase];
[array changeBase:basePath toPath:newBase];
}
@end