LTI-Lib latest version v1.9 - last update 10 Apr 2010

lti::sort2< T, U > Class Template Reference

Sort two vectors, using the first one as key. More...

#include <ltiSort.h>

Inheritance diagram for lti::sort2< T, U >:
Inheritance graph
[legend]
Collaboration diagram for lti::sort2< T, U >:
Collaboration graph
[legend]

List of all members.

Classes

class  parameters
 The parameters for the class sort. More...

Public Member Functions

 sort2 (const bool &descendingOrder=false, const bool &sortRows=true)
 sort2 (const sort2< T, U > &other)
 sort2 (const parameters &par)
virtual ~sort2 ()
virtual const char * getTypeName () const
bool apply (matrix< T > &key, matrix< U > &srcdest) const
bool apply (vector< T > &key, vector< U > &srcdest) const
bool apply (vector< T > &key, matrix< U > &srcdest) const
bool apply (const matrix< T > &key, const matrix< U > &src, matrix< T > &destkey, matrix< U > &dest) const
bool apply (const vector< T > &key, const vector< U > &src, vector< T > &destkey, vector< U > &dest) const
bool apply (const vector< T > &key, const matrix< U > &src, vector< T > &destkey, matrix< U > &dest) const
sort2copy (const sort2 &other)
virtual functorclone () const
const parametersgetParameters () const

Detailed Description

template<class T, class U = int>
class lti::sort2< T, U >

Sort two vectors, using the first one as key.

This class is used to sort the elements of two given vectors or matrices. The first one (of type T) contains always the keys to be used by sorting, and the second (of type U) one will be sorted accordingly to first one.

For example, if you have a second vector of integers, which was initialized with the indices (0 for the first element, 1 for the second and so on), at the end you can use this sorted vector to know which position has an element of the first vector after sorting:

 // the key vector
 const float fdata[] = {3.2, 1.5, 4.2, 2.0};
 lti::vector<float> a(4,fdata);

 // the data vector
 const int idata[] = {0,1,2,3};
 lti::vector<int> idx(4,idata);

 // sorting object:
 sort2<float,int> sorter;

 // sort the vector a, and accordingly the vector b
 sorter.apply(a,b);

 // after this you will get:
 // a = 1.5, 2.0, 3.2, 4.2
 // b = 1  , 3  , 0  , 2

The sort2::parameters inherit from sort::parameters, and therefore you can also here specify the sorting order and the threshold for applying bubble- sort.

This functor requires that the type T accept the operator<.

See also:
lti::scramble, lti::sort, lti::quickPartialSort2

Constructor & Destructor Documentation

template<class T, class U = int>
lti::sort2< T, U >::sort2 ( const bool &  descendingOrder = false,
const bool &  sortRows = true 
)

Default constructor.

Parameters:
descendingOrder if true, the vector/matrix will be sorted in descending order. If false, in ascending order.
sortRows used in apply(vector,matrix). If true, then the rows of the matrix are sorted using vector as a key. Otherwise the columns of the matrix will be sorted.
template<class T, class U = int>
lti::sort2< T, U >::sort2 ( const sort2< T, U > &  other  ) 

Copy constructor.

Parameters:
other the object to be copied
template<class T, class U = int>
lti::sort2< T, U >::sort2 ( const parameters par  ) 

Construct with given parameters.

template<class T, class U = int>
virtual lti::sort2< T, U >::~sort2 (  )  [virtual]

Destructor.


Member Function Documentation

template<class T, class U = int>
bool lti::sort2< T, U >::apply ( const vector< T > &  key,
const matrix< U > &  src,
vector< T > &  destkey,
matrix< U > &  dest 
) const

Sort the rows of the matrix, using as key the elements of the given vector.

For example, the matrix at the left side will be sorted in the matrix at the right side with the key-vector (5 2 3):

  | 2 8 3 |         | 1 4 5 |
  | 1 4 5 |  --->   | 7 9 6 |
  | 7 9 6 |         | 2 8 3 |

The number of rows of the matrix must be equal to the number of elements in the key vector.

