/*******************************************************************************
 Copyright (c) 2005-2006 Jordy Rose
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
 
 Except as contained in this notice, the name(s) of the above copyright holders
 shall not be used in advertising or otherwise to promote the sale, use or other
 dealings in this Software without prior authorization.
*******************************************************************************/

//
//  NSArray+PantherIndexSetSupport.m
//  Dockyard
//

#import "NSArray+PantherIndexSetSupport.h"

@interface NSArray (ComBelkadanUtils_TigerIndexSetMethods)
- (NSArray *)objectsAtIndexes:(NSIndexSet *)indexSet;
@end

/*!
 * Provides basic support for Tiger-only NSArray methods dealing with NSIndexSet.
 * If you are developing a 10.3 application, call one of these methods instead
 * of the standard ones. On a 10.4 system, these methods will simply forward
 * the message. On a 10.3 system, they will do the work themselves. (NSIndexSet
 * does not exist on pre-10.3 systems).
 */
@implementation NSArray (ComBelkadanUtils_PantherIndexSetSupport)
/*!
 * Returns an array containing the objects in the receiver at the specified indexes.
 * It is a programming error if any location in indexes exceeds the bounds of the receiver
 * (docs taken from ADC). This method will call <code>objectsAtIndexes:</code> on
 * 10.4 or later systems, and use <code>objectAtIndex:</code> multiple times on 10.3.
 *
 * @functiongroup Retrieving objects
 * @see <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Objc_classic/Classes/NSArray.html#//apple_ref/doc/uid/20000137-BAJGGAAA" class="classLink">
 *			objectsAtIndexes:</a> (NSArray)
 */
- (NSArray *)pantherObjectsAtIndexes:(NSIndexSet *)indexSet
{
	if ([self respondsToSelector:@selector(objectsAtIndexes:)]) {
		return [self objectsAtIndexes:indexSet];
	} else {
		NSMutableArray *result = [NSMutableArray new];
		unsigned index;
		for (index = [indexSet firstIndex]; index != NSNotFound; index = [indexSet indexGreaterThanIndex:index]) {
			[result addObject:[self objectAtIndex:index]];
		}
		return [result autorelease];
	}
}
@end

@interface NSMutableArray (ComBelkadanUtils_TigerIndexSetMethods)
- (void)removeObjectsAtIndexes:(NSIndexSet *)indexSet;
- (void)insertObjects:(NSArray *)additions atIndexes:(NSIndexSet *)indexSet;
@end

/*!
 * Provides basic support for Tiger-only NSMutableArray methods dealing with NSIndexSet.
 * If you are developing a 10.3 application, call one of these methods instead
 * of the standard ones. On a 10.4 system, these methods will simply forward
 * the message. On a 10.3 system, they will do the work themselves. (NSIndexSet
 * does not exist on pre-10.3 systems).
 */
@implementation NSMutableArray (ComBelkadanUtils_PantherIndexSetSupport)
/*!
 * This method is similar to removeObjectAtIndex:, but allows you to efficiently remove
 * multiple objects with a single operation. indexes specifies the locations of objects
 * to be removed given the state of the receiver when the method is invoked. The locations
 * specified by indexes must lie within the bounds of the receiver. (docs taken from ADC).
 * This method will call <code>removeObjectsAtIndexes:</code> on 10.4 or later systems, and
 * use <code>removeObjectsFromIndices:numIndices:</code> on 10.3.
 *
 * @functiongroup Modifying the array
 * @see <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Objc_classic/Classes/NSMutableArray.html#//apple_ref/doc/uid/20000138-BCIHFHJD" class="classLink">
 *			removeObjectsAtIndexes:</a> (NSMutableArray)
 * @see <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Objc_classic/Classes/NSMutableArray.html#//apple_ref/doc/uid/20000138-BABCFEBC" class="classLink">
 *			removeObjectsAtIndices:numIndices:</a> (NSMutableArray)
 */
- (void)pantherRemoveObjectsAtIndexes:(NSIndexSet *)indexSet
{
	if ([self respondsToSelector:@selector(removeObjectsAtIndexes:)]) {
		[self removeObjectsAtIndexes:indexSet];
	} else {
		unsigned count = [indexSet count];
		unsigned *indices = calloc(sizeof(unsigned), count);
		NSRange fullRange = {0, UINT_MAX};
		[indexSet getIndexes:indices maxCount:count inIndexRange:&fullRange];
		[self removeObjectsFromIndices:indices numIndices:count];
		free(indices);
	}
}

/*!
 * Inserts the objects in objects into the receiver at the indexes specified by indexSet.
 * Each object in objects is inserted into the receiver in turn at the corresponding location
 * specified in indexSet after earlier insertions have been made (docs taken from ADC).
 * This method will call <code>insertObjects:atIndexes:</code> on 10.4 or later systems,
 * and use <code>insertObject:atIndex:</code> multiple times on 10.3.
 *
 * @see <a href="http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Objc_classic/Classes/NSMutableArray.html#//apple_ref/doc/uid/20000138-BCIEBJFC" class="classLink">
 *			insertObjects:atIndexes:</a> (NSMutableArray)
 */
- (void)pantherInsertObjects:(NSArray *)additions atIndexes:(NSIndexSet *)indexSet
{
	if ([self respondsToSelector:@selector(insertObjects:atIndexes:)]) {
		[self insertObjects:additions atIndexes:indexSet];
	} else {
		int currentIndex = [indexSet firstIndex];
		int i, count = [indexSet count];
			
		for (i = 0; i < count; i++) {
			[self insertObject:[additions objectAtIndex:i] atIndex:currentIndex];
			currentIndex = [indexSet indexGreaterThanIndex:currentIndex];
		}
	}
}
@end
