File: List.m

package info (click to toggle)
libtclobjc 1.3-0
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 316 kB
  • ctags: 77
  • sloc: objc: 2,108; makefile: 237; ansic: 186; sh: 174; tcl: 13
file content (365 lines) | stat: -rw-r--r-- 7,505 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/* Implementation of Objective C NeXT-compatible List object
   Copyright (C) 1993 Free Software Foundation, Inc.
   
   Written by:  R. Andrew McCallum <mccallum@cs.rochester.edu>
   Dept. of Computer Science, U. of Rochester, Rochester, NY  14627
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.
   
   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.
   
   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free
   Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
   */ 

#include <coll/List.h>

/* Change this #define to 0 if you want -makeObjectsPerform: and 
   -makeObjectsPerform:with: to access content objects in order
   from lower indices to higher indices.
   If you want the NeXTSTEP behavior, leave the #define as 1. */
#define LIST_PERFORM_REVERSE_ORDER_DEFAULT 1

#define LIST_GROW_FACTOR 2

#if !defined(NX_MALLOC)
#define NX_MALLOC(VAR,TYPE,NUM ) \
  ((VAR) = (TYPE *) malloc((unsigned)(NUM)*sizeof(TYPE)))
#endif
#if !defined(NX_REALLOC)
#define NX_REALLOC(VAR,TYPE,NUM ) \
  ((VAR) = (TYPE *) realloc((VAR), (unsigned)(NUM)*sizeof(TYPE)))
#endif
#if !defined(NX_FREE)
#define NX_FREE(VAR) free(VAR)
#endif

/* memcpy() is a gcc builtin */


/* Do this before adding an element */
static inline void 
incrementCount(List *self)
{
  (self->numElements)++;
  if (self->numElements == self->maxElements)
    {
      [self setAvailableCapacity:(self->numElements) * LIST_GROW_FACTOR];
    }
}

/* Do this after removing an element */
static inline void
decrementCount(List *self)
{
  (self->numElements)--;
  if (self->numElements < (self->maxElements) / LIST_GROW_FACTOR)
    {
      [self setAvailableCapacity:(self->maxElements) / LIST_GROW_FACTOR];
    }
}

@implementation List
  
+ initialize
{
  if (self == [List class])
    [self setVersion:-1];	/* alpha release */
  return self;
}

// INITIALIZING, FREEING;

- initCount:(unsigned)numSlots;
{
  [super init];
  numElements = 0;
  maxElements = numSlots;
  NX_MALLOC(dataPtr, id, maxElements);
  return self;
}

- init
{
  return [self initCount:2];
}


- free
{
  if (dataPtr)
    NX_FREE(dataPtr);
  return [super free];
}

- freeObjects
{
  [self makeObjectsPerform:@selector(free)];
  [self empty];
  return self;
}

// COPYING;

- copy
{
  return [self shallowCopy];
}

- shallowCopy
{
  List *c = [super shallowCopy];
  NX_MALLOC(c->dataPtr, id, maxElements);
  memcpy(c->dataPtr, dataPtr, numElements * sizeof(id *));
  return c;
}

- deepen
{
  int i;
  
  for (i = 0; i < numElements; i++) 
    {
      dataPtr[i] = [dataPtr[i] deepCopy];
    }
  return self;
}

// COMPARING TWO LISTS;

- (BOOL)isEqual: anObject
{
  int i;
  
  if ( (![anObject isKindOf:[List class]])
      || ([self count] != [anObject count]))
    return NO;
  for (i = 0; i < numElements; i++)
    /* NeXT documentation says to compare id's. */
    if ( dataPtr[i] != [anObject objectAt:i] )
      return NO;
  return YES;
}

// MANAGING THE STORAGE CAPACITY;

- (unsigned)capacity
{
  return maxElements;
}

- setAvailableCapacity:(unsigned)numSlots
{
  if (numSlots > numElements) 
    {
      maxElements = numSlots;
      NX_REALLOC(dataPtr, id, maxElements);
      return self;
    }
  return nil;
}


/* Manipulating objects by index */

#define CHECK_INDEX(IND)  if ((IND) >= numElements) return nil 
#define CHECK_OBJECT(OBJ)  if (!(OBJ)) return nil
  
- (unsigned)count
{
  return numElements;
}

- objectAt:(unsigned)index
{
  CHECK_INDEX(index);
  return dataPtr[index];
}

