<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Index Sort: Example and Test</title>
<meta name="description" id="description" content="Index Sort: Example and Test"/>
<meta name="keywords" id="keywords" content=" index_sort example sort index test "/>
<style type='text/css'>
body { color : black }
body { background-color : white }
A:link { color : blue }
A:visited { color : purple }
A:active { color : purple }
</style>
<script type='text/javascript' language='JavaScript' src='_index_sort.cpp_xml.js'>
</script>
</head>
<body>
<table><tr>
<td>
<a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a>
</td>
<td><a href="index_sort.xml" target="_top">Prev</a>
</td><td><a href="benderquad.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</option>
<option>contents</option>
<option>reference</option>
<option>index</option>
<option>search</option>
<option>external</option>
</select>
</td>
<td>
<select onchange='choose_up0(this)'>
<option>Up-&gt;</option>
<option>CppAD</option>
<option>library</option>
<option>index_sort</option>
<option>index_sort.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>library-&gt;</option>
<option>ErrorHandler</option>
<option>NearEqual</option>
<option>speed_test</option>
<option>SpeedTest</option>
<option>time_test</option>
<option>NumericType</option>
<option>CheckNumericType</option>
<option>SimpleVector</option>
<option>CheckSimpleVector</option>
<option>nan</option>
<option>pow_int</option>
<option>Poly</option>
<option>LuDetAndSolve</option>
<option>RombergOne</option>
<option>RombergMul</option>
<option>Runge45</option>
<option>Rosen34</option>
<option>OdeErrControl</option>
<option>OdeGear</option>
<option>OdeGearControl</option>
<option>CppAD_vector</option>
<option>thread_alloc</option>
<option>index_sort</option>
<option>BenderQuad</option>
<option>opt_val_hes</option>
<option>LuRatio</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>index_sort-&gt;</option>
<option>index_sort.cpp</option>
</select>
</td>
<td>index_sort.cpp</td>
<td>Headings</td>
</tr></table><br/>


<center><b><big><big>Index Sort: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/index_sort.hpp&gt;
# include &lt;cppad/vector.hpp&gt;
# include &lt;valarray&gt;
# include &lt;vector&gt;


namespace{
	// class that uses &lt; to compare a pair of size_t values
	class Key {
	public:
		size_t first_;
		size_t second_;
		//
		Key(void)
		{ }
		//
		Key(size_t first, size_t second)
		: first_(first), second_(second)
		{ }
		//
		bool operator&lt;(const Key&amp; other) const
		{	if( first_ == other.first_ )
				return second_ &lt; other.second_;
			return first_ &lt; other.first_;
		}
	};

	template &lt;class VectorKey, class VectorSize&gt;
	bool vector_case(void)
	{	bool ok = true;
		size_t i, j;
		size_t first[]  =  { 4, 4, 3, 3, 2, 2, 1, 1};
		size_t second[] = { 0, 1, 0, 1, 0, 1, 0, 1};
		size_t size     = sizeof(first) / sizeof(first[0]);
	
		VectorKey keys(size);
		for(i = 0; i &lt; size; i++)
			keys[i] = Key(first[i], second[i]);
	
		VectorSize ind(size);
		CppAD::index_sort(keys, ind);
	
		// check that all the indices are different
		for(i = 0; i &lt; size; i++)
		{	for(j = 0; j &lt; size; j++)
				ok &amp;= (i == j) | (ind[i] != ind[j]);
		}
		
		// check for increasing order
		for(i = 0; i &lt; size-1; i++)
		{	if( first[ ind[i] ] == first[ ind[i+1] ] )
				ok &amp;= second[ ind[i] ] &lt;= second[ ind[i+1] ];
			else	ok &amp;= first[ ind[i] ] &lt; first[ ind[i+1] ];
		} 
	
		return ok;
	}
}

bool index_sort(void)
{	bool ok = true;

	// some example simple vector template classes
	ok &amp;= vector_case&lt;  std::vector&lt;Key&gt;,  std::valarray&lt;size_t&gt; &gt;();
	ok &amp;= vector_case&lt; std::valarray&lt;Key&gt;, CppAD::vector&lt;size_t&gt; &gt;();
	ok &amp;= vector_case&lt; CppAD::vector&lt;Key&gt;,   std::vector&lt;size_t&gt; &gt;();

	return ok;
}

</pre>

</font></code>


<hr/>Input File: example/index_sort.cpp

</body>
</html>
