-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathList.cc
232 lines (180 loc) · 3.83 KB
/
List.cc
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
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include "List.h"
#include "util.h"
static const int DEFAULT_CHUNK_SIZE = 10;
BaseList::BaseList(int size)
{
chunk_size = DEFAULT_CHUNK_SIZE;
if ( size < 0 )
{
num_entries = max_entries = 0;
entry = 0;
}
else
{
if ( size > 0 )
chunk_size = size;
num_entries = 0;
entry = (ent *) safe_malloc(chunk_size * sizeof(ent));
max_entries = chunk_size;
}
}
BaseList::BaseList(BaseList& b)
{
max_entries = b.max_entries;
chunk_size = b.chunk_size;
num_entries = b.num_entries;
if ( max_entries )
entry = (ent *) safe_malloc(max_entries * sizeof(ent));
else
entry = 0;
for ( int i = 0; i < num_entries; ++i )
entry[i] = b.entry[i];
}
void BaseList::sort(list_cmp_func cmp_func)
{
qsort(entry, num_entries, sizeof(ent), cmp_func);
}
void BaseList::operator=(BaseList& b)
{
if ( this == &b )
return; // i.e., this already equals itself
if ( entry )
free(entry);
max_entries = b.max_entries;
chunk_size = b.chunk_size;
num_entries = b.num_entries;
if ( max_entries )
entry = (ent *) safe_malloc(max_entries * sizeof(ent));
else
entry = 0;
for ( int i = 0; i < num_entries; ++i )
entry[i] = b.entry[i];
}
void BaseList::insert(ent a)
{
if ( num_entries == max_entries )
{
resize(max_entries + chunk_size); // make more room
chunk_size *= 2;
}
for ( int i = num_entries; i > 0; --i )
entry[i] = entry[i-1]; // move all pointers up one
++num_entries;
entry[0] = a;
}
#include <stdio.h>
void BaseList::sortedinsert(ent a, list_cmp_func cmp_func)
{
// We optimize for the case that the new element is
// larger than most of the current entries.
// First append element.
if ( num_entries == max_entries )
{
resize(max_entries + chunk_size);
chunk_size *= 2;
}
entry[num_entries++] = a;
// Then move it to the correct place.
ent tmp;
for ( int i = num_entries - 1; i > 0; --i )
{
if ( cmp_func(entry[i],entry[i-1]) <= 0 )
break;
tmp = entry[i];
entry[i] = entry[i-1];
entry[i-1] = tmp;
}
}
ent BaseList::remove(ent a)
{
int i;
for ( i = 0; i < num_entries && a != entry[i]; ++i )
;
return remove_nth(i);
}
ent BaseList::remove_nth(int n)
{
if ( n < 0 || n >= num_entries )
return 0;
ent old_ent = entry[n];
--num_entries;
for ( ; n < num_entries; ++n )
entry[n] = entry[n+1];
entry[n] = 0; // for debugging
return old_ent;
}
void BaseList::append(ent a)
{
if ( num_entries == max_entries )
{
resize(max_entries + chunk_size); // make more room
chunk_size *= 2;
}
entry[num_entries++] = a;
}
// Get and remove from the end of the list.
ent BaseList::get()
{
if ( num_entries == 0 )
return 0;
return entry[--num_entries];
}
void BaseList::clear()
{
if ( entry )
{
free(entry);
entry = 0;
}
num_entries = max_entries = 0;
chunk_size = DEFAULT_CHUNK_SIZE;
}
ent BaseList::replace(int ent_index, ent new_ent)
{
if ( ent_index < 0 )
return 0;
ent old_ent;
if ( ent_index > num_entries - 1 )
{ // replacement beyond the end of the list
resize(ent_index + 1);
for ( int i = num_entries; i < max_entries; ++i )
entry[i] = 0;
num_entries = max_entries;
old_ent = 0;
}
else
old_ent = entry[ent_index];
entry[ent_index] = new_ent;
return old_ent;
}
int BaseList::resize(int new_size)
{
if ( new_size < num_entries )
new_size = num_entries; // do not lose any entries
if ( new_size != max_entries )
{
entry = (ent*) safe_realloc((void*) entry, sizeof(ent) * new_size);
if ( entry )
max_entries = new_size;
else
max_entries = 0;
}
return max_entries;
}
ent BaseList::is_member(ent e) const
{
int i;
for ( i = 0; i < length() && e != entry[i]; ++i )
;
return (i == length()) ? 0 : e;
}
int BaseList::member_pos(ent e) const
{
int i;
for ( i = 0; i < length() && e != entry[i]; ++i )
;
return (i == length()) ? -1 : i;
}