cudf-cxx 0.2.0

cxx-based FFI bridge between Rust and NVIDIA libcudf C++ API
Documentation
#include "sorting_shim.h"
#include "order_utils.h"
#include <cudf/sorting.hpp>
#include <cudf/utilities/default_stream.hpp>
#include <stdexcept>

namespace cudf_shims {

std::unique_ptr<OwnedColumn> sorted_order(
    const OwnedTable& table,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::sorted_order(
        table.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedColumn>(std::move(result));
}

std::unique_ptr<OwnedTable> sort(
    const OwnedTable& table,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::sort(
        table.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

std::unique_ptr<OwnedTable> sort_by_key(
    const OwnedTable& values,
    const OwnedTable& keys,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::sort_by_key(
        values.view(),
        keys.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

std::unique_ptr<OwnedTable> stable_sort_by_key(
    const OwnedTable& values,
    const OwnedTable& keys,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::stable_sort_by_key(
        values.view(),
        keys.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

std::unique_ptr<OwnedColumn> rank(
    const OwnedColumn& col,
    int32_t method,
    int32_t column_order,
    int32_t null_order,
    int32_t null_handling,
    bool percentage)
{
    auto m = static_cast<cudf::rank_method>(method);
    auto co = column_order == 0 ? cudf::order::ASCENDING : cudf::order::DESCENDING;
    auto no = null_order == 0 ? cudf::null_order::AFTER : cudf::null_order::BEFORE;
    auto nh = null_handling == 0 ? cudf::null_policy::EXCLUDE : cudf::null_policy::INCLUDE;

    auto result = cudf::rank(
        col.view(),
        m,
        co,
        nh,
        no,
        percentage);

    return std::make_unique<OwnedColumn>(std::move(result));
}

bool is_sorted(
    const OwnedTable& table,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    return cudf::is_sorted(
        table.view(),
        col_order,
        nul_order);
}

std::unique_ptr<OwnedColumn> stable_sorted_order(
    const OwnedTable& table,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::stable_sorted_order(
        table.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedColumn>(std::move(result));
}

std::unique_ptr<OwnedTable> stable_sort(
    const OwnedTable& table,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::stable_sort(
        table.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

std::unique_ptr<OwnedColumn> top_k(
    const OwnedColumn& col,
    int32_t k,
    int32_t order)
{
    auto ord = order == 0 ? cudf::order::ASCENDING : cudf::order::DESCENDING;

    auto result = cudf::top_k(
        col.view(),
        k,
        ord);

    return std::make_unique<OwnedColumn>(std::move(result));
}

// ── Segmented Sorting ─────────────────────────────────────────

std::unique_ptr<OwnedColumn> segmented_sorted_order(
    const OwnedTable& table,
    const OwnedColumn& segment_offsets,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::segmented_sorted_order(
        table.view(),
        segment_offsets.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedColumn>(std::move(result));
}

std::unique_ptr<OwnedColumn> stable_segmented_sorted_order(
    const OwnedTable& table,
    const OwnedColumn& segment_offsets,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::stable_segmented_sorted_order(
        table.view(),
        segment_offsets.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedColumn>(std::move(result));
}

std::unique_ptr<OwnedTable> segmented_sort_by_key(
    const OwnedTable& values,
    const OwnedTable& keys,
    const OwnedColumn& segment_offsets,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::segmented_sort_by_key(
        values.view(),
        keys.view(),
        segment_offsets.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

std::unique_ptr<OwnedTable> stable_segmented_sort_by_key(
    const OwnedTable& values,
    const OwnedTable& keys,
    const OwnedColumn& segment_offsets,
    rust::Slice<const int32_t> column_order,
    rust::Slice<const int32_t> null_order)
{
    auto col_order = to_column_order(column_order);
    auto nul_order = to_null_order(null_order);

    auto result = cudf::stable_segmented_sort_by_key(
        values.view(),
        keys.view(),
        segment_offsets.view(),
        col_order,
        nul_order);

    return std::make_unique<OwnedTable>(std::move(result));
}

} // namespace cudf_shims