/***************************************************************
 * PrLinkedListImpl.h
 *
 * This class provides a linked list interface.
 *
 * A generic list collection class that maintains a priority list 
 * of any object(increasing order). Should be used with a template.
 *
 ****************************************************************/

/*
 * modification history
 *----------------------
 *
 * 02/19/98: K.C. Chang: created.
 */

#ifndef _PrLinkedListImpl_h
#define _PrLinkedListImpl_h

#include "PrGlobalDefn.h"

#define PR_LINKEDLISTIMPL_SIZE_DEFAULT   10
#define PR_LINKEDLISTIMPL_SIZE_UNLIMITED 0

class PrBitmap;
class PrListEntry;

class PrLinkedListImpl
{
public:
    PrLinkedListImpl (int maxLength);

    virtual ~PrLinkedListImpl ();

    //	   int compare(void *x, void *y) 
    //		returns 1 if x > y
    //		returns 0 if x == y
    //		returns -1 if x < y
    virtual int compare(void *x, void *y) const { return (x==y); };

    // Add item to list
    void newItem (void *);

    // Remove and return item from list
    void *deleteItem (void *);

    // Return number of members in list   
    int items () const { return items_; }

    // Return item if a member of the list
    void *item (void *) const;

    // Return the item of the head of the list
    void *firstItem () const;

    // Return the next item in list
    void *nextItem () const;

    /*
     * iteration:
     *
     *   item = list.firstItem();
     *   while(item != NULL)
     *     {
     *        //do stuff with item
     *        item = list.nextItem();
     *     }
     */

private:
    int maxLength_;
    PrBitmap *bitmap_;
    PrListEntry *heap_;

    PrListEntry *sentinel_;
    PrListEntry *curEntry_;             // Current entry
    int items_;                       // Number of members

    inline PrListEntry *HeadEntry() const;     // Head of list
    inline PrListEntry *TailEntry() const;     // Tail of list
    inline PrListEntry *FindEntry(void *) const;

    PrLinkedListImpl (const PrLinkedListImpl &) {}
    PrLinkedListImpl &operator= (const PrLinkedListImpl &) { return *this; }
};

class PrListEntry
{
public:
    PrListEntry (void *m=NULL)
      : prevEntry_(0), nextEntry_(0), data_(m), id_(0) {}
    void      prevEntry (PrListEntry *e) { prevEntry_ = e; }
    PrListEntry *prevEntry () const  { return prevEntry_; }
    void      nextEntry (PrListEntry *e) { nextEntry_ = e; }
    PrListEntry *nextEntry () const  { return nextEntry_; }
    void      data (void *m) { data_ = m; }
    void      *data () const     { return data_; }
    void id(int n)      { id_ = n; }
    int  id() const     { return id_; }
private:
    PrListEntry *prevEntry_;
    PrListEntry *nextEntry_;
    void      *data_;
    int id_;

    PrListEntry (const PrListEntry &) {}
    PrListEntry &operator= (const PrListEntry &) { return *this; }
};


#endif // _PrLinkedListImpl_h







