qsort.hh

00001 // Copyright (C) 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00016 // Boston, MA 02110-1301, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 #ifndef OLN_UTILS_QSORT_HH
00028 # define OLN_UTILS_QSORT_HH
00029 # include <vector>
00030 # include <oln/core/abstract/image.hh>
00031 # include <oln/core/abstract/image_with_type_with_dim.hh>
00032 
00033 # include <algorithm>
00034 # include <iostream>
00035 
00036 namespace oln
00037 {
00038   namespace utils
00039   {
00040     namespace internal
00041     {
00047       template <typename I>
00048       struct cmp_pt_value
00049       {
00050         typedef oln::abstract::non_vectorial_image<I>   im_type; 
00051         typedef oln_point_type(I)                       point_type; 
00052 
00058         cmp_pt_value(const im_type &im): im_(im)
00059         {
00060         }
00061 
00072         bool operator()(const point_type &i, const point_type &j)
00073         {
00074           if (im_[i] == im_[j])
00075             for (unsigned t(0); t < point_type::dim; ++t)
00076               if (i.nth(t) != j.nth(t))
00077                 return i.nth(t) < j.nth(t);
00078           return im_[j] > im_[i];
00079         }
00080 
00081       protected:
00082         const im_type   &im_; 
00083       };
00084 
00090       template <typename I>
00091       struct cmp_pt_value_inv
00092       {
00093         typedef oln::abstract::non_vectorial_image<I>   im_type; 
00094         typedef oln_point_type(I)                       point_type; 
00095 
00101         cmp_pt_value_inv(const im_type &im): im_(im)
00102         {
00103         }
00104 
00115         bool operator()(const point_type &i, const point_type &j)
00116         {
00117           if (im_[i] == im_[j])
00118             for (unsigned t(0); t < point_type::dim; ++t)
00119               if (i.nth(t) != j.nth(t))
00120                 return i.nth(t) < j.nth(t);
00121           return im_[i] > im_[j];
00122         }
00123 
00124       protected:
00125         const im_type   &im_; 
00126       };
00127     } // !internal
00128 
00137     template<class I>
00138     void
00139     qsort(const oln::abstract::non_vectorial_image<I>& im,
00140           std::vector<oln_point_type(I)> &v)
00141     {
00142       oln_iter_type(I)  it(im);
00143 
00144       for_all(it)
00145         v.push_back(oln_point_type(I)(it));
00146       internal::cmp_pt_value<I> c(im);
00147 
00148       std::sort(v.begin(), v.end(), c);
00149     }
00150 
00159     template<class I>
00160     void
00161     qsort_inv(const oln::abstract::non_vectorial_image<I>& im,
00162               std::vector<oln_point_type(I)> &v)
00163     {
00164       oln_iter_type(I)  it(im);
00165 
00166       for_all(it)
00167         v.push_back(oln_point_type(I)(it));
00168 
00169       internal::cmp_pt_value_inv<I> c(im);
00170 
00171       std::sort(v.begin(), v.end(), c);
00172     }
00173 
00178     template <bool reverse>
00179     struct select_q_sort
00180     {
00181       template <class I>
00182       void
00183       operator ()(const oln::abstract::non_vectorial_image<I>& im,
00184                   std::vector<oln_point_type(I)> &v)
00185       {
00186         qsort_inv(im, v);
00187       }
00188     };
00189 
00194     template <>
00195     struct select_q_sort<true>
00196     {
00197       template <class I>
00198       void
00199       operator ()(const oln::abstract::non_vectorial_image<I>& im,
00200                   std::vector<oln_point_type(I)> &v)
00201       {
00202         qsort(im, v);
00203       }
00204     };
00205   } // !utils
00206 } // !oln
00207 
00208 #endif // !QSORT

Generated on Tue Feb 20 20:20:27 2007 for Olena by  doxygen 1.5.1