- lastObject
{
  if (numElements)
    return dataPtr[numElements-1];
  else
    return nil;
}

- addObject:anObject
{
  [self insertObject:anObject at:numElements];
  return self;
}

- insertObject:anObject at:(unsigned)index
{
  int i;
  
  if (index > 0) {
    CHECK_INDEX(index-1);
  }
  CHECK_OBJECT(anObject);
  incrementCount(self);
  for (i = numElements-1; i > index; i--)
    dataPtr[i] = dataPtr[i-1];
  dataPtr[i] = anObject;
  return self;
}

- removeObjectAt:(unsigned)index
{
  id oldObject;
  int i;
  
  CHECK_INDEX(index);
  oldObject = dataPtr[index];
  for (i = index; i < numElements-1; i++)
    dataPtr[i] = dataPtr[i+1];
  decrementCount(self);
  return oldObject;
}

- removeLastObject
{
  if (numElements) 
    return [self removeObjectAt:numElements-1];
  return nil;
}

- replaceObjectAt:(unsigned)index with:newObject
{
  id oldObject;

  CHECK_INDEX(index);
  CHECK_OBJECT(newObject);
  oldObject = dataPtr[index];
  dataPtr[index] = newObject;
  return oldObject;
}

/* Inefficient to send objectAt: each time, but it handles subclasses 
   of List nicely. */
- appendList: (List *)otherList
{
  int i, c;
  
  c = [otherList count];
  /* Should we do something like this for efficiency?
     [self setCapacity:numElements+c]; */
  for (i = 0; i < c; i++)
    [self addObject:[otherList objectAt:i]];
  return self;
}

/* Manipulating objects by id */

- (unsigned) indexOf:anObject
{
  int i;
  
  for (i = 0; i < numElements; i++)
    if ([dataPtr[i] isEqual:anObject])
      return i;
  return GNU_NOT_IN_LIST;
}

- addObjectIfAbsent:anObject
{
  CHECK_OBJECT(anObject);
  if ([self indexOf:anObject] == GNU_NOT_IN_LIST)
    [self addObject:anObject];
  return self;
}

- removeObject:anObject
{
  CHECK_OBJECT(anObject);
  return [self removeObjectAt:[self indexOf:anObject]];
}

- replaceObject:anObject with:newObject
{
  return [self replaceObjectAt:[self indexOf:anObject]
	       with:newObject];
}

/* Emptying the list */

- empty
{
  int i;

  for (i = 0; i < numElements; i++)
    dataPtr[i] = nil;
  numElements = 0;
  return self;
}

/* Archiving */

- write: (TypedStream*)aStream
{
  [super write: aStream];
  objc_write_types (aStream, "II", &numElements, &maxElements);
  objc_write_array (aStream, "@", numElements, dataPtr);
  return self;
}

- read: (TypedStream*)aStream
{
  [super read: aStream];
  objc_read_types (aStream, "II", &numElements, &maxElements);
  NX_MALLOC(dataPtr, id, maxElements);
  objc_read_array (aStream, "@", numElements, dataPtr);
  return self;
}

/* Sending messages to elements of the list */

- makeObjectsPerform:(SEL)aSel
{
  int i;

  /* For better interaction with List subclasses, we could use
     objectAt: instead of accessing dataPtr directly. */
#if (LIST_PERFORM_REVERSE_ORDER_DEFAULT)
  for (i = numElements-1; i >= 0; i--)
    [dataPtr[i] perform:aSel];
#else
  for (i = 0; i < numElements; i++)
    [dataPtr[i] perform:aSel];
#endif /* LIST_PERFORM_REVERSE_ORDER_DEFAULT */
  return self;
}

- makeObjectsPerform:(SEL)aSel with:anObject
{
  int i;

  /* For better interaction with List subclasses, we could use
     objectAt: instead of accessing dataPtr directly. */
#if (LIST_PERFORM_REVERSE_ORDER_DEFAULT)
  for (i = numElements-1; i >= 0; i--)
    [dataPtr[i] perform:aSel with:anObject];
#else
  for (i = 0; i < numElements; i++)
    [dataPtr[i] perform:aSel with:anObject];
#endif /* LIST_PERFORM_REVERSE_ORDER_DEFAULT */
  return self;
}


/* Old-style creation */

+ newCount:(unsigned)numSlots
{
  return [[self alloc] initCount:numSlots];
}

@end