Back to index

texmacs  1.0.7.15
list.hpp
Go to the documentation of this file.
00001 
00002 /******************************************************************************
00003 * MODULE     : list.hpp
00004 * DESCRIPTION: linked lists with reference counting
00005 * COPYRIGHT  : (C) 1999  Joris van der Hoeven
00006 *******************************************************************************
00007 * This software falls under the GNU general public license version 3 or later.
00008 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
00009 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
00010 ******************************************************************************/
00011 
00012 #ifndef LIST_H
00013 #define LIST_H
00014 #include "tree.hpp"
00015 
00016 class tree;
00017 template<class T> class list_rep;
00018 template<class T> class list;
00019 
00020 template<class T> bool is_nil (list<T> l);
00021 template<class T> bool is_atom (list<T> l);
00022 template<class T> bool strong_equal (list<T> l1, list<T> l2);
00023 
00024 template<class T> class list {
00025   CONCRETE_NULL_TEMPLATE(list,T);
00026   inline list (T item);
00027   inline list (T item, list<T> next);
00028   inline list (T item1, T item2, list<T> next);
00029   inline list (T item1, T item2, T item3, list<T> next);
00030   T& operator [] (int i);
00031   operator tree ();
00032   static list<T> init;
00033 
00034   friend bool is_atom LESSGTR (list<T> l);
00035   friend bool strong_equal LESSGTR (list<T> l1, list<T> l2);
00036 };
00037 
00038 extern int list_count;
00039 template<class T> class list_rep: concrete_struct {
00040 public:
00041   T       item;
00042   list<T> next;
00043 
00044   inline list_rep<T> (T item2, list<T> next2): item(item2), next(next2) {
00045     TM_DEBUG(list_count++); }
00046   inline ~list_rep<T> () { TM_DEBUG(list_count--); }
00047   friend class list<T>;
00048 };
00049 
00050 CONCRETE_NULL_TEMPLATE_CODE(list,class,T);
00051 #define TMPL template<class T>
00052 TMPL inline list<T>::list (T item): rep (tm_new<list_rep<T> > (item, list<T> ())) {}
00053 TMPL inline list<T>::list (T item, list<T> next):
00054   rep (tm_new<list_rep<T> > (item, next)) {}
00055 TMPL inline list<T>::list (T item1, T item2, list<T> next):
00056   rep (tm_new<list_rep<T> > (item1, list<T> (item2, next))) {}
00057 TMPL inline list<T>::list (T item1, T item2, T item3, list<T> next):
00058   rep (tm_new<list_rep<T> > (item1, list<T> (item2, item3, next))) {}
00059 TMPL inline bool is_atom (list<T> l) { return (!is_nil (l)) && is_nil (l->next); }
00060 TMPL list<T> list<T>::init= list<T> ();
00061 
00062 TMPL int      N (list<T> l);
00063 TMPL list<T>  copy (list<T> l);
00064 TMPL list<T>  operator * (list<T> l1, T x);
00065 TMPL list<T>  operator * (list<T> l1, list<T> l2);
00066 TMPL list<T>  head (list<T> l, int n=1);
00067 TMPL list<T>  tail (list<T> l, int n=1);
00068 TMPL T        last_item (list<T> l);
00069 TMPL T&       access_last (list<T>& l);
00070 TMPL list<T>& suppress_last (list<T>& l);
00071 TMPL list<T>  reverse (list<T> l);
00072 TMPL list<T>  remove (list<T> l, T what);
00073 TMPL bool     contains (list<T> l, T what);
00074 
00075 TMPL tm_ostream& operator << (tm_ostream& out, list<T> l);
00076 TMPL list<T>& operator << (list<T>& l, T item);
00077 TMPL list<T>& operator << (list<T>& l1, list<T> l2);
00078 TMPL list<T>& operator >> (T item, list<T>& l);
00079 TMPL list<T>& operator << (T& item, list<T>& l);
00080 TMPL bool     operator == (list<T> l1, list<T> l2);
00081 TMPL bool     operator != (list<T> l1, list<T> l2);
00082 TMPL bool     operator < (list<T> l1, list<T> l2);
00083 TMPL bool     operator <= (list<T> l1, list<T> l2);
00084 #undef TMPL
00085 
00086 #include "list.cpp"
00087 
00088 #endif // defined LIST_H