Merge pull request #9 from gunyarakun/fix-typo
[cocotron.git] / Foundation / NSIndexPath.m
blob68f706388ac1fd2dd1a496de4f263b1454feafe0
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>
14 #include <string.h>
15 #include <stdlib.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 {
32    int i;
34    _length  = length;
35    _indexes = NSZoneMalloc(NULL,length*sizeof(NSUInteger));
37    for(i=0;i<length;i++)
38     _indexes[i]=indexes[i];
40    return self;
43 - (BOOL) isEqual:  other {
44         if ([other isKindOfClass: [NSIndexPath class]]) {
45                 NSIndexPath* otherPath = other;
46                 return _length == otherPath->_length && memcmp(_indexes, otherPath->_indexes, _length);
47         }
48         return NO;
51 - (void) dealloc {
52    if (_indexes)
53     free(_indexes);
54    [super dealloc];
57 - (NSIndexPath*) indexPathByAddingIndex: (NSUInteger) index {
58    NSUInteger length=[self length];
59    NSUInteger buffer[length+1];
61    [self getIndexes:buffer];
62    buffer[length]=index;
63    length++;
65    return [[[NSIndexPath alloc] initWithIndexes:buffer length:length] autorelease];
68 - (NSIndexPath*) indexPathByRemovingLastIndex {
69    if(_length==0){
70         [NSException raise:NSInvalidArgumentException format:@"Unable to remove index from zero length path."];
71     return nil;
72    }
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."];
80     return 0;
81    }
82    return _indexes[position];
85 - (NSUInteger) length {
86         return _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 {
95         int i;
96         for (i=0; i<_length; i++) {
97                 if (_indexes[i] != otherObject->_indexes[i]) {
98                         return _indexes[i] < otherObject->_indexes[i] ? NSOrderedAscending : NSOrderedDescending;
99                 }
100         }
101         return NSOrderedSame;
104 -copyWithZone:(NSZone*) zone {
105    return [self retain];
109 -copy  {
110    return [self retain];
113 -(NSUInteger) hash {
114    NSUInteger result=0;
115    int i;
117    for(i=0;i<_length;i++){
118     result = result*2 + _indexes[i];
119    }
120    result = 2*result + _length;
122    return result;
125 -(void)encodeWithCoder: (NSCoder*) coder {
126    NSUnimplementedMethod();
129 - initWithCoder: (NSCoder*) coder {
130    NSUnimplementedMethod();
131    return nil;
134 @end