[OpendTect_Developers] (BufferStringSet) sorting
Bert Bril
Bert.Bril at opendtect.org
Wed May 16 07:42:00 CEST 2012
Hi all,
In OpendTect, there are several tools for sorting arrays and sets. Most
of them can be found in Algo/sorting.h . For large sets you may want to
consider parallel sorting but I'll focus on simple smaller sets here.
As you know, very often the sorting is not the only thing you want - you
also want to know the indexing. In OpendTect, these indexing arrays are
simply arrays where arr[N] returns the index of the Nth element of the
input array in the sorted output. You can obtain them using sort_coupled:
mAllocVarLenArr( int, idxs, sz );
mAllocVarLenArr( float, vals, sz );
for ( int idx=0; idx<sz; idx++ )
{
vals[idx] = the_vals[idx];
idxs[idx] = idx;
}
sort_coupled( vals, idxs, sz );
Some classes have a sort() function ready to use, like TypeSet and
BufferStringSet. Because sorting of strings is something you often do to
in combination with other sets (other BufferStringSet's, or IDs, or
...), this class delivers a nice getSortIndexes() function. For example:
sorting two sets for strings on the names in one of them (master/slave),
you do:
int* idxs = getSortIndexes( master );
master.useIndexes( idxs );
slave.useIndexes( idxs );
delete [] idxs;
It's easy to see that the 'idxs' can be used for many other things. In
loops, you will see expressions like 'arr[ idxs[idx] ]' .
/Bert
-- Bert Bril / OpendTect developer at dGB
-- mailto:Bert.Bril at opendtect.org , http://opendtect.org
More information about the Developers
mailing list