]> git.saurik.com Git - apple/objc4.git/blob - runtime/OldClasses.subproj/List.m
objc4-437.1.tar.gz
[apple/objc4.git] / runtime / OldClasses.subproj / List.m
1 /*
2 * Copyright (c) 1999-2001, 2005-2006 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 List.m
25 Copyright 1988-1996 NeXT Software, Inc.
26 Written by: Bryan Yamamoto
27 Responsibility: Bertrand Serlet
28 */
29
30 #ifndef __OBJC2__
31
32 #import <stdlib.h>
33 #import <stdio.h>
34 #import <string.h>
35
36 #define OLD 1
37 #import <objc/List.h>
38
39 #define DATASIZE(count) ((count) * sizeof(id))
40
41 @implementation List
42
43 + initialize
44 {
45 [self setVersion: 1];
46 return self;
47 }
48
49 - initCount:(unsigned)numSlots
50 {
51 maxElements = numSlots;
52 if (maxElements)
53 dataPtr = (id *)malloc(DATASIZE(maxElements));
54 return self;
55 }
56
57 + newCount:(unsigned)numSlots
58 {
59 return [[self alloc] initCount:numSlots];
60 }
61
62 + new
63 {
64 return [self newCount:0];
65 }
66
67 - init
68 {
69 return [self initCount:0];
70 }
71
72 - free
73 {
74 free(dataPtr);
75 return [super free];
76 }
77
78 - freeObjects
79 {
80 id element;
81 while ((element = [self removeLastObject]))
82 [element free];
83 return self;
84 }
85
86 - copyFromZone:(void *)z
87 {
88 List *new = [[[self class] alloc] initCount: numElements];
89 new->numElements = numElements;
90 bcopy ((const char*)dataPtr, (char*)new->dataPtr, DATASIZE(numElements));
91 return new;
92 }
93
94 - (BOOL) isEqual: anObject
95 {
96 List *other;
97 if (! [anObject isKindOf: [self class]]) return NO;
98 other = (List *) anObject;
99 return (numElements == other->numElements)
100 && (bcmp ((const char*)dataPtr, (const char*)other->dataPtr, DATASIZE(numElements)) == 0);
101 }
102
103 - (unsigned)capacity
104 {
105 return maxElements;
106 }
107
108 - (unsigned)count
109 {
110 return numElements;
111 }
112
113 - objectAt:(unsigned)index
114 {
115 if (index >= numElements)
116 return nil;
117 return dataPtr[index];
118 }
119
120 - (unsigned)indexOf:anObject
121 {
122 register id *this = dataPtr;
123 register id *last = this + numElements;
124 while (this < last) {
125 if (*this == anObject)
126 return this - dataPtr;
127 this++;
128 }
129 return NX_NOT_IN_LIST;
130 }
131
132 - lastObject
133 {
134 if (! numElements)
135 return nil;
136 return dataPtr[numElements - 1];
137 }
138
139 - setAvailableCapacity:(unsigned)numSlots
140 {
141 volatile id *tempDataPtr;
142 if (numSlots < numElements) return nil;
143 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(numSlots));
144 dataPtr = (id *)tempDataPtr;
145 maxElements = numSlots;
146 return self;
147 }
148
149 - insertObject:anObject at:(unsigned)index
150 {
151 register id *this, *last, *prev;
152 if (! anObject) return nil;
153 if (index > numElements)
154 return nil;
155 if ((numElements + 1) > maxElements) {
156 volatile id *tempDataPtr;
157 /* we double the capacity, also a good size for malloc */
158 maxElements += maxElements + 1;
159 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(maxElements));
160 dataPtr = (id*)tempDataPtr;
161 }
162 this = dataPtr + numElements;
163 prev = this - 1;
164 last = dataPtr + index;
165 while (this > last)
166 *this-- = *prev--;
167 *last = anObject;
168 numElements++;
169 return self;
170 }
171
172 - addObject:anObject
173 {
174 return [self insertObject:anObject at:numElements];
175
176 }
177
178
179 - addObjectIfAbsent:anObject
180 {
181 register id *this, *last;
182 if (! anObject) return nil;
183 this = dataPtr;
184 last = dataPtr + numElements;
185 while (this < last) {
186 if (*this == anObject)
187 return self;
188 this++;
189 }
190 return [self insertObject:anObject at:numElements];
191
192 }
193
194
195 - removeObjectAt:(unsigned)index
196 {
197 register id *this, *last, *next;
198 id retval;
199 if (index >= numElements)
200 return nil;
201 this = dataPtr + index;
202 last = dataPtr + numElements;
203 next = this + 1;
204 retval = *this;
205 while (next < last)
206 *this++ = *next++;
207 numElements--;
208 return retval;
209 }
210
211 - removeObject:anObject
212 {
213 register id *this, *last;
214 this = dataPtr;
215 last = dataPtr + numElements;
216 while (this < last) {
217 if (*this == anObject)
218 return [self removeObjectAt:this - dataPtr];
219 this++;
220 }
221 return nil;
222 }
223
224 - removeLastObject
225 {
226 if (! numElements)
227 return nil;
228 return [self removeObjectAt: numElements - 1];
229 }
230
231 - empty
232 {
233 numElements = 0;
234 return self;
235 }
236
237 - replaceObject:anObject with:newObject
238 {
239 register id *this, *last;
240 if (! newObject)
241 return nil;
242 this = dataPtr;
243 last = dataPtr + numElements;
244 while (this < last) {
245 if (*this == anObject) {
246 *this = newObject;
247 return anObject;
248 }
249 this++;
250 }
251 return nil;
252 }
253
254 - replaceObjectAt:(unsigned)index with:newObject
255 {
256 register id *this;
257 id retval;
258 if (! newObject)
259 return nil;
260 if (index >= numElements)
261 return nil;
262 this = dataPtr + index;
263 retval = *this;
264 *this = newObject;
265 return retval;
266 }
267
268 - makeObjectsPerform:(SEL)aSelector
269 {
270 unsigned count = numElements;
271 while (count--)
272 [dataPtr[count] perform: aSelector];
273 return self;
274 }
275
276 - makeObjectsPerform:(SEL)aSelector with:anObject
277 {
278 unsigned count = numElements;
279 while (count--)
280 [dataPtr[count] perform: aSelector with: anObject];
281 return self;
282 }
283
284 -appendList: (List *)otherList
285 {
286 unsigned i, count;
287
288 for (i = 0, count = [otherList count]; i < count; i++)
289 [self addObject: [otherList objectAt: i]];
290 return self;
291 }
292
293 @end
294
295 #endif