last update 20 Sep 2009 |
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 cvrSort.h 00043 * Sort the elements in a matrix or vector 00044 * \author Pablo Alvarado 00045 * \date 17.08.2000 00046 * 00047 * $Id: cvrSort.h,v 1.10 2007/10/09 03:10:51 alvarado Exp $ 00048 */ 00049 00050 #ifndef _CVR_SORT_H_ 00051 #define _CVR_SORT_H_ 00052 00053 #include "cvrSortingOrder.h" 00054 #include "cvrFunctor.h" 00055 #include "cvrVector.h" 00056 #include "cvrMatrix.h" 00057 #include "cvrPerformanceConfig.h" 00058 00059 namespace cvr { 00060 00061 /** 00062 * Sort vectors. 00063 * 00064 * This class is used to sort the elements of a given vector or matrix. 00065 * 00066 * The sort::parameters::order specify if the elements should be sorted in 00067 * ascending or descending order. 00068 * 00069 * This functor requires that the type T accept the operator<. 00070 * 00071 * @see cvr::scramble, cvr::sort2, cvr::quickPartialSort 00072 * 00073 * The functor uses the well-known quick-sort algorithm to sort the elements 00074 * of the vector. Since the overhead of the recursion makes at some point 00075 * the quick-sort more innefficient than simpler algorithms, you can specify 00076 * in the parameters for which size the vectors should be sorted with 00077 * the bubble-sort algorithm. 00078 * 00079 * The quick-sort is not "stable", this means that elements with the same 00080 * key value can change their positions in the vector. 00081 * 00082 * You should also revise the STL algorithms std::sort() if you are using 00083 * containers of the STL. 00084 * 00085 * \ingroup gSorting 00086 */ 00087 class sort : public functor { 00088 public: 00089 00090 /** 00091 * The parameters for the class sort 00092 */ 00093 class parameters : public functor::parameters { 00094 public: 00095 00096 /** 00097 * Default constructor 00098 */ 00099 parameters(); 00100 00101 /** 00102 * Copy constructor 00103 * @param other the parameters object to be copied 00104 */ 00105 parameters(const parameters& other); 00106 00107 /** 00108 * Destructor 00109 */ 00110 ~parameters(); 00111 00112 /** 00113 * Copy the contents of a parameters object 00114 * @param other the parameters object to be copied 00115 * @return a reference to this parameters object 00116 */ 00117 parameters& copy(const parameters& other); 00118 00119 /** 00120 * Returns the name of this class. 00121 */ 00122 const std::string& name() const; 00123 00124 /** 00125 * Returns a pointer to a clone of the parameters 00126 */ 00127 virtual parameters* clone() const; 00128 00129 /** 00130 * Returns a pointer to a new instance of the parameters 00131 */ 00132 virtual parameters* newInstance() const; 00133 00134 /** 00135 * Read the parameters from the given ioHandler 00136 * @param handler the ioHandler to be used 00137 * @param complete if true (the default) the enclosing begin/end will 00138 * be also read, otherwise only the data block will be read. 00139 * @return true if write was successful 00140 */ 00141 virtual bool read(ioHandler &handler, const bool complete=true); 00142 00143 00144 /** 00145 * Write the parameters in the given ioHandler 00146 * @param handler the ioHandler to be used 00147 * @param complete if true (the default) the enclosing begin/end will 00148 * be also written, otherwise only the data block will be 00149 * written. 00150 * @return true if write was successful 00151 */ 00152 virtual bool write(ioHandler& handler,const bool complete=true) const; 00153 00154 00155 // --------------------------------------------------------- 00156 // Parameters 00157 // --------------------------------------------------------- 00158 00159 00160 /** 00161 * For vector/matrices with size smaller than this value, a bubble sort 00162 * will be used (this way is faster)! 00163 * 00164 * The default value is usually 10, but if you configured your 00165 * system for performance this value could change. 00166 * 00167 * The best value can be found in the cvrPerformanceConfig.h file, 00168 * under _CVR_PERFORMANCE_SORT_STOP_QUICKSORT. 00169 * 00170 * Default value: 10 or better. 00171 */ 00172 int thresholdForBubble; 00173 00174 /** 00175 * Order of the sorting 00176 * 00177 * Default: Ascending 00178 */ 00179 eSortingOrder sortingOrder; 00180 }; 00181 00182 /** 00183 * Default constructor 00184 */ 00185 sort(const eSortingOrder sortingOrder=Ascending); 00186 00187 /** 00188 * Construct with given parameters 00189 */ 00190 sort(const parameters& par); 00191 00192 /** 00193 * Copy constructor 00194 * @param other the object to be copied 00195 */ 00196 sort(const sort& other); 00197 00198 /** 00199 * Destructor 00200 */ 00201 virtual ~sort(); 00202 00203 /** 00204 * Sort all the elements of the matrix. The elements will be 00205 * ordered row-wise. For example, the matrix at the left will 00206 * be sorted into the matrix at the right side: 00207 * \code 00208 * 00209 * | 2 8 3 | | 1 2 3 | 00210 * | 1 4 5 | ---> | 4 5 6 | 00211 * | 7 9 6 | | 7 8 9 | 00212 * 00213 * \endcode 00214 * @param srcdest matrix<T> with the source data. The result 00215 * will be left here too. 00216 * @return true if successful, false otherwise 00217 */ 00218 template <class T> 00219 bool apply(matrix<T>& srcdest) const; 00220 00221 /** 00222 * Operates on the given parameter. 00223 * @param srcdest vector<T> with the source data. The result 00224 * will be left here too. 00225 * @return true if successful, false otherwise 00226 */ 00227 template <class T> 00228 bool apply(vector<T>& srcdest) const; 00229 00230 /** 00231 * Sort all the elements of the matrix. The elements will be 00232 * ordered row-wise. For example, the matrix at the left will 00233 * be sorted into the matrix at the right side: 00234 * \code 00235 * 00236 * | 2 8 3 | | 1 2 3 | 00237 * | 1 4 5 | ---> | 4 5 6 | 00238 * | 7 9 6 | | 7 8 9 | 00239 * 00240 * \endcode 00241 * @param src matrix<T> with the source data. 00242 * @param dest matrix<T> where the result will be left. 00243 * @return true if successful, false otherwise 00244 */ 00245 template <class T> 00246 bool apply(const matrix<T>& src,matrix<T>& dest) const; 00247 00248 /** 00249 * Operates on a copy of the given parameters. 00250 * @param src vector<T> with the source data. 00251 * @param dest vector<T> where the result will be left. 00252 * @return true if successful, false otherwise 00253 */ 00254 template <class T> 00255 bool apply(const vector<T>& src,vector<T>& dest) const; 00256 00257 /** 00258 * Copy data of "other" functor. 00259 * @param other the functor to be copied 00260 * @return a reference to this functor object 00261 */ 00262 sort& copy(const sort& other); 00263 00264 /** 00265 * Returns the name of this class. 00266 */ 00267 virtual const std::string& name() const; 00268 00269 /** 00270 * Returns a pointer to a clone of this functor. 00271 */ 00272 virtual sort* clone() const; 00273 00274 /** 00275 * Returns a pointer to a new instance of this functor. 00276 */ 00277 virtual sort* newInstance() const; 00278 00279 /** 00280 * Returns used parameters 00281 */ 00282 const parameters& getParameters() const; 00283 00284 /** 00285 * Set parameters 00286 */ 00287 bool updateParameters(); 00288 00289 private: 00290 /** 00291 * Quick sort entry point 00292 */ 00293 template <class T> 00294 void quicksort(vector<T>& vct, 00295 const int begin, 00296 const int end) const; 00297 /** 00298 * Partition vector for ascending order 00299 */ 00300 template <class T> 00301 int partitionAsc(vector<T>& vct, 00302 const int begin, 00303 const int end) const; 00304 /** 00305 * Insertion sort for ascending order 00306 */ 00307 template <class T> 00308 void insertionsortAsc(vector<T>& vct, 00309 const int begin, 00310 const int end) const; 00311 /** 00312 * Partition vector for descending order 00313 */ 00314 template <class T> 00315 int partitionDesc(vector<T>& vct, 00316 const int begin, 00317 const int end) const; 00318 /** 00319 * Insertion sort for descending order 00320 */ 00321 template <class T> 00322 void insertionsortDesc(vector<T>& vct, 00323 const int begin, 00324 const int end) const; 00325 00326 /** 00327 * @name Shadows of the parameters to avoid a function access 00328 */ 00329 //@{ 00330 /** 00331 * Threshold for bubble sort 00332 */ 00333 int thresholdForBubble_; 00334 00335 /** 00336 * Sorting order 00337 */ 00338 eSortingOrder order_; 00339 //@} 00340 00341 }; 00342 00343 } 00344 00345 #include "cvrSort_template.h" 00346 00347 #endif