00001 /** @file LinkedList.h 00002 * @author Gabor Madl 00003 * @date Created 10/2006 00004 * @brief Doubly linked list template class. 00005 * 00006 * 00007 * ================================================================= 00008 * DREAM License v2.0 00009 * 00010 * DREAM - Distributed Real-time Embedded Analysis Method 00011 * http://dre.sourceforge.net. 00012 * Copyright (c) 2005-2007 Gabor Madl, All Rights Reserved. 00013 * 00014 * This file is part of DREAM. 00015 * 00016 * DREAM is free software; you can redistribute it and/or modify it 00017 * under the terms of the GNU General Public License version 2 as 00018 * published by the Free Software Foundation. No future versions of 00019 * the GPL license may be automatically applied to DREAM. It is in 00020 * the sole discretion of the copyright holder to determine whether 00021 * DREAM may be released under a different license or terms. There 00022 * are no restrictions on the use of DREAM for any purpose. 00023 * 00024 * DREAM is distributed in the hope that it will be useful, 00025 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00026 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00027 * GNU General Public License for more details. 00028 * 00029 * You should have received a copy of the GNU General Public License 00030 * along with this program; if not, write to the Free Software 00031 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00032 * MA 02110-1301, USA. 00033 * 00034 * By submitting comments, suggestions, code, code snippets, 00035 * techniques (including that of usage), and algorithms, submitters 00036 * acknowledge that they have the right to do so, that any such 00037 * submissions are given freely and unreservedly, and that they 00038 * waive any claims to copyright or ownership. In addition, 00039 * submitters acknowledge that any such submission might become 00040 * part of the copyright maintained on the overall body of code, 00041 * which comprises DREAM. By making a submission, submitter agrees 00042 * to these terms. Furthermore, submitters acknowledge that the 00043 * incorporation or modification of such submissions is entirely 00044 * at the discretion of the moderators of the DREAM project. 00045 * 00046 * DREAM links to the Libxml2 third party library. Please see 00047 * COPYING-libxml for the copyright information of Libxml2. 00048 * ================================================================= 00049 */ 00050 00051 #if (_MSC_VER >= 1400) // VC8+ 00052 #pragma warning (disable:4996) // Disable all deprecation warnings 00053 #endif 00054 00055 #ifndef DREAM_LINKEDLIST 00056 #define DREAM_LINKEDLIST 00057 00058 /** Unsigned int type definition. 00059 * Redefining the basic type allows easy portability to 8 or 16 bit machines. 00060 */ 00061 typedef unsigned int uint; 00062 #define maxuint 4294967295 00063 00064 #include "Iterator.h" 00065 #include "Iterator.cpp" 00066 00067 namespace DREAM 00068 { 00069 00070 template <class item_type> class LinkedList; 00071 00072 /** Struct for one element in the LinkedList. */ 00073 template <class item_type> 00074 class LinkedListNode 00075 { 00076 // friend class declarations and typedefs confuse Doxygen... Starting with protected members first. 00077 protected: 00078 /** ++ operator. 00079 * This operator does not actually change the node's value and should only be used from within Iterators. 00080 */ 00081 LinkedListNode <item_type>* iter_next (); 00082 00083 /** -- operator. 00084 * This operator does not actually change the node's value and should only be used from within Iterators. 00085 */ 00086 LinkedListNode <item_type>* iter_previous (); 00087 00088 /** ++ operator. 00089 * This operator does not actually change the node's value and should only be used from within Iterators. 00090 */ 00091 const LinkedListNode <item_type>* iter_next () const; 00092 00093 /** -- operator. 00094 * This operator does not actually change the node's value and should only be used from within Iterators. 00095 */ 00096 const LinkedListNode <item_type>* iter_previous () const; 00097 00098 /** The left child of the node. */ 00099 LinkedListNode <item_type>* left_; 00100 00101 /** The right child of the node. */ 00102 LinkedListNode <item_type>* right_; 00103 00104 /** The item of the node. */ 00105 item_type item_; 00106 00107 public: 00108 /** Constructor. */ 00109 LinkedListNode (item_type item, LinkedListNode <item_type>* left = NULL); 00110 00111 /** Destructor. */ 00112 ~LinkedListNode (); 00113 00114 /** Returns the item of the node. */ 00115 item_type item (); 00116 00117 /** Returns the item of the node. */ 00118 const item_type item () const; 00119 00120 /** Sets the item of the node. */ 00121 void item (item_type the_item); 00122 00123 /** Sets the left child of the node. */ 00124 void left (LinkedListNode <item_type>* node_ptr); 00125 00126 /** Sets the right child of the node. */ 00127 void right (LinkedListNode <item_type>* node_ptr); 00128 00129 /** Assignment operator */ 00130 LinkedListNode <item_type>& operator= (LinkedListNode <item_type>& node); 00131 00132 /** Allow iterators to manipulate protected members. */ 00133 friend class Iterator <LinkedListNode <item_type> >; 00134 00135 /** Allow iterators to manipulate protected members. */ 00136 friend class Const_Iterator <LinkedListNode <item_type> >; 00137 00138 /** Allow LinkedList to manipulate protected members. */ 00139 friend class LinkedList <item_type>; 00140 }; 00141 00142 00143 00144 /** This class implements a doubly linked list. */ 00145 template <class item_type> 00146 class LinkedList 00147 { 00148 // friend class declarations and typedefs confuse Doxygen... Starting with protected members first. 00149 protected: 00150 /** Pointer to the first element of the linked list. */ 00151 LinkedListNode <item_type>* root_ptr_; 00152 00153 /** Pointer to the last element of the linked list. */ 00154 LinkedListNode <item_type>* last_ptr_; 00155 00156 /** Stores the number of items in the AVLTree. */ 00157 uint size_; 00158 00159 public: 00160 /** Constructor. */ 00161 LinkedList (); 00162 00163 /** Destructor. */ 00164 ~LinkedList (); 00165 00166 /** Assign operator. */ 00167 void assign (const LinkedListNode <item_type>* begin_node, const LinkedListNode <item_type>* end_node); 00168 00169 /** Assign operator. */ 00170 void assign (Iterator <LinkedListNode <item_type> >& begin_iter, Iterator <LinkedListNode <item_type> >& end_iter); 00171 00172 /** Assign operator. */ 00173 void assign (Const_Iterator <LinkedListNode <item_type> >& begin_iter, Const_Iterator <LinkedListNode <item_type> >& end_iter); 00174 00175 /** Returns a pointer to the smallest element in the LinkedList. */ 00176 LinkedListNode <item_type>* begin (); 00177 00178 /** Returns a pointer to the smallest element in the LinkedList. */ 00179 const LinkedListNode <item_type>* begin () const; 00180 00181 /** Erases elements in the LinkedList. */ 00182 void clear (); 00183 00184 /** Erases elements in the LinkedList, and calls delete on the item_type. 00185 * Use this function whenever you would like to iterate through the LinkedList, and erase dynamically allocated data. 00186 */ 00187 void destroy (); 00188 00189 /** Returns true if the LinkedList is empty. */ 00190 bool empty (); 00191 00192 /** Returns NULL. */ 00193 LinkedListNode <item_type>* end (); 00194 00195 /** Returns NULL. */ 00196 const LinkedListNode <item_type>* end () const; 00197 00198 /** Erase a node from the LinkedList. */ 00199 void erase (Iterator <LinkedListNode <item_type> >& iter); 00200 00201 /** Finds a LinkedList node in the LinkedList. */ 00202 LinkedListNode <item_type>* find (Iterator <LinkedListNode <item_type> >& node_iter); 00203 00204 /** Finds a LinkedList node in the LinkedList. */ 00205 const LinkedListNode <item_type>* find (Const_Iterator <LinkedListNode <item_type> >& node_iter) const; 00206 00207 /** Insert a LinkedList node in the LinkedList. */ 00208 void push_back (item_type item); 00209 00210 /** Returns a pointer to the last element in the LinkedList. */ 00211 LinkedListNode <item_type>* last (); 00212 00213 /** Returns a pointer to the last element in the LinkedList. */ 00214 const LinkedListNode <item_type>* last () const; 00215 00216 /** operator [].*/ 00217 item_type& operator [] (uint i); 00218 00219 /** operator [].*/ 00220 const item_type& operator [] (uint i) const; 00221 00222 /** Return the number of items in the LinkedList. */ 00223 uint size () const; 00224 00225 /** iterator class for convenience. */ 00226 typedef Iterator <LinkedListNode <item_type> > iterator; 00227 00228 /** const_iterator class for convenience. */ 00229 typedef Const_Iterator <LinkedListNode <item_type> > const_iterator; 00230 00231 /** Allow iterators to manipulate protected members. */ 00232 friend class Iterator <LinkedListNode <item_type> >; 00233 00234 /** Allow iterators to manipulate protected members. */ 00235 friend class Const_Iterator <LinkedListNode <item_type> >; 00236 }; 00237 00238 }; 00239 00240 #endif