summary refs log tree commit diff stats
path: root/libs/cocos2d/Support/CCArray.m
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
committerStarla Insigna <starla4444@gmail.com>2011-07-30 11:19:14 -0400
commit9cd57b731ab1c666d4a1cb725538fdc137763d12 (patch)
tree5bac45ae5157a1cb10c6e45500cbf72789917980 /libs/cocos2d/Support/CCArray.m
downloadcartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.gz
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.tar.bz2
cartcollect-9cd57b731ab1c666d4a1cb725538fdc137763d12.zip
Initial commit (version 0.2.1)
Diffstat (limited to 'libs/cocos2d/Support/CCArray.m')
-rwxr-xr-xlibs/cocos2d/Support/CCArray.m290
1 files changed, 290 insertions, 0 deletions
diff --git a/libs/cocos2d/Support/CCArray.m b/libs/cocos2d/Support/CCArray.m new file mode 100755 index 0000000..a48a5f3 --- /dev/null +++ b/libs/cocos2d/Support/CCArray.m
@@ -0,0 +1,290 @@
1/*
2 * cocos2d for iPhone: http://www.cocos2d-iphone.org
3 *
4 * Copyright (c) 2010 ForzeField Studios S.L. http://forzefield.com
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25#import "CCArray.h"
26#import "../ccMacros.h"
27
28
29@implementation CCArray
30
31+ (id) array
32{
33 return [[[self alloc] init] autorelease];
34}
35
36+ (id) arrayWithCapacity:(NSUInteger)capacity
37{
38 return [[[self alloc] initWithCapacity:capacity] autorelease];
39}
40
41+ (id) arrayWithArray:(CCArray*)otherArray
42{
43 return [[(CCArray*)[self alloc] initWithArray:otherArray] autorelease];
44}
45
46+ (id) arrayWithNSArray:(NSArray*)otherArray
47{
48 return [[(CCArray*)[self alloc] initWithNSArray:otherArray] autorelease];
49}
50
51- (id) init
52{
53 self = [self initWithCapacity:2];
54 return self;
55}
56
57- (id) initWithCapacity:(NSUInteger)capacity
58{
59 self = [super init];
60 if (self != nil) {
61 data = ccArrayNew(capacity);
62 }
63 return self;
64}
65
66- (id) initWithArray:(CCArray*)otherArray
67{
68 self = [self initWithCapacity:otherArray->data->num];
69 if (self != nil) {
70 [self addObjectsFromArray:otherArray];
71 }
72 return self;
73}
74
75- (id) initWithNSArray:(NSArray*)otherArray
76{
77 self = [self initWithCapacity:otherArray.count];
78 if (self != nil) {
79 [self addObjectsFromNSArray:otherArray];
80 }
81 return self;
82}
83
84- (id) initWithCoder:(NSCoder*)coder
85{
86 self = [self initWithNSArray:[coder decodeObjectForKey:@"nsarray"]];
87 return self;
88}
89
90
91#pragma mark Querying an Array
92
93- (NSUInteger) count
94{
95 return data->num;
96}
97
98- (NSUInteger) capacity
99{
100 return data->max;
101}
102
103- (NSUInteger) indexOfObject:(id)object
104{
105 return ccArrayGetIndexOfObject(data, object);
106}
107
108- (id) objectAtIndex:(NSUInteger)index
109{
110 NSAssert2( index < data->num, @"index out of range in objectAtIndex(%d), index %i", data->num, index );
111
112 return data->arr[index];
113}
114
115- (BOOL) containsObject:(id)object
116{
117 return ccArrayContainsObject(data, object);
118}
119
120- (id) lastObject
121{
122 if( data->num > 0 )
123 return data->arr[data->num-1];
124 return nil;
125}
126
127- (id) randomObject
128{
129 if(data->num==0) return nil;
130 return data->arr[(int)(data->num*CCRANDOM_0_1())];
131}
132
133- (NSArray*) getNSArray
134{
135 return [NSArray arrayWithObjects:data->arr count:data->num];
136}
137
138
139#pragma mark Adding Objects
140
141- (void) addObject:(id)object
142{
143 ccArrayAppendObjectWithResize(data, object);
144}
145
146- (void) addObjectsFromArray:(CCArray*)otherArray
147{
148 ccArrayAppendArrayWithResize(data, otherArray->data);
149}
150
151- (void) addObjectsFromNSArray:(NSArray*)otherArray
152{
153 ccArrayEnsureExtraCapacity(data, otherArray.count);
154 for(id object in otherArray)
155 ccArrayAppendObject(data, object);
156}
157
158- (void) insertObject:(id)object atIndex:(NSUInteger)index
159{
160 ccArrayInsertObjectAtIndex(data, object, index);
161}
162
163
164#pragma mark Removing Objects
165
166- (void) removeObject:(id)object
167{
168 ccArrayRemoveObject(data, object);
169}
170
171- (void) removeObjectAtIndex:(NSUInteger)index
172{
173 ccArrayRemoveObjectAtIndex(data, index);
174}
175
176- (void) fastRemoveObject:(id)object
177{
178 ccArrayFastRemoveObject(data, object);
179}
180
181- (void) fastRemoveObjectAtIndex:(NSUInteger)index
182{
183 ccArrayFastRemoveObjectAtIndex(data, index);
184}
185
186- (void) removeObjectsInArray:(CCArray*)otherArray
187{
188 ccArrayRemoveArray(data, otherArray->data);
189}
190
191- (void) removeLastObject
192{
193 NSAssert( data->num > 0, @"no objects added" );
194
195 ccArrayRemoveObjectAtIndex(data, data->num-1);
196}
197
198- (void) removeAllObjects
199{
200 ccArrayRemoveAllObjects(data);
201}
202
203
204#pragma mark Rearranging Content
205
206- (void) exchangeObject:(id)object1 withObject:(id)object2
207{
208 NSUInteger index1 = ccArrayGetIndexOfObject(data, object1);
209 if(index1 == NSNotFound) return;
210 NSUInteger index2 = ccArrayGetIndexOfObject(data, object2);
211 if(index2 == NSNotFound) return;
212
213 ccArraySwapObjectsAtIndexes(data, index1, index2);
214}
215
216- (void) exchangeObjectAtIndex:(NSUInteger)index1 withObjectAtIndex:(NSUInteger)index2
217{
218 ccArraySwapObjectsAtIndexes(data, index1, index2);
219}
220
221- (void) reverseObjects
222{
223 if (data->num > 1)
224 {
225 //floor it since in case of a oneven number the number of swaps stays the same
226 int count = (int) floorf(data->num/2.f);
227 NSUInteger maxIndex = data->num - 1;
228
229 for (int i = 0; i < count ; i++)
230 {
231 ccArraySwapObjectsAtIndexes(data, i, maxIndex);
232 maxIndex--;
233 }
234 }
235}
236
237- (void) reduceMemoryFootprint
238{
239 ccArrayShrink(data);
240}
241
242#pragma mark Sending Messages to Elements
243
244- (void) makeObjectsPerformSelector:(SEL)aSelector
245{
246 ccArrayMakeObjectsPerformSelector(data, aSelector);
247}
248
249- (void) makeObjectsPerformSelector:(SEL)aSelector withObject:(id)object
250{
251 ccArrayMakeObjectsPerformSelectorWithObject(data, aSelector, object);
252}
253
254
255#pragma mark CCArray - NSFastEnumeration protocol
256
257- (NSUInteger)countByEnumeratingWithState:(NSFastEnumerationState *)state objects:(id *)stackbuf count:(NSUInteger)len
258{
259 if(state->state == 1) return 0;
260
261 state->mutationsPtr = (unsigned long *)self;
262 state->itemsPtr = &data->arr[0];
263 state->state = 1;
264 return data->num;
265}
266
267
268#pragma mark CCArray - NSCopying protocol
269
270- (id)copyWithZone:(NSZone *)zone
271{
272 NSArray *nsArray = [self getNSArray];
273 CCArray *newArray = [[[self class] allocWithZone:zone] initWithNSArray:nsArray];
274 return newArray;
275}
276
277- (void) encodeWithCoder:(NSCoder *)coder
278{
279 [coder encodeObject:[self getNSArray] forKey:@"nsarray"];
280}
281
282#pragma mark
283
284- (void) dealloc
285{
286 ccArrayFree(data);
287 [super dealloc];
288}
289
290@end