00001 /*========================================================================== 00002 * Copyright (c) 2001 Carnegie Mellon University. All Rights Reserved. 00003 * 00004 * Use of the Lemur Toolkit for Language Modeling and Information Retrieval 00005 * is subject to the terms of the software license set forth in the LICENSE 00006 * file included with this software, and also available at 00007 * http://www.cs.cmu.edu/~lemur/license.html 00008 * 00009 *========================================================================== 00010 */ 00011 00012 00013 #ifndef _LISTH_ 00014 #define _LISTH_ 00015 00016 // #pragma interface 00017 00018 #include <LL.hpp> 00019 #include <Null.hpp> 00020 #include "common_headers.hpp" 00021 #include <cassert> 00022 00023 #define LIST_NOTFOUND (-1) 00024 00025 template <class Type> 00026 class List { 00027 public: 00028 // ****************** Constructors *************************** 00029 // Zero-argument constructor 00030 List() : head((LL<Type> *) NULL), tail((LL<Type> *) NULL), cur((LL<Type> *) NULL), lengthmem(0) { } 00031 00032 // Copy constructor 00033 List(const List<Type> &l) : head((LL<Type> *) NULL), tail((LL<Type> *) NULL), cur((LL<Type> *) NULL) { *this = l; } 00034 00035 // ****************** Destructor *************************** 00036 // destructor: delete list elements 00037 ~List(); 00038 00039 // A method to clear data so it is not deleted when list is deleted 00040 void ClearData(); 00041 // A method to delete data without deleting list 00042 void DeleteData(); 00043 00044 // ****************** Overloaded Operators ****************** 00045 // Create a unique copy of the list and it's data 00046 List<Type> &operator=(const List<Type> &l) 00047 { CopyList(l); DuplicateData(); return *this; } 00048 00049 int operator!=(List<Type> &l) const { return (*this == l); } 00050 int operator==(const List<Type> &l) const; 00051 00052 // Add to head 00053 List<Type> &operator+=(Type &d) { return AddToHead(d); } 00054 List<Type> &operator+=(Type *pD) { return AddToHead(pD); } 00055 List<Type> &operator<<(Type &d) { return AddToHead(d); } 00056 List<Type> &operator<<(Type *pD) { return AddToHead(pD); } 00057 List<Type> &AddToHead(Type &d); 00058 List<Type> &AddToHead(Type *pD); 00059 00060 // Add to tail 00061 List<Type> &AddToTail(Type &d); 00062 List<Type> &AddToTail(Type *pD); 00063 00064 // Add relative to Current() 00065 void AddAfter(const Type &d); 00066 void AddAfter(Type *d); 00067 void AddBefore(const Type &d); 00068 void AddBefore(Type *d); 00069 00070 // Replace Current 00071 void Replace(const Type &d); 00072 void Replace(Type *d); 00073 00074 // Append l to end of this list and clear l 00075 List<Type> &operator+=(List<Type> &l); 00076 00077 // Concatenate lists and return result, ClearDatas arguments 00078 List<Type> operator+(List<Type> &); 00079 00080 // Remove Functions 00081 Type &PopHead(); 00082 Type &PopTail(); 00083 // Remove and delete datum d 00084 List<Type> &operator-=(Type &d); 00085 // Remove datum d (but don't delete it) 00086 List<Type> &operator-=(Type *d) { RemoveDatum(d); return *this; } 00087 00088 // Accessor functions 00089 Type &Find(const Type &d) const; 00090 Type &Find(const Type *d) const { return Find(*d); } 00091 00092 int operator[](const Type &d) const; 00093 int operator[](const Type *d) const { return (*this)[d]; } 00094 Type &operator[](int idx) const 00095 { return ((idx >= lengthmem) || (idx < 0)) ? null(Type) : *(*head)[idx]; } 00096 00097 // Functions for Traversing list 00098 Type &GoTo(const Type &d) 00099 { return (cur = (*head)[&d]) ? *cur->datamem : null(Type); } 00100 Type &GoTo(int idx) 00101 { 00102 cur = head; 00103 while ((idx-- > 0) && cur) cur = cur->nextmem; 00104 return cur ? *cur->datamem : null(Type); 00105 } 00106 void GoToHead() { cur = head; } 00107 void GoToTail() { cur = tail; } 00108 void GoToNext() { cur = cur->nextmem; } 00109 void GoToPrev() { cur = cur->prevmem; } 00110 int EndOfList() { return (!cur); } 00111 00112 // ************************ Accessor Functions ******************* 00113 Type &Head() const { return *head->datamem; } 00114 Type &Tail() const { return *tail->datamem; } 00115 Type &Current() { return *cur->datamem; } 00116 int Length() const { return lengthmem; } 00117 private: 00118 // Private method for copying from list 00119 void CopyList(const List<Type> &l); 00120 // Private method for replacing data in list with duplicate copy 00121 void DuplicateData(); 00122 // Private method for removing a datum from the list, returns ref to datum 00123 Type *RemoveDatum(Type *d); 00124 00125 // ********************** Member Data *********************** 00126 LL<Type> *head; 00127 LL<Type> *tail; 00128 LL<Type> *cur; 00129 int lengthmem; // Was unsigned int; changed to avoid g++ warning -- JDL 00130 }; 00131 00132 #endif 00133 00134