// // ZNMutable2DArray.m // Znarhg // // Created by Julian Vrieslander on 1/20/05. // Copyright 2005 University of Washington. All rights reserved. // // This is a category that extends the NSMutableArray class to a two dimensional array. #import "ZNMutable2DArray.h" @implementation NSMutableArray (ZNMutable2DArray) #pragma mark - #pragma mark Startup and Shutdown + (NSMutableArray *)arrayWithRows:(short)rowCount andColumns:(short)colCount { NSMutableArray *rowArray = [NSMutableArray arrayWithCapacity:rowCount]; short rowIndex, colIndex; for (rowIndex = 0; rowIndex < rowCount; rowIndex++) { NSMutableArray *colArray = [NSMutableArray arrayWithCapacity:colCount]; for (colIndex = 0; colIndex < colCount; colIndex++) [colArray addObject:[NSNull null]]; // placeholder [rowArray addObject:colArray]; } return rowArray; } #pragma mark - #pragma mark Accessors // The following accessors do not perform bounds checking. If an index is beyond the dimensions of the array, an exception will be thrown. The code that created the 2D array is responsible for remembering the dimensions. That is because this module is a protocol - it can add methods, but not ivars to the NSMutableArray class. - (id)objectAtRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; return [row objectAtIndex:colIndex]; } - (void)setObject:(id)obj atRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; [row replaceObjectAtIndex:colIndex withObject:obj]; } - (int)intAtRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; return [[row objectAtIndex:colIndex] intValue]; } - (void)setInt:(int)value atRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; [row replaceObjectAtIndex:colIndex withObject:[NSNumber numberWithInt:value]]; } - (float)floatAtRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; return [[row objectAtIndex:colIndex] floatValue]; } - (void)setFloat:(float)value atRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; [row replaceObjectAtIndex:colIndex withObject:[NSNumber numberWithFloat:value]]; } - (double)doubleAtRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; return [[row objectAtIndex:colIndex] doubleValue]; } - (void)setDouble:(double)value atRowIndex:(short)rowIndex columnIndex:(short)colIndex { NSMutableArray *row = [self objectAtIndex:rowIndex]; [row replaceObjectAtIndex:colIndex withObject:[NSNumber numberWithDouble:value]]; } @end