-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDSSparseArrayEnumerator.m
More file actions
62 lines (57 loc) · 1.99 KB
/
Copy pathDSSparseArrayEnumerator.m
File metadata and controls
62 lines (57 loc) · 1.99 KB
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
//
// DSSparseArrayEnumerator.m
// A component of DSSparseArray
//
// Created by David W. Stockton on 5/20/14.
// Copyright (c) 2014 Syntonicity. All rights reserved.
//
#import "DSSparseArrayEnumerator.h"
#import "DSSparseArray.h"
@interface DSSparseArrayEnumerator ()
@property (nonatomic, strong) DSSparseArray *theSparseArray;
@property (nonatomic, assign) NSUInteger currentIndex;
@property (nonatomic, assign) BOOL reverse;
@end
@implementation DSSparseArrayEnumerator
// Of the NSEnumerationOptions, accepts only NSEnumerationReverse
+ (instancetype) enumeratorForSparseArray: (DSSparseArray *) sparseArray withOptions: (NSEnumerationOptions) opts {
DSSparseArrayEnumerator *enumerator = [[DSSparseArrayEnumerator alloc] init];
enumerator.theSparseArray = sparseArray;
if( !sparseArray ) {
enumerator.currentIndex = NSNotFound;
} else if( opts & NSEnumerationReverse ) {
enumerator.currentIndex = [[sparseArray allIndexes] lastIndex];
enumerator.reverse = 1;
} else {
enumerator.currentIndex = [[sparseArray allIndexes] firstIndex];
}
return enumerator;
}
// Return the index for the next object from the sparse array but don't advance
- (NSUInteger) indexOfNextObject {
return self.currentIndex;
}
// Return the next object from the sparse array and advance the pointer
- (id) nextObject {
id obj = nil;
if( self.currentIndex != NSNotFound ) {
obj = [self.theSparseArray objectAtIndex: self.currentIndex];
if( self.reverse )
self.currentIndex = [[self.theSparseArray allIndexes] indexLessThanIndex: self.currentIndex];
else self.currentIndex = [[self.theSparseArray allIndexes] indexGreaterThanIndex: self.currentIndex];
}
return obj;
}
// Return the rest of the objects, not really all of the objects
- (NSArray *) allObjects {
if( self.currentIndex != NSNotFound ) {
NSMutableArray *all = [NSMutableArray arrayWithCapacity: [self.theSparseArray count]];
id obj;
while( (obj = [self nextObject]) != nil ) {
[all addObject: obj];
}
return [all copy];
}
return nil;
}
@end