2 * Copyright (c) 1999-2001, 2005-2006 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
25 Copyright 1988-1996 NeXT Software, Inc.
26 Written by: Bryan Yamamoto
27 Responsibility: Bertrand Serlet
39 #define DATASIZE(count) ((count) * sizeof(id))
49 - initCount:(unsigned)numSlots
51 maxElements = numSlots;
53 dataPtr = (id *)malloc(DATASIZE(maxElements));
57 + newCount:(unsigned)numSlots
59 return [[self alloc] initCount:numSlots];
64 return [self newCount:0];
69 return [self initCount:0];
81 while ((element = [self removeLastObject]))
86 - copyFromZone:(void *)z
88 List *new = [[[self class] alloc] initCount: numElements];
89 new->numElements = numElements;
90 bcopy ((const char*)dataPtr, (char*)new->dataPtr, DATASIZE(numElements));
94 - (BOOL) isEqual: anObject
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);
113 - objectAt:(unsigned)index
115 if (index >= numElements)
117 return dataPtr[index];
120 - (unsigned)indexOf:anObject
122 register id *this = dataPtr;
123 register id *last = this + numElements;
124 while (this < last) {
125 if (*this == anObject)
126 return this - dataPtr;
129 return NX_NOT_IN_LIST;
136 return dataPtr[numElements - 1];
139 - setAvailableCapacity:(unsigned)numSlots
141 volatile id *tempDataPtr;
142 if (numSlots < numElements) return nil;
143 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(numSlots));
144 dataPtr = (id *)tempDataPtr;
145 maxElements = numSlots;
149 - insertObject:anObject at:(unsigned)index
151 register id *this, *last, *prev;
152 if (! anObject) return nil;
153 if (index > numElements)
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;
162 this = dataPtr + numElements;
164 last = dataPtr + index;
174 return [self insertObject:anObject at:numElements];
179 - addObjectIfAbsent:anObject
181 register id *this, *last;
182 if (! anObject) return nil;
184 last = dataPtr + numElements;
185 while (this < last) {
186 if (*this == anObject)
190 return [self insertObject:anObject at:numElements];
195 - removeObjectAt:(unsigned)index
197 register id *this, *last, *next;
199 if (index >= numElements)
201 this = dataPtr + index;
202 last = dataPtr + numElements;
211 - removeObject:anObject
213 register id *this, *last;
215 last = dataPtr + numElements;
216 while (this < last) {
217 if (*this == anObject)
218 return [self removeObjectAt:this - dataPtr];
228 return [self removeObjectAt: numElements - 1];
237 - replaceObject:anObject with:newObject
239 register id *this, *last;
243 last = dataPtr + numElements;
244 while (this < last) {
245 if (*this == anObject) {
254 - replaceObjectAt:(unsigned)index with:newObject
260 if (index >= numElements)
262 this = dataPtr + index;
268 - makeObjectsPerform:(SEL)aSelector
270 unsigned count = numElements;
272 [dataPtr[count] perform: aSelector];
276 - makeObjectsPerform:(SEL)aSelector with:anObject
278 unsigned count = numElements;
280 [dataPtr[count] perform: aSelector with: anObject];
284 -appendList: (List *)otherList
288 for (i = 0, count = [otherList count]; i < count; i++)
289 [self addObject: [otherList objectAt: i]];