// list.h // Data structures to manage LISP-like lists. // // As in LISP, a list can contain any type of data structure // as an item on the list: thread control blocks, // pending interrupts, etc. Allocation and deallocation of the // items on the list are to be done by the caller. // // Copyright (c) 1992-1996 The Regents of the University of California. // All rights reserved. See copyright.h for copyright notice and limitation // of liability and disclaimer of warranty provisions. #ifndef QUEUE_H #define QUEUE_H // The following class defines a "list element" -- which is // used to keep track of one item on a list. It is equivalent to a // LISP cell, with a "car" ("next") pointing to the next element on the list, // and a "cdr" ("item") pointing to the item on the list. // // This class is private to this module (and classes that inherit // from this module). Made public for notational convenience. class PGNode; class QueueElement { public: QueueElement(PGNode *itm); // initialize a list element QueueElement *next; // next element on list, NULL if this is last PGNode *item; // item on the list }; // The following class defines a "list" -- a singly linked list of // list elements, each of which points to a single item on the list. // The class has been tested only for primitive types (ints, pointers); // no guarantees it will work in general. For instance, all types // to be inserted into a list must have a "==" operator defined. class Queue { public: Queue(); // initialize the list virtual ~Queue(); // de-allocate the list virtual void Prepend(PGNode * item);// Put item at the beginning of the list virtual void Append(PGNode * item); // Put item at the end of the list PGNode * Front() { return first->item; } // Return first item on list // without removing it PGNode * RemoveFront(); // Take item off the front of the list void Remove(PGNode * item); // Remove specific item from list bool IsInQueue(PGNode * item) const;// is the item in the list? unsigned int NumInQueue() { return numInList;}; // how many items in the list? bool IsEmpty() { return (numInList == 0); }; // is the list empty? void Apply(void (*f)(PGNode *)) const; // apply function to all elements in list virtual void SanityCheck() const; // has this list been corrupted? protected: QueueElement *first; // Head of the list, NULL if list is empty QueueElement *last; // Last element of list int numInList; // number of elements in list }; // The following class defines a "sorted list" -- a singly linked list of // list elements, arranged so that "Remove" always returns the smallest // element. // All types to be inserted onto a sorted list must have a "Compare" // function defined: // int Compare(T x, T y) // returns -1 if x < y // returns 0 if x == y // returns 1 if x > y class SortedList : public Queue { public: SortedList(int (*comp)(PGNode * x, PGNode * y)) : Queue() { compare = comp;}; SortedList() {}; // base class destructor called automatically void Insert(PGNode * item); // insert an item onto the list in sorted order void SanityCheck() const; // has this list been corrupted? private: int (*compare)(PGNode * x, PGNode * y); // function for sorting list elements void Prepend(PGNode * item) { Insert(item); } // *pre*pending has no meaning // in a sorted list void Append(PGNode * item) { Insert(item); } // neither does *ap*pend }; #ifndef ASSERT #define ASSERT (assert) #endif #endif // QUEUE_H