/* This file is part of cardwords
   (c) 1998 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_myslist.hh

/* This file defines a very simple singly linked list, as I have problems with
   the one provided by the stl. This is either a replacement for the stl-slist
   or a debugging tool for my CardWords_HashTable code.
*/

#ifndef CARDWORDS_MYSLIST_HH
#define CARDWORDS_MYSLIST_HH

template <class data_type>
struct CardWords_MySlistNode
{
public:
  CardWords_MySlistNode<data_type> * next_node;

  data_type data;

  template<class data_type_initialisator>
  CardWords_MySlistNode (data_type_initialisator d) :
    next_node(0), data(d) {}

  template<class data_type_initialisator>
  CardWords_MySlistNode (data_type_initialisator d,
                       CardWords_MySlistNode<data_type> * next_node_ptr)
    :
    next_node(next_node_ptr), data(d) {}

  CardWords_MySlistNode () : next_node(0){}
};

template <class data_type>
struct CardWords_MySlistIterator {
  CardWords_MySlistNode<data_type> * pointer;

  CardWords_MySlistIterator () : pointer (0){}
  CardWords_MySlistIterator (CardWords_MySlistNode<data_type> * p)
    : pointer(p) {}
  CardWords_MySlistIterator (CardWords_MySlistNode<data_type> & p)
    : pointer(&p) {}
  
  data_type & operator*()const {return pointer->data;};

  CardWords_MySlistIterator<data_type> & operator ++() {
    pointer = pointer->next_node;
    return (*this);
  }
  CardWords_MySlistIterator<data_type> operator ++(int) {
    CardWords_MySlistIterator<data_type> tmp = *this;
    ++(*this);
    return tmp;
  }
  bool operator != (const CardWords_MySlistIterator<data_type>& other) {
    return pointer != other.pointer;
  }
  bool operator == (const CardWords_MySlistIterator<data_type>& other) {
    return pointer == other.pointer;
  }
};

template <class data_type>
class CardWords_MySlist {
private:
  size_t list_size;

  typedef CardWords_MySlistNode<data_type> list_node;

  list_node head;
public:
  typedef CardWords_MySlistIterator<data_type> iterator;
  
  CardWords_MySlist() : list_size(0) {};

  size_t size(void) const {return list_size;};

  iterator end(void) const {return (list_node *) 0;}

  iterator begin(void) const {return head.next_node;}

  template <class data_type_initialisator>
  void push_front(const data_type_initialisator & d) {
    list_node * new_node = new list_node (d, head.next_node);
    head.next_node = new_node;
    ++list_size;
  }
  void pop_front (void) {
    if (list_size) {
      --list_size;
      list_node * old_node = head.next_node;
      head.next_node = old_node->next_node;
      delete old_node;
    }
  }
  void erase_after( iterator i ) {
    if (list_size && i.pointer->next_node != 0) {
      --list_size;
      list_node * old_node = i.pointer->next_node;
      i.pointer->next_node = old_node->next_node;
      delete old_node;
    }
  }
  ~CardWords_MySlist(){while (list_size) pop_front();};
};

#endif
