Skip to content

Commit 6c7572b

Browse files
committed
sort functionality and a note about internal aliasing
1 parent ca0a2d1 commit 6c7572b

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

src/SuiteSparseGraphBLAS.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ include("operations/reduce.jl")
6767
include("operations/kronecker.jl")
6868
include("operations/concat.jl")
6969
include("operations/resize.jl")
70+
include("operations/sort.jl")
7071
#
7172
include("print.jl")
7273
include("import.jl")
7374
include("export.jl")
7475
include("pack.jl")
7576
include("unpack.jl")
7677
include("options.jl")
77-
#EXPERIMENTAL
78+
7879
include("operations/broadcasts.jl")
7980
include("chainrules/chainruleutils.jl")
8081
include("chainrules/mulrules.jl")
@@ -83,6 +84,8 @@ include("chainrules/maprules.jl")
8384
include("chainrules/reducerules.jl")
8485
include("chainrules/selectrules.jl")
8586
include("chainrules/constructorrules.jl")
87+
88+
#EXPERIMENTAL
8689
include("misc.jl")
8790
include("asjulia.jl")
8891
include("spmgb/sparsemat.jl")

src/operations/sort.jl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+

src/types.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ The storage type is automatically determined by the library.
254254
"""
255255
mutable struct GBMatrix{T} <: AbstractSparseArray{T, UInt64, 2}
256256
p::LibGraphBLAS.GrB_Matrix
257+
# NOTE WELL: The alias kwarg IS NOT for public use.
258+
# It is used in very few places to convert a GBVector to a GBMatrix internally.
257259
function GBMatrix{T}(p::LibGraphBLAS.GrB_Matrix; aliased=false) where {T}
258260
A = new(p)
259261
function f(matrix)

0 commit comments

Comments
 (0)