![]() |
00001 // Copyright (C) 2003 Johan Hoffman and Anders Logg. 00002 // Licensed under the GNU GPL Version 2. 00003 00004 #ifndef __LIST_H 00005 #define __LIST_H 00006 00007 #include <list> 00008 #include <iterator> 00009 #include <dolfin/dolfin_log.h> 00010 00011 namespace dolfin { 00012 00014 00017 00018 template <class T> class List { 00019 public: 00020 00022 List(); 00023 00025 ~List(); 00026 00028 void clear(); 00029 00031 void add(const T& element); 00032 00034 int size() const; 00035 00037 bool empty() const; 00038 00040 bool contains(const T& element); 00041 00043 void remove(const T& element); 00044 00046 T pop(); 00047 00053 00054 class Iterator { 00055 public: 00056 00058 Iterator(List<T>& list); 00059 00061 Iterator(const Iterator& it); 00062 00064 int index() const; 00065 00067 Iterator& operator++(); 00068 00070 bool end() const; 00071 00073 bool last() const; 00074 00076 T& operator*() const; 00077 00079 T* operator->() const; 00080 00082 operator T*() const; 00083 00085 T* pointer() const; 00086 00088 void operator=(const Iterator& it); 00089 00091 bool operator==(const Iterator& it) const; 00092 00094 bool operator!=(const Iterator& it) const; 00095 00096 private: 00097 00098 typename std::list<T>::iterator it; 00099 std::list<T>& l; 00100 int _index; 00101 00102 }; 00103 00104 // Friends 00105 friend class Iterator; 00106 00107 private: 00108 00109 std::list<T> l; 00110 00111 }; 00112 00113 //--------------------------------------------------------------------------- 00114 // Implementation of List 00115 //--------------------------------------------------------------------------- 00116 template <class T> List<T>::List() 00117 { 00118 // Do nothing 00119 } 00120 //--------------------------------------------------------------------------- 00121 template <class T> List<T>::~List() 00122 { 00123 // Do nothing 00124 } 00125 //--------------------------------------------------------------------------- 00126 template <class T> void List<T>::clear() 00127 { 00128 l.clear(); 00129 } 00130 //--------------------------------------------------------------------------- 00131 template <class T> void List<T>::add(const T& element) 00132 { 00133 l.push_back(element); 00134 } 00135 //--------------------------------------------------------------------------- 00136 template <class T> int List<T>::size() const 00137 { 00138 // Note the following from the stl manual: You should not assume 00139 // that this function is constant time. It is permitted to be 00140 // O(N), where N is the number of elements in the list. 00141 00142 return l.size(); 00143 } 00144 //--------------------------------------------------------------------------- 00145 template <class T> bool List<T>::empty() const 00146 { 00147 return l.empty(); 00148 } 00149 //--------------------------------------------------------------------------- 00150 template <class T> bool List<T>::contains(const T& element) 00151 { 00152 for (typename std::list<T>::iterator it = l.begin(); it != l.end(); ++it) 00153 if ( *it == element ) 00154 return true; 00155 00156 return false; 00157 } 00158 //--------------------------------------------------------------------------- 00159 template <class T> void List<T>::remove(const T& element) 00160 { 00161 for (typename std::list<T>::iterator it = l.begin(); it != l.end(); ++it) 00162 if ( *it == element ) { 00163 l.erase(it); 00164 return; 00165 } 00166 dolfin_error("Element is not in the list."); 00167 } 00168 //--------------------------------------------------------------------------- 00169 template <class T> T List<T>::pop() 00170 { 00171 T first = l.front(); 00172 l.pop_front(); 00173 return first; 00174 } 00175 //--------------------------------------------------------------------------- 00176 // Implementation of List::Iterator 00177 //--------------------------------------------------------------------------- 00178 template <class T> List<T>::Iterator::Iterator (List<T>& list) : l(list.l) 00179 { 00180 it = l.begin(); 00181 _index = 0; 00182 } 00183 //--------------------------------------------------------------------------- 00184 template <class T> List<T>::Iterator::Iterator(const Iterator& it) : l(it.l) 00185 { 00186 this->it = it.it; 00187 this->_index = it._index; 00188 } 00189 //--------------------------------------------------------------------------- 00190 template <class T> int List<T>::Iterator::index() const 00191 { 00192 return _index; 00193 } 00194 //--------------------------------------------------------------------------- 00195 template <class T> typename List<T>::Iterator& List<T>::Iterator::operator++() 00196 { 00197 ++it; 00198 ++_index; 00199 return *this; 00200 } 00201 //--------------------------------------------------------------------------- 00202 template <class T> bool List<T>::Iterator::end() const 00203 { 00204 return it == l.end(); 00205 } 00206 //--------------------------------------------------------------------------- 00207 template <class T> bool List<T>::Iterator::last() const 00208 { 00209 // FIXME: This is probably not the correct way to make the check 00210 typename std::list<T>::iterator new_it = it; 00211 ++new_it; 00212 return new_it == l.end(); 00213 } 00214 //--------------------------------------------------------------------------- 00215 template <class T> T& List<T>::Iterator::operator*() const 00216 { 00217 return *it; 00218 } 00219 //--------------------------------------------------------------------------- 00220 template <class T> T* List<T>::Iterator::operator->() const 00221 { 00222 return &(*it); 00223 } 00224 //--------------------------------------------------------------------------- 00225 template <class T> List<T>::Iterator::operator T*() const 00226 { 00227 return &(*it); 00228 } 00229 //--------------------------------------------------------------------------- 00230 template <class T> T* List<T>::Iterator::pointer() const 00231 { 00232 return &(*it); 00233 } 00234 //--------------------------------------------------------------------------- 00235 template <class T> void List<T>::Iterator::operator=(const Iterator& it) 00236 { 00237 this->it = it; 00238 } 00239 //--------------------------------------------------------------------------- 00240 template <class T> bool List<T>::Iterator::operator== 00241 (const Iterator& it) const 00242 { 00243 return this->it == it; 00244 } 00245 //--------------------------------------------------------------------------- 00246 template <class T>bool List<T>::Iterator::operator!= 00247 (const Iterator& it) const 00248 { 00249 return this->it != it; 00250 } 00251 //--------------------------------------------------------------------------- 00252 00253 } 00254 00255 #endif
Documentation automatically generated with Doxygen on 10 Sep 2004.