1 //------------------------------------------------------------------------------
2 // gb_mxarray_to_list: convert a MATLAB array to a list of integers
3 //------------------------------------------------------------------------------
4
5 // SuiteSparse:GraphBLAS, Timothy A. Davis, (c) 2017-2021, All Rights Reserved.
6 // SPDX-License-Identifier: GPL-3.0-or-later
7
8 //------------------------------------------------------------------------------
9
10 // The MATLAB list may be double, int64, or uint64. If double or 1-based
11 // int64, a new integer list is created, and the 1-based input list is
12 // converted to the 0-based integer list.
13
14 #include "gb_matlab.h"
15
gb_mxarray_to_list(const mxArray * mxList,base_enum_t base,bool * allocated,int64_t * len,int64_t * List_max)16 int64_t *gb_mxarray_to_list // return List of integers
17 (
18 const mxArray *mxList, // list to extract
19 base_enum_t base, // input is zero-based or one-based
20 bool *allocated, // true if output list was allocated
21 int64_t *len, // length of list
22 int64_t *List_max // max entry in the list, if computed
23 )
24 {
25
26 //--------------------------------------------------------------------------
27 // check inputs
28 //--------------------------------------------------------------------------
29
30 CHECK_ERROR (!mxIsNumeric (mxList), "index list must be numeric") ;
31 CHECK_ERROR (mxIsSparse (mxList), "index list cannot be sparse") ;
32 CHECK_ERROR (mxIsComplex (mxList), "index list cannot be complex") ;
33
34 //--------------------------------------------------------------------------
35 // get the length and class of the MATLAB list
36 //--------------------------------------------------------------------------
37
38 (*len) = mxGetNumberOfElements (mxList) ;
39 mxClassID class = mxGetClassID (mxList) ;
40
41 //--------------------------------------------------------------------------
42 // extract the contents and convert to int64_t
43 //--------------------------------------------------------------------------
44
45 (*List_max) = -1 ;
46 bool zerobased = (base == BASE_0_INT64) ;
47
48 if (*len == 0)
49 {
50 (*allocated) = true ;
51 int64_t *List = (int64_t *) mxMalloc (1 * sizeof (int64_t)) ;
52 List [0] = 0 ;
53 return (List) ;
54 }
55 else if (class == mxINT64_CLASS && zerobased)
56 {
57 // input list is int64; just return a shallow pointer
58 (*allocated) = false ;
59 return ((int64_t *) mxGetInt64s (mxList)) ;
60 }
61 else if (class == mxUINT64_CLASS && zerobased)
62 {
63 // input list is uint64; just return a shallow pointer
64 (*allocated) = false ;
65 return ((int64_t *) mxGetUint64s (mxList)) ;
66 }
67 else if (class == mxINT64_CLASS || class == mxUINT64_CLASS ||
68 class == mxDOUBLE_CLASS)
69 {
70 // input list 1-based: decrement to convert to 0-based
71 (*allocated) = true ;
72 int64_t *List = mxMalloc ((*len) * sizeof (int64_t)) ;
73 if (class == mxDOUBLE_CLASS)
74 {
75 // input list is 1-based double
76 double *List_double = mxGetDoubles (mxList) ;
77 CHECK_ERROR (List_double == NULL, "index list must be integer") ;
78 bool ok = GB_matlab_helper3 (List, List_double, (*len), List_max) ;
79 CHECK_ERROR (!ok, "index must be integer") ;
80 }
81 else if (class == mxINT64_CLASS)
82 {
83 // input list is 1-based int64
84 int64_t *List_int64 = (int64_t *) mxGetInt64s (mxList) ;
85 GB_matlab_helper3i (List, List_int64, (*len), List_max) ;
86 }
87 else // if (class == mxUINT64_CLASS)
88 {
89 // input list is 1-based uint64
90 int64_t *List_int64 = (int64_t *) mxGetUint64s (mxList) ;
91 GB_matlab_helper3i (List, List_int64, (*len), List_max) ;
92 }
93 return (List) ;
94 }
95 else
96 {
97 ERROR ("integer array must be double, int64, or uint64") ;
98 return (NULL) ;
99 }
100 }
101
102