GList.h 1.72 KB
//
// GList.h
//
// This class defines a list container class that
// can hold items of any type.  Items can be appended to
// the end of the list or inserted into a specific location
// in the list.  Items can be removed from the list and
// the ordered of the remaining items is preserved.
//
// Note that the Remove message finds and removes the
// item with the given value.  So if the list contains
// items of the same value, it will remove the first
// occurence of the item in the list.  (This is not
// a problem for allocated pointer types, but it could
// be for other types.)  If this is a problem, you may
// need to add a RemoveByIndex message.
//

#ifndef __GList__
#define __GList__

#include "AeTypes.h"

#define kDefAllocItems	32


#ifdef NDEBUG
#define invariant()
#endif


typedef enum { kForward = 0, kReverse } TListDir;


template <class T> class GList
{
public:

                        GList (void);
                        GList (int numAllocItems);
    virtual		~GList (void);

    Boolean		Append (const T&);
    Boolean		Insert (const T&, int beforeIndex);
    void		Remove (const T&);
    Boolean	       	Find (const T&, int &);

    int			GetNum (void) { return fNumItems; }
    T&			operator[] (int);

    // iteration methods
    void		Start (int& i, TListDir dir = kForward);
    Boolean		Next (int&, T&);
    Boolean		Prev (int&, T&);
    

private:
    Boolean    		grow (void);
    Boolean		shrink (void);

    int			fNumItems;
    int			fNumAvailItems;
    int			fNumAllocItems;
    T *			fList;
    
    Boolean		fDidInsertFlag;		// debugging only
    Boolean		fDidRemoveFlag;		// debugging only

#ifndef NDEBUG
public:
    static Boolean	Test (void);

private:
    void		invariant (void);
#endif

};



#endif __GList__