Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

MNList.h

Go to the documentation of this file.
00001 /* Copyright (C) 2000 KOM/Darmstadt University of Technology 00002 * 00003 * You are allowed to use all other parts of the code under the following terms: 00004 * 00005 * For non-commercial use, code may be used in unmodified form provided 00006 * that this copyright notice and this permission notice appear in 00007 * supporting documentation. 00008 * 00009 * This software is provided "as is" and without any express or implied 00010 * warranties, including, without limitation, the implied warranty of 00011 * fitness for a particular purpose. 00012 * 00013 * The code may be subjected to the GNU General Public License, Version 2, 00014 * and re-distributed under the terms of this license. 00015 * As a special exception, permission is granted to link this code 00016 * with the Qt library and distribute executables, as long as you 00017 * follow the requirements of the GNU GPL in regard to all of the 00018 * software in the executable aside from Qt. 00019 * 00020 * Commercial use other than under the terms of the GNU General Public 00021 * License is allowed only after express negotiation of conditions 00022 * with the authors. 00023 */ 00024 #ifndef OS_UTIL_MNLIST_H 00025 #define OS_UTIL_MNLIST_H 00026 00027 #include <assert.h> 00028 #include <string.h> 00029 00030 /* 00031 * We have successfully used the LEDA lists in the recent 00032 * past. Unfortunately, some of our projects can not make use 00033 * of this set of tools due to its licensing restrictions. 00034 * This list is supposed to be one of the drop-on replacements 00035 * for the most basic uses of LEDA. 00036 */ 00037 00038 #ifdef __cplusplus 00039 00040 #ifndef ASSERT_NON_NULL 00041 #define ASSERT_NON_NULL(a) 00042 #endif 00043 00044 /************************************************************* 00045 * MNForall_items() macro 00046 *************************************************************/ 00047 00048 template <class T, class M> 00049 inline void* MNForall_assign(T& r, void* p, const M& l) { 00050 if(p) 00051 { 00052 r = l.inf(p); 00053 ASSERT_NON_NULL(r); 00054 } 00055 return p; 00056 } 00057 00058 #define MNForall_items(r,l) \ 00059 for ( r = l.first(); \ 00060 r != NULL; \ 00061 r = l.succ(r) ) 00062 00063 #define MNForall_var MNForall_runner##__LINE__ 00064 00065 #define MNForall(r,l) \ 00066 for ( void* \ 00067 MNForall_var = MNForall_assign(r,l.first(),l); \ 00068 MNForall_var != NULL; \ 00069 MNForall_var = MNForall_assign(r,l.succ(MNForall_var),l) ) 00070 00071 /************************************************************* 00072 * MNVPListItem 00073 * 00074 * This basic Item class can be overridden by the template 00075 * classes. Its basic functionality remains 00076 */ 00077 00078 class MNVPListItem 00079 { 00080 MNVPListItem* _succ; 00081 MNVPListItem* _pred; 00082 public: 00083 MNVPListItem( ); 00084 virtual ~MNVPListItem() { } 00085 00086 MNVPListItem* succ() const; 00087 MNVPListItem* pred() const; 00088 void setPred( MNVPListItem* pred ); 00089 void setSucc( MNVPListItem* succ ); 00090 void validityCheck(); 00091 00092 virtual MNVPListItem* clone() const = 0; 00093 }; 00094 00095 /************************************************************* 00096 * MNVPList 00097 * 00098 * This is a basic list class. Its implementation is 00099 * template-free and made in a library. This 00100 * implements a plain double-linked list. 00101 *************************************************************/ 00102 00103 class MNVPList 00104 { 00105 private: 00106 int _len; 00107 MNVPListItem* _first; 00108 MNVPListItem* _last; 00109 00110 protected: 00111 void clone ( const MNVPList& orig ); 00112 00113 public: 00114 MNVPList ( const MNVPList& orig ); 00115 MNVPList ( ); 00116 00117 ~MNVPList ( ); 00118 00119 MNVPList& operator=( const MNVPList& orig ); 00120 00121 MNVPListItem* first() const; 00122 MNVPListItem* last() const; 00123 MNVPListItem* pred(MNVPListItem* item) const; 00124 MNVPListItem* succ(MNVPListItem* item) const; 00125 bool empty() const; 00126 int length() const; 00127 00128 void del_item( MNVPListItem* x ); 00129 void del_back(); 00130 void del_front(); 00131 void push_front( MNVPListItem* x ); 00132 void push_back( MNVPListItem* x ); 00133 void ins_before ( MNVPListItem* x, MNVPListItem* y ); 00134 void ins_after ( MNVPListItem* x, MNVPListItem* y ); 00135 void clear ( ); 00136 }; 00137 00138 /************************************************************* 00139 * MNList 00140 *************************************************************/ 00141 00142 template <class T> class MNList 00143 { 00144 MNVPList l; 00145 private: 00146 /********************************************************* 00147 * MNList::Item 00148 *********************************************************/ 00149 00150 class Item : public MNVPListItem 00151 { 00152 public: 00153 T _data; 00154 Item(const T& t) : _data(t) { 00155 ASSERT_NON_NULL ( _data ); 00156 } 00157 00158 virtual ~Item() { } 00159 00160 virtual const T* data() const { 00161 return &_data; 00162 } 00163 00164 virtual MNVPListItem* clone() const { 00165 ASSERT_NON_NULL ( _data ); 00166 return new Item(_data); 00167 } 00168 }; 00169 00170 public: 00171 MNList ( const MNList<T>& orig ) : l(orig.l) { 00172 } 00173 MNList ( ) { 00174 } 00175 00176 ~MNList ( ) { } 00177 00178 MNList<T>& operator=( const MNList<T>& orig ) { 00179 l=orig.l; 00180 return *this; 00181 } 00182 00183 inline void* first() const { return l.first(); } 00184 inline void* last() const { return l.last(); } 00185 inline void* pred(void* item) const { return l.pred((MNVPListItem*)item); } 00186 inline void* succ(void* item) const { return l.succ((MNVPListItem*)item); } 00187 inline bool empty() const { return l.empty(); } 00188 inline int length() const { return l.length(); } 00189 00190 inline const T& peek_front() const { return ((Item*)l.first())->_data; } 00191 inline T peek_front() { return ((Item*)l.first())->_data; } 00192 inline const T& peek_back() const { return ((Item*)l.last())->_data; } 00193 inline T peek_back() { return ((Item*)l.last())->_data; } 00194 inline const T& peek_here(void* r) const { return ((Item*)r)->_data; } 00195 00196 inline const T& inf(void* r) const { 00197 assert ( r ); 00198 Item* rc = (Item*)r; 00199 ASSERT_NON_NULL ( rc->_data ); 00200 // return rc->_data; 00201 return *(rc->data()); 00202 } 00203 inline T inf(void* r) { 00204 assert ( r ); 00205 Item* rc = (Item*)r; 00206 ASSERT_NON_NULL ( rc->_data ); 00207 // return rc->_data; 00208 return *(rc->data()); 00209 } 00210 inline void del_item(void* item) { l.del_item((MNVPListItem*)item); } 00211 inline void del_front() { l.del_front(); } 00212 inline void del_back() { l.del_back(); } 00213 inline void clear() { l.clear(); } 00214 00215 inline T pop() { 00216 T ret = peek_front(); 00217 l.del_front(); 00218 ASSERT_NON_NULL ( ret ); 00219 return ret; 00220 } 00221 00222 inline void push_front( T x ) { 00223 ASSERT_NON_NULL ( x ); 00224 l.push_front(new Item(x)); 00225 } 00226 inline void prepend( T x ) { 00227 ASSERT_NON_NULL ( x ); 00228 l.push_front(new Item(x)); 00229 } 00230 inline void push_back( T x ) { 00231 ASSERT_NON_NULL ( x ); 00232 l.push_back(new Item(x)); 00233 } 00234 inline void append( T x ) { 00235 ASSERT_NON_NULL ( x ); 00236 l.push_back(new Item(x)); 00237 } 00238 inline void ins_before( void* x, T y ) { 00239 ASSERT_NON_NULL ( y ); 00240 l.ins_before((MNVPListItem*)x, new Item(y)); 00241 } 00242 inline void ins_after( void* x, T y ) { 00243 ASSERT_NON_NULL ( y ); 00244 l.ins_after((MNVPListItem*)x, new Item(y)); 00245 } 00246 }; 00247 00248 #endif /* __cplusplus */ 00249 00250 #endif /* OS_UTIL_MNLIST_H */ 00251

Generated on Sun Mar 6 13:35:49 2005 for Komssys by doxygen 1.3.8