1 /* Copyright (c) 2007 Dirk Theisen
3 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:
5 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 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. */
9 #import <Foundation/NSIndexPath.h>
10 #import <Foundation/NSString.h>
11 #import <Foundation/NSException.h>
12 #import <Foundation/NSSet.h>
13 #import <Foundation/NSRaise.h>
17 @implementation NSIndexPath
19 + (NSIndexPath*) indexPathWithIndex: (NSUInteger) index {
20 return [[[self alloc] initWithIndexes: &index length: 1] autorelease];
23 + (NSIndexPath*) indexPathWithIndexes: (NSUInteger*) indexes length: (NSUInteger) length {
24 return [[[self alloc] initWithIndexes: indexes length: length] autorelease];
27 - initWithIndex: (NSUInteger) index {
28 return [self initWithIndexes: &index length: 1];
31 - initWithIndexes: (NSUInteger*) indexes length: (NSUInteger) length {
35 _indexes = NSZoneMalloc(NULL,length*sizeof(NSUInteger));
38 _indexes[i]=indexes[i];
43 - (BOOL) isEqual: other {
44 if ([other isKindOfClass: [NSIndexPath class]]) {
45 NSIndexPath* otherPath = other;
46 return _length == otherPath->_length && memcmp(_indexes, otherPath->_indexes, _length);
57 - (NSIndexPath*) indexPathByAddingIndex: (NSUInteger) index {
58 NSUInteger length=[self length];
59 NSUInteger buffer[length+1];
61 [self getIndexes:buffer];
65 return [[[NSIndexPath alloc] initWithIndexes:buffer length:length] autorelease];
68 - (NSIndexPath*) indexPathByRemovingLastIndex {
70 [NSException raise:NSInvalidArgumentException format:@"Unable to remove index from zero length path."];
74 return [[[NSIndexPath alloc] initWithIndexes: _indexes length: _length-1] autorelease];
77 - (NSUInteger) indexAtPosition: (NSUInteger) position {
78 if(position>=_length){
79 [NSException raise:NSInvalidArgumentException format:@"Unable to remove index from zero length path."];
82 return _indexes[position];
85 - (NSUInteger) length {
89 - (void) getIndexes: (NSUInteger*) indexes {
90 memcpy(indexes, _indexes, _length * sizeof(NSUInteger));
93 /** Note: Sorting an array of indexPaths using this comparison method results in an array representing nodes in depth-first traversal order. */
94 - (NSComparisonResult) compare: (NSIndexPath*) otherObject {
96 for (i=0; i<_length; i++) {
97 if (_indexes[i] != otherObject->_indexes[i]) {
98 return _indexes[i] < otherObject->_indexes[i] ? NSOrderedAscending : NSOrderedDescending;
101 return NSOrderedSame;
104 -copyWithZone:(NSZone*) zone {
105 return [self retain];
110 return [self retain];
117 for(i=0;i<_length;i++){
118 result = result*2 + _indexes[i];
120 result = 2*result + _length;
125 -(void)encodeWithCoder: (NSCoder*) coder {
126 NSUnimplementedMethod();
129 - initWithCoder: (NSCoder*) coder {
130 NSUnimplementedMethod();