LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiSort.h

00001 /*
00002  * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiSort.h
00027  * authors ....: Pablo Alvarado
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 17.8.2000
00030  * revisions ..: $Id: ltiSort.h,v 1.10 2006/02/08 12:44:52 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_SORT_H_
00034 #define _LTI_SORT_H_
00035 
00036 #include "ltiFunctor.h"
00037 #include "ltiVector.h"
00038 #include "ltiMatrix.h"
00039 #include "ltiPerformanceConfig.h"
00040 
00041 namespace lti {
00042 
00043   /**
00044    * Sort vectors.
00045    *
00046    * This class is used to sort the elements of a given vector or matrix.
00047    *
00048    * The sort::parameters::order specify if the elements should be sorted in
00049    * ascending or descending order.
00050    *
00051    * This functor requires that the type T accept the operator<.
00052    *
00053    * @see lti::scramble, lti::sort2, lti::quickPartialSort
00054    *
00055    * The functor uses the well-known quick-sort algorithm to sort the elements
00056    * of the vector.  Since the overhead of the recursion makes at some point
00057    * the quick-sort more innefficient than simpler algorithms, you can specify
00058    * in the parameters for which size the vectors should be sorted with 
00059    * the bubble-sort algorithm.
00060    *
00061    * The quick-sort is not "stable", this means that elements with the same
00062    * key value can change their positions in the vector.
00063    *
00064    * You should also revise the STL algorithms std::sort() if you are using 
00065    * containers of the STL.
00066    */
00067   template <class T>
00068   class sort : public functor {
00069   public:
00070 
00071     /**
00072      * The parameters for the class sort
00073      */
00074     class parameters : public functor::parameters {
00075     public:
00076       /**
00077        * Type used in the specification of the sorting order
00078        */
00079       enum eOrder {
00080         Ascending, /*!< ascending order  */
00081         Descending /*!< descending order */
00082       };
00083 
00084       /**
00085        * Default constructor
00086        */
00087       parameters()
00088         : functor::parameters() {
00089 
00090         thresholdForBubble = int(_LTI_PERFORMANCE_SORT_STOP_QUICKSORT);
00091         order = Ascending;
00092       };
00093 
00094       /**
00095        * Copy constructor
00096        * @param other the parameters object to be copied
00097        */
00098       parameters(const parameters& other) 
00099         : functor::parameters() {
00100         copy(other);
00101       };
00102 
00103       /**
00104        * Destructor
00105        */
00106       ~parameters() {};
00107 
00108       /**
00109        * Returns name of this type
00110        */
00111       const char* getTypeName() const {
00112         return "sort<T>::parameters";
00113       };
00114 
00115       /**
00116        * Copy the contents of a parameters object
00117        * @param other the parameters object to be copied
00118        * @return a reference to this parameters object
00119        */
00120       parameters& copy(const parameters& other) {
00121 #     ifndef _LTI_MSC_6
00122         // MS Visual C++ 6 is not able to compile this...
00123         functor::parameters::copy(other);
00124 #     else
00125         // ...so we have to use this workaround.
00126         // Conditional on that, copy may not be virtual.
00127         functor::parameters& (functor::parameters::* p_copy)
00128          (const functor::parameters&) =
00129          functor::parameters::copy;
00130         (this->*p_copy)(other);
00131 #     endif
00132 
00133         thresholdForBubble = other.thresholdForBubble;
00134         order = other.order;
00135 
00136         return *this;
00137       };
00138 
00139       /**
00140        * Returns a pointer to a clone of the parameters
00141        */
00142       virtual functor::parameters* clone() const {
00143         return new parameters(*this);
00144       };
00145 
00146 #     ifndef _LTI_MSC_6
00147       /**
00148        * Read the parameters from the given ioHandler
00149        * @param handler the ioHandler to be used
00150        * @param complete if true (the default) the enclosing begin/end will
00151        *        be also read, otherwise only the data block will be read.
00152        * @return true if write was successful
00153        */
00154       virtual bool read(ioHandler &handler, const bool complete=true)
00155 #     else
00156       bool readMS(ioHandler& handler,const bool complete=true)
00157 #     endif
00158       {
00159         bool b=true;
00160 
00161         if (complete) {
00162           b=handler.readBegin();
00163         }
00164 
00165         if (b) {
00166           lti::read(handler,"thresholdForBubble",thresholdForBubble);
00167 
00168           std::string str;
00169           lti::read(handler,"order",str);
00170           if (str == "Ascending") {
00171             order = Ascending;
00172           } else {
00173             order = Descending;
00174           }
00175         }
00176 
00177 #     ifndef _LTI_MSC_6
00178         // This is the standard C++ code, which MS Visual C++ 6 is not
00179         // able to compile...
00180         b = b && functor::parameters::read(handler,false);
00181 #     else
00182         bool (functor::parameters::* p_readMS)(ioHandler&,const bool) =
00183           functor::parameters::readMS;
00184         b = b && (this->*p_readMS)(handler,false);
00185 #     endif
00186 
00187         if (complete) {
00188           b=b && handler.readEnd();
00189         }
00190 
00191         return b;
00192       }
00193 
00194 #     ifdef _LTI_MSC_6
00195       /**
00196        * Read the parameters from the given ioHandler
00197        * @param handler the ioHandler to be used
00198        * @param complete if true (the default) the enclosing begin/end will
00199        *        be also read, otherwise only the data block will be read.
00200        * @return true if write was successful
00201        */
00202       virtual bool read(ioHandler& handler,const bool complete=true) {
00203         // ...we need this workaround to cope with another really awful MSVC
00204         // bug.
00205         return readMS(handler,complete);
00206       }
00207 #     endif
00208 
00209 #     ifndef _LTI_MSC_6
00210       /**
00211        * Write the parameters in the given ioHandler
00212        * @param handler the ioHandler to be used
00213        * @param complete if true (the default) the enclosing begin/end will
00214        *        be also written, otherwise only the data block will be
00215        *        written.
00216        * @return true if write was successful
00217        */
00218       virtual bool write(ioHandler& handler,const bool complete=true) const
00219 #     else
00220       bool writeMS(ioHandler& handler,const bool complete=true) const
00221 #     endif
00222       {
00223         bool b=true;
00224 
00225         if (complete) {
00226           b=handler.writeBegin();
00227         }
00228 
00229         if (b) {
00230           lti::write(handler,"thresholdForBubble",thresholdForBubble);
00231           if (order == Ascending) {
00232             lti::write(handler,"order","Ascending");
00233           } else {
00234             lti::write(handler,"order","Descending");
00235           }
00236         }
00237 
00238 #       ifndef _LTI_MSC_6
00239         // This is the standard C++ code, which MS Visual C++ 6 is not
00240         // able to compile...
00241         b = b && functor::parameters::write(handler,false);
00242 #       else
00243         bool (functor::parameters::* p_writeMS)(ioHandler&,const bool) const =
00244           functor::parameters::writeMS;
00245         b = b && (this->*p_writeMS)(handler,false);
00246 #       endif
00247 
00248         if (complete) {
00249           b=b && handler.writeEnd();
00250         }
00251 
00252         return b;
00253       }
00254 
00255 #     ifdef _LTI_MSC_6
00256       /**
00257        * Write the parameters to the given ioHandler
00258        * @param handler the ioHandler to be used
00259        * @param complete if true (the default) the enclosing begin/end will
00260        *        be also writen, otherwise only the data block will be writen.
00261        * @return true if write was successful
00262        */
00263       virtual bool write(ioHandler& handler,const bool complete=true) {
00264         // ...we need this workaround to cope with another really awful MSVC
00265         // bug.
00266         return writeMS(handler,complete);
00267       }
00268 #     endif
00269 
00270       // ---------------------------------------------------------
00271       //  Parameters
00272       // ---------------------------------------------------------
00273 
00274 
00275       /**
00276        * For vector/matrices with size smaller than this value, a bubble sort
00277        * will be used (this way is faster)!
00278        *
00279        * The default value is usually 10, but if you configured your
00280        * system for performance this value could change.
00281        *
00282        * The best value can be found in the ltiPerformanceConfig.h file,
00283        * under _LTI_PERFORMANCE_SORT_STOP_QUICKSORT.
00284        *
00285        * Default value: 10 or better.
00286        */
00287       int thresholdForBubble;
00288 
00289       /**
00290        * Order of the sorting
00291        * the default value is ascending order
00292        */
00293       eOrder order;
00294     };
00295 
00296     /**
00297      * Default constructor
00298      */
00299     sort(const bool& descendingOrder = false);
00300 
00301     /**
00302      * Construct with given parameters
00303      */
00304     sort(const parameters& par);
00305 
00306     /**
00307      * Copy constructor
00308      * @param other the object to be copied
00309      */
00310     sort(const sort<T>& other);
00311 
00312     /**
00313      * Destructor
00314      */
00315     virtual ~sort();
00316 
00317     /**
00318      * Returns the name of this type ("sort")
00319      */
00320     virtual const char* getTypeName() const;
00321 
00322     /**
00323      * Sort all the elements of the matrix.  The elements will be
00324      * ordered row-wise.  For example, the matrix at the left will
00325      * be sorted into the matrix at the right side:
00326      * \code
00327      *
00328      *  | 2 8 3 |         | 1 2 3 |
00329      *  | 1 4 5 |  --->   | 4 5 6 |
00330      *  | 7 9 6 |         | 7 8 9 |
00331      *
00332      * \endcode
00333      * @param srcdest matrix<T> with the source data.  The result
00334      *                 will be left here too.
00335      * @return true if successful, false otherwise
00336      */
00337     virtual bool apply(matrix<T>& srcdest) const;
00338 
00339     /**
00340      * Operates on the given parameter.
00341      * @param srcdest vector<T> with the source data.  The result
00342      *                 will be left here too.
00343      * @return true if successful, false otherwise
00344      */
00345     virtual bool apply(vector<T>& srcdest) const;
00346 
00347     /**
00348      * Sort all the elements of the matrix.  The elements will be
00349      * ordered row-wise.  For example, the matrix at the left will
00350      * be sorted into the matrix at the right side:
00351      * \code
00352      *
00353      *  | 2 8 3 |         | 1 2 3 |
00354      *  | 1 4 5 |  --->   | 4 5 6 |
00355      *  | 7 9 6 |         | 7 8 9 |
00356      *
00357      * \endcode
00358      * @param src matrix<T> with the source data.
00359      * @param dest matrix<T> where the result will be left.
00360      * @return true if successful, false otherwise
00361      */
00362     virtual bool apply(const matrix<T>& src,matrix<T>& dest) const;
00363 
00364     /**
00365      * Operates on a copy of the given parameters.
00366      * @param src vector<T> with the source data.
00367      * @param dest vector<T> where the result will be left.
00368      * @return true if successful, false otherwise
00369      */
00370     virtual bool apply(const vector<T>& src,vector<T>& dest) const;
00371 
00372     /**
00373      * Copy data of "other" functor.
00374      * @param other the functor to be copied
00375      * @return a reference to this functor object
00376      */
00377     sort& copy(const sort& other);
00378 
00379     /**
00380      * Returns a pointer to a clone of this functor.
00381      */
00382     virtual functor* clone() const;
00383 
00384     /**
00385      * Returns used parameters
00386      */
00387     const parameters& getParameters() const;
00388 
00389     /**
00390      * Set parameters
00391      */
00392     bool updateParameters();
00393 
00394   protected:
00395     void quicksort(vector<T>& vct,
00396                    const int& begin,
00397                    const int& end) const;
00398     int partitionAsc(vector<T>& vct,
00399                      const int& begin,
00400                      const int& end) const;
00401     void insertionsortAsc(vector<T>& vct,
00402                           const int& begin,
00403                           const int& end) const;
00404     int partitionDesc(vector<T>& vct,
00405                       const int& begin,
00406                       const int& end) const;
00407     void insertionsortDesc(vector<T>& vct,
00408                            const int& begin,
00409                            const int& end) const;
00410 
00411     /**
00412      * @name Shadows of the parameters to avoid a function access
00413      */
00414     //@{
00415     int thresholdForBubble;
00416     typename parameters::eOrder order;
00417     //@}
00418       
00419   };
00420 
00421   /**
00422    * Sort two vectors, using the first one as key.
00423    *
00424    * This class is used to sort the elements of two given vectors or
00425    * matrices.  The first one (of type T) contains always the keys to
00426    * be used by sorting, and the second (of type U) one will be sorted
00427    * accordingly to first one. 
00428    *
00429    * For example, if you have a second vector of integers, which was
00430    * initialized with the indices (0 for the first element, 1 for the second
00431    * and so on), at the end you can use this sorted vector to know which
00432    * position has an element of the first vector after sorting:
00433    *
00434    * \code
00435    * // the key vector
00436    * const float fdata[] = {3.2, 1.5, 4.2, 2.0};
00437    * lti::vector<float> a(4,fdata);
00438    *
00439    * // the data vector
00440    * const int idata[] = {0,1,2,3};
00441    * lti::vector<int> idx(4,idata);
00442    *
00443    * // sorting object:
00444    * sort2<float,int> sorter;
00445    *
00446    * // sort the vector a, and accordingly the vector b
00447    * sorter.apply(a,b);
00448    *
00449    * // after this you will get:
00450    * // a = 1.5, 2.0, 3.2, 4.2
00451    * // b = 1  , 3  , 0  , 2
00452    * \endcode
00453    *
00454    * The sort2::parameters inherit from sort::parameters, and therefore you can
00455    * also here specify the sorting order and the threshold for applying bubble-
00456    * sort.
00457    *
00458    * This functor requires that the type T accept the operator<.
00459    *
00460    * @see lti::scramble, lti::sort, lti::quickPartialSort2
00461    */
00462   template <class T,class U = int>
00463   class sort2 : public sort<T> {
00464   public:
00465     /**
00466      * The parameters for the class sort
00467      */
00468     class parameters : public sort<T>::parameters {
00469     public:
00470       /**
00471        * Type used in the specification of the sorting order
00472        * (used when sorting the rows or columns of a matrix using as keys
00473        *  the values of a vector)
00474        */
00475       enum eWhichVectors {
00476         Columns, /*!< sort the columns of the matrix  */
00477         Rows     /*!< sort the rows of the matrix     */
00478       };
00479 
00480       /**
00481        * Default constructor
00482        */
00483       parameters()
00484         : sort<T>::parameters() {
00485 
00486         whichVectors = Rows;
00487       };
00488 
00489       /**
00490        * Copy constructor
00491        * @param other the parameters object to be copied
00492        */
00493       parameters(const parameters& other) 
00494         : sort<T>::parameters() {
00495         copy(other);
00496       };
00497 
00498       /**
00499        * Destructor
00500        */
00501       ~parameters() {};
00502 
00503       /**
00504        * Returns name of this type
00505        */
00506       const char* getTypeName() const {
00507         return "sort2<T>::parameters";
00508       };
00509 
00510       /**
00511        * Copy the contents of a parameters object
00512        * @param other the parameters object to be copied
00513        * @return a reference to this parameters object
00514        */
00515       parameters& copy(const parameters& other) {
00516 #     ifndef _LTI_MSC_6
00517         // MS Visual C++ 6 is not able to compile this...
00518         sort<T>::parameters::copy(other);
00519 #     else
00520         // ...so we have to use this workaround.
00521         // Conditional on that, copy may not be virtual.
00522         sort<T>::parameters& (sort<T>::parameters::* p_copy)
00523           (const sort<T>::parameters&) = sort<T>::parameters::copy;
00524         (this->*p_copy)(other);
00525 #     endif
00526 
00527         whichVectors = other.whichVectors;
00528 
00529         return *this;
00530       };
00531 
00532       /**
00533        * Returns a pointer to a clone of the parameters
00534        */
00535       virtual functor::parameters* clone() const {
00536         return new parameters(*this);
00537       };
00538 
00539 #     ifndef _LTI_MSC_6
00540       /**
00541        * Read the parameters from the given ioHandler
00542        * @param handler the ioHandler to be used
00543        * @param complete if true (the default) the enclosing begin/end will
00544        *        be also read, otherwise only the data block will be read.
00545        * @return true if write was successful
00546        */
00547       virtual bool read(ioHandler &handler, const bool complete=true)
00548 #     else
00549       bool readMS(ioHandler& handler,const bool complete=true)
00550 #     endif
00551       {
00552         bool b=true;
00553 
00554         if (complete) {
00555           b=handler.readBegin();
00556         }
00557 
00558         if (b) {
00559           std::string str;
00560           lti::read(handler,"whichVectors",str);
00561           if (str == "Columns") {
00562             whichVectors = Columns;
00563           } else {
00564             whichVectors = Rows;
00565           }
00566         }
00567 
00568 #     ifndef _LTI_MSC_6
00569         // This is the standard C++ code, which MS Visual C++ 6 is not
00570         // able to compile...
00571         b = b && sort<T>::parameters::read(handler,false);
00572 #     else
00573         bool (sort<T>::parameters::* p_readMS)(ioHandler&,const bool) =
00574           sort<T>::parameters::readMS;
00575         b = b && (this->*p_readMS)(handler,false);
00576 #     endif
00577 
00578         if (complete) {
00579           b=b && handler.readEnd();
00580         }
00581 
00582         return b;
00583       }
00584 
00585 #     ifdef _LTI_MSC_6
00586       /**
00587        * Read the parameters from the given ioHandler
00588        * @param handler the ioHandler to be used
00589        * @param complete if true (the default) the enclosing begin/end will
00590        *        be also read, otherwise only the data block will be read.
00591        * @return true if write was successful
00592        */
00593       virtual bool read(ioHandler& handler,const bool complete=true) {
00594         // ...we need this workaround to cope with another really awful MSVC
00595         // bug.
00596         return readMS(handler,complete);
00597       }
00598 #     endif
00599 
00600 #     ifndef _LTI_MSC_6
00601       /**
00602        * Write the parameters in the given ioHandler
00603        * @param handler the ioHandler to be used
00604        * @param complete if true (the default) the enclosing begin/end will
00605        *        be also written, otherwise only the data block will be
00606        *        written.
00607        * @return true if write was successful
00608        */
00609       virtual bool write(ioHandler& handler,const bool complete=true) const
00610 #     else
00611       bool writeMS(ioHandler& handler,const bool complete=true) const
00612 #     endif
00613       {
00614         bool b=true;
00615 
00616         if (complete) {
00617           b=handler.writeBegin();
00618         }
00619 
00620         if (b) {
00621           if (whichVectors == Columns) {
00622             lti::write(handler,"whichVectors","Columns");
00623           } else {
00624             lti::write(handler,"whichVectors","Rows");
00625           }
00626         }
00627 
00628 #       ifndef _LTI_MSC_6
00629         // This is the standard C++ code, which MS Visual C++ 6 is not
00630         // able to compile...
00631         b = b && sort<T>::parameters::write(handler,false);
00632 #       else
00633         bool (sort<T>::parameters::* p_writeMS)(ioHandler&,const bool) const =
00634           sort<T>::parameters::writeMS;
00635         b = b && (this->*p_writeMS)(handler,false);
00636 #       endif
00637 
00638         if (complete) {
00639           b=b && handler.writeEnd();
00640         }
00641 
00642         return b;
00643       }
00644 
00645 #     ifdef _LTI_MSC_6
00646       /**
00647        * Write the parameters to the given ioHandler
00648        * @param handler the ioHandler to be used
00649        * @param complete if true (the default) the enclosing begin/end will
00650        *        be also writen, otherwise only the data block will be writen.
00651        * @return true if write was successful
00652        */
00653       virtual bool write(ioHandler& handler,const bool complete=true) {
00654         // ...we need this workaround to cope with another really awful MSVC
00655         // bug.
00656         return writeMS(handler,complete);
00657       }
00658 #     endif
00659 
00660       // ---------------------------------------------------------
00661       //  Parameters
00662       // ---------------------------------------------------------
00663 
00664 
00665       /**
00666        * Specify if in the apply(vector<T>,matrix<U>) the rows or the columns
00667        * of the matrix should be sorted.
00668        * The default value is Rows
00669        */
00670       eWhichVectors whichVectors;
00671 
00672     };
00673 
00674 
00675     /**
00676      * Default constructor.
00677      * @param descendingOrder if true, the vector/matrix will be sorted
00678      *                        in descending order.  If false, in ascending
00679      *                        order.
00680      * @param sortRows used in apply(vector,matrix).  If true, then the
00681      *                        rows of the matrix are sorted using vector as
00682      *                        a key.  Otherwise the columns of the matrix
00683      *                        will be sorted.
00684      */
00685     sort2(const bool& descendingOrder=false,const bool& sortRows=true);
00686 
00687     /**
00688      * Copy constructor
00689      * @param other the object to be copied
00690      */
00691     sort2(const sort2<T,U>& other);
00692 
00693     /**
00694      * Construct with given parameters
00695      */
00696     sort2(const parameters& par);
00697 
00698     /**
00699      * Destructor
00700      */
00701     virtual ~sort2();
00702 
00703     /**
00704      * Returns the name of this type ("sort2")
00705      */
00706     virtual const char* getTypeName() const;
00707 
00708     /**
00709      * Sort all the elements of the matrix.  The elements will be
00710      * ordered row-wise.  For example, the key matrix at the left will
00711      * be sorted into the matrix at the right side:
00712      * \code
00713      *
00714      *  | 2 8 3 |         | 1 2 3 |
00715      *  | 1 4 5 |  --->   | 4 5 6 |
00716      *  | 7 9 6 |         | 7 8 9 |
00717      *
00718      * \endcode
00719      * @param key matrix<T> with the key data.  The result will be
00720      * left here too.
00721      * @param srcdest matrix<U> with the data that will be sorted
00722      * according to the key data
00723      * @return true if successful, false otherwise
00724      */
00725     bool apply(matrix<T>& key,matrix<U>& srcdest) const;
00726 
00727     /**
00728      * Operates on the given parameter.
00729      * @param key vector<T> with the key data.  The result
00730      *                      will be left here too.
00731      * @param srcdest vector<U> with the data that will be sorted according
00732      *                          to the key data
00733      * @return true if successful, false otherwise
00734      */
00735     bool apply(vector<T>& key,vector<U>& srcdest) const;
00736 
00737     /**
00738      * Sort the rows of the matrix, using as key the elements of the given
00739      * vector.  For example, the matrix at the left side will be sorted in
00740      * the matrix at the right side with the key-vector (5 2 3):
00741      *
00742      * \code
00743      *
00744      *  | 2 8 3 |         | 1 4 5 |
00745      *  | 1 4 5 |  --->   | 7 9 6 |
00746      *  | 7 9 6 |         | 2 8 3 |
00747      *
00748      * \endcode
00749      *
00750      * The number of rows of the matrix must be equal to the number of
00751      * elements in the key vector.
00752      *
00753      * @param key vector<T> with the key data.  The result
00754      *                      will be left here too.
00755      * @param srcdest matrix<U> with the rows that will be sorted according
00756      *                          to the key data
00757      * @return true if successful, false otherwise
00758      */
00759     bool apply(vector<T>& key,matrix<U>& srcdest) const;
00760 
00761 
00762     /**
00763      * Sort all the elements of the matrix.  The elements will be
00764      * ordered row-wise.  For example, the key matrix at the left will
00765      * be sorted into the matrix at the right side:
00766      *
00767      * \code
00768      *
00769      *  | 2 8 3 |         | 1 2 3 |
00770      *  | 1 4 5 |  --->   | 4 5 6 |
00771      *  | 7 9 6 |         | 7 8 9 |
00772      *
00773      * \endcode
00774      * @param key matrix<T> with the key data.
00775      * @param src matrix<U> with the source data.
00776      * @param destkey matrix<T> where the sorted keys will be left.
00777      * @param dest matrix<U> where the sorted data (using the key) will
00778      *                       be left.
00779      * @return true if successful, false otherwise
00780      */
00781     bool apply(const matrix<T>& key, const matrix<U>& src,
00782                      matrix<T>& destkey,matrix<U>& dest) const;
00783 
00784     /**
00785      * Operates on a copy of the given parameters.
00786      * @param key vector<T> with the key data.
00787      * @param src vector<U> with the source data.
00788      * @param destkey vector<T> where the sorted keys will be left.
00789      * @param dest vector<U> where the sorted data (using the key) will
00790      *                       be left.
00791      * @return true if successful, false otherwise
00792      */
00793     bool apply(const vector<T>& key,const vector<U>& src,
00794                      vector<T>& destkey, vector<U>& dest) const;
00795 
00796     /**
00797      * Sort the rows of the matrix, using as key the elements of the given
00798      * vector.  For example, the matrix at the left side will be sorted in
00799      * the matrix at the right side with the key-vector (5 2 3):
00800      *
00801      * \code
00802      *
00803      *  | 2 8 3 |         | 1 4 5 |
00804      *  | 1 4 5 |  --->   | 7 9 6 |
00805      *  | 7 9 6 |         | 2 8 3 |
00806      *
00807      * \endcode
00808      *
00809      * The number of rows of the matrix must be equal to the number of
00810      * elements in the key vector.
00811      *
00812      * @param key vector<T> with the key data.
00813      * @param src matrix<U> with the rows that will be sorted according
00814      *                          to the key data
00815      * @param destkey the sorted key-vector
00816      * @param dest the matrix with sorted rows.
00817      * @return true if successful, false otherwise
00818      */
00819     bool apply(const vector<T>& key,const matrix<U>& src,
00820                      vector<T>& destkey, matrix<U>& dest) const;
00821 
00822     /**
00823      * Copy data of "other" functor.
00824      * @param other the functor to be copied
00825      * @return a reference to this functor object
00826      */
00827     sort2& copy(const sort2& other);
00828 
00829     /**
00830      * Returns a pointer to a clone of this functor.
00831      */
00832     virtual functor* clone() const;
00833 
00834     /**
00835      * Returns used parameters
00836      */
00837     const parameters& getParameters() const;
00838 
00839 
00840   protected:
00841     void quicksort(vector<T>& vct,vector<U>& vct2,
00842                    const int& begin,
00843                    const int& end) const;
00844     int partitionAsc(vector<T>& vct,vector<U>& vct2,
00845                      const int& begin,
00846                      const int& end) const;
00847     void insertionsortAsc(vector<T>& vct,vector<U>& vct2,
00848                           const int& begin,
00849                           const int& end) const;
00850     int partitionDesc(vector<T>& vct,vector<U>& vct2,
00851                       const int& begin,
00852                       const int& end) const;
00853     void insertionsortDesc(vector<T>& vct,vector<U>& vct2,
00854                            const int& begin,
00855                            const int& end) const;
00856     void reorder(const ivector& indices,
00857                  const matrix<U>& src,
00858                        matrix<U>& dest) const;
00859   };
00860 
00861 
00862 }
00863 
00864 #include "ltiSort_template.h"
00865 
00866 #endif

Generated on Sat Apr 10 15:26:14 2010 for LTI-Lib by Doxygen 1.6.1