CVR-Lib last update 20 Sep 2009

cvrQuickPartialSort.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   cvrQuickPartialSort.h
00043  *         This file contains the functor quickPartialSort, which
00044  *         calculates the median quickly
00045  * \author Guy Wafo
00046  * \date   27.03.2001
00047  *
00048  * $Id: cvrQuickPartialSort.h,v 1.2 2008/01/16 21:52:38 alvarado Exp $
00049  */
00050 
00051 #ifndef _CVR_QUICK_PARTIAL_SORT_H_
00052 #define _CVR_QUICK_PARTIAL_SORT_H_
00053 
00054 #include "cvrFunctor.h"
00055 #include "cvrVector.h"
00056 #include "cvrMatrix.h"
00057 
00058 namespace cvr {
00059 
00060   /**
00061    * Quick partial sort.
00062    *
00063    * This class is used to find the n-th element of an ascending sorted vector
00064    * or matrix, while avoiding to sort the complete container (that is the
00065    * reason for the "partial" part in the class name). The algorithm used is
00066    * based on the quick sort.
00067    *
00068    * On-place applies are in this implementation faster than on copy ones.
00069    *
00070    * Note that this class does not has a nested parameters class, since they
00071    * are not necessary.
00072    *
00073    * \ingroup gSorting
00074    */
00075   class quickPartialSort : public functor {
00076   public:
00077     /**
00078      * Default constructor
00079      */
00080     quickPartialSort();
00081 
00082     /**
00083      * copy constructor
00084      * @param other the object to be copied
00085      */
00086     quickPartialSort(const quickPartialSort& other);
00087 
00088     /**
00089      * destructor
00090      */
00091     virtual ~quickPartialSort();
00092 
00093     /**
00094      * Returns the name of this class.
00095      */
00096     const std::string& name() const;
00097 
00098     /**
00099      * Calculates which element of the sorted matrix would be at(row,col) if
00100      * the whole matrix would be sorted.  The given matrix will be modified so
00101      * that all elements "before" the given coordinates are less than or equal
00102      * the element at(row,col), and the elements "after" will be greater.
00103      *
00104      * The terms "before" and "after" are stated in the context of
00105      * concatenation of all rows of the matrix into a large vector.
00106      *
00107      * @param row row index for the element that has to be found.
00108      * @param col column index for the element that has to be found.
00109      * @param src matrix<T> with the source data.
00110      * @param data the data value of the given matrix.
00111      * @return \c true on success \c false otherwise
00112      */
00113     template <typename T>
00114     bool apply(const int row, const int col,
00115                matrix<T>& src, T& data) const;
00116 
00117     /**
00118      * Calculates the data of the given matrix, which is NOT modified.
00119      * The elements of the matrix will be considered as part of a vector
00120      * with "columns()" times "rows()" elements.
00121      *
00122      * This method needs to save the contents of the src data, and therefore is
00123      * not as fast as the one with interface
00124      * apply(const int,const int,matrix<T>&,T&).
00125      *
00126      * @param row row index for the element that has to be found.
00127      * @param col column index for the element that has to be found.
00128      * @param src matrix<T> with the source data.
00129      * @param data the data value of the given matrix.
00130      * @return \c true on success \c false otherwise
00131      */
00132     template <typename T>
00133     bool apply(const int row, const int col,
00134                const matrix<T>& src, T& data) const;
00135 
00136     /**
00137      * Calculates the data of \a src, the result is left in \a dest.
00138      * The elements of the matrix will be considered as part of a vector
00139      * with "columns()" times "rows()" elements.
00140      *
00141      * This method needs to transfer the contents of the src data into the dest
00142      * matrix, and therefore is not as fast as the one with interface
00143      * apply(const int,const int,matrix<T>&,T&).
00144      *
00145      * @param row row index for the element that has to be found.
00146      * @param col column index for the element that has to be found.
00147      * @param src matrix<T> with the source data.
00148      * @param dest partially sorted matrix.
00149      * @param data the data value of the given matrix.
00150      * @return true on success false otherwise
00151      */
00152     template <typename T>
00153     bool apply(const int row, const int col,
00154                const matrix<T>& src, matrix<T>& dest, T& data) const;
00155 
00156     /**
00157      * Find the n-th element of a sorted vector of type V, which can be an
00158      * cvr::genericVector or any of its derived classes, or a std::vector.  In
00159      * principle the container type V just needs to support:
00160      * - the type definitions V::size_type and V::value_type,
00161      * - the operator[] returning elements of type V::value_type
00162      * - the V::value_type has to be comparable with the operator<.
00163      * - the method empty()
00164      * - the method size() returning a V::size_type
00165      *
00166      * The resulting vector contains the elements less or equal than the data
00167      * for the indexes <code>x</code> such that <code>x < size()/2</code>,
00168      * and higher or equal otherwise.
00169      *
00170      * @param pos the position of the element that wants to be found.
00171      * @param srcdest vector with the source data.  The resultwill be left
00172      *                here too.
00173      * @param data    the data value
00174      *
00175      * @return true on success false otherwise
00176      */
00177     template <class V>
00178     bool apply(const int pos,V& srcdest, typename V::value_type& data) const;
00179 
00180     /**
00181      * Operates on the given parameter.
00182      *
00183      * @param pos the position of the element that wants to be found.
00184      * @param src vector with the source data.
00185      * @param data the data value
00186      * @return true on success false otherwise
00187      */
00188     template <class V>
00189     bool apply(const int pos,const V& src, typename V::value_type& data) const;
00190 
00191     /**
00192      * Operates on the given parameter.
00193      *
00194      * @param pos the position of the element that wants to be found.
00195      * @param src vector with the source data.
00196      * @param dest the partially sorted vector.  The elements at the
00197      *             first half of the vector are less or equal than the data
00198      *             and on the other half greater or equal.
00199      * @param data  the data value
00200      * @return true on success false otherwise
00201      */
00202     template <class V>
00203     bool apply(const int pos,const V& src, V& dest,
00204                typename V::value_type& data) const;
00205 
00206     /**
00207      * A shortcut function for apply(const vector<T>&, T&) const. Note
00208      * that internally another vector and T are used.
00209      *
00210      * @param pos the position of the element that wants to be found.
00211      * @param src vector whose data is sought
00212      * @returns the data of \a src
00213      */
00214     template <class V>
00215     typename V::value_type nth(const int pos,const V& src) const;
00216 
00217     /**
00218      * Copy data of "other" functor.
00219      *
00220      * @param other the functor to be copied
00221      * @return a reference to this functor object
00222      */
00223     quickPartialSort& copy(const quickPartialSort& other);
00224 
00225     /**
00226      * Returns a pointer to a clone of this functor.
00227      */
00228     virtual quickPartialSort* clone() const;
00229 
00230     /**
00231      * Returns a pointer to a new instance of this functor.
00232      */
00233     virtual quickPartialSort* newInstance() const;
00234 
00235   protected:
00236     /**
00237      * This method calculates the data in a recursively form.
00238      * The template type V has to be a vector following an interface like
00239      * the cvr::vector or the std::vector, implementing the operator[]
00240      */
00241     template <class V>
00242     typename V::value_type findNth(V& vct,
00243                                    const int begin,
00244                                    const int end,
00245                                    const int dataPos) const;
00246 
00247     /**
00248      * This method chooses a pivot-value and ensures that lower values lie
00249      * at the left and higher values at the right of its final position, which
00250      * will be returned.
00251      * The template type V has to be a vector following an interface like
00252      * the cvr::vector or the std::vector, implementing the operator[]
00253      */
00254     template <class V>
00255     int partition(V& vct,
00256                   const int begin,
00257                   const int end) const;
00258 
00259   };
00260 
00261 }
00262 
00263 
00264 #include "cvrQuickPartialSort_template.h"
00265 
00266 #endif
00267 
00268 

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