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
36 #include <objc/List.h>
38 #define DATASIZE(count) ((count) * sizeof(id))
48 - (id)initCount:(unsigned)numSlots
50 maxElements = numSlots;
52 dataPtr = (id *)malloc(DATASIZE(maxElements));
56 + (id)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 - (id)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 - (id)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 - (id)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 - (id)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;
171 - (id)addObject:anObject
173 return [self insertObject:anObject at:numElements];
178 - (id)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 - (id)removeObjectAt:(unsigned)index
196 register id *this, *last, *next;
198 if (index >= numElements)
200 this = dataPtr + index;
201 last = dataPtr + numElements;
210 - (id)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];
223 - (id)removeLastObject
227 return [self removeObjectAt: numElements - 1];
236 - (id)replaceObject:anObject with:newObject
238 register id *this, *last;
242 last = dataPtr + numElements;
243 while (this < last) {
244 if (*this == anObject) {
253 - (id)replaceObjectAt:(unsigned)index with:newObject
259 if (index >= numElements)
261 this = dataPtr + index;
267 - (id)makeObjectsPerform:(SEL)aSelector
269 unsigned count = numElements;
271 [dataPtr[count] perform: aSelector];
275 - (id)makeObjectsPerform:(SEL)aSelector with:anObject
277 unsigned count = numElements;
279 [dataPtr[count] perform: aSelector with: anObject];
283 -(id)appendList: (List *)otherList
287 for (i = 0, count = [otherList count]; i < count; i++)
288 [self addObject: [otherList objectAt: i]];