1+ mutable struct OrientedGBMatrix{T, F, O} <: AbstractGBMatrix{T, F}
2+ p:: Ref{LibGraphBLAS.GrB_Matrix}
3+ fill:: F
4+ end
5+
6+ const GBMatrixC{T, F} = OrientedGBMatrix{T, F, StorageOrders. ColMajor ()}
7+ const GBMatrixR{T, F} = OrientedGBMatrix{T, F, StorageOrders. RowMajor ()}
8+ StorageOrders. storageorder (:: OrientedGBMatrix{T, F, O} ) where {T, F, O} = O
9+
10+ # Constructors:
11+ # ##############
12+ """
13+ GBMatrix{T}(nrows, ncols; fill = nothing)
14+
15+ Create a GBMatrix of the specified size, defaulting to the maximum on each dimension, 2^60.
16+ """
17+ function OrientedGBMatrix {T, O} (nrows:: Integer , ncols:: Integer ; fill:: F = nothing ) where {T, F, O}
18+ m = Ref {LibGraphBLAS.GrB_Matrix} ()
19+ @wraperror LibGraphBLAS. GrB_Matrix_new (m, gbtype (T), nrows, ncols)
20+ A = GBMatrix {T, F} (finalizer (m) do ref
21+ @wraperror LibGraphBLAS. GrB_Matrix_free (ref)
22+ end , fill)
23+ gbset (A, :format , O === StorageOrders. ColMajor () ? :bycol : :byrow )
24+ end
25+
26+ OrientedGBMatrix {T, O} (dims:: Dims{2} ; fill = nothing ) where {T, O} = OrientedGBMatrix {T, O} (dims... ; fill)
27+ OrientedGBMatrix {T, O} (dims:: Tuple{<:Integer} ; fill = nothing ) where {T, O} = OrientedGBMatrix {T, O} (dims... ; fill)
28+ OrientedGBMatrix {T, O} (size:: Tuple{Base.OneTo, Base.OneTo} ; fill = nothing ) where {T, O} =
29+ OrientedGBMatrix {T, O} (size[1 ]. stop, size[2 ]. stop; fill)
30+
31+ """
32+ GBMatrix(I, J, X; combine = +, nrows = maximum(I), ncols = maximum(J))
33+
34+ Create an nrows x ncols GBMatrix M such that M[I[k], J[k]] = X[k]. The combine function defaults
35+ to `|` for booleans and `+` for nonbooleans.
36+ """
37+ function OrientedGBMatrix {O} (
38+ I:: AbstractVector , J:: AbstractVector , X:: AbstractVector{T} ;
39+ combine = + , nrows = maximum (I), ncols = maximum (J), fill = nothing
40+ ) where {T, O}
41+ I isa Vector || (I = collect (I))
42+ J isa Vector || (J = collect (J))
43+ X isa Vector || (X = collect (X))
44+ A = OrientedGBMatrix {T, O} (nrows, ncols; fill)
45+ build (A, I, J, X; combine)
46+ return A
47+ end
48+
49+ # iso constructors
50+ """
51+ GBMatrix(I, J, x; nrows = maximum(I), ncols = maximum(J))
52+
53+ Create an nrows x ncols GBMatrix M such that M[I[k], J[k]] = x.
54+ The resulting matrix is "iso-valued" such that it only stores `x` once rather than once for
55+ each index.
56+ """
57+ function OrientedGBMatrix {O} (I:: AbstractVector , J:: AbstractVector , x:: T ;
58+ nrows = maximum (I), ncols = maximum (J), fill = nothing ) where {T, O}
59+ A = OrientedGBMatrix {T, O} (nrows, ncols; fill)
60+ build (A, I, J, x)
61+ return A
62+ end
63+
64+
65+ function OrientedGBMatrix {O} (dims:: Dims{2} , x:: T ; fill = nothing ) where {T, O}
66+ A = OrientedGBMatrix {T, O} (dims; fill)
67+ A[:, :] = x
68+ return A
69+ end
70+
71+ OrientedGBMatrix {O} (nrows, ncols, x:: T ; fill:: F = nothing ) where {T, F, O} = OrientedGBMatrix {O} ((nrows, ncols), x; fill)
72+ function OrientedGBMatrix {O} (v:: GBVector ) where {O}
73+ # this copies, I think that's ideal, and I can implement @view or something at a later date.
74+ return copy (OrientedGBMatrix {eltype(v), typeof(v.fill), O} (v. p, v. fill))
75+ end
76+
77+ Base. unsafe_convert (:: Type{LibGraphBLAS.GrB_Matrix} , A:: OrientedGBMatrix ) = A. p[]
78+
79+ function Base. copy (A:: OrientedGBMatrix{T, F, O} ) where {T, F, O}
80+ C = Ref {LibGraphBLAS.GrB_Matrix} ()
81+ LibGraphBLAS. GrB_Matrix_dup (C, gbpointer (A))
82+ return OrientedGBMatrix {T, F, O} (C, A. fill) # This should automatically be the same orientation.
83+ end
84+
85+ # because of the fill kwarg we have to redo a lot of the Base.similar dispatch stack.
86+ function Base. similar (
87+ A:: OrientedGBMatrix{T, F, O} , :: Type{TNew} = T,
88+ dims:: Tuple{Int64, Vararg{Int64, N}} = size (A); fill = parent (A). fill
89+ ) where {T, TNew, N, F, O}
90+ if dims isa Dims{1 }
91+ return GBVector {TNew} (dims... ; fill)
92+ else
93+ A = OrientedGBMatrix {TNew, O} (dims... ; fill)
94+ end
95+ end
96+
97+ function Base. similar (A:: OrientedGBMatrix{T} , dims:: Tuple ; fill = parent (A). fill) where {T}
98+ return similar (A, T, dims; fill)
99+ end
100+
101+ function Base. similar (
102+ A:: OrientedGBMatrix{T} , :: Type{TNew} ,
103+ dims:: Integer ; fill = parent (A). fill
104+ ) where {T, TNew}
105+ return similar (A, TNew, (dims,); fill)
106+ end
107+
108+ function Base. similar (
109+ A:: OrientedGBMatrix{T} , :: Type{TNew} ,
110+ dim1:: Integer , dim2:: Integer ; fill = parent (A). fill
111+ ) where {T, TNew}
112+ return similar (A, TNew, (dim1, dim2); fill)
113+ end
114+
115+ function Base. similar (
116+ A:: OrientedGBMatrix{T} ,
117+ dims:: Integer ; fill = parent (A). fill
118+ ) where {T}
119+ return similar (A, (dims,); fill)
120+ end
121+
122+ function Base. similar (
123+ A:: OrientedGBMatrix{T} ,
124+ dim1:: Integer , dim2:: Integer ; fill = parent (A). fill
125+ ) where {T}
126+ return similar (A, (dim1, dim2); fill)
127+ end
0 commit comments