1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 
16 /*----------------------------------------------------------------------------------*/
17 /* File    : sciMatrix.c                                                                     */
18 /* Desc.   : Allocation and deletion and modifications of matrices of pointers               */
19 /*           The matrix is stored by colmuns like in Scilab.                                 */
20 /*           These matrices acan be used as generic matrices since they used void * pointers */
21 /*----------------------------------------------------------------------------------*/
22 
23 #include <string.h>
24 #include "sciMatrix.h"
25 #include "sci_malloc.h"
26 
27 /*----------------------------------------------------------------------------------*/
emptyMatrix(void)28 sciMatrix * emptyMatrix(void)
29 {
30     sciMatrix * newMat;
31     newMat = MALLOC(sizeof(sciMatrix));
32     if (newMat == NULL)
33     {
34         return NULL ;
35     }
36     newMat->data  = NULL;
37     newMat->nbCol = 0   ;
38     newMat->nbRow = 0   ;
39 
40     return newMat;
41 }
42 /*----------------------------------------------------------------------------------*/
newMatrix(int nbRow,int nbCol)43 sciMatrix * newMatrix(int nbRow, int nbCol)
44 {
45     int i = 0;
46     /* create the matrix */
47     sciMatrix * newMat = emptyMatrix();
48 
49     /* allocate the data */
50     newMat->data  = MALLOC((nbRow * nbCol) * sizeof(void *));
51     newMat->nbRow = nbRow;
52     newMat->nbCol = nbCol;
53 
54     /* initialize to NULL */
55     for (i = 0 ; i < nbRow * nbCol ; i++)
56     {
57         newMat->data[i] = NULL;
58     }
59 
60     return newMat;
61 }
62 /*----------------------------------------------------------------------------------*/
newCompleteMatrix(void ** dataMat,int nbRow,int nbCol)63 sciMatrix * newCompleteMatrix(void ** dataMat, int nbRow, int nbCol)
64 {
65     /* create the matrix */
66     sciMatrix * newMat = emptyMatrix();
67 
68     newMat->data  = dataMat;
69     newMat->nbRow = nbRow  ;
70     newMat->nbCol = nbCol  ;
71 
72     return newMat;
73 }
74 /*----------------------------------------------------------------------------------*/
deleteMatrix(sciMatrix * mat)75 void deleteMatrix(sciMatrix * mat)
76 {
77     int i = 0;
78     for (i = 0 ; i < mat->nbRow * mat->nbCol ; i++)
79     {
80         FREE(mat->data[i]);
81         mat->data[i] = NULL;
82     }
83     FREE(mat->data);
84     mat->data = NULL;
85 
86     mat->nbCol = 0;
87     mat->nbRow = 0;
88 
89     FREE(mat);
90 }
91 /*----------------------------------------------------------------------------------*/
desallocateMatrix(sciMatrix * mat)92 void desallocateMatrix(sciMatrix * mat)
93 {
94     mat->nbCol = 0;
95     mat->nbRow = 0;
96     mat->data  = NULL;
97     FREE(mat);
98 }
99 /*----------------------------------------------------------------------------------*/
getMatElement(const sciMatrix * mat,int row,int col)100 void * getMatElement(const sciMatrix * mat, int row, int col)
101 {
102     /* the matrix is stored by column like in scilab */
103 
104     return mat->data[row + col * mat->nbRow];
105 }
106 /*----------------------------------------------------------------------------------*/
setMatElement(sciMatrix * mat,int row,int col,void * newValue)107 void setMatElement(sciMatrix * mat, int row, int col, void * newValue)
108 {
109     mat->data[row + col * mat->nbRow] = newValue;
110 }
111 /*----------------------------------------------------------------------------------*/
changeMatElement(sciMatrix * mat,int row,int col,void * newValue)112 void changeMatElement(sciMatrix * mat, int row, int col, void * newValue)
113 {
114     if (mat->data[row + col * mat->nbRow] != NULL)
115     {
116         FREE(mat->data[row + col * mat->nbRow]);
117     }
118     mat->data[row + col * mat->nbRow] = newValue;
119 }
120 /*----------------------------------------------------------------------------------*/
copyMatElement(sciMatrix * mat,int row,int col,const void * copyValue,int valueSize)121 void copyMatElement(      sciMatrix     * mat      ,
122                           int             row      ,
123                           int             col      ,
124                           const void          * copyValue,
125                           int             valueSize)
126 {
127     /* copy the value */
128     void * newValue = MALLOC(valueSize);
129     memcpy(newValue, copyValue, valueSize);
130 
131     /* change the current one */
132     changeMatElement(mat, row, col, newValue);
133 }
134 /*----------------------------------------------------------------------------------*/
getMatNbRow(const sciMatrix * mat)135 int getMatNbRow(const sciMatrix * mat)
136 {
137     return mat->nbRow ;
138 }
139 /*----------------------------------------------------------------------------------*/
getMatNbCol(const sciMatrix * mat)140 int getMatNbCol(const sciMatrix * mat)
141 {
142     return mat->nbCol ;
143 }
144 /*----------------------------------------------------------------------------------*/
getMatData(const sciMatrix * mat)145 void ** getMatData(const sciMatrix * mat)
146 {
147     return mat->data ;
148 }
149 /*----------------------------------------------------------------------------------*/
150