Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

list.h

Go to the documentation of this file.
00001 // list.h 
00002 //      Data structures to manage LISP-like lists.  
00003 //
00004 //      As in LISP, a list can contain any type of data structure
00005 //      as an item on the list: thread control blocks, 
00006 //      pending interrupts, etc.  Allocation and deallocation of the
00007 //      items on the list are to be done by the caller.
00008 //
00009 // Copyright (c) 1992-1996 The Regents of the University of California.
00010 // All rights reserved.  See copyright.h for copyright notice and limitation 
00011 // of liability and disclaimer of warranty provisions.
00012 
00013 #ifndef LIST_H
00014 #define LIST_H
00015 
00016 #include "copyright.h"
00017 #include "debug.h"
00018 
00026 
00027 template <class T>
00028 class ListElement {
00029   public:
00030     ListElement(T itm);         
00031     ListElement *next;          
00032     T item;                     
00033 };
00034 
00040 
00041 template <class T>
00042 class List {
00043   public:
00044     List();                     
00045     virtual ~List();            
00046 
00047     virtual void Prepend(T item);
00048     virtual void Append(T item); 
00049 
00050     T Front() { return first->item; }
00053     T RemoveFront();            
00054     void Remove(T item);        
00055 
00056     bool IsInList(T item) const;
00057 
00058     unsigned int NumInList() { return numInList;};
00060     bool IsEmpty() { return (numInList == 0); };
00062 
00063     void Apply(void (*f)(T)) const; 
00065 
00066     virtual void SanityCheck() const;   
00068     void SelfTest(T *p, int numEntries);
00070 
00071   protected:
00072     ListElement<T> *first;      
00073     ListElement<T> *last;       
00074     int numInList;              
00075 
00076 friend ListIterator<T>;
00077 };
00078 
00088 
00089 template <class T>
00090 class SortedList : public List<T> {
00091   public:
00092     SortedList(int (*comp)(T x, T y)) : List<T>() { compare = comp;};
00093     ~SortedList() {};           
00094 
00095     void Insert(T item);        
00096 
00097     void SanityCheck() const;   
00098     void SelfTest(T *p, int numEntries);
00100 
00101   private:
00102     int (*compare)(T x, T y);   
00103 
00104     void Prepend(T item) { Insert(item); }  
00105 
00106     void Append(T item) { Insert(item); }   
00107 
00108 };
00109 
00117 
00118 template <class T>
00119 class ListIterator {
00120   public:
00121     ListIterator(List<T> *list) { current = list->first; } 
00123 
00124     bool IsDone() { return current == NULL; };
00126 
00127     T Item() { ASSERT(!IsDone()); return current->item; };
00129 
00130     void Next() { current = current->next; };           
00132 
00133   private:
00134     ListElement<T> *current;    
00135 };
00136 
00137 #include "list.cc"              
00138 
00139 
00140 #endif // LIST_H

Generated at Wed Jul 4 11:32:22 2001 for Nachos by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001