Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

t_algo.h

Go to the documentation of this file.
00001 /* liblookdb: t_algo.h - cut down std::algorithm equivalent 
00002     Copyright (C) 1998-2001 LOOK Systems
00003 
00004     This library is free software; you can redistribute it and/or
00005     modify it under the terms of the GNU Lesser General Public
00006     License as published by the Free Software Foundation; either
00007     version 2.1 of the License, or (at your option) any later version.
00008 
00009     This library is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     Lesser General Public License for more details.
00013 
00014     You should have received a copy of the GNU Lesser General Public
00015     License along with this library; if not, write to the Free Software
00016     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017 */
00018 #ifndef LIBLOOKDB_T_ALGO_H
00019 #define LIBLOOKDB_T_ALGO_H
00020 
00021 // $Id: t_algo.h,v 1.8 2001/07/04 15:53:22 clive Exp $
00022 
00023 // pick up compiler specifics
00024 #include "lookcompileropts.h"
00025 
00033 namespace look {
00034 
00036 template<class tIter, class tBody> inline
00037 tIter LookTLfind(tIter first, tIter last, const tBody& val)
00038 {
00039         for (; first != last; ++first)
00040         {
00041                 if( *first == val )
00042                 {
00043                         break;
00044                 }
00045         }
00046         return first;
00047 }
00048 
00050 
00058 template<class tIter, class tBody> inline
00059 tIter LookTLfindByPtr(tIter first, tIter last, const tBody* val)
00060 {
00061         for (; first != last; ++first)
00062         {
00063                 if( *first != NULL && (val == *first || **first == *val) )
00064                 {
00065                         break;
00066                 }
00067         }
00068         return first;
00069 }
00070 
00072 
00078 template<class tList, class tBody> inline
00079 void LookTLremoveMatching(tList& theList, const tBody& val)
00080 {
00081         typename tList::iterator anIter = theList.begin();
00082 
00083         while( anIter != theList.end() )
00084         {
00085                 if( *anIter == val )
00086                 {
00087                         // hoorah! erase returns the next iterator, ie == iter++
00088                         anIter = theList.erase( anIter );
00089                 }
00090                 else
00091                 {
00092                         anIter++;
00093                 }
00094         }
00095 }
00096 
00098 
00101 template<class containerClass> inline
00102 void ClearAndDestroy( containerClass& aContainer )
00103 {
00104 // the container must contain pointers
00105         typename containerClass::iterator anIter;
00106 
00107         for( anIter = aContainer.begin();
00108                 anIter != aContainer.end();
00109                 anIter++ )
00110         {
00111                 delete *anIter;
00112         }
00113         aContainer.clear();
00114 }
00115 
00116 // if you use this, the class must have GetNewCopy defined
00117 // CThing* GetNewCopy( void )
00118 // {
00119 //    return new CThing( *this );
00120 // }
00121 
00123 
00131 template<class containerClass> inline
00132 void CopyAsNew( containerClass& theToContainer,
00133         const containerClass& theFromContainer )
00134 {
00135 // the container must contain pointers
00136         typename containerClass::const_iterator anIter;
00137 
00138         for( anIter = theFromContainer.begin();
00139                 anIter != theFromContainer.end();
00140                 anIter++ )
00141         {
00142                 typename containerClass::value_type anItem = (*anIter)->GetNewCopy();
00143                 theToContainer.push_back( anItem );
00144         }
00145 }
00146 
00147 // index : like find except return position.
00148 // return -1 for not found
00150 
00153 template<class tList, class tBody> inline
00154 int LookTLindex(tList& aList, const tBody& val)
00155 {
00156         int end = aList.size();
00157         for( int loop = 0; loop < end; loop++ )
00158         {
00159                 if( aList.at( loop ) == val )
00160                 {
00161                         return loop;
00162                 }
00163         }
00164         return -1;
00165 }
00166 
00168 
00171 template<class tList, class tBody> inline
00172 int LookTLindexByPtr(tList& aList, const tBody* val)
00173 {
00174         int end = aList.size();
00175         for( int loop = 0; loop < end; loop++ )
00176         {
00177                 tBody* test = aList.at( loop );
00178                 if( test != NULL && ( val == test || *val == *test ) )
00179                 {
00180                         return loop;
00181                 }
00182         }
00183         return -1;
00184 }
00185 
00186 // lists equal - are two lists identical?
00188 
00191 template<class tList> inline
00192 bool LookTLlistsEqual(const tList& aList, const tList& bList)
00193 {
00194         if( aList.size() != bList.size() )
00195         {
00196                 return false;
00197         }
00198 
00199         typename tList::const_iterator aIter = aList.begin();
00200         typename tList::const_iterator bIter = bList.begin();
00201 
00202         for( ; aIter != aList.end(); aIter++, bIter++ )
00203         {
00204                 if( *aIter != *bIter )
00205                 {
00206                         return false;
00207                 }
00208         }
00209 
00210         return true;
00211 }
00212 
00213 
00214 } // end of "look" namespace
00215 #endif

Generated at Thu Jan 17 12:53:06 2002 for liblookdb by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001