6c7987e252b8c20f176a655f4e8e99e5d705ed96
[enjoyable.git] / NSMutableArray+MoveObject.m
1 //
2 // NSMutableArray+MoveObject.m
3 // Enjoyable
4 //
5 // Created by Joe Wreschnig on 3/7/13.
6 //
7 //
8
9 #import "NSMutableArray+MoveObject.h"
10
11 @implementation NSMutableArray (MoveObject)
12
13 - (void)moveObjectAtIndex:(NSUInteger)src toIndex:(NSUInteger)dst {
14 id obj = self[src];
15 [self removeObjectAtIndex:src];
16 [self insertObject:obj atIndex:dst > src ? dst - 1 : dst];
17 }
18
19 - (BOOL)moveFirstwards:(id)object upTo:(NSUInteger)minIndex {
20 NSUInteger idx = [self indexOfObject:object];
21 if (idx > minIndex && idx != NSNotFound) {
22 [self exchangeObjectAtIndex:idx withObjectAtIndex:idx - 1];
23 return YES;
24 } else {
25 return NO;
26 }
27 }
28
29 - (BOOL)moveLastwards:(id)object upTo:(NSUInteger)maxIndex {
30 maxIndex = MIN(self.count - 1, maxIndex);
31 NSUInteger idx = [self indexOfObject:object];
32 if (idx < maxIndex && idx != NSNotFound) {
33 [self exchangeObjectAtIndex:idx withObjectAtIndex:idx + 1];
34 return YES;
35 } else {
36 return NO;
37 }
38 }
39
40 - (BOOL)moveFirstwards:(id)object {
41 return [self moveFirstwards:object upTo:0];
42 }
43
44 - (BOOL)moveLastwards:(id)object {
45 return [self moveLastwards:object upTo:NSNotFound];
46 }
47
48
49 @end