Main Page | Alphabetical List | Class List | File List | Class Members | File Members

list.cc

Go to the documentation of this file.
00001 // list.cc 00002 // 00003 // Routines to manage a singly-linked list of "things". 00004 // 00005 // A "ListElement" is allocated for each item to be put on the 00006 // list; it is de-allocated when the item is removed. This means 00007 // we don't need to keep a "next" pointer in every object we 00008 // want to put on a list. 00009 // 00010 // NOTE: Mutual exclusion must be provided by the caller. 00011 // If you want a synchronized list, you must use the routines 00012 // in synchlist.cc. 00013 // 00014 // Copyright (c) 1992-1993 The Regents of the University of California. 00015 // All rights reserved. See copyright.h for copyright notice and limitation 00016 // of liability and disclaimer of warranty provisions. 00017 00018 #include "copyright.h" 00019 #include "list.h" 00020 00021 //---------------------------------------------------------------------- 00022 // ListElement::ListElement 00023 // Initialize a list element, so it can be added somewhere on a list. 00024 // 00025 // "itemPtr" is the item to be put on the list. It can be a pointer 00026 // to anything. 00027 // "sortKey" is the priority of the item, if any. 00028 //---------------------------------------------------------------------- 00029 00030 ListElement::ListElement(void *itemPtr, int sortKey) 00031 { 00032 item = itemPtr; 00033 key = sortKey; 00034 next = NULL; // assume we'll put it at the end of the list 00035 } 00036 00037 //---------------------------------------------------------------------- 00038 // List::List 00039 // Initialize a list, empty to start with. 00040 // Elements can now be added to the list. 00041 //---------------------------------------------------------------------- 00042 00043 List::List() 00044 { 00045 first = last = NULL; 00046 } 00047 00048 //---------------------------------------------------------------------- 00049 // List::~List 00050 // Prepare a list for deallocation. If the list still contains any 00051 // ListElements, de-allocate them. However, note that we do *not* 00052 // de-allocate the "items" on the list -- this module allocates 00053 // and de-allocates the ListElements to keep track of each item, 00054 // but a given item may be on multiple lists, so we can't 00055 // de-allocate them here. 00056 //---------------------------------------------------------------------- 00057 00058 List::~List() 00059 { 00060 while (Remove() != NULL) 00061 ; // delete all the list elements 00062 } 00063 00064 //---------------------------------------------------------------------- 00065 // List::Append 00066 // Append an "item" to the end of the list. 00067 // 00068 // Allocate a ListElement to keep track of the item. 00069 // If the list is empty, then this will be the only element. 00070 // Otherwise, put it at the end. 00071 // 00072 // "item" is the thing to put on the list, it can be a pointer to 00073 // anything. 00074 //---------------------------------------------------------------------- 00075 00076 void List::Append(void *item) 00077 { 00078 ListElement *element = new ListElement(item, 0); 00079 00080 if (IsEmpty()) 00081 { 00082 // list is empty 00083 first = element; 00084 last = element; 00085 } 00086 else 00087 { // else put it after last 00088 last->next = element; 00089 last = element; 00090 } 00091 } 00092 00093 //---------------------------------------------------------------------- 00094 // List::Prepend 00095 // Put an "item" on the front of the list. 00096 // 00097 // Allocate a ListElement to keep track of the item. 00098 // If the list is empty, then this will be the only element. 00099 // Otherwise, put it at the beginning. 00100 // 00101 // "item" is the thing to put on the list, it can be a pointer to 00102 // anything. 00103 //---------------------------------------------------------------------- 00104 00105 void List::Prepend(void *item) 00106 { 00107 ListElement *element = new ListElement(item, 0); 00108 00109 if (IsEmpty()) 00110 { // list is empty 00111 first = element; 00112 last = element; 00113 } 00114 else 00115 { // else put it before first 00116 element->next = first; 00117 first = element; 00118 } 00119 } 00120 00121 //---------------------------------------------------------------------- 00122 // List::Remove 00123 // Remove the first "item" from the front of the list. 00124 // 00125 // Returns: 00126 // Pointer to removed item, NULL if nothing on the list. 00127 //---------------------------------------------------------------------- 00128 00129 void* List::Remove() 00130 { 00131 return SortedRemove(NULL); // Same as SortedRemove, but ignore the key 00132 } 00133 00134 //---------------------------------------------------------------------- 00135 // List::Mapcar 00136 // Apply a function to each item on the list, by walking through 00137 // the list, one element at a time. 00138 // 00139 // Unlike LISP, this mapcar does not return anything! 00140 // 00141 // "func" is the procedure to apply to each element of the list. 00142 //---------------------------------------------------------------------- 00143 00144 void List::Mapcar(VoidFunctionPtr func) 00145 { 00146 for (ListElement *ptr = first; ptr != NULL; ptr = ptr->next) 00147 { 00148 DEBUG('l', "In mapcar, about to invoke %x(%x)\n", func, ptr->item); 00149 (*func)((int)ptr->item); 00150 } 00151 } 00152 00153 00154 //---------------------------------------------------------------------- 00155 // List::IsEmpty 00156 // Returns TRUE if the list is empty (has no items). 00157 //---------------------------------------------------------------------- 00158 00159 bool List::IsEmpty() 00160 { 00161 if (first == NULL) 00162 return TRUE; 00163 else 00164 return FALSE; 00165 } 00166 00167 //---------------------------------------------------------------------- 00168 // List::SortedInsert 00169 // Insert an "item" into a list, so that the list elements are 00170 // sorted in increasing order by "sortKey". 00171 // 00172 // Allocate a ListElement to keep track of the item. 00173 // If the list is empty, then this will be the only element. 00174 // Otherwise, walk through the list, one element at a time, 00175 // to find where the new item should be placed. 00176 // 00177 // "item" is the thing to put on the list, it can be a pointer to 00178 // anything. 00179 // "sortKey" is the priority of the item. 00180 //---------------------------------------------------------------------- 00181 00182 void List::SortedInsert(void *item, int sortKey) 00183 { 00184 ListElement *element = new ListElement(item, sortKey); 00185 ListElement *ptr;// keep track 00186 00187 if (IsEmpty()) 00188 { // if list is empty, put 00189 first = element; 00190 last = element; 00191 } 00192 else if (sortKey < first->key) 00193 { 00194 // item goes on front of list 00195 element->next = first; 00196 first = element; 00197 } 00198 else 00199 { // look for first elt in list bigger than item 00200 for (ptr = first; ptr->next != NULL; ptr = ptr->next) 00201 { 00202 if (sortKey < ptr->next->key) 00203 { 00204 element->next = ptr->next; 00205 ptr->next = element; 00206 return; 00207 } 00208 } 00209 last->next = element;// item goes at end of list 00210 last = element; 00211 } 00212 } 00213 00214 //---------------------------------------------------------------------- 00215 // List::SortedRemove 00216 // Remove the first "item" from the front of a sorted list. 00217 // 00218 // Returns: 00219 // Pointer to removed item, NULL if nothing on the list. 00220 // Sets *keyPtr to the priority value of the removed item 00221 // (this is needed by interrupt.cc, for instance). 00222 // 00223 // "keyPtr" is a pointer to the location in which to store the 00224 // priority of the removed item. 00225 //---------------------------------------------------------------------- 00226 00227 void* List::SortedRemove(int *keyPtr) 00228 { 00229 ListElement *element = first; 00230 void *thing; 00231 00232 if (IsEmpty()) 00233 return NULL; 00234 00235 thing = first->item; 00236 if (first == last) 00237 { // list had one item, now has none 00238 first = NULL; 00239 last = NULL; 00240 } 00241 else 00242 { 00243 first = element->next; 00244 } 00245 if (keyPtr != NULL) 00246 *keyPtr = element->key; 00247 delete element; 00248 return thing; 00249 } 00250 00251 ListElement* List::GetFirst() 00252 { 00253 return first; 00254 } 00255 00256 DeltaList::DeltaList() 00257 { 00258 m_first = 0; 00259 } 00260 00261 DeltaList::~DeltaList() 00262 { 00263 } 00264 00265 void DeltaList::Insert(void *item, int key) 00266 { 00267 00268 } 00269 00270 void* DeltaList::Remove(void *item) 00271 { 00272 return 0; 00273 } 00274 00275 // remove the first element if its key is zero; otherwise return NULL 00276 void* DeltaList::RemoveZero() 00277 { 00278 return 0; 00279 } 00280 00281 // decrement the first non-zero element 00282 void DeltaList::Decrement() 00283 { 00284 00285 } 00286 00287 void DeltaList::ShowList() 00288 { 00289 Elem *elem; 00290 00291 printf("Showing Delta List contents\n"); 00292 for(elem = m_first; elem != 0; elem = elem->next) 00293 { 00294 printf("%d ", elem->key); 00295 } 00296 printf("\n"); 00297 }

Generated on Thu Sep 16 12:33:45 2004 for NachOS by doxygen 1.3.8