sort_utils.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file sort_utils.h
00026  *  Various helper functions for sorting sequences.
00027  *
00028  *  Copyright (C) 2002-2015 Max-Planck-Society
00029  *  \author Martin Reinecke
00030  */
00031 
00032 #ifndef PLANCK_SORT_UTILS_H
00033 #define PLANCK_SORT_UTILS_H
00034 
00035 #include <algorithm>
00036 #include <functional>
00037 #include <vector>
00038 
00039 template<typename It, typename Comp> class IdxComp__
00040   {
00041   private:
00042     It begin;
00043     Comp comp;
00044   public:
00045     IdxComp__ (It begin_, Comp comp_): begin(begin_), comp(comp_) {}
00046     bool operator() (std::size_t a, std::size_t b) const
00047       { return comp(*(begin+a),*(begin+b)); }
00048   };
00049 
00050 /*! Performs an indirect sort on the supplied iterator range and returns in
00051     \a idx a \a vector containing the indices of the smallest, second smallest,
00052     third smallest, etc. element, according to \a comp. */
00053 template<typename It, typename T2, typename Comp>
00054   inline void buildIndex (It begin, It end, std::vector<T2> &idx, Comp comp)
00055   {
00056   using namespace std;
00057   T2 num=end-begin;
00058   idx.resize(num);
00059   for (T2 i=0; i<num; ++i) idx[i] = i;
00060   sort (idx.begin(),idx.end(),IdxComp__<It,Comp>(begin,comp));
00061   }
00062 
00063 /*! Performs an indirect sort on the supplied iterator range and returns in
00064     \a idx a \a vector containing the indices of the smallest, second smallest,
00065     third smallest, etc. element. */
00066 template<typename It, typename T2> inline void buildIndex (It begin, It end,
00067   std::vector<T2> &idx)
00068   {
00069   using namespace std;
00070   typedef typename iterator_traits<It>::value_type T;
00071   buildIndex(begin,end,idx,less<T>());
00072   }
00073 
00074 /*! Sorts the supplied iterator range according to the order given by \a idx.
00075     The operation is done out of place and requires temporary extra storage. */
00076 template<typename It, typename T2> inline void sortByIndex (It begin, It end,
00077   const std::vector<T2> &idx)
00078   {
00079   using namespace std;
00080   typedef typename iterator_traits<It>::value_type T;
00081   T2 num=end-begin;
00082   T *tmp= new T[num];
00083   for (T2 i=0; i<num; ++i) swap(tmp[i],*(begin+i));
00084   for (T2 i=0; i<num; ++i) swap(*(begin+i),tmp[idx[i]]);
00085   delete[] tmp;
00086   }
00087 
00088 /*! Sorts the supplied iterator range according to the order given by \a idx.
00089     The operation is done in place. */
00090 template<typename It, typename T2> inline void sortByIndex_inplace
00091   (It begin, It end, const std::vector<T2> &idx)
00092   {
00093   using namespace std;
00094   typedef typename iterator_traits<It>::value_type T;
00095   T2 num=end-begin;
00096   vector<bool> done(num,false);
00097   T2 cnt=0;
00098   while (cnt<num)
00099     {
00100     if (!done[cnt]) // new cycle
00101       {
00102       T tmp(*(begin+cnt));
00103       T2 cnt2 = cnt;
00104       T2 cnt3 = idx[cnt];
00105       while (cnt3!=cnt)
00106         {
00107         done[cnt2]=true;
00108         *(begin+cnt2)=*(begin+cnt3);
00109         cnt2=cnt3;
00110         cnt3=idx[cnt3];
00111         }
00112       *(begin+cnt2) = tmp;
00113       }
00114     ++cnt;
00115     }
00116   }
00117 
00118 template<typename It, typename Comp> inline void indirectSort (It begin, It end,
00119   Comp comp)
00120   {
00121   using namespace std;
00122   vector<std::size_t> idx;
00123   buildIndex (begin,end,idx,comp);
00124   sortByIndex (begin,end,idx);
00125   }
00126 
00127 template<typename It> inline void indirectSort (It begin, It end)
00128   {
00129   using namespace std;
00130   typedef typename iterator_traits<It>::value_type T;
00131   indirectSort(begin,end,less<T>());
00132   }
00133 
00134 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library