SearchList.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2001-2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef _SearchList_
00023 #define _SearchList_
00024 
00025 #include "OSDefines.hxx"
00026 #include "List.hxx"
00027 
00028 namespace CLAM {
00029 
00030 template <class T> class SearchList
00031 {
00032 private:
00033         List<T>* mpList;
00034         Elem<T>* mpPrevElem;
00035         TIndex mPrevIndex;
00036 public:
00037         SearchList()
00038         {
00039                 mPrevIndex = 0;
00040                 mpPrevElem = NULL;
00041         }
00042         SearchList(List<T>& list)
00043         {
00044                 Set(list);
00045                 mPrevIndex = 0;
00046                 mpPrevElem = NULL;
00047         }
00048         void Set(List<T>& list)
00049         {
00050                 mpList = &list;
00051         }
00052         const TIndex& Find(const T& value);
00053 };
00054 
00055 template <class T> 
00056         inline const TIndex& SearchList<T>::Find(const T& value)
00057 {
00058         if (mpPrevElem==NULL) {
00059                 mpPrevElem = (Elem<T>*) mpList->First();
00060                 mPrevIndex = 0;
00061         }
00062         if (value>mpPrevElem->Value())
00063         {
00064                 if (value>((Elem<T>*) mpList->Last())->Value()) {
00065                         mpPrevElem = (Elem<T>*) mpList->Last();
00066                         mPrevIndex = mpList->Size()-1;          
00067                 } else {
00068                         do {
00069                                 mpPrevElem = (Elem<T>*) mpPrevElem->Next();
00070                                 mPrevIndex++;
00071                         } while (value>mpPrevElem->Value());            
00072                 }
00073         }
00074         if (value<mpPrevElem->Value())
00075         {
00076                 if (value<((Elem<T>*) mpList->First())->Value()) {
00077                         mpPrevElem = NULL;
00078                         mPrevIndex = -1;
00079                 } else {
00080                         do {
00081                                 mpPrevElem = (Elem<T>*) mpPrevElem->Prev();
00082                                 mPrevIndex--;
00083                         } while (value<mpPrevElem->Value());                    
00084                 }
00085         }
00086         return mPrevIndex;
00087 }
00088 
00089 }
00090 
00091 #endif
00092 

Generated on Tue Aug 12 22:33:44 2008 for CLAM by  doxygen 1.5.5