API  0.9.10
CPIndexPath.j
Go to the documentation of this file.
1 /*
2  * CPIndexPath.j
3  * Foundation
4  *
5  * Copyright 2008, 280 North, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 
23 @implementation CPIndexPath : CPObject
24 {
25  CPArray _indexes;
26 }
27 
28 + (id)indexPathWithIndex:(int)index
29 {
30  return [[self alloc] initWithIndexes:[index] length:1];
31 }
32 
33 + (id)indexPathWithIndexes:(CPArray)indexes length:(int)length
34 {
35  return [[self alloc] initWithIndexes:indexes length:length];
36 }
37 
38 + (id)indexPathWithIndexes:(CPArray)indexes
39 {
40  return [[self alloc] initWithIndexes:indexes];
41 }
42 
43 - (id)initWithIndexes:(CPArray)indexes length:(int)length
44 {
45  self = [super init];
46 
47  if (self)
48  _indexes = [indexes subarrayWithRange:CPMakeRange(0, length)];
49 
50  return self;
51 }
52 
53 - (id)initWithIndexes:(CPArray)indexes
54 {
55  self = [super init];
56 
57  if (self)
58  _indexes = [indexes copy];
59 
60  return self;
61 }
62 
64 {
65  return [super description] + " " + _indexes;
66 }
67 
68 #pragma mark -
69 #pragma mark Accessing
70 
71 - (id)length
72 {
73  return [_indexes count];
74 }
75 
76 - (int)indexAtPosition:(int)position
77 {
78  return [_indexes objectAtIndex:position];
79 }
80 
81 - (void)setIndexes:(CPArray)theIndexes
82 {
83  _indexes = [theIndexes copy];
84 }
85 
86 - (CPArray)indexes
87 {
88  return [_indexes copy];
89 }
90 
91 #pragma mark -
92 #pragma mark Modification
93 
94 - (CPIndexPath)indexPathByAddingIndex:(int)index
95 {
96  return [CPIndexPath indexPathWithIndexes:[_indexes arrayByAddingObject:index]];
97 }
98 
100 {
101  return [CPIndexPath indexPathWithIndexes:_indexes length:[self length] - 1];
102 }
103 
104 #pragma mark -
105 #pragma mark Comparison
106 
107 - (BOOL)isEqual:(id)anObject
108 {
109  if (anObject === self)
110  return YES;
111 
112  if ([anObject class] !== [CPIndexPath class])
113  return NO;
114 
115  return [_indexes isEqualToArray:[anObject indexes]];
116 }
117 
118 - (CPComparisonResult)compare:(CPIndexPath)anIndexPath
119 {
120  if (!anIndexPath)
121  [CPException raise:CPInvalidArgumentException reason:"indexPath to " + self + " was nil"];
122 
123  var lhsIndexes = [self indexes],
124  rhsIndexes = [anIndexPath indexes],
125  lhsCount = [lhsIndexes count],
126  rhsCount = [rhsIndexes count];
127 
128  var index = 0,
129  count = MIN(lhsCount, rhsCount);
130 
131  for (; index < count; ++index)
132  {
133  var lhs = lhsIndexes[index],
134  rhs = rhsIndexes[index];
135 
136  if (lhs < rhs)
137  return CPOrderedAscending;
138 
139  else if (lhs > rhs)
140  return CPOrderedDescending;
141  }
142 
143  if (lhsCount === rhsCount)
144  return CPOrderedSame;
145 
146  if (lhsCount === count)
147  return CPOrderedAscending;
148 
149  return CPOrderedDescending;
150 }
151 
152 @end
153 
154 var CPIndexPathIndexesKey = @"CPIndexPathIndexes";
155 
156 @implementation CPIndexPath (CPCoding)
157 
158 - (id)initWithCoder:(CPCoder)theCoder
159 {
160  if (self = [self init])
161  {
162  _indexes = [theCoder decodeObjectForKey:CPIndexPathIndexesKey];
163  }
164 
165  return self;
166 }
167 
168 - (void)encodeWithCoder:(CPCoder)theCoder
169 {
170  [theCoder encodeObject:_indexes forKey:CPIndexPathIndexesKey];
171 }
172 
173 @end
174 
176 
180 - (CPArray)indexes
181 {
182  return _indexes;
183 }
184 
188 - (void)setIndexes:(CPArray)aValue
189 {
190  _indexes = aValue;
191 }
192 
193 @end
Used to implement exception handling (creating & raising).
Definition: CPException.h:2
CPOrderedAscending
Definition: CPObjJRuntime.j:48
var isEqual
CPOrderedSame
Definition: CPObjJRuntime.j:54
void raise:reason:(CPString aName, [reason] CPString aReason)
Definition: CPException.j:66
CPIndexPath indexPathByRemovingLastIndex()
Definition: CPIndexPath.j:99
CPString description()
Definition: CPObject.j:358
An immutable string (collection of characters).
Definition: CPString.h:2
CPOrderedDescending
Definition: CPObjJRuntime.j:60
id initWithIndexes:length:(CPArray indexes, [length] int length)
Definition: CPIndexPath.j:43
CPArray indexes()
Definition: CPIndexPath.j:86
id indexPathWithIndexes:(CPArray indexes)
Definition: CPIndexPath.j:38
Defines methods for use when archiving & restoring (enc/decoding).
Definition: CPCoder.h:2
id initWithIndexes:(CPArray indexes)
Definition: CPIndexPath.j:53
id init()
Definition: CPObject.j:145
CPString description()
Definition: CPIndexPath.j:63
var CPIndexPathIndexesKey
Definition: CPIndexPath.j:154
id indexPathWithIndexes:length:(CPArray indexes, [length] int length)
Definition: CPIndexPath.j:33
id alloc()
Definition: CPObject.j:130