list.h 2.72 KB
//====================================================================
// list.h
//
// Copyright 1994, Silicon Graphics, Inc.
// All Rights Reserved.
//
// Author(s)
//  Kevin Smith
//
// This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
// the contents of this file may not be disclosed to third parties, copied or
// duplicated in any form, in whole or in part, without the prior written
// permission of Silicon Graphics, Inc.
//
// RESTRICTED RIGHTS LEGEND:
// Use, duplication or disclosure by the Government is subject to restrictions
// as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
// and Computer Software clause at DFARS 252.227-7013, and/or in similar or
// successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
// rights reserved under the Copyright Laws of the United States.
//====================================================================

#pragma once

//
// A real simple list class.  It does next to nothing, but does 
// encapsulate a linked list well.
//


//
// A list iterator.  Used to iterate over any list that inherits from 
// List.  The actual methods for iterating are in the List class.
//
class ListNode {
private:
    ListNode 	*next;
    void 	*thing;

    friend	List;
};

typedef ListNode **ListIter;

class List {
  private:
    ListNode *first;
    ListNode *last;
    int count;
  public:
    List (void) { first = last = 0; count = 0; }
    virtual ~List ();

    // Get the length of the list
    int GetCount (void) const
            { return count; }

    // List iteration support routines.
    void PointToFirst (ListIter &iter) const
            { iter = (ListNode **) &first; }
    void PointToLast (ListIter &iter) const
            { iter = (ListNode **) &last; }
    void PointToNext (ListIter &iter) const
            { if ((*iter) != 0) iter = &((*iter)->next); }

    // This routine should be heavily overloaded by routines that
    // inherit from this class
    void *GetIteratorValue (ListIter iter) const
            { return ((*iter) != 0) ? (*iter)->thing : 0; }

    // Append a new entry to the list at specified location
    // If "where" is 0, the new entry will go at the beginning
    // of the list.  This function needs to be overloaded for lists
    // of a specific type (instead of "void *").
    void AppendData (ListIter where, void *data);

    // Remove a list entry from the list.  The list iterator is invalid
    // after this operation.
    void RemoveData (ListIter where);

    // Basic list searching
    // Find a key.  If found, return value is 1, otherwise 0.
    int FindByKey (ListIter &iter, void *key);

    // Key comparison.  Return one if key matches, zero otherwise.
    virtual int KeyCompare (void *key, void *element);
};