File: raplist.cc

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (430 lines) | stat: -rw-r--r-- 12,605 bytes parent folder | download | duplicates (8)
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
/* -*-	Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
/*
 * Copyright (c) 1993 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *	This product includes software developed by the Computer Systems
 *	Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Author:
 *   Mohit Talwar (mohit@catarina.usc.edu)
 *
 * $Header: /cvsroot/nsnam/ns-2/rap/raplist.cc,v 1.4 2005/09/18 23:33:34 tomh Exp $
 * 
 * This is taken from UCB Nachos project
 * 
 * list.cc 
 *
 *      Routines to manage a singly-linked list of "things".
 *
 *      A "ListElement" is allocated for each item to be put on the
 *      list; it is de-allocated when the item is removed. This means
 *      we don't need to keep a "next" pointer in every object we
 *      want to put on a list.
 */

#include "utilities.h"
#include "raplist.h"

//----------------------------------------------------------------------
// ListElement::ListElement
// 	Initialize a List element, so it can be added somewhere on a List.
//
//	"itemPtr" is the item to be put on the List.  It can be a pointer
//		to anything.
//	"sortKey" is the priority of the item, if any.
//----------------------------------------------------------------------

ListElement::ListElement(void *itemPtr, float sortKey)
{
  item = itemPtr;
  key = sortKey;
  next = NULL;	// assume we'll put it at the end of the List 
}

//----------------------------------------------------------------------
// List::List
//	Initialize a List, empty to start with.
//	Elements can now be added to the List.
//----------------------------------------------------------------------

List::List()
{
  size  = 0;
  first = last = NULL; 
}

//----------------------------------------------------------------------
// List::~List
//	Prepare a List for deallocation.  If the List still contains any 
//	ListElements, de-allocate them.  However, note that we do *not*
//	de-allocate the "items" on the List -- this module allocates
//	and de-allocates the ListElements to keep track of each item,
//	but a given item may be on multiple Lists, so we can't
//	de-allocate them here.
//----------------------------------------------------------------------

List::~List()
{ 
  while (Remove() != NULL)
    ;	 // delete all the List elements
}

//----------------------------------------------------------------------
// List::Append
//      Append an "item" to the end of the List.
//      
//	Allocate a ListElement to keep track of the item.
//      If the List is empty, then this will be the only element.
//	Otherwise, put it at the end.
//
//	"item" is the thing to put on the List, it can be a pointer to 
//		anything.
//----------------------------------------------------------------------

void List::Append(void *item)
{
  ListElement *element = new ListElement(item, 0);
  if (IsEmpty()) 
    {				// List is empty
      first = element;
      last = element;
    } 
  else 
    {				// else put it after last
      last->next = element;
      last = element;
    }
  
  size++;
}

//----------------------------------------------------------------------
// List::Prepend
//      Put an "item" on the front of the List.
//      
//	Allocate a ListElement to keep track of the item.
//      If the List is empty, then this will be the only element.
//	Otherwise, put it at the beginning.
//
//	"item" is the thing to put on the List, it can be a pointer to 
//		anything.
//----------------------------------------------------------------------

void List::Prepend(void *item)
{
  ListElement *element = new ListElement(item, 0);
  
  if (IsEmpty()) 
    {				// List is empty
      first = element;
      last = element;
    } 
  else 
    {				// else put it before first
      element->next = first;
      first = element;
    }

  size++;
}

//----------------------------------------------------------------------
// List::Remove
//      Remove the first "item" from the front of the List.
// 
// Returns:
//	Pointer to removed item, NULL if nothing on the List.
//----------------------------------------------------------------------

void *List::Remove()
{
  return SortedRemove(NULL);  // Same as SortedRemove, but ignore the key
}

//----------------------------------------------------------------------
// List::Mapcar
//	Apply a function to each item on the List, by walking through  
//	the List, one element at a time.
//
//	Unlike LISP, this mapcar does not return anything!
//
//	"func" is the procedure to apply to each element of the List.
//----------------------------------------------------------------------

void List::Mapcar(VoidFunctionPtr func)
{
  for (ListElement *ptr = first; ptr != NULL; ptr = ptr->next)
    (*func)((long)ptr->item);
}

//----------------------------------------------------------------------
// List::IsEmpty
//      Returns TRUE if the List is empty (has no items).
//----------------------------------------------------------------------

int List::IsEmpty() 
{
  if (first == NULL)
    return TRUE;
  else
    return FALSE;
}

//----------------------------------------------------------------------
// List::SortedInsert
//      Insert an "item" into a List, so that the List elements are
//	sorted in increasing order by "sortKey".
//      
//	Allocate a ListElement to keep track of the item.
//      If the List is empty, then this will be the only element.
//	Otherwise, walk through the List, one element at a time,
//	to find where the new item should be placed.
//
//	"item" is the thing to put on the List, it can be a pointer to 
//		anything.
//	"sortKey" is the priority of the item.
//----------------------------------------------------------------------

