File: MKLinkedList.m

package info (click to toggle)
popplerkit.framework 0.0.20051227svn-16
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,632 kB
  • sloc: objc: 2,303; cpp: 672; sh: 475; ansic: 55; makefile: 29
file content (367 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download | duplicates (9)
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
366
367
/*
 * Copyright (C) 2005  Stefan Kleine Stegemann
 *
 * The sources from MissigKit are not released under any particular
 * license. Do with them what ever you want to do. Include them in
 * your projects, use MissingKit standalone or simply ignore it.
 * Whatever you do, keep in mind the this piece of software may
 * have errors and that it is distributed WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.
 */

#import "MKLinkedList.h"

/**
 * Non-Public methods of MKLinkedList.
 */
@interface MKLinkedList (Private)
- (void) _setFirst: (MKLinkedListElement*)anElement;
- (void) _setLast: (MKLinkedListElement*)anElement;
@end

/**
 * Non-Public methods of MKLinkedListElement.
 */
@interface MKLinkedListElement (Private)
- (id) _initWithObject: (id)anObject list: (MKLinkedList*)aList;
- (void) _setList: (MKLinkedList*)aList;
- (void) _setNext: (MKLinkedListElement*)anElement;
- (void) _setPrevious: (MKLinkedListElement*)anElement;
@end


@implementation MKLinkedList

- (id) init
{
   self = [super init];
   if (self)
   {
      first = nil;
      last  = nil;
      size  = 0;
   }
   return self;
}

- (void) dealloc
{
   // remove remaining elements
   while ([self first])
   {
      [self remove: [self first]];
   }
   NSAssert(![self last], @"still elements in the list");
   [super dealloc];
}

- (MKLinkedListElement*) addObject: (id)anObject
{
   MKLinkedListElement* theElement;
   
   theElement = [[MKLinkedListElement alloc] _initWithObject: anObject list: self];
   [[self last] _setNext: theElement];
   [theElement _setPrevious: [self last]];
   [theElement _setNext: nil];
   [self _setLast: theElement];

   size++;
   
   // empty list?
   if (!first)
   {
      [self _setFirst: theElement];
   }

   return theElement;
}

- (MKLinkedListElement*) insertObject: (id)anObject
                               before: (MKLinkedListElement*)anElement
{
   MKLinkedListElement* theElement;
   
   if ([anElement list] != self)
   {
      [NSException raise: NSInvalidArgumentException
                  format: @"element does not belong to this list"];
   }
   
   theElement = [[MKLinkedListElement alloc] _initWithObject: anObject list: self];

   [theElement _setNext: anElement];
   [theElement _setPrevious: [anElement previous]];
   [[anElement previous] _setNext: theElement];
   [anElement _setPrevious: theElement];
   
   if (anElement == [self first])
   {
      [self _setFirst: theElement];
   }
   
   size++;

   return theElement;
}

- (MKLinkedListElement*) insertObject: (id)anObject
                 beforeElementAtIndex: (unsigned)anIndex
{
   return [self insertObject: anObject before: [self elementAtIndex: anIndex]];
}

- (void) remove: (MKLinkedListElement*)anElement
{
   if ([anElement list] != self)
   {
      [NSException raise: NSInvalidArgumentException
                  format: @"element does not belong to this list"];
   }

   size--;
   
   if (anElement == [self first])
   {
      [self _setFirst: [anElement next]];
   }
   
   if (anElement == [self last])
   {
      [self _setLast: [anElement previous]];
   }
   
   [[anElement previous] _setNext: [anElement next]];
   [[anElement next] _setPrevious: [anElement previous]];
   [anElement _setList: nil];
   [anElement release];
}

- (unsigned) count
{
   return size;
}

- (MKLinkedListElement*) first
{
   return first;
}

- (id) firstObject
{
   return [[self first] object];
}

- (MKLinkedListElement*) last
{
   return last;
}

- (id) lastObject
{
   return [[self last] object];
}

- (MKLinkedListElement*) elementAtIndex: (unsigned)anIndex
{
   unsigned middle;
   MKLinkedListElement* theElement = nil;
   
   if (anIndex >= [self count])
   {
      [NSException raise: NSRangeException
                  format: @"index %d is out of range", anIndex];
   }
   
   // start from end or from beginning?
   middle = (unsigned)([self count] / 2);
   if (anIndex < middle)
   {
      int i;
      theElement = [self first];
      for (i = 0; i < anIndex; i++)
      {
         theElement = [theElement next];
      }
   }
   else
   {
      int i;
      theElement = [self last];
      for (i = 0; i < ([self count] - (anIndex + 1)); i++)
      {
         theElement = [theElement previous];
      }
   }
   
   return theElement;
}

- (id) objectAtIndex: (int)anIndex
{
   return [[self elementAtIndex: anIndex] object];
}

- (void) makeLast: (MKLinkedListElement*) anElement
{
   if ([anElement list] != self)
   {
      [NSException raise: NSInvalidArgumentException
                  format: @"element does not belong to this list"];
   }

   if (anElement == [self last])
   {
      return;
   }
   
   if (anElement == [self first])
   {
      [self _setFirst: [anElement next]];
   }

   [[anElement previous] _setNext: [anElement next]];
   [[anElement next] _setPrevious: [anElement previous]];
   [[self last] _setNext: anElement];
   [anElement _setPrevious: [self last]];
   [anElement _setNext: nil];
   [self _setLast: anElement];
}

- (void) makeFirst: (MKLinkedListElement*) anElement
{
   if ([anElement list] != self)
   {
      [NSException raise: NSInvalidArgumentException
                  format: @"element does not belong to this list"];
   }

   if (anElement == [self first])
   {
      return;
   }
   
   if (anElement == [self last])
   {
      [self _setLast: [anElement previous]];
   }
   
   [[anElement previous] _setNext: [anElement next]];
   [[anElement next] _setPrevious: [anElement previous]];
   [[self first] _setPrevious: anElement];
   [anElement _setPrevious: nil];
   [anElement _setNext: [self first]];
   [self _setFirst: anElement];
}

@end


/* ----------------------------------------------------- */
/*  Category Private                                     */
/* ----------------------------------------------------- */

@implementation MKLinkedList (Private)

- (void) _setFirst: (MKLinkedListElement*)anElement
{
   first = anElement;
}

- (void) _setLast: (MKLinkedListElement*)anElement
{
   last = anElement;
}

@end


/* ----------------------------------------------------- */
/*  Class MKLinkedListElement                            */
/* ----------------------------------------------------- */

@implementation MKLinkedListElement

- (id) init
{
   self = [super init];
   if (self)
   {
      object   = nil;
      list     = nil;
      previous = nil;
      next     = nil;
   }
   return self;
}

- (void) dealloc
{
   [self setObject: nil];
   [super dealloc];
}

- (id) object
{
   return object;
}

- (void) setObject: (id)anObject
{
   if (anObject == object)
      return;
   [object release];
   object = [anObject retain];
}

- (MKLinkedListElement*) previous
{
   return previous;
}

- (MKLinkedListElement*) next
{
   return next;
}

- (MKLinkedList*) list
{
   return list;
}

@end


/* ----------------------------------------------------- */
/*  Category Private of MKLinkedListElement              */
/* ----------------------------------------------------- */

@implementation MKLinkedListElement (Private)

- (id) _initWithObject: (id)anObject list: (MKLinkedList*)aList
{
   self = [self init];
   if (self)
   {
      object = [anObject retain];
      [self _setList: aList];
   }
   return self;
}

- (void) _setList: (MKLinkedList*)aList
{
   list = aList;
}

- (void) _setNext: (MKLinkedListElement*)anElement
{
   next = anElement;
}

- (void) _setPrevious: (MKLinkedListElement*)anElement
{
   previous = anElement;
}

@end