![]() |
API 0.9.5
|
00001 /* 00002 * CPIndexPath.j 00003 * Foundation 00004 * 00005 * Copyright 2008, 280 North, Inc. 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00022 00023 @implementation CPIndexPath : CPObject 00024 { 00025 CPArray _indexes; 00026 } 00027 00028 + (id)indexPathWithIndex:(int)index 00029 { 00030 return [[self alloc] initWithIndexes:[index] length:1]; 00031 } 00032 00033 + (id)indexPathWithIndexes:(CPArray)indexes length:(int)length 00034 { 00035 return [[self alloc] initWithIndexes:indexes length:length]; 00036 } 00037 00038 + (id)indexPathWithIndexes:(CPArray)indexes 00039 { 00040 return [[self alloc] initWithIndexes:indexes]; 00041 } 00042 00043 - (id)initWithIndexes:(CPArray)indexes length:(int)length 00044 { 00045 self = [super init]; 00046 00047 if (self) 00048 _indexes = [indexes subarrayWithRange:CPMakeRange(0, length)]; 00049 00050 return self; 00051 } 00052 00053 - (id)initWithIndexes:(CPArray)indexes 00054 { 00055 self = [super init]; 00056 00057 if (self) 00058 _indexes = [indexes copy]; 00059 00060 return self; 00061 } 00062 00063 - (CPString)description 00064 { 00065 return [super description] + " " + _indexes; 00066 } 00067 00068 #pragma mark - 00069 #pragma mark Accessing 00070 00071 - (id)length 00072 { 00073 return [_indexes count]; 00074 } 00075 00076 - (int)indexAtPosition:(int)position 00077 { 00078 return [_indexes objectAtIndex:position]; 00079 } 00080 00081 - (void)setIndexes:(CPArray)theIndexes 00082 { 00083 _indexes = [theIndexes copy]; 00084 } 00085 00086 - (CPArray)indexes 00087 { 00088 return [_indexes copy]; 00089 } 00090 00091 #pragma mark - 00092 #pragma mark Modification 00093 00094 - (CPIndexPath)indexPathByAddingIndex:(int)index 00095 { 00096 return [CPIndexPath indexPathWithIndexes:[_indexes arrayByAddingObject:index]]; 00097 } 00098 00099 - (CPIndexPath)indexPathByRemovingLastIndex 00100 { 00101 return [CPIndexPath indexPathWithIndexes:_indexes length:[self length] - 1]; 00102 } 00103 00104 #pragma mark - 00105 #pragma mark Comparison 00106 00107 - (BOOL)isEqual:(id)anObject 00108 { 00109 if (anObject === self) 00110 return YES; 00111 00112 if ([anObject class] !== [CPIndexPath class]) 00113 return NO; 00114 00115 return [_indexes isEqualToArray:[anObject indexes]]; 00116 } 00117 00118 - (CPComparisonResult)compare:(CPIndexPath)anIndexPath 00119 { 00120 if (!anIndexPath) 00121 [CPException raise:CPInvalidArgumentException reason:"indexPath to " + self + " was nil"]; 00122 00123 var lhsIndexes = [self indexes], 00124 rhsIndexes = [anIndexPath indexes], 00125 lhsCount = [lhsIndexes count], 00126 rhsCount = [rhsIndexes count]; 00127 00128 var index = 0, 00129 count = MIN(lhsCount, rhsCount); 00130 00131 for (; index < count; ++index) 00132 { 00133 var lhs = lhsIndexes[index], 00134 rhs = rhsIndexes[index]; 00135 00136 if (lhs < rhs) 00137 return CPOrderedAscending; 00138 00139 else if (lhs > rhs) 00140 return CPOrderedDescending; 00141 } 00142 00143 if (lhsCount === rhsCount) 00144 return CPOrderedSame; 00145 00146 if (lhsCount === count) 00147 return CPOrderedAscending; 00148 00149 return CPOrderedDescending; 00150 } 00151 00152 @end 00153 00154 var CPIndexPathIndexesKey = @"CPIndexPathIndexes"; 00155 00156 @implementation CPIndexPath (CPCoding) 00157 00158 - (id)initWithCoder:(CPCoder)theCoder 00159 { 00160 if (self = [self init]) 00161 { 00162 _indexes = [theCoder decodeObjectForKey:CPIndexPathIndexesKey]; 00163 } 00164 00165 return self; 00166 } 00167 00168 - (void)encodeWithCoder:(CPCoder)theCoder 00169 { 00170 [theCoder encodeObject:_indexes forKey:CPIndexPathIndexesKey]; 00171 } 00172 00173 @end 00174 00175 @implementation CPIndexPath (CPSynthesizedAccessors) 00176 00180 - (CPArray)indexes 00181 { 00182 return _indexes; 00183 } 00184 00188 - (void)setIndexes:(CPArray)aValue 00189 { 00190 _indexes = aValue; 00191 } 00192 00193 @end