latest version v1.9 - last update 10 Apr 2010 |
Sort two vectors, using the first one as key. More...
#include <ltiSort.h>
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 |
sort2 & | copy (const sort2 &other) |
virtual functor * | clone () const |
const parameters & | getParameters () const |
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<.
lti::sort2< T, U >::sort2 | ( | const bool & | descendingOrder = false , |
|
const bool & | sortRows = true | |||
) |
Default constructor.
lti::sort2< T, U >::sort2 | ( | const sort2< T, U > & | other | ) |
Copy constructor.
other | the object to be copied |
lti::sort2< T, U >::sort2 | ( | const parameters & | par | ) |
Construct with given parameters.
virtual lti::sort2< T, U >::~sort2 | ( | ) | [virtual] |
Destructor.
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.
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. |
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.
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. |
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 |
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. |
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.
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 |
bool lti::sort2< T, U >::apply | ( | vector< T > & | key, | |
vector< U > & | srcdest | |||
) | const |
Operates on the given parameter.
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 |
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 |
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 |
virtual functor* lti::sort2< T, U >::clone | ( | ) | const [virtual] |
Returns a pointer to a clone of this functor.
Reimplemented from lti::sort< T >.
sort2& lti::sort2< T, U >::copy | ( | const sort2< T, U > & | other | ) |
Copy data of "other" functor.
other | the functor to be copied |
Reimplemented from lti::sort< T >.
const parameters& lti::sort2< T, U >::getParameters | ( | ) | const |
Returns used parameters.
Reimplemented from lti::sort< T >.
virtual const char* lti::sort2< T, U >::getTypeName | ( | ) | const [virtual] |
Returns the name of this type ("sort2").
Reimplemented from lti::sort< T >.