MyraMath
sortperm


Source: tests/utility/sortperm.cpp

1 // ========================================================================= //
2 // This file is part of MyraMath, copyright (c) 2014-2019 by Ryan A Chilton //
3 // and distributed by MyraCore, LLC. See LICENSE.txt for license terms. //
4 // ========================================================================= //
5 
14 
15 #include <tests/myratest.h>
16 
17 using namespace myra;
18 using namespace myra_stlprint;
19 
20 ADD_TEST("sortperm","[utility]")
21  {
22  int V = 10;
23  // Fill vector v1 with random values.
24  std::vector<float> v1;
25  for (int v = 0; v < V; ++v)
26  v1.push_back( random<float>() );
27  myra::out() << "v1 = " << v1 << std::endl;
28  // Get a sortperm() for v1.
29  std::vector<int> perm = sortperm(v1.begin(), v1.end(), std::less<float>());
30  myra::out() << "perm = " << perm << std::endl;
31  // Fill vector v2 with sorted values from v1.
32  std::vector<float> v2;
33  for (int v = 0; v < V; ++v)
34  v2.push_back( v1[perm[v]] );
35  myra::out() << "v2 = " << v2 << std::endl;
36  // Verify v2 is sorted.
37  bool sorted = true;
38  for (int v = 0; v < V-1; ++v)
39  if (v2[v] > v2[v+1])
40  sorted = false;
41  REQUIRE(sorted);
42  };
std::vector< int > sortperm(Iterator begin, Iterator end, const Comparator &cmp)
Given a range, returns the permutation that will place it in sorted order according to cmp()...
Definition: sortperm.h:50
Given a range, returns the permutation that will place it in sorted order.
Definition: syntax.dox:1
Definition: stlprint.h:32
Routines for printing the contents of various std::container&#39;s to a std::ostream using operator <<...
Simplistic random number functions.


Results: [PASS]

v1 = [ 0.753231 0.612978 0.773258 -0.568787 0.0041765 -0.0856677 0.346901 0.450497 0.232718 -0.556931 ] (10)
perm = [ 3 9 5 4 8 6 7 1 0 2 ] (10)
v2 = [ -0.568787 -0.556931 -0.0856677 0.0041765 0.232718 0.346901 0.450497 0.612978 0.753231 0.773258 ] (10)


Go back to Summary of /test programs.