forked from electronicarts/CnC_Tiberian_Dawn
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLINK.CPP
416 lines (374 loc) · 25.1 KB
/
LINK.CPP
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
** Command & Conquer(tm)
** Copyright 2025 Electronic Arts Inc.
**
** This program 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 3 of the License, or
** (at your option) any later version.
**
** This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/* $Header: F:\projects\c&c\vcs\code\link.cpv 2.18 16 Oct 1995 16:52:18 JOE_BOSTIC $ */
/***********************************************************************************************
*** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
***********************************************************************************************
* *
* Project Name : Command & Conquer *
* *
* File Name : LINK.CPP *
* *
* Programmer : Joe L. Bostic *
* *
* Start Date : 01/15/95 *
* *
* Last Update : January 19, 1995 [JLB] *
* *
*---------------------------------------------------------------------------------------------*
* Functions: *
* LinkClass::LinkClass -- Default constructor for linked list object. *
* LinkClass::LinkClass -- Copy constructor for linked list object. *
* LinkClass::~LinkClass -- Default destructor for linked list object. *
* LinkClass::Zap -- Forces the link pointers to NULL. *
* LinkClass::operator= -- Assignment operator for linked list class object. *
* LinkClass::Get_Next -- Fetches the next object in list. *
* LinkClass::Get_Prev -- Fetches previous object in linked list. *
* LinkClass::Head_Of_List -- Finds the head of the list. *
* LinkClass::Tail_Of_List -- Scans for the object at the end of the list. *
* LinkClass::Add -- This object adds itself to the given list *
* LinkClass::Add_Head -- This gadget makes itself the head of the given list. *
* LinkClass::Add_Tail -- Add myself to the end of the given list. *
* LinkClass::Remove -- Removes the specified object from the list. *
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
#include "function.h"
#include "link.h"
/***********************************************************************************************
* LinkClass::LinkClass -- Default constructor for linked list object. *
* *
* This is the default constructor for a linked list object. It merely initializes the *
* next and previous pointers to NULL. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass::LinkClass(void)
{
LinkClass::Zap();
}
/***********************************************************************************************
* LinkClass::LinkClass -- Copy constructor for linked list object. *
* *
* This copy constructor, unlike the assigment operator, doesn't have to deal with an *
* already initialized and legal link object to the left of the "=". It merely puts the *
* destination object into the same list as the source object. *
* *
* INPUT: link -- Reference to the object on the right of the "=". *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/16/1995 JLB : Created. *
*=============================================================================================*/
LinkClass::LinkClass(LinkClass & link)
{
LinkClass::Zap();
Add(link);
}
/***********************************************************************************************
* LinkClass::~LinkClass -- Default destructor for linked list object. *
* *
* This default destructor will remove the object from any linked list it may be part of. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass::~LinkClass(void)
{
Remove();
}
/***********************************************************************************************
* LinkClass::Zap -- Forces the link pointers to NULL. *
* *
* This routine will "zap" out the link pointers. This is usually necessary when the link *
* pointers start in an undefined state, but we KNOW that they aren't pointing to anything *
* valid. In such a case it becomes necessary to zap them so that when the object is added *
* to a list, it will be added corrrectly. *
* *
* INPUT: none *
* *
* OUTPUT: none *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/19/1995 JLB : Created. *
*=============================================================================================*/
void LinkClass::Zap(void)
{
Next = 0;
Prev = 0;
}
/***********************************************************************************************
* LinkClass::operator= -- Assignment operator for linked list class object. *
* *
* The assignment operator makes sure that the previous and next pointers remain valid. *
* Because this class only consists of pointers, the assignment operator doesn't actually *
* transfer any data from the source object. It merely makes the destination object part *
* of the same list as the source object. In essence, this is transferring information *
* but not the actual values. *
* *
* If the destination object is already part of another list, it is removed from that list *
* before being added to the source object's list. This ensures that either list remains *
* in a valid condition. *
* *
* INPUT: link -- The object to the right of the "=" operator. *
* *
* OUTPUT: Returns a reference to the rightmost object -- per standard assigment rules. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/16/1995 JLB : Created. *
*=============================================================================================*/
LinkClass & LinkClass::operator=(LinkClass & link)
{
Remove();
Add(link);
return(link);
}
/***********************************************************************************************
* LinkClass::Get_Next -- Fetches the next object in list. *
* *
* This routine will return with a pointer to the next object in the list. If there are *
* no more objects, then NULL is returned. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with pointer to next object in list or NULL if at end of list. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass * LinkClass::Get_Next(void) const
{
return(Next);
}
/***********************************************************************************************
* LinkClass::Get_Prev -- Fetches previous object in linked list. *
* *
* Use this routine to get a pointer to the previous object in the linked list. If there *
* are no previous objects (such as at the head of the list), then NULL is returned. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a pointer to the previous object in the list or NULL if none. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass * LinkClass::Get_Prev(void) const
{
return(Prev);
}
/***********************************************************************************************
* LinkClass::Head_Of_List -- Finds the head of the list. *
* *
* Use this routine to scan for and return a reference to the object at the head of the *
* list. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a reference to the object at the head of the list. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/19/1995 JLB : Created. *
*=============================================================================================*/
LinkClass const & LinkClass::Head_Of_List(void) const
{
LinkClass const * link = this;
while (link->Prev) {
link = link->Prev;
if (link == this) break; // Safety check
}
return(*link);
}
/***********************************************************************************************
* LinkClass::Tail_Of_List -- Scans for the object at the end of the list. *
* *
* Use this routine to scan for and return a reference to the object at the end of the *
* list. *
* *
* INPUT: none *
* *
* OUTPUT: Returns with a reference to the object at the end of the list. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/19/1995 JLB : Created. *
*=============================================================================================*/
LinkClass const & LinkClass::Tail_Of_List(void) const
{
LinkClass const * link = this;
while (link->Next) {
link = link->Next;
if (link == this) break; // Safety check
}
return(*link);
}
/***********************************************************************************************
* LinkClass::Add -- This object adds itself to the given list *
* *
* Use this routine to add a link object to the list, but to be added right after the *
* given link. This allows inserting a link in the middle of the chain. A quite necessary *
* ability if the chain is order dependant (e.g., the gadget system). *
* *
* INPUT: list -- gadget object to add this one to *
* *
* OUTPUT: Returns with a pointer to the head of the list. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/19/1995 JLB : Created. *
*=============================================================================================*/
LinkClass & LinkClass::Add(LinkClass & list)
{
LinkClass *ptr;
/*
** Save ptr to next gadget.
*/
ptr = list.Next;
/*
** Link myself in after 'list'.
*/
list.Next = this;
Prev = &list;
/*
** Link myself to next gadget, if there is one.
*/
Next = ptr;
if (ptr) {
ptr->Prev = this;
}
return(Head_Of_List());
}
/***********************************************************************************************
* LinkClass::Add_Head -- This gadget makes itself the head of the given list. *
* *
* INPUT: list -- the list to make myself the head of *
* *
* OUTPUT: Returns with a reference to the object at the head of the list. This should be *
* the same object that is passed in. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/19/1995 JLB : Created. *
*=============================================================================================*/
LinkClass & LinkClass::Add_Head(LinkClass & list)
{
LinkClass *ptr;
/*
** Get head of given list.
*/
ptr = &list.Head_Of_List();
/*
** Link myself in front of it.
*/
ptr->Prev = this;
Next = ptr;
Prev = NULL;
return(*this);
}
/***********************************************************************************************
* LinkClass::Add_Tail -- Add myself to the end of the given list. *
* *
* INPUT: list -- list to add myself to *
* *
* OUTPUT: the head of the list *
* *
* WARNINGS: The previous and next pointers for the added object MUST have been properly *
* initialized for this routine to work correctly. *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass & LinkClass::Add_Tail(LinkClass & list)
{
LinkClass *ptr;
/*
** Get head of given list.
*/
ptr = &list.Tail_Of_List();
/*
** Link myself in front of it.
*/
ptr->Next = this;
Prev = ptr;
Next = NULL;
return(Head_Of_List());
}
/***********************************************************************************************
* LinkClass::Remove -- Removes the specified object from the list. *
* *
* This routine will remove the specified object from the list of objects. Because of the *
* previous and next pointers, it is possible to remove an object from the list without *
* knowing the head of the list. To do this, just call Remove() with the parameter of *
* "this". *
* *
* INPUT: none *
* *
* OUTPUT: Returns with the new head of list. *
* *
* WARNINGS: none *
* *
* HISTORY: *
* 01/15/1995 JLB : Created. *
*=============================================================================================*/
LinkClass * LinkClass::Remove(void)
{
LinkClass * head = &Head_Of_List();
LinkClass * tail = &Tail_Of_List();
if (Prev) {
Prev->Next = Next;
}
if (Next) {
Next->Prev = Prev;
}
Prev = 0;
Next = 0;
if (head==this) {
if (tail==this) {
return(0);
}
return(&tail->Head_Of_List());
}
return(head);
}