Parameters:
key vector<T> with the key data.
src matrix<U> with the rows that will be sorted according to the key data
destkey the sorted key-vector
dest the matrix with sorted rows.
Returns:
true if successful, false otherwise
template<class T, class U = int>
bool lti::sort2< T, U >::apply ( const vector< T > &  key,
const vector< U > &  src,
vector< T > &  destkey,
vector< U > &  dest 
) const

Operates on a copy of the given parameters.

Parameters:
key vector<T> with the key data.
src vector<U> with the source data.
destkey vector<T> where the sorted keys will be left.
dest vector<U> where the sorted data (using the key) will be left.
Returns:
true if successful, false otherwise
template<class T, class U = int>
bool lti::sort2< T, U >::apply ( const matrix< T > &  key,
const matrix< U > &  src,
matrix< T > &  destkey,
matrix< U > &  dest 
) const

Sort all the elements of the matrix.

The elements will be ordered row-wise. For example, the key matrix at the left will be sorted into the matrix at the right side:

  | 2 8 3 |         | 1 2 3 |
  | 1 4 5 |  --->   | 4 5 6 |
  | 7 9 6 |         | 7 8 9 |
Parameters:
key matrix<T> with the key data.
src matrix<U> with the source data.
destkey matrix<T> where the sorted keys will be left.
dest matrix<U> where the sorted data (using the key) will be left.
Returns:
true if successful, false otherwise
template<class T, class U = int>
bool lti::sort2< T, U >::apply ( vector< T > &  key,
matrix< U > &  srcdest 
) const

Sort the rows of the matrix, using as key the elements of the given vector.

For example, the matrix at the left side will be sorted in the matrix at the right side with the key-vector (5 2 3):

  | 2 8 3 |         | 1 4 5 |
  | 1 4 5 |  --->   | 7 9 6 |
  | 7 9 6 |         | 2 8 3 |

The number of rows of the matrix must be equal to the number of elements in the key vector.

Parameters:
key vector<T> with the key data. The result will be left here too.
srcdest matrix<U> with the rows that will be sorted according to the key data
Returns:
true if successful, false otherwise
template<class T, class U = int>
bool lti::sort2< T, U >::apply ( vector< T > &  key,
vector< U > &  srcdest 
) const

Operates on the given parameter.

Parameters:
key vector<T> with the key data. The result will be left here too.
srcdest vector<U> with the data that will be sorted according to the key data
Returns:
true if successful, false otherwise
template<class T, class U = int>
bool lti::sort2< T, U >::apply ( matrix< T > &  key,
matrix< U > &  srcdest 
) const

Sort all the elements of the matrix.

The elements will be ordered row-wise. For example, the key matrix at the left will be sorted into the matrix at the right side:

  | 2 8 3 |         | 1 2 3 |
  | 1 4 5 |  --->   | 4 5 6 |
  | 7 9 6 |         | 7 8 9 |
Parameters:
key matrix<T> with the key data. The result will be left here too.
srcdest matrix<U> with the data that will be sorted according to the key data
Returns:
true if successful, false otherwise
template<class T, class U = int>
virtual functor* lti::sort2< T, U >::clone (  )  const [virtual]

Returns a pointer to a clone of this functor.

Reimplemented from lti::sort< T >.

template<class T, class U = int>
sort2& lti::sort2< T, U >::copy ( const sort2< T, U > &  other  ) 

Copy data of "other" functor.

Parameters:
other the functor to be copied
Returns:
a reference to this functor object

Reimplemented from lti::sort< T >.

template<class T, class U = int>
const parameters& lti::sort2< T, U >::getParameters (  )  const

Returns used parameters.

Reimplemented from lti::sort< T >.

template<class T, class U = int>
virtual const char* lti::sort2< T, U >::getTypeName (  )  const [virtual]

Returns the name of this type ("sort2").

Reimplemented from lti::sort< T >.


The documentation for this class was generated from the following file:

Generated on Sat Apr 10 15:28:44 2010 for LTI-Lib by Doxygen 1.6.1