/* Gridlock Copyright (c) 2002-2003 by Brian Nenninger. All rights reserved. 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. */ #import "DCHypergridPosition.h" #include #include #include "hypergrid.h" #include "string.h" static DCHypergridPosition **_cachedPositions = NULL; static int CACHE_MIN = -25; static int CACHE_MAX = +25; static int CACHE_SIZE = 50; // private subclasses used for cached positions to avoid retains and releases @interface DCArrayWithCachedPosition : NSArray { id position; } @end @implementation DCArrayWithCachedPosition -(id)initWithPosition:(id)pos { self = [super init]; position = pos; return self; } // this initializer is needed for GNUstep #ifdef GNUSTEP -(id)initWithObjects:(id *)objects count:(unsigned)count { // GNUstep calls this from -[NSArray init] with arguments of nil and 0 if (count==0) return self; else [NSException raise:@"IndexOutOfBounds" format:@"%@: count (%d) must be 0 for %@", NSStringFromSelector(_cmd), index, NSStringFromClass([self class])]; return nil; } #endif -(unsigned)count { return 1; } -(id)objectAtIndex:(unsigned)index { if (index==0) return position; else [NSException raise:@"IndexOutOfBounds" format:@"%@: index (%d) must be 0 for %@", NSStringFromSelector(_cmd), index, NSStringFromClass([self class])]; return nil; } -(id)retain { return self; } -(void)release { return; } -(id)autorelease { return self; } @end @interface DCCachedHypergridPosition : DCHypergridPosition { NSArray *array; } @end @implementation DCCachedHypergridPosition : DCHypergridPosition -(id)retain { return self; } -(void)release { return; } -(id)autorelease { return self; } -(NSArray *)arrayWithSelf_ { if (!array) { array = [[DCArrayWithCachedPosition alloc] initWithPosition:self]; } return array; } @end @implementation DCHypergridPosition -(id)initWithSize:(int)cap { _numberOfDimensions = cap; _data = (int *)calloc(cap, sizeof(int)); return self; } -(id)initWithSize:(int)cap data:(int *)values { _numberOfDimensions = cap; _data = (int *)calloc(cap, sizeof(int)); memcpy(_data, values, cap*(sizeof(int))); return self; } -(id)initWithRow:(int)r column:(int)c { int values[] = {r,c}; return [self initWithSize:2 data:values]; } +(void)_fillCache { if (!_cachedPositions) { int r,c; int index=0; int numCachedPositions = CACHE_SIZE*CACHE_SIZE; _cachedPositions = (DCHypergridPosition **)malloc(numCachedPositions*sizeof(DCHypergridPosition *)); for(r=0; r=CACHE_MIN && values[0]=CACHE_MIN && values[1]=CACHE_MIN && r=CACHE_MIN && cv1) return NSOrderedAscending; if (v2