Skip to content

Instantly share code, notes, and snippets.

@ahayman
Created April 20, 2012 12:29
Show Gist options
  • Save ahayman/2428198 to your computer and use it in GitHub Desktop.
Save ahayman/2428198 to your computer and use it in GitHub Desktop.
Extensions for NSMutableArray
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return;
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : [self objectAtIndex:i + 1]];
}
} else {
id cObject;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
cObject = [self objectAtIndex:i];
[self replaceObjectAtIndex:i withObject:(i == toIndex) ? movingObject : prevObject];
prevObject = cObject;
}
}
}
- (void) moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex withBlock:(void (^)(id object, NSUInteger from, NSUInteger to))block{
if (fromIndex == toIndex) return;
if (fromIndex >= self.count) return;
if (toIndex >= self.count) toIndex = self.count - 1; //toIndex too large, assume a move to end
id movingObject = [self objectAtIndex:fromIndex];
id replacementObject;
NSUInteger from;
if (fromIndex < toIndex){
for (int i = fromIndex; i <= toIndex; i++){
from = (i == toIndex) ? fromIndex : i + 1;
replacementObject = (i == toIndex) ? movingObject : [self objectAtIndex:from];
[self replaceObjectAtIndex:i withObject:replacementObject];
if (block) block(replacementObject, from, i);
}
} else {
id cObject;
id prevObject;
for (int i = toIndex; i <= fromIndex; i++){
cObject = [self objectAtIndex:i];
replacementObject = (i == toIndex) ? movingObject : prevObject;
from = (i == toIndex) ? fromIndex : i - 1;
[self replaceObjectAtIndex:i withObject:replacementObject];
prevObject = cObject;
if (block) block(replacementObject, from, i);
}
}
}
- (void) invertArray{
NSUInteger operationCount = self.count / 2;
NSUInteger lastIndex = self.count - 1;
id tmpObject;
for (int i = 0; i < operationCount; i++){
tmpObject = [self objectAtIndex:i];
[self replaceObjectAtIndex:i withObject:[self objectAtIndex:lastIndex - i]];
[self replaceObjectAtIndex:lastIndex - i withObject:tmpObject];
}
}
- (void) invertArrayWithOperationBlock:(void(^)(id object, NSUInteger from, NSUInteger to))block{
NSUInteger operationCount = self.count / 2;
NSUInteger lastIndex = self.count - 1;
id tmpObject1;
id tmpObject2;
for (int i = 0; i < operationCount; i++){
tmpObject1 = [self objectAtIndex:i];
tmpObject2 = [self objectAtIndex:lastIndex - i];
[self replaceObjectAtIndex:i withObject:tmpObject2];
[self replaceObjectAtIndex:lastIndex - i withObject:tmpObject1];
if (block){
block(tmpObject1, i, lastIndex - i);
block(tmpObject2, lastIndex - i, i);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment