source: trunk/yat/utility/sort_index.h @ 1492

Last change on this file since 1492 was 1492, checked in by Peter, 13 years ago

fixes #439

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.9 KB
Line 
1#ifndef _theplu_yat_utility_sort_index_
2#define _theplu_yat_utility_sort_index_
3
4// $Id: sort_index.h 1492 2008-09-12 01:15:53Z peter $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include "StrideIterator.h"
26
27#include <gsl/gsl_sort.h>
28
29#include <algorithm>
30#include <iterator>
31#include <vector>
32
33namespace theplu {
34namespace yat {
35namespace utility {
36
37  /**
38     Create a vector \a sort_index containing the indeces of elements
39     in a range [first, last). The elements of \a sort_index give the
40     index of the range element which would have been stored in that
41     position if the range had been sorted in place. The first element
42     of \a sort_index gives the index of the least element in the
43     range, and the last element of \a sort_index gives the index of
44     the greatest element in the range. The function will not affect
45     the range, i.e., ForwardIterator can be read-only.
46  */
47  template<typename ForwardIterator>
48  void sort_index(ForwardIterator first, ForwardIterator last, 
49                  std::vector<size_t>& sort_index);
50   
51   
52  /**
53     Specialization for StrideIterator<double*>
54  */
55  void sort_index(StrideIterator<double*> first, 
56                  StrideIterator<double*> last, 
57                  std::vector<size_t>& sort_index);
58
59  /**
60     Specialization for StrideIterator<const double*>
61  */
62  void sort_index(StrideIterator<const double*> first, 
63                  StrideIterator<const double*> last, 
64                  std::vector<size_t>& sort_index);
65
66  /**
67     Specialization for std::vector<double>::iterator
68  */
69  void sort_index(std::vector<double>::iterator first, 
70                  std::vector<double>::iterator last, 
71                  std::vector<size_t>& sort_index);
72
73  /**
74     Specialization for std::vector<double>::const_iterator
75  */
76  void sort_index(std::vector<double>::const_iterator first, 
77                  std::vector<double>::const_iterator last, 
78                  std::vector<size_t>& sort_index);
79
80
81  //  template implementation
82
83  template<typename ForwardIterator>
84  void sort_index(ForwardIterator first, ForwardIterator last, 
85                  std::vector<size_t>& result)
86  {
87    std::vector<double> vec;
88    vec.reserve(std::distance(first, last));
89    std::copy(first, last, 
90              std::back_insert_iterator<std::vector<double> >(vec));
91    sort_index(vec.begin(), vec.end(), result);
92  }
93
94}}} // of namespace utility, yat, and theplu
95
96#endif
Note: See TracBrowser for help on using the repository browser.