-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIDList.cpp
More file actions
323 lines (273 loc) · 5.29 KB
/
Copy pathIDList.cpp
File metadata and controls
323 lines (273 loc) · 5.29 KB
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
/*
This file is part of W3ZMapEditor (c).
W3ZMapEditor is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
W3ZMapEditor 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with W3ZMapEditor; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
// IDList.cpp: implementation of the CIDList class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "War3TypesAndConstants.h"
#include "IDList.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CIDList::CIDList()
{
m_pHead = NULL;
}
CIDList::~CIDList()
{
ClearList();
}
/*
AddID
*****
Add a >= 0 ID to the IDs list. If the ID is < 0, it'll be ignored.
In:
int ID: new ID
Out:
none
Return:
none
*/
void CIDList::AddID(int ID)
{
PCListedInt pLint, pLintPrev;
if (ID < 0)
{// ignore negative IDs
TRACE("AddID: negative ID not added: %i\n", ID);
return;
}
TRACE("AddID: trying to add ID %i\n", ID);
pLint = m_pHead;
pLintPrev = NULL;
// sorted list, so find the right place to put the ID
while (pLint && (pLint->i < ID))
{
pLintPrev = pLint;
if (pLint == pLint->next)
{// in case of a cyclic list bug
TRACE("AddID: cyclic list bug detected!\n");
pLint->next = pLint = NULL;
}
else
{// next one!
pLint = pLint->next;
}
}
if (pLintPrev)
{// not the head
if (pLint)
{// there is one after, check if the ID is already in the list
if (pLint->i != ID)
{ // ID not already here
pLint = new CListedInt;
pLint->i = ID;
pLint->next = pLintPrev->next;
pLintPrev->next = pLint;
}
}
else
{// last one
pLint = new CListedInt;
pLint->i = ID;
pLint->next = NULL;
pLintPrev->next = pLint;
}
}
else
{//first of the list
pLint = new CListedInt;
pLint->i = ID;
pLint->next = m_pHead;
m_pHead = pLint;
}
}
/*
GetAndRemoveID
**************
Returns the next ID and remove that ID from the list.
In:
none
Out:
none
Return:
int: a >= 0 ID number or -1 if the list is empty.
*/
int CIDList::GetAndRemoveID()
{
int i;
PCListedInt pLint;
if (m_pHead)
{
i = m_pHead->i;
pLint = m_pHead;
m_pHead = m_pHead->next;
delete pLint;
}
else
{
i = -1;
}
return i;
}
/*
ClearList
*********
Empty the ID list and free memory.
In:
none
Out:
none
Return:
none
*/
void CIDList::ClearList()
{
PCListedInt pLint;
//remove each element of the list
while(m_pHead)
{
pLint = m_pHead;
if (m_pHead != m_pHead->next)
{
m_pHead = m_pHead->next;
}
else
{
TRACE("ClearList: cyclic list bug detected!\n");
m_pHead = NULL;
}
// free memory
delete pLint;
}
}
/*
RemoveID
********
Remove an ID from the list.
In:
int ID: ID to remove
Out:
none
Remove:
none
*/
void CIDList::RemoveID(int ID)
{
PCListedInt pLint, pLintPrev;
pLint = m_pHead;
pLintPrev = NULL;
// search the ID
while (pLint && (pLint->i != ID))
{
pLintPrev = pLint;
if (pLint == pLint->next)
{// in case of a cyclic list bug
TRACE("RemoveID: cyclic list bug detected!\n");
pLint->next = pLint = NULL;
}
else
{// next one!
pLint = pLint->next;
}
}
if (pLint)
{// ID found
//check if it's the head
if (pLintPrev)
{// not the head
pLintPrev->next = pLint->next;
delete pLint;
}
else
{// is the head
pLint = m_pHead;
m_pHead = m_pHead->next;
delete pLint;
}
}
}
/*
AddIDRange
**********
Add >= 0 IDs to the IDs list. If one of the IDs is < 0, it'll be ignored.
If last <= first, nothing will be added.
In:
int first: first ID of the range
int last: last ID of the range
Out:
none
Return:
none
*/
void CIDList::AddIDRange(int first, int last)
{
PCListedInt pLint, pLintPrev;
int ifrom, ito;
ifrom = first;
ito = last;
if (last < 0)
{// ignore negative IDs
TRACE("AddIDRange: negative IDs not added!\n");
return;
}
if (ifrom < 0)
{// add only >= 0 IDs
ifrom = 0;
}
if (ifrom > ito)
{// ignore
return;
}
pLint = m_pHead;
pLintPrev = NULL;
if ((!m_pHead) || (ifrom < m_pHead->i))
{// must replace the head
pLint = new CListedInt;
pLint->i = ifrom;
pLint->next = m_pHead;
pLintPrev = m_pHead = pLint;
ifrom++;
}
// add the range
while (ifrom <= ito)
{
if (pLint && (pLint->i < ifrom))
{// ifrom is bigger, go to next
pLintPrev = pLint;
pLint = pLint->next;
}
else if (pLint && (pLint->i == ifrom))
{// skip this ID which is already there
pLintPrev = pLint;
pLint = pLint->next;
ifrom++;
}
else
{// (pLint == NULL) or (pLint->i > ifrom) so add the ID
pLint = new CListedInt;
pLint->i = ifrom;
pLint->next = pLintPrev->next;
pLintPrev->next = pLint;
pLintPrev = pLint;
pLint = pLint->next;
ifrom++;
}
}
}