2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
26 Copyright 1988-1996 NeXT Software, Inc.
27 Written by: Bryan Yamamoto
28 Responsibility: Bertrand Serlet
37 #define DATASIZE(count) ((count) * sizeof(id))
47 - initCount:(unsigned)numSlots
49 maxElements = numSlots;
51 dataPtr = (id *)malloc(DATASIZE(maxElements));
55 + newCount:(unsigned)numSlots
57 return [[self alloc] initCount:numSlots];
62 return [self newCount:0];
67 return [self initCount:0];
79 while ((element = [self removeLastObject]))
84 - copyFromZone:(void *)z
86 List *new = [[[self class] alloc] initCount: numElements];
87 new->numElements = numElements;
88 bcopy ((const char*)dataPtr, (char*)new->dataPtr, DATASIZE(numElements));
92 - (BOOL) isEqual: anObject
95 if (! [anObject isKindOf: [self class]]) return NO;
96 other = (List *) anObject;
97 return (numElements == other->numElements)
98 && (bcmp ((const char*)dataPtr, (const char*)other->dataPtr, DATASIZE(numElements)) == 0);
111 - objectAt:(unsigned)index
113 if (index >= numElements)
115 return dataPtr[index];
118 - (unsigned)indexOf:anObject
120 register id *this = dataPtr;
121 register id *last = this + numElements;
122 while (this < last) {
123 if (*this == anObject)
124 return this - dataPtr;
127 return NX_NOT_IN_LIST;
134 return dataPtr[numElements - 1];
137 - setAvailableCapacity:(unsigned)numSlots
139 volatile id *tempDataPtr;
140 if (numSlots < numElements) return nil;
141 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(numSlots));
142 dataPtr = (id *)tempDataPtr;
143 maxElements = numSlots;
147 - insertObject:anObject at:(unsigned)index
149 register id *this, *last, *prev;
150 if (! anObject) return nil;
151 if (index > numElements)
153 if ((numElements + 1) > maxElements) {
154 volatile id *tempDataPtr;
155 /* we double the capacity, also a good size for malloc */
156 maxElements += maxElements + 1;
157 tempDataPtr = (id *) realloc (dataPtr, DATASIZE(maxElements));
158 dataPtr = (id*)tempDataPtr;
160 this = dataPtr + numElements;
162 last = dataPtr + index;
172 return [self insertObject:anObject at:numElements];
177 - addObjectIfAbsent:anObject
179 register id *this, *last;
180 if (! anObject) return nil;
182 last = dataPtr + numElements;
183 while (this < last) {
184 if (*this == anObject)
188 return [self insertObject:anObject at:numElements];
193 - removeObjectAt:(unsigned)index
195 register id *this, *last, *next;
197 if (index >= numElements)
199 this = dataPtr + index;
200 last = dataPtr + numElements;
209 - removeObject:anObject
211 register id *this, *last;
213 last = dataPtr + numElements;
214 while (this < last) {
215 if (*this == anObject)
216 return [self removeObjectAt:this - dataPtr];
226 return [self removeObjectAt: numElements - 1];
235 - replaceObject:anObject with:newObject
237 register id *this, *last;
241 last = dataPtr + numElements;
242 while (this < last) {
243 if (*this == anObject) {
252 - replaceObjectAt:(unsigned)index with:newObject
258 if (index >= numElements)
260 this = dataPtr + index;
266 - makeObjectsPerform:(SEL)aSelector
268 unsigned count = numElements;
270 [dataPtr[count] perform: aSelector];
274 - makeObjectsPerform:(SEL)aSelector with:anObject
276 unsigned count = numElements;
278 [dataPtr[count] perform: aSelector with: anObject];
282 -appendList: (List *)otherList
286 for (i = 0, count = [otherList count]; i < count; i++)
287 [self addObject: [otherList objectAt: i]];