template<typename SORTCLASS, BASESORTFLAGS FLAGS = BASESORTFLAGS::NONE>
class maxon::BaseSort< SORTCLASS, FLAGS >
Template for sorting arrays and searching in sorted arrays
To use this template derive a class from it and define the member 'LessThan' that implements an element comparison. If you do searching the member 'IsEqual' needs to be implemented that does an element comparison with the search key as the first argument. If the search key is different from the element type a second LessThan routine is needed (comparing SEARCHKEY to an element).
- Template Parameters
-
SORTCLASS | The class itself. This has to be a derived class of BaseSort. |
FLAGS | See BASESORTFLAGS. |
- Note
- Note that the classes that will be sorted have special requirements regarding copy and move constructors .
-
Note that the comparison must fulfill the condition (a < b) == !(b < a). If this is not the case the sort algorithm will crash. To avoid mistakes when comparing tuples use LexicographicalCompare.
Example for traditional C-style arrays:
class Sort1 : public BaseSort<Sort1>
{
public:
{
return a < b;
}
};
...
Int array1[100];
...
Sort1 sort1;
sort1.Sort(&array1[0], &array1[100]);
sort1.Sort(array1, 100);
Int64 Int
signed 32/64 bit int, size depends on the platform
Definition: apibase.h:187
bool Bool
boolean type, possible values are only false/true, 8 bit
Definition: apibase.h:180
Bool LessThan(UInt a1, UInt a2, UInt b1, UInt b2)
Definition: integer.h:151
- Note
- LessThan can be a member of the sort class itself (not static), but needs to be const in that case.
Example for arrays:
class Sort2 : public BaseSort<Sort2>
{
public:
{
return a < b;
}
};
...
...
Sort2 sort2;
sort2.Sort(array2.Begin(), array2.End());
sort2.Sort(array2);
Example for arbitrary structures with multiple sort criteria:
class MyStruct
{
public:
String _str;
};
class Sort3 : public BaseSort<Sort3>
{
public:
static inline Bool LessThan(
const MyStruct& a,
const MyStruct& b)
{
if (a._weight < b._weight) return true;
if (a._weight > b._weight) return false;
return a._index < b._index;
}
};
...
BlockArray<MyStruct> array3;
...
Sort3 sort3;
sort3.Sort(array3);
Float64 Float
Definition: apibase.h:196
Searching Example:
class MyStruct
{
public:
String _str;
};
class MySearch : public BaseSort <MySearch, BlockArray<MyStruct>::Iterator>
{
public:
static inline Bool LessThan(
const MyStruct& a,
const MyStruct& b)
{
}
{
return weight < b._weight;
}
{
return weight == b._weight;
}
};
...
BlockArray<MyStruct> array4;
...
MySearch search;
search.Sort(array4);
...
MyStruct*
result = search.Find(5.0, array4);
PyObject PyObject * result
Definition: abstract.h:43
COMPARERESULT LexicographicalCompare()
Definition: compare.h:668
MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
Definition: collection.h:102
|
template<typename ITERATOR > |
void | Sort (ITERATOR start, ITERATOR end) const |
|
template<typename ITERATOR > |
void | Sort (ITERATOR start, Int count) const |
|
template<typename ARRAY > |
void | Sort (ARRAY &arr) const |
|
template<typename SEARCHTYPE , typename ITERATOR > |
Int | FindIndex (const SEARCHTYPE &key, ITERATOR arr, Int count) const |
|
template<typename SEARCHTYPE , typename ITERATOR > |
ITERATOR | Find (const SEARCHTYPE &key, ITERATOR arr, Int count) const |
|
template<typename ARRAY , typename SEARCHTYPE > |
ARRAY::ValueType * | Find (const SEARCHTYPE &key, const ARRAY &arr) const |
|
template<typename SEARCHTYPE , typename ITERATOR > |
ITERATOR | FindInsertionIndex (const SEARCHTYPE &key, ITERATOR arr, Int count, Int &insertionIndex) const |
|
template<typename ARRAY , typename SEARCHTYPE > |
ARRAY::ValueType * | FindInsertionIndex (const SEARCHTYPE &key, const ARRAY &arr, Int &insertionIndex) const |
|