CVR-Lib last update 20 Sep 2009

cvrSort2.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 1998-2005
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  *
00006  * This file is part of the Computer Vision and Robotics Library (CVR-Lib)
00007  *
00008  * The CVR-Lib is free software; you can redistribute it and/or
00009  * modify it under the terms of the BSD License.
00010  *
00011  * All rights reserved.
00012  *
00013  * Redistribution and use in source and binary forms, with or without
00014  * modification, are permitted provided that the following conditions are met:
00015  *
00016  * 1. Redistributions of source code must retain the above copyright notice,
00017  *    this list of conditions and the following disclaimer.
00018  *
00019  * 2. Redistributions in binary form must reproduce the above copyright notice,
00020  *    this list of conditions and the following disclaimer in the documentation
00021  *    and/or other materials provided with the distribution.
00022  *
00023  * 3. Neither the name of the authors nor the names of its contributors may be
00024  *    used to endorse or promote products derived from this software without
00025  *    specific prior written permission.
00026  *
00027  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00028  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00029  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00030  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00031  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00032  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00033  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00034  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00035  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00036  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00037  * POSSIBILITY OF SUCH DAMAGE.
00038  */
00039 
00040 
00041 /**
00042  * \file   cvrSort2.h
00043  *         Sort the elements in a matrix or vector
00044  * \author Pablo Alvarado
00045  * \date   17.08.2000
00046  *
00047  * $Id: cvrSort2.h,v 1.5 2007/10/09 03:10:50 alvarado Exp $
00048  */
00049 
00050 #ifndef _CVR_SORT2_H_
00051 #define _CVR_SORT2_H_
00052 
00053 #include "cvrFunctor.h"
00054 #include "cvrVector.h"
00055 #include "cvrMatrix.h"
00056 #include "cvrPerformanceConfig.h"
00057 #include "cvrSortingOrder.h"
00058 
00059 namespace cvr {
00060 
00061   /**
00062    * Sort two vectors, using the first one as key.
00063    *
00064    * This class is used to sort the elements of two given vectors or
00065    * matrices.  The first one (of type T) contains always the keys to
00066    * be used by sorting, and the second (of type U) one will be sorted
00067    * accordingly to first one.
00068    *
00069    * For example, if you have a second vector of integers, which was
00070    * initialized with the indices (0 for the first element, 1 for the second
00071    * and so on), at the end you can use this sorted vector to know which
00072    * position has an element of the first vector after sorting:
00073    *
00074    * \code
00075    * // the key vector
00076    * const float fdata[] = {3.2, 1.5, 4.2, 2.0};
00077    * cvr::vector<float> a(4,fdata);
00078    *
00079    * // the data vector
00080    * const int idata[] = {0,1,2,3};
00081    * cvr::vector<int> idx(4,idata);
00082    *
00083    * // sorting object:
00084    * sort2<float,int> sorter;
00085    *
00086    * // sort the vector a, and accordingly the vector b
00087    * sorter.apply(a,b);
00088    *
00089    * // after this you will get:
00090    * // a = 1.5, 2.0, 3.2, 4.2
00091    * // b = 1  , 3  , 0  , 2
00092    * \endcode
00093    *
00094    * The sort2::parameters inherit from sort::parameters, and therefore you can
00095    * also here specify the sorting order and the threshold for applying bubble-
00096    * sort.
00097    *
00098    * This functor requires that the type T accept the operator<.
00099    *
00100    * @see cvr::scramble, cvr::sort, cvr::quickPartialSort2
00101    *
00102    * \ingroup gSorting
00103    */
00104   class sort2 : public functor {
00105   public:
00106     /**
00107      * Type used in the specification of the sorting order
00108      * (used when sorting the rows or columns of a matrix using as keys
00109      *  the values of a vector)
00110      */
00111     enum eWhichVectors {
00112       Columns, /*!< sort the columns of the matrix  */
00113       Rows     /*!< sort the rows of the matrix     */
00114     };
00115 
00116     /**
00117      * The parameters for the class sort
00118      */
00119     class parameters : public functor::parameters {
00120     public:
00121 
00122       /**
00123        * Default constructor
00124        */
00125       parameters();
00126 
00127       /**
00128        * Copy constructor
00129        * @param other the parameters object to be copied
00130        */
00131       parameters(const parameters& other);
00132 
00133       /**
00134        * Destructor
00135        */
00136       ~parameters();
00137 
00138       /**
00139        * Copy the contents of a parameters object
00140        * @param other the parameters object to be copied
00141        * @return a reference to this parameters object
00142        */
00143       parameters& copy(const parameters& other);
00144 
00145       /**
00146        * Returns the name of this class.
00147        */
00148       virtual const std::string& name() const;
00149       /**
00150        * Returns a pointer to a clone of the parameters
00151        */
00152       virtual parameters* clone() const;
00153 
00154       /**
00155        * Returns a pointer to a new instance of the parameters
00156        */
00157       virtual parameters* newInstance() const;
00158 
00159       /**
00160        * Read the parameters from the given ioHandler
00161        * @param handler the ioHandler to be used
00162        * @param complete if true (the default) the enclosing begin/end will
00163        *        be also read, otherwise only the data block will be read.
00164        * @return true if write was successful
00165        */
00166       virtual bool read(ioHandler &handler, const bool complete=true);
00167 
00168       /**
00169        * Write the parameters in the given ioHandler
00170        * @param handler the ioHandler to be used
00171        * @param complete if true (the default) the enclosing begin/end will
00172        *        be also written, otherwise only the data block will be
00173        *        written.
00174        * @return true if write was successful
00175        */
00176       virtual bool write(ioHandler& handler,const bool complete=true) const;
00177 
00178       // ---------------------------------------------------------
00179       //  Parameters
00180       // ---------------------------------------------------------
00181 
00182 
00183       /**
00184        * Specify if in the apply(vector<T>,matrix<U>) the rows or the columns
00185        * of the matrix should be sorted.
00186        * The default value is Rows
00187        */
00188       eWhichVectors whichVectors;
00189 
00190       /**
00191        * Order of the sorting
00192        * the default value is ascending order
00193        */
00194       eSortingOrder sortingOrder;
00195 
00196       /**
00197        * For vector/matrices with size smaller than this value, a bubble sort
00198        * will be used (this way is faster)!
00199        *
00200        * The default value is usually 10, but if you configured your
00201        * system for performance this value could change.
00202        *
00203        * The best value can be found in the cvrPerformanceConfig.h file,
00204        * under _CVR_PERFORMANCE_SORT_STOP_QUICKSORT.
00205        *
00206        * Default value: 10 or better.
00207        */
00208       int thresholdForBubble;
00209 
00210     };
00211 
00212 
00213     /**
00214      * Default constructor.
00215      * @param sortingOrder sets the sorting order in the parameters.
00216      *                     Default is Ascending.
00217      * @param whichVectors sets the vectors treated in a matrix.
00218      *                     Default is Rows
00219      */
00220     sort2(const eSortingOrder sortingOrder=Ascending,
00221           const eWhichVectors whichVectors=Rows);
00222 
00223     /**
00224      * Copy constructor
00225      * @param other the object to be copied
00226      */
00227     sort2(const sort2& other);
00228 
00229     /**
00230      * Construct with given parameters
00231      */
00232     sort2(const parameters& par);
00233 
00234     /**
00235      * Destructor
00236      */
00237     virtual ~sort2();
00238 
00239     /**
00240      * Sort all the elements of the matrix.  The elements will be
00241      * ordered row-wise.  For example, the key matrix at the left will
00242      * be sorted into the matrix at the right side:
00243      * \code
00244      *
00245      *  | 2 8 3 |         | 1 2 3 |
00246      *  | 1 4 5 |  --->   | 4 5 6 |
00247      *  | 7 9 6 |         | 7 8 9 |
00248      *
00249      * \endcode
00250      * @param key matrix<T> with the key data.  The result will be
00251      * left here too.
00252      * @param srcdest matrix<U> with the data that will be sorted
00253      * according to the key data
00254      * @return true if successful, false otherwise
00255      */
00256     template <typename T,typename U>
00257     bool apply(matrix<T>& key,matrix<U>& srcdest) const;
00258 
00259     /**
00260      * Operates on the given parameter.
00261      * @param key vector<T> with the key data.  The result
00262      *                      will be left here too.
00263      * @param srcdest vector<U> with the data that will be sorted according
00264      *                          to the key data
00265      * @return true if successful, false otherwise
00266      */
00267     template <typename T,typename U>
00268     bool apply(vector<T>& key,vector<U>& srcdest) const;
00269 
00270     /**
00271      * Sort the rows of the matrix, using as key the elements of the given
00272      * vector.  For example, the matrix at the left side will be sorted in
00273      * the matrix at the right side with the key-vector (5 2 3):
00274      *
00275      * \code
00276      *
00277      *  | 2 8 3 |         | 1 4 5 |
00278      *  | 1 4 5 |  --->   | 7 9 6 |
00279      *  | 7 9 6 |         | 2 8 3 |
00280      *
00281      * \endcode
00282      *
00283      * The number of rows of the matrix must be equal to the number of
00284      * elements in the key vector.
00285      *
00286      * @param key vector<T> with the key data.  The result
00287      *                      will be left here too.
00288      * @param srcdest matrix<U> with the rows that will be sorted according
00289      *                          to the key data
00290      * @return true if successful, false otherwise
00291      */
00292     template <typename T,typename U>
00293     bool apply(vector<T>& key,matrix<U>& srcdest) const;
00294 
00295 
00296     /**
00297      * Sort all the elements of the matrix.  The elements will be
00298      * ordered row-wise.  For example, the key matrix at the left will
00299      * be sorted into the matrix at the right side:
00300      *
00301      * \code
00302      *
00303      *  | 2 8 3 |         | 1 2 3 |
00304      *  | 1 4 5 |  --->   | 4 5 6 |
00305      *  | 7 9 6 |         | 7 8 9 |
00306      *
00307      * \endcode
00308      * @param key matrix<T> with the key data.
00309      * @param src matrix<U> with the source data.
00310      * @param destkey matrix<T> where the sorted keys will be left.
00311      * @param dest matrix<U> where the sorted data (using the key) will
00312      *                       be left.
00313      * @return true if successful, false otherwise
00314      */
00315     template <typename T,typename U>
00316     bool apply(const matrix<T>& key, const matrix<U>& src,
00317                      matrix<T>& destkey,matrix<U>& dest) const;
00318 
00319     /**
00320      * Operates on a copy of the given parameters.
00321      * @param key vector<T> with the key data.
00322      * @param src vector<U> with the source data.
00323      * @param destkey vector<T> where the sorted keys will be left.
00324      * @param dest vector<U> where the sorted data (using the key) will
00325      *                       be left.
00326      * @return true if successful, false otherwise
00327      */
00328     template <typename T,typename U>
00329     bool apply(const vector<T>& key,const vector<U>& src,
00330                      vector<T>& destkey, vector<U>& dest) const;
00331 
00332     /**
00333      * Sort the rows of the matrix, using as key the elements of the given
00334      * vector.  For example, the matrix at the left side will be sorted in
00335      * the matrix at the right side with the key-vector (5 2 3):
00336      *
00337      * \code
00338      *
00339      *  | 2 8 3 |         | 1 4 5 |
00340      *  | 1 4 5 |  --->   | 7 9 6 |
00341      *  | 7 9 6 |         | 2 8 3 |
00342      *
00343      * \endcode
00344      *
00345      * The number of rows of the matrix must be equal to the number of
00346      * elements in the key vector.
00347      *
00348      * @param key vector<T> with the key data.
00349      * @param src matrix<U> with the rows that will be sorted according
00350      *                          to the key data
00351      * @param destkey the sorted key-vector
00352      * @param dest the matrix with sorted rows.
00353      * @return true if successful, false otherwise
00354      */
00355     template <typename T,typename U>
00356     bool apply(const vector<T>& key,const matrix<U>& src,
00357                      vector<T>& destkey, matrix<U>& dest) const;
00358 
00359     /**
00360      * Copy data of "other" functor.
00361      * @param other the functor to be copied
00362      * @return a reference to this functor object
00363      */
00364     sort2& copy(const sort2& other);
00365 
00366     /**
00367      * Returns the name of this class.
00368      */
00369     const std::string& name() const;
00370 
00371     /**
00372      * Returns a pointer to a clone of this functor.
00373      */
00374     virtual sort2* clone() const;
00375 
00376     /**
00377      * Returns a pointer to a new instance of this functor.
00378      */
00379     virtual sort2* newInstance() const;
00380 
00381     /**
00382      * Returns used parameters
00383      */
00384     const parameters& getParameters() const;
00385 
00386 
00387   protected:
00388 
00389     template <typename T,typename U>
00390     void quicksort(vector<T>& vct,vector<U>& vct2,
00391                    const int begin,
00392                    const int end) const;
00393 
00394     template <typename T,typename U>
00395     int partitionAsc(vector<T>& vct,vector<U>& vct2,
00396                      const int begin,
00397                      const int end) const;
00398 
00399     template <typename T,typename U>
00400     void insertionsortAsc(vector<T>& vct,vector<U>& vct2,
00401                           const int begin,
00402                           const int end) const;
00403 
00404     template <typename T,typename U>
00405     int partitionDesc(vector<T>& vct,vector<U>& vct2,
00406                       const int begin,
00407                       const int end) const;
00408 
00409     template <typename T,typename U>
00410     void insertionsortDesc(      vector<T>& vct,
00411                                  vector<U>& vct2,
00412                            const int begin,
00413                            const int end) const;
00414 
00415     template <typename U>
00416     void reorder(const ivector& indices,
00417                  const matrix<U>& src,
00418                        matrix<U>& dest) const;
00419 
00420   };
00421 
00422 }
00423 
00424 #include "cvrSort2_template.h"
00425 
00426 #endif

Generated on Sun Sep 20 22:08:00 2009 for CVR-Lib by Doxygen 1.5.8