forked from robbiehanson/XMPPFramework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DDList.m
252 lines (200 loc) · 4.44 KB
/
DDList.m
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
#import "DDList.h"
@interface DDListEnumerator (PrivateAPI)
- (id)initWithList:(DDListNode *)list reverse:(BOOL)reverse;
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation DDList
- (id)init
{
if ((self = [super init]))
{
list = NULL;
}
return self;
}
- (void)add:(void *)element
{
if(element == NULL) return;
DDListNode *node = malloc(sizeof(DDListNode));
node->element = element;
// Remember: The list is a linked list of DDListNode objects.
// Each node object is allocated and placed in the list.
// It is not deallocated until it is later removed from the linked list.
if (list == NULL)
{
node->prev = NULL;
node->next = NULL;
}
else
{
node->prev = NULL;
node->next = list;
node->next->prev = node;
}
list = node;
}
- (void)remove:(void *)element allInstances:(BOOL)allInstances
{
if(element == NULL) return;
DDListNode *node = list;
while (node != NULL)
{
if (element == node->element)
{
// Remove the node from the list.
// This is done by editing the pointers of the node's neighbors to skip it.
//
// In other words:
// node->prev->next = node->next
// node->next->prev = node->prev
//
// We also want to properly update our list pointer,
// which always points to the "first" element in the list. (Most recently added.)
if (node->prev != NULL)
node->prev->next = node->next;
else
list = node->next;
if (node->next != NULL)
node->next->prev = node->prev;
free(node);
if (!allInstances) break;
}
else
{
node = node->next;
}
}
}
- (void)remove:(void *)element
{
[self remove:element allInstances:NO];
}
- (void)removeAll:(void *)element
{
[self remove:element allInstances:YES];
}
- (void)removeAllElements
{
DDListNode *node = list;
while (node != NULL)
{
DDListNode *next = node->next;
free(node);
node = next;
}
list = NULL;
}
- (NSUInteger)count
{
NSUInteger count = 0;
DDListNode *node;
for (node = list; node != NULL; node = node->next)
{
count++;
}
return count;
}
- (DDListEnumerator *)listEnumerator
{
return [[[DDListEnumerator alloc] initWithList:list reverse:NO] autorelease];
}
- (DDListEnumerator *)reverseListEnumerator
{
return [[[DDListEnumerator alloc] initWithList:list reverse:YES] autorelease];
}
- (void)dealloc
{
[self removeAllElements];
[super dealloc];
}
@end
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@implementation DDListEnumerator
- (id)initWithList:(DDListNode *)aList reverse:(BOOL)reverse
{
if ((self = [super init]))
{
numElements = 0;
currentElementIndex = 0;
// First get a count of the number of elements in the given list.
// Also, get a reference to the last node in the list.
DDListNode *node = aList;
if (node != NULL)
{
numElements++;
while (node->next != NULL)
{
numElements++;
node = node->next;
}
}
// At this point:
//
// aList -> points to the first element in the list
// node -> points to the last element in the list
// Recall that new elements are added to the beginning of the linked list.
// The last element in the list is the first element that was added.
if (numElements > 0)
{
elements = malloc(numElements * sizeof(void *));
if (reverse)
{
NSUInteger i = 0;
while (aList != NULL)
{
elements[i] = aList->element;
i++;
aList = aList->next;
}
}
else
{
NSUInteger i = 0;
while (node != NULL)
{
elements[i] = node->element;
i++;
node = node->prev;
}
}
}
}
return self;
}
- (NSUInteger)numTotal
{
return numElements;
}
- (NSUInteger)numLeft
{
if (currentElementIndex < numElements)
return numElements - currentElementIndex;
else
return 0;
}
- (void *)nextElement
{
if (currentElementIndex < numElements)
{
void *element = elements[currentElementIndex];
currentElementIndex++;
return element;
}
else
{
return NULL;
}
}
- (void)dealloc
{
if (elements)
{
free(elements);
}
[super dealloc];
}
@end