|
| 1 | +# inplace sorts |
| 2 | +function Base.sort!( |
| 3 | + C::Union{GBArray, Nothing}, #output matrix, possibly aliased with A, |
| 4 | + #or Nothing if just interested in P |
| 5 | + P::Union{GBArray, Nothing}, # permutation matrix, possibly Nothing if just interested in C |
| 6 | + A::GBArray; #input matrix, possibly aliased with C. |
| 7 | + dims = nothing, |
| 8 | + lt = <, |
| 9 | + desc = nothing |
| 10 | + # We only support a limited set of the keywords for now. |
| 11 | + # Missing by, rev, order, alg |
| 12 | +) |
| 13 | + A isa GBMatOrTranspose && dims === nothing && throw(ArgumentError("dims must be either 1 (sort columns) or 2 (sort rows) for matrix arguments.")) |
| 14 | + A isa GBVector && (dims = 1) |
| 15 | + C, P = _handlenothings(C, P) |
| 16 | + C == C_NULL && P == C_NULL && throw(ArgumentError("One (or both) of C and P must not be nothing.")) |
| 17 | + op = BinaryOp(lt)(eltype(A)) |
| 18 | + if dims == 1 |
| 19 | + transpose = true |
| 20 | + elseif dims == 2 |
| 21 | + transpose = false |
| 22 | + else |
| 23 | + throw(ArgumentError("dims must be either 1 (sort columns) or 2 (sort rows)")) |
| 24 | + end |
| 25 | + desc = _handledescriptor(desc; in1=A) |
| 26 | + desc.transpose_input1 = transpose |
| 27 | + @wraperror LibGraphBLAS.GxB_Matrix_sort(C, P, op, parent(A), desc) |
| 28 | + return C |
| 29 | +end |
| 30 | + |
| 31 | +function Base.sort!(A::GBArray; dims = nothing, lt = <, desc = nothing) |
| 32 | + return sort!(A, nothing, A; dims, lt, desc) |
| 33 | +end |
| 34 | + |
| 35 | +function Base.sort!(C::GBArray, A::GBArray; dims, lt = <, desc = nothing) |
| 36 | + return sort!(C, nothing, A; dims, lt, desc) |
| 37 | +end |
| 38 | + |
| 39 | +function Base.sortperm!(P::GBArray, A::GBArray; dims = nothing, lt = <, desc = nothing) |
| 40 | + sort!(nothing, P, A; dims, lt, desc) |
| 41 | + return P |
| 42 | +end |
| 43 | + |
| 44 | +function Base.sort(A::GBArray; dims = nothing, lt = <, desc = nothing) |
| 45 | + C = similar(A) |
| 46 | + return sort!(C, A; dims, lt, desc) |
| 47 | +end |
| 48 | + |
| 49 | +function Base.sortperm(A::GBArray; dims = nothing, lt = <, desc = nothing) |
| 50 | + P = similar(A, Int64) |
| 51 | + return sortperm!(P, A; dims, lt, desc) |
| 52 | +end |
| 53 | + |
0 commit comments