Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members

tlist.h

00001 00003 // This software is distributed as part of the Improla library. 00004 // Improla is a GUI framework for image processing. 00005 // 00006 // Copyright (c) 2004, B. R. Siva Chandra, India 00007 // 00008 // This program is free software; you can redistribute it and/or 00009 // modify it under the terms of the GNU General Public License 00010 // as published by the Free Software Foundation; either version 2 00011 // of the License, or (at your option) any later version. 00012 // 00013 // This program is distributed in the hope that it will be useful, 00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 // GNU General Public License for more details. 00017 // 00018 // You should have received a copy of the GNU General Public License 00019 // along with this program; if not, write to the Free Software 00020 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00021 // 00022 // In case you would like to contact the author, use the following e-mail 00023 // address: sivachandra_br@yahoo.com 00025 00026 #ifndef __T_LIST_H__ 00027 #define __T_LIST_H__ 00028 00029 #ifndef NULL 00030 #define NULL 0 00031 #endif // NULL 00032 00033 namespace tutk 00034 { 00035 00039 template <class T> 00040 class NODE 00041 { 00042 public: 00047 NODE(void); 00048 00052 ~NODE(); 00053 00056 NODE *nextNodePtr; 00059 NODE *previousNodePtr; 00062 T data; 00063 }; 00064 00065 template <class T> 00066 NODE<T>::NODE(void) 00067 { 00068 nextNodePtr = previousNodePtr = NULL; 00069 data = T(); 00070 } 00071 00072 template <class T> 00073 NODE<T>::~NODE() { ; } 00074 00077 template <class T> 00078 class List 00079 { 00080 public: 00084 List(void); 00085 00103 List(const List &l); 00104 00107 ~List(); 00108 00111 //bool insert(T node, int location); // TO DO... 00112 00115 void append(T node); 00116 00119 void push(T node); 00120 00126 void pop(void); 00127 00130 //bool remove(int location); // TO DO... 00131 00138 void destroy(void); 00139 00143 void refresh(void); // This resets the currentNodePtr to the firstNode. 00144 00148 T getLastNode(bool& validity); 00149 00156 T getNode(bool& validity); 00157 00161 T* getPointerToNodeItem(void); 00162 00165 int getSize(void); 00166 00169 T* getPointerToLastItem(void); 00170 00174 void operator=(const List &l); 00175 00176 private: 00177 int *size; 00178 int *refCount; 00179 NODE<T> *firstNodePtr, *lastNodePtr; 00180 NODE<T> *currentNodePtr; 00181 }; 00182 00183 // List implementation... 00184 00185 // Constructor... 00186 template <class T> 00187 List<T>::List(void) 00188 { 00189 firstNodePtr = lastNodePtr = currentNodePtr = NULL; 00190 00191 size = new int; refCount = new int; 00192 *size = 0; *refCount = 1; 00193 } 00194 00195 template <class T> 00196 List<T>::List(const List &l) 00197 : firstNodePtr(l.firstNodePtr), currentNodePtr(l.currentNodePtr), 00198 lastNodePtr(l.lastNodePtr), size(l.size), refCount(l.refCount) 00199 { 00200 (*refCount)++; 00201 } 00202 00203 template <class T> 00204 List<T>::~List() 00205 { 00206 if (*refCount <= 1) 00207 { 00208 destroy(); 00209 delete size; delete refCount; 00210 } 00211 else 00212 (*refCount)--; 00213 } 00214 00215 template <class T> 00216 void List<T>::append(T node) 00217 { 00218 NODE<T> *newNodePtr = new NODE<T>(); 00219 00220 if (*size == 0) 00221 firstNodePtr = currentNodePtr = newNodePtr; 00222 else 00223 { 00224 newNodePtr->previousNodePtr = lastNodePtr; 00225 lastNodePtr->nextNodePtr = newNodePtr; 00226 } 00227 00228 lastNodePtr = newNodePtr; 00229 lastNodePtr->data = T(node); 00230 (*size)++; 00231 00232 return; 00233 } 00234 00235 template <class T> 00236 void List<T>::refresh(void) 00237 { 00238 currentNodePtr = firstNodePtr; 00239 return; 00240 } 00241 00242 template <class T> 00243 T List<T>::getNode(bool& validity) 00244 { 00245 if (*size == 0 || currentNodePtr == NULL) 00246 { 00247 T dummy; 00248 validity = false; 00249 return dummy; 00250 } 00251 else 00252 { 00253 T nodeData = T(currentNodePtr->data); 00254 currentNodePtr = currentNodePtr->nextNodePtr; 00255 validity = true; 00256 return nodeData; 00257 } 00258 } 00259 00260 template <class T> 00261 T* List<T>::getPointerToNodeItem(void) 00262 { 00263 if (*size == 0 || currentNodePtr == NULL) 00264 return NULL; 00265 else 00266 { 00267 T *dataPtr = &(currentNodePtr->data); 00268 return dataPtr; 00269 } 00270 } 00271 00272 template <class T> 00273 void List<T>::pop(void) 00274 { 00275 if (*size == 0) 00276 return; 00277 else if (*size == 1) 00278 { 00279 delete firstNodePtr; 00280 lastNodePtr = firstNodePtr = currentNodePtr = NULL; 00281 (*size)--; 00282 00283 return; 00284 } 00285 00286 lastNodePtr = lastNodePtr->previousNodePtr; 00287 delete lastNodePtr->nextNodePtr; 00288 lastNodePtr->nextNodePtr = NULL; 00289 (*size)--; 00290 00291 return; 00292 } 00293 00294 template <class T> 00295 void List<T>::push(T node) 00296 { 00297 append(node); 00298 return; 00299 } 00300 00301 template <class T> 00302 void List<T>::destroy(void) 00303 { 00304 int s = *size; 00305 00306 for (int i = 1; i <= s; i++) 00307 pop(); 00308 00309 return; 00310 } 00311 00312 template <class T> 00313 T List<T>::getLastNode(bool& validity) 00314 { 00315 if (*size == 0) 00316 { 00317 T dummy; 00318 validity = false; 00319 return dummy; 00320 } 00321 else 00322 { 00323 T lastNodeData = T(lastNodePtr->data); 00324 validity = true; 00325 return lastNodeData; 00326 } 00327 } 00328 00329 template <class T> 00330 T* List<T>::getPointerToLastItem(void) 00331 { 00332 if (lastNodePtr == NULL) 00333 return NULL; 00334 else 00335 return &(lastNodePtr->data); 00336 } 00337 00338 template <class T> 00339 int List<T>::getSize(void) 00340 { 00341 return *size; 00342 } 00343 00344 template <class T> 00345 void List<T>::operator=(const List &l) 00346 { 00347 if (*refCount <= 1) 00348 { 00349 destroy(); 00350 delete size; delete refCount; 00351 00352 firstNodePtr = l.firstNodePtr; 00353 currentNodePtr = l.currentNodePtr; 00354 lastNodePtr = l.lastNodePtr; 00355 size = l.size; 00356 refCount = l.refCount; 00357 (*refCount)++; 00358 } 00359 else 00360 { 00361 (*refCount)--; 00362 00363 firstNodePtr = l.firstNodePtr; 00364 currentNodePtr = l.currentNodePtr; 00365 lastNodePtr = l.lastNodePtr; 00366 size = l.size; 00367 refCount = l.refCount; 00368 (*refCount)++; 00369 } 00370 00371 return; 00372 } 00373 00374 } // End of namespace... 00375 00376 #endif // __T_LIST_H__ 00377

Generated on Mon Nov 22 11:12:41 2004 for ImprolaLib by doxygen 1.3.7