~ubuntu-branches/ubuntu/trusty/libthrust/trusty

« back to all changes in this revision

Viewing changes to sort.h

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Beckmann
  • Date: 2011-05-28 09:32:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110528093248-np3euv5sj7fw3nyv
Tags: upstream-1.4.0
ImportĀ upstreamĀ versionĀ 1.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  Copyright 2008-2011 NVIDIA Corporation
 
3
 *
 
4
 *  Licensed under the Apache License, Version 2.0 (the "License");
 
5
 *  you may not use this file except in compliance with the License.
 
6
 *  You may obtain a copy of the License at
 
7
 *
 
8
 *      http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 *  Unless required by applicable law or agreed to in writing, software
 
11
 *  distributed under the License is distributed on an "AS IS" BASIS,
 
12
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 *  See the License for the specific language governing permissions and
 
14
 *  limitations under the License.
 
15
 */
 
16
 
 
17
 
 
18
/*! \file sort.h
 
19
 *  \brief Defines the interface to various
 
20
 *         sorting functions.
 
21
 */
 
22
 
 
23
#pragma once
 
24
 
 
25
#include <thrust/detail/config.h>
 
26
 
 
27
namespace thrust
 
28
{
 
29
 
 
30
 
 
31
/*! \addtogroup sorting
 
32
 *  \ingroup algorithms
 
33
 *  \{
 
34
 */
 
35
 
 
36
/*! \p sort sorts the elements in <tt>[first, last)</tt> into
 
37
 *  ascending order, meaning that if \c i and \c j are any two valid
 
38
 *  iterators in <tt>[first, last)</tt> such that \c i precedes \c j,
 
39
 *  then \c *j is not less than \c *i. Note: \c sort is not guaranteed
 
40
 *  to be stable. That is, suppose that \c *i and \c *j are equivalent:
 
41
 *  neither one is less than the other. It is not guaranteed that the
 
42
 *  relative order of these two elements will be preserved by \p sort.
 
43
 *
 
44
 *  This version of \p sort compares objects using \c operator<.
 
45
 *
 
46
 *  \param first The beginning of the sequence.
 
47
 *  \param last The end of the sequence.
 
48
 *
 
49
 *  \tparam RandomAccessIterator is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
50
 *          \p RandomAccessIterator is mutable,
 
51
 *          and \p RandomAccessIterator's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>,
 
52
 *          and the ordering relation on \p RandomAccessIterator's \c value_type is a <em>strict weak ordering</em>, as defined in the
 
53
 *          <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a> requirements.
 
54
 *
 
55
 *  The following code snippet demonstrates how to use \p sort to sort
 
56
 *  a sequence of integers.
 
57
 *
 
58
 *  \code
 
59
 *  #include <thrust/sort.h>
 
60
 *  ...
 
61
 *  const int N = 6;
 
62
 *  int A[N] = {1, 4, 2, 8, 5, 7};
 
63
 *  thrust::sort(A, A + N);
 
64
 *  // A is now {1, 2, 4, 5, 7, 8}
 
65
 *  \endcode
 
66
 *
 
67
 *  \see http://www.sgi.com/tech/stl/sort.html
 
68
 *  \see \p stable_sort
 
69
 *  \see \p sort_by_key
 
70
 */
 
71
template<typename RandomAccessIterator>
 
72
  void sort(RandomAccessIterator first,
 
73
            RandomAccessIterator last);
 
74
 
 
75
/*! \p sort sorts the elements in <tt>[first, last)</tt> into
 
76
 *  ascending order, meaning that if \c i and \c j are any two valid
 
77
 *  iterators in <tt>[first, last)</tt> such that \c i precedes \c j,
 
78
 *  then \c *j is not less than \c *i. Note: \c sort is not guaranteed
 
79
 *  to be stable. That is, suppose that \c *i and \c *j are equivalent:
 
80
 *  neither one is less than the other. It is not guaranteed that the
 
81
 *  relative order of these two elements will be preserved by \p sort.
 
82
 *
 
83
 *  This version of \p sort compares objects using a function object
 
84
 *  \p comp.
 
85
 *
 
86
 *  \param first The beginning of the sequence.
 
87
 *  \param last The end of the sequence.
 
88
 *  \param comp  Comparison operator.
 
89
 *
 
90
 *  \tparam RandomAccessIterator is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
91
 *          \p RandomAccessIterator is mutable,
 
92
 *          and \p RandomAccessIterator's \c value_type is convertible to \p StrictWeakOrdering's
 
93
 *          \c first_argument_type and \c second_argument_type.
 
94
 *  \tparam StrictWeakOrdering is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
95
 *
 
96
 *  The following code demonstrates how to sort integers in descending order
 
97
 *  using the greater<int> comparison operator.
 
98
 *
 
99
 *  \code
 
100
 *  #include <thrust/sort.h>
 
101
 *  #include <thrust/functional.h>
 
102
 *  ...
 
103
 *  const int N = 6;
 
104
 *  int A[N] = {1, 4, 2, 8, 5, 7};
 
105
 *  thrust::sort(A, A + N, thrust::greater<int>());
 
106
 *  // A is now {8, 7, 5, 4, 2, 1};
 
107
 *  \endcode
 
108
 *
 
109
 *  \see http://www.sgi.com/tech/stl/sort.html
 
110
 *  \see \p stable_sort
 
111
 *  \see \p sort_by_key
 
112
 */
 
113
template<typename RandomAccessIterator,
 
114
         typename StrictWeakOrdering>
 
115
  void sort(RandomAccessIterator first,
 
116
            RandomAccessIterator last,
 
117
            StrictWeakOrdering comp);
 
118
 
 
119
/*! \p stable_sort is much like \c sort: it sorts the elements in
 
120
 *  <tt>[first, last)</tt> into ascending order, meaning that if \c i
 
121
 *  and \c j are any two valid iterators in <tt>[first, last)</tt> such
 
122
 *  that \c i precedes \c j, then \c *j is not less than \c *i.
 
123
 *
 
124
 *  As the name suggests, \p stable_sort is stable: it preserves the
 
125
 *  relative ordering of equivalent elements. That is, if \c x and \c y
 
126
 *  are elements in <tt>[first, last)</tt> such that \c x precedes \c y,
 
127
 *  and if the two elements are equivalent (neither <tt>x < y</tt> nor
 
128
 *  <tt>y < x</tt>) then a postcondition of \p stable_sort is that \c x
 
129
 *  still precedes \c y.
 
130
 *
 
131
 *  This version of \p stable_sort compares objects using \c operator<.
 
132
 *
 
133
 *  \param first The beginning of the sequence.
 
134
 *  \param last The end of the sequence.
 
135
 *
 
136
 *  \tparam RandomAccessIterator is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
137
 *          \p RandomAccessIterator is mutable,
 
138
 *          and \p RandomAccessIterator's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>,
 
139
 *          and the ordering relation on \p RandomAccessIterator's \c value_type is a <em>strict weak ordering</em>, as defined in the
 
140
 *          <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a> requirements.
 
141
 *
 
142
 *  The following code snippet demonstrates how to use \p sort to sort
 
143
 *  a sequence of integers.
 
144
 *
 
145
 *  \code
 
146
 *  #include <thrust/sort.h>
 
147
 *  ...
 
148
 *  const int N = 6;
 
149
 *  int A[N] = {1, 4, 2, 8, 5, 7};
 
150
 *  thrust::stable_sort(A, A + N);
 
151
 *  // A is now {1, 2, 4, 5, 7, 8}
 
152
 *  \endcode
 
153
 *
 
154
 *  \see http://www.sgi.com/tech/stl/stable_sort.html
 
155
 *  \see \p sort
 
156
 *  \see \p stable_sort_by_key
 
157
 */
 
158
template<typename RandomAccessIterator>
 
159
  void stable_sort(RandomAccessIterator first,
 
160
                   RandomAccessIterator last);
 
161
 
 
162
/*! \p stable_sort is much like \c sort: it sorts the elements in
 
163
 *  <tt>[first, last)</tt> into ascending order, meaning that if \c i
 
164
 *  and \c j are any two valid iterators in <tt>[first, last)</tt> such
 
165
 *  that \c i precedes \c j, then \c *j is not less than \c *i.
 
166
 *
 
167
 *  As the name suggests, \p stable_sort is stable: it preserves the
 
168
 *  relative ordering of equivalent elements. That is, if \c x and \c y
 
169
 *  are elements in <tt>[first, last)</tt> such that \c x precedes \c y,
 
170
 *  and if the two elements are equivalent (neither <tt>x < y</tt> nor
 
171
 *  <tt>y < x</tt>) then a postcondition of \p stable_sort is that \c x
 
172
 *  still precedes \c y.
 
173
 *
 
174
 *  This version of \p stable_sort compares objects using a function object
 
175
 *  \p comp.
 
176
 *
 
177
 *  \param first The beginning of the sequence.
 
178
 *  \param last The end of the sequence.
 
179
 *  \param comp Comparison operator.
 
180
 *
 
181
 *  \tparam RandomAccessIterator is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
182
 *          \p RandomAccessIterator is mutable,
 
183
 *          and \p RandomAccessIterator's \c value_type is convertible to \p StrictWeakOrdering's
 
184
 *          \c first_argument_type and \c second_argument_type.
 
185
 *  \tparam StrictWeakOrdering is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
186
 *
 
187
 *  The following code demonstrates how to sort integers in descending order
 
188
 *  using the greater<int> comparison operator.
 
189
 *
 
190
 *  \code
 
191
 *  #include <thrust/sort.h>
 
192
 *  #include <thrust/functional.h>
 
193
 *  ...
 
194
 *  const int N = 6;
 
195
 *  int A[N] = {1, 4, 2, 8, 5, 7};
 
196
 *  thrust::sort(A, A + N, thrust::greater<int>());
 
197
 *  // A is now {8, 7, 5, 4, 2, 1};
 
198
 *  \endcode
 
199
 *
 
200
 *  \see http://www.sgi.com/tech/stl/stable_sort.html
 
201
 *  \see \p sort
 
202
 *  \see \p stable_sort_by_key
 
203
 */
 
204
template<typename RandomAccessIterator,
 
205
         typename StrictWeakOrdering>
 
206
  void stable_sort(RandomAccessIterator first,
 
207
                   RandomAccessIterator last,
 
208
                   StrictWeakOrdering comp);
 
209
 
 
210
///////////////
 
211
// Key Value //
 
212
///////////////
 
213
 
 
214
/*! \p sort_by_key performs a key-value sort. That is, \p sort_by_key sorts the
 
215
 *  elements in <tt>[keys_first, keys_last)</tt> and <tt>[values_first,
 
216
 *  values_first + (keys_last - keys_first))</tt> into ascending key order,
 
217
 *  meaning that if \c i and \c j are any two valid iterators in <tt>[keys_first,
 
218
 *  keys_last)</tt> such that \c i precedes \c j, and \c p and \c q are iterators
 
219
 *  in <tt>[values_first, values_first + (keys_last - keys_first))</tt>
 
220
 *  corresponding to \c i and \c j respectively, then \c *j is not less than
 
221
 *  \c *i.
 
222
 *
 
223
 *  Note: \c sort_by_key is not guaranteed to be stable. That is, suppose that
 
224
 *  \c *i and \c *j are equivalent: neither one is less than the other. It is not
 
225
 *  guaranteed that the relative order of these two keys or the relative
 
226
 *  order of their corresponding values will be preserved by \p sort_by_key.
 
227
 *
 
228
 *  This version of \p sort_by_key compares key objects using \c operator<.
 
229
 *
 
230
 *  \param keys_first The beginning of the key sequence.
 
231
 *  \param keys_last The end of the key sequence.
 
232
 *  \param values_first The beginning of the value sequence.
 
233
 *
 
234
 *  \tparam RandomAccessIterator1 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
235
 *          \p RandomAccessIterator1 is mutable,
 
236
 *          and \p RandomAccessIterator1's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>,
 
237
 *          and the ordering relation on \p RandomAccessIterator1's \c value_type is a <em>strict weak ordering</em>, as defined in the
 
238
 *          <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a> requirements.
 
239
 *  \tparam RandomAccessIterator2 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.hml">Random Access Iterator</a>,
 
240
 *          and \p RandomAccessIterator2 is mutable.
 
241
 *
 
242
 *  The following code snippet demonstrates how to use \p sort_by_key to sort
 
243
 *  an array of character values using integers as sorting keys.
 
244
 *
 
245
 *  \code
 
246
 *  #include <thrust/sort.h>
 
247
 *  ...
 
248
 *  const int N = 6;
 
249
 *  int    keys[N] = {  1,   4,   2,   8,   5,   7};
 
250
 *  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
 
251
 *  thrust::sort_by_key(keys, keys + N, values);
 
252
 *  // keys is now   {  1,   2,   4,   5,   7,   8}
 
253
 *  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}
 
254
 *  \endcode
 
255
 *
 
256
 *  \see http://www.sgi.com/tech/stl/sort.html
 
257
 *  \see \p stable_sort_by_key
 
258
 *  \see \p sort
 
259
 */
 
260
template<typename RandomAccessIterator1,
 
261
         typename RandomAccessIterator2>
 
262
  void sort_by_key(RandomAccessIterator1 keys_first,
 
263
                   RandomAccessIterator1 keys_last,
 
264
                   RandomAccessIterator2 values_first);
 
265
 
 
266
/*! \p sort_by_key performs a key-value sort. That is, \p sort_by_key sorts the
 
267
 *  elements in <tt>[keys_first, keys_last)</tt> and <tt>[values_first,
 
268
 *  values_first + (keys_last - keys_first))</tt> into ascending key order,
 
269
 *  meaning that if \c i and \c j are any two valid iterators in <tt>[keys_first,
 
270
 *  keys_last)</tt> such that \c i precedes \c j, and \c p and \c q are iterators
 
271
 *  in <tt>[values_first, values_first + (keys_last - keys_first))</tt>
 
272
 *  corresponding to \c i and \c j respectively, then \c *j is not less than
 
273
 *  \c *i.
 
274
 *
 
275
 *  Note: \c sort_by_key is not guaranteed to be stable. That is, suppose that
 
276
 *  \c *i and \c *j are equivalent: neither one is less than the other. It is not
 
277
 *  guaranteed that the relative order of these two keys or the relative
 
278
 *  order of their corresponding values will be preserved by \p sort_by_key.
 
279
 *
 
280
 *  This version of \p sort_by_key compares key objects using a function object
 
281
 *  \c comp.
 
282
 *
 
283
 *  \param keys_first The beginning of the key sequence.
 
284
 *  \param keys_last The end of the key sequence.
 
285
 *  \param values_first The beginning of the value sequence.
 
286
 *  \param comp Comparison operator.
 
287
 *
 
288
 *  \tparam RandomAccessIterator1 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
289
 *          \p RandomAccessIterator1 is mutable,
 
290
 *          and \p RandomAccessIterator1's \c value_type is convertible to \p StrictWeakOrdering's
 
291
 *          \c first_argument_type and \c second_argument_type.
 
292
 *  \tparam RandomAccessIterator2 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.hml">Random Access Iterator</a>,
 
293
 *          and \p RandomAccessIterator2 is mutable.
 
294
 *  \tparam StrictWeakOrdering is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
295
 *
 
296
 *  The following code snippet demonstrates how to use \p sort_by_key to sort
 
297
 *  an array of character values using integers as sorting keys.  The keys
 
298
 *  are sorted in descending order using the greater<int> comparison operator.
 
299
 *
 
300
 *  \code
 
301
 *  #include <thrust/sort.h>
 
302
 *  ...
 
303
 *  const int N = 6;
 
304
 *  int    keys[N] = {  1,   4,   2,   8,   5,   7};
 
305
 *  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
 
306
 *  thrust::sort_by_key(keys, keys + N, values, thrust::greater<int>());
 
307
 *  // keys is now   {  8,   7,   5,   4,   2,   1}
 
308
 *  // values is now {'d', 'f', 'e', 'b', 'c', 'a'}
 
309
 *  \endcode
 
310
 *
 
311
 *  \see http://www.sgi.com/tech/stl/sort.html
 
312
 *  \see \p stable_sort_by_key
 
313
 *  \see \p sort
 
314
 */
 
315
template<typename RandomAccessIterator1,
 
316
         typename RandomAccessIterator2,
 
317
         typename StrictWeakOrdering>
 
318
  void sort_by_key(RandomAccessIterator1 keys_first,
 
319
                   RandomAccessIterator1 keys_last,
 
320
                   RandomAccessIterator2 values_first,
 
321
                   StrictWeakOrdering comp);
 
322
 
 
323
/*! \p stable_sort_by_key performs a key-value sort. That is, \p stable_sort_by_key
 
324
 *  sorts the elements in <tt>[keys_first, keys_last)</tt> and <tt>[values_first,
 
325
 *  values_first + (keys_last - keys_first))</tt> into ascending key order,
 
326
 *  meaning that if \c i and \c j are any two valid iterators in <tt>[keys_first,
 
327
 *  keys_last)</tt> such that \c i precedes \c j, and \c p and \c q are iterators
 
328
 *  in <tt>[values_first, values_first + (keys_last - keys_first))</tt>
 
329
 *  corresponding to \c i and \c j respectively, then \c *j is not less than
 
330
 *  \c *i.
 
331
 *
 
332
 *  As the name suggests, \p stable_sort_by_key is stable: it preserves the
 
333
 *  relative ordering of equivalent elements. That is, if \c x and \c y
 
334
 *  are elements in <tt>[keys_first, keys_last)</tt> such that \c x precedes \c y,
 
335
 *  and if the two elements are equivalent (neither <tt>x < y</tt> nor
 
336
 *  <tt>y < x</tt>) then a postcondition of \p stable_sort_by_key is that \c x
 
337
 *  still precedes \c y.
 
338
 *
 
339
 *  This version of \p stable_sort_by_key compares key objects using \c operator<.
 
340
 *
 
341
 *  \param keys_first The beginning of the key sequence.
 
342
 *  \param keys_last The end of the key sequence.
 
343
 *  \param values_first The beginning of the value sequence.
 
344
 *
 
345
 *  \tparam RandomAccessIterator1 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
346
 *          \p RandomAccessIterator1 is mutable,
 
347
 *          and \p RandomAccessIterator1's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>,
 
348
 *          and the ordering relation on \p RandomAccessIterator1's \c value_type is a <em>strict weak ordering</em>, as defined in the
 
349
 *          <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a> requirements.
 
350
 *  \tparam RandomAccessIterator2 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.hml">Random Access Iterator</a>,
 
351
 *          and \p RandomAccessIterator2 is mutable.
 
352
 *
 
353
 *  The following code snippet demonstrates how to use \p stable_sort_by_key to sort
 
354
 *  an array of characters using integers as sorting keys.
 
355
 *
 
356
 *  \code
 
357
 *  #include <thrust/sort.h>
 
358
 *  ...
 
359
 *  const int N = 6;
 
360
 *  int    keys[N] = {  1,   4,   2,   8,   5,   7};
 
361
 *  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
 
362
 *  thrust::stable_sort_by_key(keys, keys + N, values);
 
363
 *  // keys is now   {  1,   2,   4,   5,   7,   8}
 
364
 *  // values is now {'a', 'c', 'b', 'e', 'f', 'd'}
 
365
 *  \endcode
 
366
 *
 
367
 *  \see http://www.sgi.com/tech/stl/sort.html
 
368
 *  \see \p sort_by_key
 
369
 *  \see \p stable_sort
 
370
 */
 
371
template<typename RandomAccessIterator1,
 
372
         typename RandomAccessIterator2>
 
373
  void stable_sort_by_key(RandomAccessIterator1 keys_first,
 
374
                          RandomAccessIterator1 keys_last,
 
375
                          RandomAccessIterator2 values_first);
 
376
 
 
377
/*! \p stable_sort_by_key performs a key-value sort. That is, \p stable_sort_by_key
 
378
 *  sorts the elements in <tt>[keys_first, keys_last)</tt> and <tt>[values_first,
 
379
 *  values_first + (keys_last - keys_first))</tt> into ascending key order,
 
380
 *  meaning that if \c i and \c j are any two valid iterators in <tt>[keys_first,
 
381
 *  keys_last)</tt> such that \c i precedes \c j, and \c p and \c q are iterators
 
382
 *  in <tt>[values_first, values_first + (keys_last - keys_first))</tt>
 
383
 *  corresponding to \c i and \c j respectively, then \c *j is not less than
 
384
 *  \c *i.
 
385
 *
 
386
 *  As the name suggests, \p stable_sort_by_key is stable: it preserves the
 
387
 *  relative ordering of equivalent elements. That is, if \c x and \c y
 
388
 *  are elements in <tt>[keys_first, keys_last)</tt> such that \c x precedes \c y,
 
389
 *  and if the two elements are equivalent (neither <tt>x < y</tt> nor
 
390
 *  <tt>y < x</tt>) then a postcondition of \p stable_sort_by_key is that \c x
 
391
 *  still precedes \c y.
 
392
 *
 
393
 *  This version of \p stable_sort_by_key compares key objects using the function
 
394
 *  object \p comp.
 
395
 *
 
396
 *  \param keys_first The beginning of the key sequence.
 
397
 *  \param keys_last The end of the key sequence.
 
398
 *  \param values_first The beginning of the value sequence.
 
399
 *  \param comp Comparison operator.
 
400
 *
 
401
 *  \tparam RandomAccessIterator1 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.html">Random Access Iterator</a>,
 
402
 *          \p RandomAccessIterator1 is mutable,
 
403
 *          and \p RandomAccessIterator1's \c value_type is convertible to \p StrictWeakOrdering's
 
404
 *          \c first_argument_type and \c second_argument_type.
 
405
 *  \tparam RandomAccessIterator2 is a model of <a href="http://www.sgi.com/tech/stl/RandomAccessIterator.hml">Random Access Iterator</a>,
 
406
 *          and \p RandomAccessIterator2 is mutable.
 
407
 *  \tparam StrictWeakOrdering is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
408
 *
 
409
 *  The following code snippet demonstrates how to use \p sort_by_key to sort
 
410
 *  an array of character values using integers as sorting keys.  The keys
 
411
 *  are sorted in descending order using the greater<int> comparison operator.
 
412
 *
 
413
 *  \code
 
414
 *  #include <thrust/sort.h>
 
415
 *  ...
 
416
 *  const int N = 6;
 
417
 *  int    keys[N] = {  1,   4,   2,   8,   5,   7};
 
418
 *  char values[N] = {'a', 'b', 'c', 'd', 'e', 'f'};
 
419
 *  thrust::stable_sort_by_key(keys, keys + N, values, thrust::greater<int>());
 
420
 *  // keys is now   {  8,   7,   5,   4,   2,   1}
 
421
 *  // values is now {'d', 'f', 'e', 'b', 'c', 'a'}
 
422
 *  \endcode
 
423
 *
 
424
 *
 
425
 *  \see http://www.sgi.com/tech/stl/sort.html
 
426
 *  \see \p sort_by_key
 
427
 *  \see \p stable_sort
 
428
 */
 
429
template<typename RandomAccessIterator1,
 
430
         typename RandomAccessIterator2,
 
431
         typename StrictWeakOrdering>
 
432
  void stable_sort_by_key(RandomAccessIterator1 keys_first,
 
433
                          RandomAccessIterator1 keys_last,
 
434
                          RandomAccessIterator2 values_first,
 
435
                          StrictWeakOrdering comp);
 
436
 
 
437
/*! \} // end sorting
 
438
 */
 
439
 
 
440
/*! \addtogroup reductions
 
441
 *  \{
 
442
 *  \addtogroup predicates
 
443
 *  \{
 
444
 */
 
445
 
 
446
/*! \p is_sorted returns \c true if the range <tt>[first, last)</tt> is
 
447
 *  sorted in ascending order, and \c false otherwise.
 
448
 *
 
449
 *  Specifically, this version of \p is_sorted returns \c false if for
 
450
 *  some iterator \c i in the range <tt>[first, last - 1)</tt> the
 
451
 *  expression <tt>*(i + 1) < *i</tt> is \c true.
 
452
 *
 
453
 *  \param first The beginning of the sequence.
 
454
 *  \param last  The end of the sequence.
 
455
 *  \return \c true, if the sequence is sorted; \c false, otherwise.
 
456
 *
 
457
 *  \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
 
458
 *          \p ForwardIterator's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>,
 
459
 *          and the ordering on objects of \p ForwardIterator's \c value_type is a <em>strict weak ordering</em>, as defined
 
460
 *          in the <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a> requirements.
 
461
 *
 
462
 *
 
463
 *  The following code demonstrates how to use \p is_sorted to test whether the
 
464
 *  contents of a \c device_vector are stored in ascending order.
 
465
 *
 
466
 *  \code
 
467
 *  #include <thrust/sort.h>
 
468
 *  #include <thrust/device_vector.h>
 
469
 *  #include <thrust/sort.h>
 
470
 *  ...
 
471
 *  thrust::device_vector<int> v(6);
 
472
 *  v[0] = 1;
 
473
 *  v[1] = 4;
 
474
 *  v[2] = 2;
 
475
 *  v[3] = 8;
 
476
 *  v[4] = 5;
 
477
 *  v[5] = 7;
 
478
 *
 
479
 *  bool result = thrust::is_sorted(v.begin(), v.end());
 
480
 *
 
481
 *  // result == false
 
482
 *
 
483
 *  thrust::sort(v.begin(), v.end());
 
484
 *  result = thrust::is_sorted(v.begin(), v.end());
 
485
 *
 
486
 *  // result == true
 
487
 *  \endcode
 
488
 *
 
489
 *  \see http://www.sgi.com/tech/stl/is_sorted.html
 
490
 *  \see is_sorted_until
 
491
 *  \see \c sort
 
492
 *  \see \c stable_sort
 
493
 *  \see \c less<T>
 
494
 */
 
495
template<typename ForwardIterator>
 
496
  bool is_sorted(ForwardIterator first,
 
497
                 ForwardIterator last);
 
498
 
 
499
/*! \p is_sorted returns \c true if the range <tt>[first, last)</tt> is sorted in ascending 
 
500
 *  order accoring to a user-defined comparison operation, and \c false otherwise.
 
501
 *
 
502
 *  Specifically, this version of \p is_sorted returns \c false if for some iterator \c i in
 
503
 *  the range <tt>[first, last - 1)</tt> the expression <tt>comp(*(i + 1), *i)</tt> is \c true.
 
504
 *
 
505
 *  \param first The beginning of the sequence.
 
506
 *  \param last  The end of the sequence.
 
507
 *  \param comp  Comparison operator.
 
508
 *  \return \c true, if the sequence is sorted according to comp; \c false, otherwise.
 
509
 *
 
510
 *  \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a>,
 
511
 *          and \p ForwardIterator's \c value_type is convertible to both \c StrictWeakOrdering's \c first_argument_type
 
512
 *          and \c second_argument_type.
 
513
 *  \tparam Compare is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
514
 *
 
515
 *  The following code snippet demonstrates how to use \p is_sorted to test whether the
 
516
 *  contents of a \c device_vector are stored in descending order.
 
517
 *
 
518
 *  \code
 
519
 *  #include <thrust/sort.h>
 
520
 *  #include <thrust/functional.h>
 
521
 *  #include <thrust/device_vector.h>
 
522
 *  ...
 
523
 *  thrust::device_vector<int> v(6);
 
524
 *  v[0] = 1;
 
525
 *  v[1] = 4;
 
526
 *  v[2] = 2;
 
527
 *  v[3] = 8;
 
528
 *  v[4] = 5;
 
529
 *  v[5] = 7;
 
530
 *
 
531
 *  thrust::greater<int> comp;
 
532
 *  bool result = thrust::is_sorted(v.begin(), v.end(), comp);
 
533
 *
 
534
 *  // result == false
 
535
 *
 
536
 *  thrust::sort(v.begin(), v.end(), comp);
 
537
 *  result = thrust::is_sorted(v.begin(), v.end(), comp);
 
538
 *
 
539
 *  // result == true
 
540
 *  \endcode
 
541
 *
 
542
 *  \see http://www.sgi.com/tech/stl/is_sorted.html
 
543
 *  \see \c sort
 
544
 *  \see \c stable_sort
 
545
 *  \see \c less<T>
 
546
 */
 
547
template<typename ForwardIterator, typename Compare>
 
548
  bool is_sorted(ForwardIterator first,
 
549
                 ForwardIterator last,
 
550
                 Compare comp);
 
551
 
 
552
/*! This version of \p is_sorted_until returns the last iterator \c i in <tt>[first,last]</tt> for
 
553
 *  which the range <tt>[first,last)</tt> is sorted using \c operator<. If <tt>distance(first,last) < 2</tt>,
 
554
 *  \p is_sorted_until simply returns \p last.
 
555
 *
 
556
 *  \param first The beginning of the range of interest.
 
557
 *  \param last The end of the range of interest.
 
558
 *  \return The last iterator in the input range for which it is sorted.
 
559
 *
 
560
 *  \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a> and
 
561
 *          \p ForwardIterator's \c value_type is a model of <a href="http://www.sgi.com/tech/stl/LessThanComparable.html">LessThan Comparable</a>.
 
562
 *  
 
563
 *  \code
 
564
 *  #include <thrust/sort.h>
 
565
 *
 
566
 *  ...
 
567
 *   
 
568
 *  int A[8] = {0, 1, 2, 3, 0, 1, 2, 3};
 
569
 *  
 
570
 *  int * B = thrust::is_sorted_until(A, A + 8);
 
571
 *  
 
572
 *  // B - A is 4
 
573
 *  // [A, B) is sorted
 
574
 *  \endcode
 
575
 *
 
576
 *  \see \p is_sorted
 
577
 *  \see \p sort
 
578
 *  \see \p sort_by_key
 
579
 *  \see \p stable_sort
 
580
 *  \see \p stable_sort_by_key
 
581
 */
 
582
template<typename ForwardIterator>
 
583
  ForwardIterator is_sorted_until(ForwardIterator first,
 
584
                                  ForwardIterator last);
 
585
 
 
586
/*! This version of \p is_sorted_until returns the last iterator \c i in <tt>[first,last]</tt> for
 
587
 *  which the range <tt>[first,last)</tt> is sorted using the function object \c comp. If <tt>distance(first,last) < 2</tt>,
 
588
 *  \p is_sorted_until simply returns \p last.
 
589
 *
 
590
 *  \param first The beginning of the range of interest.
 
591
 *  \param last The end of the range of interest.
 
592
 *  \param comp The function object to use for comparison.
 
593
 *  \return The last iterator in the input range for which it is sorted.
 
594
 *
 
595
 *  \tparam ForwardIterator is a model of <a href="http://www.sgi.com/tech/stl/ForwardIterator.html">Forward Iterator</a> and
 
596
 *          \p ForwardIterator's \c value_type is convertible to \p Compare's \c argument_type.
 
597
 *  \tparam Compare is a model of <a href="http://www.sgi.com/tech/stl/StrictWeakOrdering.html">Strict Weak Ordering</a>.
 
598
 *
 
599
 *  \code
 
600
 *  #include <thrust/sort.h>
 
601
 *  #include <thrust/functional.h>
 
602
 *
 
603
 *  ...
 
604
 *   
 
605
 *  int A[8] = {3, 2, 1, 0, 3, 2, 1, 0};
 
606
 *  
 
607
 *  thrust::greater<int> comp;
 
608
 *  int * B = thrust::is_sorted_until(A, A + 8, comp);
 
609
 *  
 
610
 *  // B - A is 4
 
611
 *  // [A, B) is sorted in descending order
 
612
 *  \endcode
 
613
 *
 
614
 *  \see \p is_sorted
 
615
 *  \see \p sort
 
616
 *  \see \p sort_by_key
 
617
 *  \see \p stable_sort
 
618
 *  \see \p stable_sort_by_key
 
619
 */
 
620
template<typename ForwardIterator, typename Compare>
 
621
  ForwardIterator is_sorted_until(ForwardIterator first,
 
622
                                  ForwardIterator last,
 
623
                                  Compare comp);
 
624
 
 
625
/*! \} // end predicates
 
626
 *  \} // end reductions
 
627
 */
 
628
 
 
629
} // end namespace thrust
 
630
 
 
631
#include <thrust/detail/sort.inl>
 
632