forked from pmougin/F-Script
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathFSArrayEnumerator.m
57 lines (50 loc) · 1.12 KB
/
FSArrayEnumerator.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
// FSArrayEnumerator.m Copyright (c) 2003-2009 Philippe Mougin.
// This software is open source. See the license.
#import "FSArrayEnumerator.h"
#import "FSArray.h"
@implementation FSArrayEnumerator
- (void) dealloc
{
[array release];
[super dealloc];
}
- (FSArrayEnumerator *)initWithArray:(FSArray *)theArray reverse:(BOOL)reverse
{
if ((self = [super init]))
{
NSUInteger count = [theArray count];
iterationCompleted = (count == 0);
array = [theArray retain];
next = reverse ? count-1 : 0;
increment = reverse ? -1 : 1;
last = reverse ? 0 : count-1;
return self;
}
return nil;
}
- (id)nextObject
{
if (iterationCompleted)
return nil;
else
{
id r = [array objectAtIndex:next];
iterationCompleted = (next == last);
next += increment;
return r;
}
}
- (FSArray *)allObjects
{
if (iterationCompleted)
return [FSArray array];
else
{
iterationCompleted = YES;
if (increment == 1)
return (FSArray *)[array subarrayWithRange:NSMakeRange(next, (last-next)+1)];
else
return (FSArray *)[array subarrayWithRange:NSMakeRange(0, next+1)];
}
}
@end