2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
27 Copyright 1988-1996 NeXT Software, Inc.
28 Written by: Bryan Yamamoto
29 Responsibility: Bertrand Serlet
38 #define DATASIZE(count) ((count) * sizeof(id))
48 - initCount:(unsigned)numSlots
50 maxElements = numSlots;
52 dataPtr = (id *)malloc(DATASIZE(maxElements));
56 + newCount:(unsigned)numSlots
58 return [[self alloc] initCount:numSlots];
63 return [self newCount:0];
68 return [self initCount:0];
80 while ((element = [self removeLastObject]))
85 - copyFromZone:(void *)z
87 List *new = [[[self class] alloc] initCount: numElements];
88 new->numElements = numElements;
89 bcopy ((const char*)dataPtr, (char*)new->dataPtr, DATASIZE(numElements));
93 - (BOOL) isEqual: anObject
96 if (! [anObject isKindOf: [self class]]) return NO;
97 other = (List *) anObject;
98 return (numElements == other->numElements)
99 && (bcmp ((const char*)dataPtr, (const char*)other->dataPtr, DATASIZE(numElements)) == 0);
112 - objectAt:(unsigned)index
114 if (index >= numElements)
116 return dataPtr[index];
119 - (unsigned)indexOf:anObject
121 register id *this = dataPtr;
122 register id *last = this + numElements;
123 while (this < last) {
124 if (*this == anObject)
125 return this - dataPtr;
128 return NX_NOT_IN_LIST;
135 return dataPtr[numElements - 1];
138 - setAvailableCapacity:(unsigned)numSlots
140 volatile id *tempDataPtr;
141 if (numSlots < numElements) return nil;
142 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(numSlots));
143 dataPtr = (id *)tempDataPtr;
144 maxElements = numSlots;
148 - insertObject:anObject at:(unsigned)index
150 register id *this, *last, *prev;
151 if (! anObject) return nil;
152 if (index > numElements)
154 if ((numElements + 1) > maxElements) {
155 volatile id *tempDataPtr;
156 /* we double the capacity, also a good size for malloc */
157 maxElements += maxElements + 1;
158 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(maxElements));
159 dataPtr = (id*)tempDataPtr;
161 this = dataPtr + numElements;
163 last = dataPtr + index;
173 return [self insertObject:anObject at:numElements];
178 - addObjectIfAbsent:anObject
180 register id *this, *last;
181 if (! anObject) return nil;
183 last = dataPtr + numElements;
184 while (this < last) {
185 if (*this == anObject)
189 return [self insertObject:anObject at:numElements];
194 - removeObjectAt:(unsigned)index
196 register id *this, *last, *next;
198 if (index >= numElements)
200 this = dataPtr + index;
201 last = dataPtr + numElements;
210 - removeObject:anObject
212 register id *this, *last;
214 last = dataPtr + numElements;
215 while (this < last) {
216 if (*this == anObject)
217 return [self removeObjectAt:this - dataPtr];
227 return [self removeObjectAt: numElements - 1];
236 - replaceObject:anObject with:newObject
238 register id *this, *last;
242 last = dataPtr + numElements;
243 while (this < last) {
244 if (*this == anObject) {
253 - replaceObjectAt:(unsigned)index with:newObject
259 if (index >= numElements)
261 this = dataPtr + index;
267 - makeObjectsPerform:(SEL)aSelector
269 unsigned count = numElements;
271 [dataPtr[count] perform: aSelector];
275 - makeObjectsPerform:(SEL)aSelector with:anObject
277 unsigned count = numElements;
279 [dataPtr[count] perform: aSelector with: anObject];
283 -appendList: (List *)otherList
287 for (i = 0, count = [otherList count]; i < count; i++)
288 [self addObject: [otherList objectAt: i]];