CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
index_sort.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_UTILITY_INDEX_SORT_HPP
2 # define CPPAD_UTILITY_INDEX_SORT_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 /*
16 $begin index_sort$$
17 $spell
18  cppad.hpp
19  ind
20  const
21 $$
22 
23 $section Returns Indices that Sort a Vector$$
24 $mindex index_sort$$
25 
26 
27 $head Syntax$$
28 $codei%# include <cppad/utility/index_sort.hpp>
29 %$$
30 $codei%index_sort(%keys%, %ind%)%$$
31 
32 $head keys$$
33 The argument $icode keys$$ has prototype
34 $codei%
35  const %VectorKey%& %keys%
36 %$$
37 where $icode VectorKey$$ is
38 a $cref SimpleVector$$ class with elements that support the $code <$$
39 operation.
40 
41 $head ind$$
42 The argument $icode ind$$ has prototype
43 $codei%
44  %VectorSize%& %ind%
45 %$$
46 where $icode VectorSize$$ is
47 a $cref SimpleVector$$ class with elements of type $code size_t$$.
48 The routine $cref CheckSimpleVector$$ will generate an error message
49 if this is not the case.
50 
51 $subhead Input$$
52 The size of $icode ind$$ must be the same as the size of $icode keys$$
53 and the value of its input elements does not matter.
54 
55 $subhead Return$$
56 Upon return, $icode ind$$ is a permutation of the set of indices
57 that yields increasing order for $icode keys$$.
58 In other words, for all $icode%i% != %j%$$,
59 $codei%
60  %ind%[%i%] != %ind%[%j%]
61 %$$
62 and for $icode%i% = 0 , %...% , %size%-2%$$,
63 $codei%
64  ( %keys%[ %ind%[%i%+1] ] < %keys%[ %ind%[%i%] ] ) == false
65 %$$
66 
67 
68 $head Example$$
69 $children%
70  example/utility/index_sort.cpp
71 %$$
72 The file $cref index_sort.cpp$$ contains an example
73 and test of this routine.
74 It return true if it succeeds and false otherwise.
75 
76 $end
77 */
78 # include <algorithm>
81 # include <cppad/core/define.hpp>
82 
83 namespace CppAD { // BEGIN_CPPAD_NAMESPACE
84 /*!
85 \file index_sort.hpp
86 File used to implement the CppAD index sort utility
87 */
88 
89 /*!
90 Helper class used by index_sort
91 */
92 template <class Compare>
94 private:
95  /// key used to determine position of this element
96  Compare key_;
97  /// index vlaue corresponding to this key
98  size_t index_;
99 public:
100  /// operator requried by std::sort
101  bool operator<(const index_sort_element& other) const
102  { return key_ < other.key_; }
103  /// set the key for this element
104  void set_key(const Compare& value)
105  { key_ = value; }
106  /// set the index for this element
107  void set_index(const size_t& index)
108  { index_ = index; }
109  /// get the key for this element
110  Compare get_key(void) const
111  { return key_; }
112  /// get the index for this element
113  size_t get_index(void) const
114  { return index_; }
115 };
116 
117 /*!
118 Compute the indices that sort a vector of keys
119 
120 \tparam VectorKey
121 Simple vector type that deterimene the sorting order by \c < operator
122 on its elements.
123 
124 \tparam VectorSize
125 Simple vector type with elements of \c size_t
126 that is used to return index values.
127 
128 \param keys [in]
129 values that determine the sorting order.
130 
131 \param ind [out]
132 must have the same size as \c keys.
133 The input value of its elements does not matter.
134 The output value of its elements satisfy
135 \code
136 ( keys[ ind[i] ] < keys[ ind[i+1] ] ) == false
137 \endcode
138 */
139 template <class VectorKey, class VectorSize>
140 void index_sort(const VectorKey& keys, VectorSize& ind)
141 { typedef typename VectorKey::value_type Compare;
142  CheckSimpleVector<size_t, VectorSize>();
143 
144  typedef index_sort_element<Compare> element;
145 
147  size_t(keys.size()) == size_t(ind.size()),
148  "index_sort: vector sizes do not match"
149  );
150 
151  size_t size_work = size_t(keys.size());
152  size_t size_out;
153  element* work =
154  thread_alloc::create_array<element>(size_work, size_out);
155 
156  // copy initial order into work
157  size_t i;
158  for(i = 0; i < size_work; i++)
159  { work[i].set_key( keys[i] );
160  work[i].set_index( i );
161  }
162 
163  // sort the work array
164  std::sort(work, work+size_work);
165 
166  // copy the indices to the output vector
167  for(i = 0; i < size_work; i++)
168  ind[i] = work[i].get_index();
169 
170  // we are done with this work array
172 
173  return;
174 }
175 
176 } // END_CPPAD_NAMESPACE
177 
178 # endif
void set_index(const size_t &index)
set the index for this element
Definition: index_sort.hpp:107
#define CPPAD_ASSERT_KNOWN(exp, msg)
Check that exp is true, if not print msg and terminate execution.
Define processor symbols and macros that are used by CppAD.
size_t get_index(void) const
get the index for this element
Definition: index_sort.hpp:113
static void delete_array(Type *array)
Return Memory Used for an Array to the Available Pool (include destructor call for each element)...
void index_sort(const VectorKey &keys, VectorSize &ind)
Compute the indices that sort a vector of keys.
Definition: index_sort.hpp:140
Compare get_key(void) const
get the key for this element
Definition: index_sort.hpp:110
Helper class used by index_sort.
Definition: index_sort.hpp:93
bool operator<(const index_sort_element &other) const
operator requried by std::sort
Definition: index_sort.hpp:101
Compare key_
key used to determine position of this element
Definition: index_sort.hpp:96
size_t index_
index vlaue corresponding to this key
Definition: index_sort.hpp:98
File used to define the CppAD multi-threading allocator class.
void set_key(const Compare &value)
set the key for this element
Definition: index_sort.hpp:104
Scalar value_type