1function [I, whole] = gb_index (I)
2%GB_INDEX helper function for subsref and subsasgn
3% [I, whole] = gb_index (I) converts I into a cell array of MATLAB
4% matrices or vectors containing integer indices, to access A(I).
5%
6%   I = { }: this denotes A(:), accessing all rows or all columns.
7%       In this case, the parameter whole is returned as true.
8%
9%   I = { list }: denotes A(list)
10%
11%   I = { start,fini }: denotes A(start:fini), without forming
12%       the explicit list start:fini.
13%
14%   I = { start,inc,fini }: denotes A(start:inc:fini), without forming
15%       the explicit list start:inc:fini.
16%
17% The input I can be a GraphBLAS matrix (as an object or its opaque
18% struct).  In this case, it is wrapped in a cell, I = {gb_index1(I)},
19% but kept as 1-based indices (they are later translated to 0-based).
20%
21% If the input is already a cell array, then it is already in one of the
22% above forms.  Any member of the cell array that is a GraphBLAS matrix or
23% struct is converted into an index list, with gb_index1(I{k}).
24%
25% MATLAB passes the string I = ':' to the subsref and subsasgn methods.
26% This is converted into I = { }.
27%
28% If I is a MATLAB matrix or vector (not a cell array), then it is
29% wrapped in a cell array, { I }, to denote A(I).
30
31% SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
32% SPDX-License-Identifier: GPL-3.0-or-later
33
34whole = false ;
35
36if (isobject (I))
37
38    % C (I) where I is a GraphBLAS matrix/vector of integer indices
39    I = I.opaque ;
40    I = { (gb_index1 (I)) } ;
41
42elseif (isstruct (I))
43
44    % C (I) where I is the opaque struct of a GrB matrix/vector
45    I = { (gb_index1 (I)) } ;
46
47elseif (iscell (I))
48
49    % The index I already appears as a cell, for the usage
50    % C ({ }), C ({ I }), C ({start,fini}), or C ({start,inc,fini}).
51    len = length (I) ;
52    if (len > 3)
53        error ('invalid indexing: usage is A ({start,inc,fini})') ;
54    elseif (len == 0)
55        % C ({ })
56        whole = true ;
57    else
58        % C ({ I }), C ({start,fini}), or C ({start,inc,fini})
59        for k = 1:length(I)
60            K = I {k} ;
61            if (isobject (K))
62                % C ({ ..., K, ... }) where K is a GraphBLAS object
63                K = K.opaque ;
64            end
65            if (isstruct (K))
66                % C ({ ..., K, ... }) where I is a GraphBLAS struct
67                I {k} = gb_index1 (K) ;
68            end
69        end
70    end
71
72elseif (ischar (I) && isequal (I, ':'))
73
74    % C (:)
75    I = { } ;
76    whole = true ;
77
78else
79
80    % C (I) where I is a MATLAB matrix/vector of integer indices
81    I = { I } ;
82
83end
84
85