//
//  NSArrayExtensions.m
//  longspear
//
//  Created by Niko Matsakis on Sat Jun 19 2004.
//  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//

#import "NSArrayExtensions.h"


@implementation NSArray (LongSpear)

- (NSArray*) objectsAtIndices:(NSIndexSet*)indexset
{
    NSMutableArray *array = [[[NSMutableArray alloc] initWithCapacity:[indexset count]] autorelease];
    unsigned int index;
    
    index = [indexset firstIndex];
    while (index != NSNotFound) {
        id object = [self objectAtIndex:index];
        [array addObject:object];
        index = [indexset indexGreaterThanIndex:index];
    }
    
    return array;
}

- (NSIndexSet*) asIndexSet
{
    NSEnumerator *myobjs = [self objectEnumerator];
    id obj;
    NSMutableIndexSet *result = [[[NSMutableIndexSet alloc] init] autorelease];
    
    while ((obj = [myobjs nextObject])) {
        if ([obj respondsToSelector:@selector(intValue)]) {
            [result addIndex:[obj intValue]];
        }
    }
    
    return result;
}

- (NSArray*) mappedArray:(SEL)sel
{
    NSMutableArray *result = [[[NSMutableArray alloc] initWithCapacity:[self count]] autorelease];
    
    NSEnumerator *myobjs = [self objectEnumerator];
    id obj;

    while ((obj = [myobjs nextObject])) {
        if ([obj respondsToSelector:sel])
            [result addObject:[obj performSelector:sel]];
    }
    
    return result;
}

@end
