LevelS C++ support library  3.83
sort_utils.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file sort_utils.h
26  * Various helper functions for sorting sequences.
27  *
28  * Copyright (C) 2002-2015 Max-Planck-Society
29  * \author Martin Reinecke
30  */
31 
32 #ifndef PLANCK_SORT_UTILS_H
33 #define PLANCK_SORT_UTILS_H
34 
35 #include <algorithm>
36 #include <functional>
37 #include <vector>
38 
39 template<typename It, typename Comp> class IdxComp__
40  {
41  private:
42  It begin;
43  Comp comp;
44  public:
45  IdxComp__ (It begin_, Comp comp_): begin(begin_), comp(comp_) {}
46  bool operator() (std::size_t a, std::size_t b) const
47  { return comp(*(begin+a),*(begin+b)); }
48  };
49 
50 /*! Performs an indirect sort on the supplied iterator range and returns in
51  \a idx a \a vector containing the indices of the smallest, second smallest,
52  third smallest, etc. element, according to \a comp. */
53 template<typename It, typename T2, typename Comp>
54  inline void buildIndex (It begin, It end, std::vector<T2> &idx, Comp comp)
55  {
56  using namespace std;
57  T2 num=end-begin;
58  idx.resize(num);
59  for (T2 i=0; i<num; ++i) idx[i] = i;
60  sort (idx.begin(),idx.end(),IdxComp__<It,Comp>(begin,comp));
61  }
62 
63 /*! Performs an indirect sort on the supplied iterator range and returns in
64  \a idx a \a vector containing the indices of the smallest, second smallest,
65  third smallest, etc. element. */
66 template<typename It, typename T2> inline void buildIndex (It begin, It end,
67  std::vector<T2> &idx)
68  {
69  using namespace std;
70  typedef typename iterator_traits<It>::value_type T;
71  buildIndex(begin,end,idx,less<T>());
72  }
73 
74 /*! Sorts the supplied iterator range according to the order given by \a idx.
75  The operation is done out of place and requires temporary extra storage. */
76 template<typename It, typename T2> inline void sortByIndex (It begin, It end,
77  const std::vector<T2> &idx)
78  {
79  using namespace std;
80  typedef typename iterator_traits<It>::value_type T;
81  T2 num=end-begin;
82  T *tmp= new T[num];
83  for (T2 i=0; i<num; ++i) swap(tmp[i],*(begin+i));
84  for (T2 i=0; i<num; ++i) swap(*(begin+i),tmp[idx[i]]);
85  delete[] tmp;
86  }
87 
88 /*! Sorts the supplied iterator range according to the order given by \a idx.
89  The operation is done in place. */
90 template<typename It, typename T2> inline void sortByIndex_inplace
91  (It begin, It end, const std::vector<T2> &idx)
92  {
93  using namespace std;
94  typedef typename iterator_traits<It>::value_type T;
95  T2 num=end-begin;
96  vector<bool> done(num,false);
97  T2 cnt=0;
98  while (cnt<num)
99  {
100  if (!done[cnt]) // new cycle
101  {
102  T tmp(*(begin+cnt));
103  T2 cnt2 = cnt;
104  T2 cnt3 = idx[cnt];
105  while (cnt3!=cnt)
106  {
107  done[cnt2]=true;
108  *(begin+cnt2)=*(begin+cnt3);
109  cnt2=cnt3;
110  cnt3=idx[cnt3];
111  }
112  *(begin+cnt2) = tmp;
113  }
114  ++cnt;
115  }
116  }
117 
118 template<typename It, typename Comp> inline void indirectSort (It begin, It end,
119  Comp comp)
120  {
121  using namespace std;
122  vector<std::size_t> idx;
123  buildIndex (begin,end,idx,comp);
124  sortByIndex (begin,end,idx);
125  }
126 
127 template<typename It> inline void indirectSort (It begin, It end)
128  {
129  using namespace std;
130  typedef typename iterator_traits<It>::value_type T;
131  indirectSort(begin,end,less<T>());
132  }
133 
134 #endif
void sortByIndex_inplace(It begin, It end, const std::vector< T2 > &idx)
Definition: sort_utils.h:91
void buildIndex(It begin, It end, std::vector< T2 > &idx, Comp comp)
Definition: sort_utils.h:54
void sortByIndex(It begin, It end, const std::vector< T2 > &idx)
Definition: sort_utils.h:76

Generated on Wed Nov 13 2024 12:18:16 for LevelS C++ support library