Open Search
    BaseSort Manual

    About

    maxon::BaseSort is a class template that allows to sort data in arrays and to search in sorted arrays. For a default implementation handling simple data types see maxon::SimpleSort.

    Note
    See also SortedArray.

    Create

    A new sorting class is created by implementing a custom class based on maxon::BaseSort. This custom class may implement:

    • LessThan(): For sorting elements in an array.
    • IsEqual(): For finding elements in an array.

    Further flags that can be set are:

    Sorting

    An instance of the custom sorting class can be used to sort the elements stored in a maxon::BaseArray.

    // This example defines a BaseSort class for a simple data type
    // and applies it to a BaseArray.
    // custom BaseSort based class to sort integer values
    class IntegerSort : public maxon::BaseSort<IntegerSort, maxon::BASESORTFLAGS::MOVEANDCOPYOBJECTS>
    {
    public:
    static inline Bool LessThan(maxon::Int a, maxon::Int b)
    {
    return a < b;
    }
    };
    // prepare array
    // custom function to fill the array with random values
    FillWithRandomNumbers(numbers);
    // sort the elements stored in the array
    IntegerSort sort;
    sort.Sort(numbers);
    // check result
    for (const maxon::Int& number : numbers)
    DiagnosticOutput("Number: @", number);
    Py_ssize_t count
    Definition: abstract.h:640
    Definition: basearray.h:415
    ResultMem Resize(Int newCnt, COLLECTION_RESIZE_FLAGS resizeFlags=COLLECTION_RESIZE_FLAGS::DEFAULT)
    Definition: basearray.h:1209
    Definition: sort.h:176
    maxon::Bool Bool
    Definition: ge_sys_math.h:55
    Int64 Int
    signed 32/64 bit int, size depends on the platform
    Definition: apibase.h:213
    Bool LessThan(UInt a1, UInt a2, UInt b1, UInt b2)
    Definition: integer.h:151
    #define DiagnosticOutput(formatString,...)
    Definition: debugdiagnostics.h:176
    #define iferr_return
    Definition: resultbase.h:1521

    Searching

    To find elements in the given array, the array must be sorted. The custom sorting class must implement both LessThan() and IsEqual().

    // This example defines a BaseSort class for a custom data type
    // and uses it to sort and search in a BaseArray.
    // custom BaseSort based class to sort CustomDate data.
    // it also allows to compare maxon::Int with CustomDate.
    class DateSort : public maxon::BaseSort<DateSort>
    {
    public:
    // compare CustomDate with CustomDate
    static inline Bool LessThan(CustomDate key, CustomDate element)
    {
    if (key.year > element.year)
    return false;
    if (key.year < element.year)
    return true;
    if (key.month > element.month)
    return false;
    if (key.month < element.month)
    return true;
    if (key.day >= element.day)
    return false;
    return true;
    }
    static inline Bool IsEqual(CustomDate key, CustomDate element)
    {
    if (key.year != element.year)
    return false;
    if (key.month != element.month)
    return false;
    if (key.day != element.day)
    return false;
    return true;
    }
    // compare maxon::Int with CustomDate
    static inline Bool LessThan(maxon::Int key, CustomDate element)
    {
    return key < element.year;
    }
    static inline Bool IsEqual(maxon::Int key, CustomDate element)
    {
    return key == element.year;
    }
    };
    // create data
    // custom function to fill the array with random dates
    FillWithRandomDates(dates);
    // sort array
    DateSort sort;
    sort.Sort(dates);
    // search in the array
    const CustomDate* const date = sort.Find(2000, dates);
    if (date)
    DiagnosticOutput("@:@:@", date->year, date->month, date->day);
    const maxon::Int index = sort.FindIndex(2000, dates.Begin(), dates.GetCount());
    if (index > 0)
    DiagnosticOutput("Index: @", index);
    // new element
    CustomDate newDate;
    newDate.year = 1970;
    newDate.month = 1;
    newDate.day = 1;
    // insert new element
    maxon::Int insertionIndex;
    sort.FindInsertionIndex(newDate, dates, insertionIndex);
    dates.Insert(insertionIndex, newDate) iferr_return;
    PyObject * key
    Definition: abstract.h:289
    MAXON_ATTRIBUTE_FORCE_INLINE ResultRef< T > Insert(Int position, ARG &&x)
    Inserts a new element at index position and constructs it using the forwarded value.
    Definition: basearray.h:812
    MAXON_ATTRIBUTE_FORCE_INLINE ConstIterator Begin() const
    Definition: basearray.h:1477
    MAXON_ATTRIBUTE_FORCE_INLINE Int GetCount() const
    Definition: basearray.h:576
    Py_ssize_t * index
    Definition: abstract.h:374
    MAXON_ATTRIBUTE_FORCE_INLINE Bool IsEqual(PREDICATE &&predicate, const T1 &a, const T2 &b)
    Definition: collection.h:102
    PyObject * element
    Definition: unicodeobject.h:1016

    Further Reading