LinkedList.cpp

Go to the documentation of this file.
00001 /** @file LinkedList.cpp
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 #ifndef DREAM_LINKEDLISTCPP
00052 #define DREAM_LINKEDLISTCPP
00053 
00054 #include "LinkedList.h"
00055 
00056 namespace DREAM
00057 {
00058 
00059 /////////////////////////////////////////////////////////////////////////////
00060 //
00061 // LinkedListNode
00062 //
00063 /////////////////////////////////////////////////////////////////////////////
00064 
00065 template <class item_type>
00066 LinkedListNode <item_type>::LinkedListNode (item_type item, LinkedListNode <item_type>* left) 
00067 : item_ (item), left_ (left), right_ (NULL)
00068 {
00069 };
00070 
00071 template <class item_type>
00072 LinkedListNode <item_type>::~LinkedListNode ()
00073 {
00074         if (right_)
00075                 delete right_;
00076 }
00077 
00078 template <class item_type>
00079 inline item_type LinkedListNode <item_type>::item ()
00080 {
00081         return item_;
00082 }
00083 
00084 template <class item_type>
00085 inline const item_type LinkedListNode <item_type>::item () const
00086 {
00087         return item_;
00088 }
00089 
00090 template <class item_type>
00091 inline void LinkedListNode <item_type>::item (item_type the_item)
00092 {
00093         item_ = the_item;
00094 }
00095 
00096 template <class item_type>
00097 inline void LinkedListNode <item_type>::left (LinkedListNode <item_type>* node_ptr)
00098 {
00099         left_ = node_ptr;
00100 }
00101 
00102 template <class item_type>
00103 inline void LinkedListNode <item_type>::right (LinkedListNode <item_type>* node_ptr)
00104 {
00105         right_ = node_ptr;
00106 }
00107 
00108 template <class item_type>
00109 LinkedListNode <item_type>& LinkedListNode <item_type>::operator= (LinkedListNode <item_type>& node)
00110 {
00111         item_ = node.item_;
00112         left_ = node.left_;
00113         right_ = node.right_;
00114         return *this;
00115 }
00116 
00117 template <class item_type>
00118 inline LinkedListNode <item_type>* LinkedListNode <item_type>::iter_next ()
00119 {
00120         return right_;
00121 }
00122 
00123 template <class item_type>
00124 inline LinkedListNode <item_type>* LinkedListNode <item_type>::iter_previous ()
00125 {
00126         return left_;
00127 }
00128 
00129 template <class item_type>
00130 inline const LinkedListNode <item_type>* LinkedListNode <item_type>::iter_next () const
00131 {
00132         return right_;
00133 }
00134 
00135 template <class item_type>
00136 inline const LinkedListNode <item_type>* LinkedListNode <item_type>::iter_previous () const
00137 {
00138         return left_;
00139 }
00140 
00141 /////////////////////////////////////////////////////////////////////////////
00142 //
00143 // LinkedList
00144 //
00145 /////////////////////////////////////////////////////////////////////////////
00146 
00147 template <class item_type>
00148 LinkedList <item_type>::LinkedList ()
00149 : root_ptr_ (NULL), last_ptr_ (NULL), size_ (0) {};
00150 
00151 template <class item_type>
00152 LinkedList <item_type>::~LinkedList ()
00153 {
00154         if (root_ptr_)
00155                 delete root_ptr_;
00156 }
00157 
00158 template <class item_type>
00159 void LinkedList <item_type>::assign (const LinkedListNode <item_type>* begin_node, const LinkedListNode <item_type>* end_node)
00160 {
00161         Iterator <LinkedListNode <item_type> > iter;
00162 
00163         for (iter = begin_node; iter != end_node; iter++)
00164                 push_back (iter);
00165 }
00166 
00167 
00168 template <class item_type>
00169 void LinkedList <item_type>::assign (Iterator <LinkedListNode <item_type> >& begin_iter, Iterator <LinkedListNode <item_type> >& end_iter)
00170 {
00171         Iterator <LinkedListNode <item_type> > iter;
00172 
00173         for (iter = begin_iter; iter != end_iter; iter++)
00174                 push_back (iter);
00175 }
00176 
00177 template <class item_type>
00178 void LinkedList <item_type>::assign (Const_Iterator <LinkedListNode <item_type> >& begin_iter, Const_Iterator <LinkedListNode <item_type> >& end_iter)
00179 {
00180         Const_Iterator <LinkedListNode <item_type> > iter;
00181 
00182         for (iter = begin_iter; iter != end_iter; iter++)
00183                 push_back (iter);
00184 }
00185 
00186 template <class item_type>
00187 inline LinkedListNode <item_type>* LinkedList <item_type>::begin ()
00188 {
00189         return root_ptr_;
00190 }
00191 
00192 template <class item_type>
00193 inline const LinkedListNode <item_type>* LinkedList <item_type>::begin () const
00194 {
00195         return root_ptr_;
00196 }
00197 
00198 template <class item_type>
00199 void LinkedList <item_type>::clear ()
00200 {
00201         if (root_ptr_)
00202         {
00203                 delete root_ptr_;
00204                 size_ = 0;
00205                 root_ptr_ = NULL;
00206         }
00207 }
00208 
00209 template <class item_type>
00210 void LinkedList <item_type>::destroy ()
00211 {
00212         if (root_ptr_)
00213         {
00214                 Iterator <LinkedListNode <item_type> > iter;
00215                 for (iter = begin (); iter != end (); iter++)
00216                         delete iter->item_;
00217         
00218                 delete root_ptr_;
00219                 size_ = 0;
00220                 root_ptr_ = NULL;
00221         }
00222 }
00223 
00224 template <class item_type>
00225 inline bool LinkedList <item_type>::empty ()
00226 {
00227         return (size_ == 0) ? true : false;
00228 }
00229 
00230 template <class item_type>
00231 inline LinkedListNode <item_type>* LinkedList <item_type>::end ()
00232 {
00233         return NULL;
00234 }
00235 
00236 template <class item_type>
00237 inline const LinkedListNode <item_type>* LinkedList <item_type>::end () const
00238 {
00239         return NULL;
00240 }
00241 
00242 template <class item_type>
00243 void LinkedList <item_type>::erase (Iterator <LinkedListNode <item_type> >& iter)
00244 {
00245         LinkedListNode <item_type>* left_ptr = iter->iter_previous ();
00246         LinkedListNode <item_type>* right_ptr = iter->iter_next ();
00247         --size_;
00248 
00249         left_ptr->right_ = right_ptr;
00250         right_ptr->left_ = left_ptr;
00251 
00252         iter->right_ = NULL;
00253         delete *iter;
00254 }
00255 
00256 template <class item_type>
00257 LinkedListNode <item_type>* LinkedList <item_type>::find (Iterator <LinkedListNode <item_type> >& node_iter)
00258 {
00259         Iterator <LinkedListNode <item_type> > iter;
00260 
00261         for (iter = begin (); iter != end (); iter++)
00262         {
00263                 if (iter == node_iter)
00264                 {
00265                         return *iter;
00266                 }
00267         }
00268 
00269         return NULL;
00270 }
00271 
00272 template <class item_type>
00273 const LinkedListNode <item_type>* LinkedList <item_type>::find (Const_Iterator <LinkedListNode <item_type> >& node_iter) const
00274 {
00275         Iterator <LinkedListNode <item_type> > iter;
00276 
00277         for (iter = begin (); iter != end (); iter++)
00278         {
00279                 if (iter == node_iter)
00280                 {
00281                         return *iter;
00282                 }
00283         }
00284 
00285         return NULL;
00286 }
00287 
00288 template <class item_type>
00289 void LinkedList <item_type>::push_back (item_type item)
00290 {
00291         ++size_;
00292 
00293         if (!root_ptr_)
00294         {
00295                 // First element
00296                 root_ptr_ = new LinkedListNode <item_type> (item);
00297                 last_ptr_ = root_ptr_;
00298         }
00299         else
00300         {
00301                 // Append item to LinkedList
00302                 last_ptr_->right_ = new LinkedListNode <item_type> (item, last_ptr_);
00303                 last_ptr_ = last_ptr_->iter_next ();
00304         }
00305 }
00306 
00307 template <class item_type>
00308 inline LinkedListNode <item_type>* LinkedList <item_type>::last ()
00309 {
00310         return last_ptr_;
00311 }
00312 
00313 template <class item_type>
00314 inline const LinkedListNode <item_type>* LinkedList <item_type>::last () const
00315 {
00316         return last_ptr_;
00317 }
00318 
00319 template <class item_type>
00320 item_type& LinkedList <item_type>::operator [] (uint i)
00321 {
00322         Iterator <LinkedListNode <item_type> > iter;
00323         iter = begin ();
00324 
00325         uint j;
00326         for (j = 0; j < i; j++)
00327                 ++iter;
00328 
00329         return *iter;
00330 }
00331 
00332 template <class item_type>
00333 const item_type& LinkedList <item_type>::operator [] (uint i) const
00334 {
00335         Const_Iterator <LinkedListNode <item_type> > iter;
00336         iter = begin ();
00337 
00338         uint j;
00339         for (j = 0; j < i; j++)
00340                 ++iter;
00341 
00342         return *iter;
00343 }
00344 
00345 template <class item_type>
00346 inline uint LinkedList <item_type>::size () const
00347 {
00348         return size_;
00349 }
00350 
00351 };
00352 
00353 #endif

Generated on Fri Jul 27 18:30:03 2007 for DREAM by  doxygen 1.5.1