/*
 *  Copyright (C)  2000-2001 Marc Wandschneider <mw@kiltdown.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation.
 *
 *  For more information look at the file COPYRIGHT in this package
 *
 */
#ifndef _LLIST_H_

#ifndef _SANITY_H_
#error did you forget to include sanity.h at the beginning of your file?
#endif


/**
 * This is a C++ template for a list class that we'll use for holding various
 * things.  This will be a list based version, in which addition is very fast
 * (to the front of the list), but removals are O(n).
 *
 * Please note that in the case of refcounted objects, this class does NOT 
 * add a reference to it.  It merely holds on it to it, and assumes the 
 * lifetime is valid for it's entire lifetime.
 *
 * Please also note that no efforts are made to filter or check for 
 * duplicates.
 *
 * This class is not thread-safe.
 */


/**
 * This is a helper class for the linked list.
 */
template <class Type>
class LLNode {
  public:
    /**
     * The item to store.
     */
    Type item;

    /**
     * Pointer to the next item in the list.
     */
    LLNode *next;
};


/**
 * This is the actual linked list class.
 */
template <class Type>
class LinkList {

  public:

    LinkList();
    LinkList(LinkList &);
    ~LinkList();
    void operator=(LinkList &);

    /**
     * Adds the given item to the linked list.
     */
    ERRCODE addElement(Type);

    /**
     * Counts the number of elements in the list.
     */
    int countElements();

    /**
     * gets the element at the given index.
     */
    ERRCODE elementAt(int, Type *);

    /**
     * empties out the list.  Note that any elements themselves are not
     * touched.
     */
    void emptyList();

    /**
     * returns the first element in the list and resets the iterator.
     */
    ERRCODE firstElement(Type *);

    /**
     * Returns the next element in the iterator list, or S_EOF if there
     * are no more.
     */
    ERRCODE nextElement(Type *);

    /**
     * Removes the given item from the linked list.
     */
    ERRCODE removeElement(Type);

    /**
     * indicates whether the list is empty or not.
     */
    bool isEmpty() { return front == NULL; }

  private:
    LLNode<Type> *front, *end;
    LLNode<Type> *last;            // used for iteration.

    /**
     * Private method to copy over the list.
     */
    void copyList(LinkList &source);
};


/**
 * This is a template class, so we have to include the code here too.  It 
 * would be a very good idea to use -fno-implicit-templates to help save
 * on code size
 */
//#include <llist.cc>



#define _LLIST_H_
#endif // _LLIST_H_




syntax highlighted by Code2HTML, v. 0.9.1