void List::SortedInsert(void *item, float sortKey)
{
  ListElement *element = new ListElement(item, sortKey);
  ListElement *ptr;	        // keep track
  
  if (IsEmpty()) 
    {				// List is empty, put in front
      first = element;
      last = element;
    } 
  else if (sortKey < first->key) 
    {				// item goes on front of List	
      element->next = first;
      first = element;
    } 
  else 
    {				// look for first elt in List bigger than item
      for (ptr = first; ptr->next != NULL; ptr = ptr->next) {
	if (sortKey < ptr->next->key) 
	  {
	    element->next = ptr->next;
	    ptr->next = element;
	    return;
	  }	
      }
      last->next = element;	// item goes at end of List
      last = element;
    }
  
  size++;
}

//----------------------------------------------------------------------
// List::SortedRemove
//      Remove the first "item" from the front of a sorted List.
// 
// Returns:
//	Pointer to removed item, NULL if nothing on the List.
//	Sets *keyPtr to the priority value of the removed item
//	(this is needed by interrupt.cc, for instance).
//
//	"keyPtr" is a pointer to the location in which to store the 
//		priority of the removed item.
//----------------------------------------------------------------------

void *List::SortedRemove(float *keyPtr)
{
  ListElement *element = first;
  void *thing;
  
  if (IsEmpty()) 
    return NULL;
  
  thing = first->item;
  if (first == last) 
    {				// List had one item, now has none 
      first = NULL;
      last = NULL;
    } 
  else 
    first = element->next;

  if (keyPtr != NULL)
    *keyPtr = element->key;
  delete element;
  
  size--;
  return thing;
}

//----------------------------------------------------------------------
// List::MinKey
//      Return the key of the item in the front of a sorted List.
// 
// Returns :
//	-1 if List is empty
//----------------------------------------------------------------------

float List::MinKey()
{
  if (IsEmpty())
    return -1;
  else
    return first->key;
}

//----------------------------------------------------------------------
// List::SetInsert
//      Insert "key" into a List, so that the List elements are unique.
//
// Returns :
//      TRUE if "key" inserted, FALSE o/w
//
// Caveat :
//      Does not allocate space for key! Provided by calling function.
//      If FALSE, calling function should delete allocated space.
//
//	"key" is the thing to put on the List.
//      "eq" is the comparison function used to compare two items.
//----------------------------------------------------------------------

int List::SetInsert(void *key, CompareFunction eq)
{
  if (IsPresent(key, eq))
    return FALSE;

  Prepend(key);
  return TRUE;
}

//----------------------------------------------------------------------
// List::SetRemove
//      Remove "key" from List.
// 
// Returns :
//      handle to "key" in list if removed, NULL o/w
//
// Caveat :
//      if not NULL, Calling function should delete allocated space.
//
//	"key" is the item to be removed.
//      "eq" is the comparison function used to compare two items.
//----------------------------------------------------------------------

void *List::SetRemove(void *key, CompareFunction eq)
{
  ListElement *prev, *curr;
  void *thing;

  if (!IsPresent(key, eq))
    return NULL;
 
  for (prev = NULL, curr = first; 
       curr != NULL; 
       prev = curr, curr = curr->next)
    if ((*eq)(key, curr->item))
      break;

  assert(curr != NULL);		// Since its present we'd better find it

  if (curr == first)
    first = curr->next;
  else
    prev->next = curr->next;
  
  if (curr == last)
    last = prev;

  thing = curr->item;
  delete curr;

  size--;
  return thing;
}

//----------------------------------------------------------------------
// List::IsPresent
//      Is "key" there in the Set
//
// Returns :
//      handle to the "key" in list (NULL if not found)
//      
//	"key" is the item to be searched.
//      "eq" is the comparison function used to compare two items.
//----------------------------------------------------------------------

void *List::IsPresent(void *key, CompareFunction eq)
{
  ListElement *ptr;

  for (ptr = first; ptr != NULL; ptr = ptr->next) // Check all items
    if ((*eq)(key, ptr->item))
      return ptr->item;

  return NULL;
}

//----------------------------------------------------------------------
// List::Purge
//      Remove all elements with (key eq "key") from the Set
//
//      "key" is the item to be searched and deleted.
//      "eq" is the comparison function used to compare two items.
//      "destroy" is the function used to delete the purged "key".
//----------------------------------------------------------------------

void List::Purge(void *key, CompareFunction eq, VoidFunctionPtr destroy)
{
  ListElement *prev, *curr, *temp;
  void *thing;

  for (prev = NULL, curr = first; curr != NULL; ) // Check all items
    if ((*eq)(key, curr->item))
      {
        if (curr == first)
          first = curr->next;
        else
          prev->next = curr->next;

	if (curr == last)
          last = prev;

        thing = curr->item;
	temp = curr;

	curr = curr->next;

        (* destroy)((long) thing);
        delete temp;
        size--;
      }
  else
    {
      prev = curr;
      curr = curr->next;
    }
}