summaryrefslogtreecommitdiff
path: root/modules/graphics/src/c/sciMatrix.c
blob: 07e2e4f6e5943728d14321a4e404d5450633f1ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2006 - INRIA - Jean-Baptiste Silvy
 *
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at
 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
 *
 */

/*----------------------------------------------------------------------------------*/
/* File    : sciMatrix.c                                                                     */
/* Desc.   : Allocation and deletion and modifications of matrices of pointers               */
/*           The matrix is stored by colmuns like in Scilab.                                 */
/*           These matrices acan be used as generic matrices since they used void * pointers */
/*----------------------------------------------------------------------------------*/

#include <string.h>
#include "sciMatrix.h"
#include "MALLOC.h"

/*----------------------------------------------------------------------------------*/
sciMatrix * emptyMatrix(void)
{
    sciMatrix * newMat;
    newMat = MALLOC(sizeof(sciMatrix));
    if (newMat == NULL)
    {
        return NULL ;
    }
    newMat->data  = NULL;
    newMat->nbCol = 0   ;
    newMat->nbRow = 0   ;

    return newMat;
}
/*----------------------------------------------------------------------------------*/
sciMatrix * newMatrix(int nbRow, int nbCol)
{
    int i = 0;
    /* create the matrix */
    sciMatrix * newMat = emptyMatrix();

    /* allocate the data */
    newMat->data  = MALLOC((nbRow * nbCol) * sizeof(void *));
    newMat->nbRow = nbRow;
    newMat->nbCol = nbCol;

    /* initialize to NULL */
    for (i = 0 ; i < nbRow * nbCol ; i++)
    {
        newMat->data[i] = NULL;
    }

    return newMat;
}
/*----------------------------------------------------------------------------------*/
sciMatrix * newCompleteMatrix(void ** dataMat, int nbRow, int nbCol)
{
    /* create the matrix */
    sciMatrix * newMat = emptyMatrix();

    newMat->data  = dataMat;
    newMat->nbRow = nbRow  ;
    newMat->nbCol = nbCol  ;

    return newMat;
}
/*----------------------------------------------------------------------------------*/
void deleteMatrix(sciMatrix * mat)
{
    int i = 0;
    for (i = 0 ; i < mat->nbRow * mat->nbCol ; i++)
    {
        FREE(mat->data[i]);
        mat->data[i] = NULL;
    }
    FREE(mat->data);
    mat->data = NULL;

    mat->nbCol = 0;
    mat->nbRow = 0;

    FREE(mat);
}
/*----------------------------------------------------------------------------------*/
void desallocateMatrix(sciMatrix * mat)
{
    mat->nbCol = 0;
    mat->nbRow = 0;
    mat->data  = NULL;
    FREE(mat);
}
/*----------------------------------------------------------------------------------*/
void * getMatElement(const sciMatrix * mat, int row, int col)
{
    /* the matrix is stored by column like in scilab */

    return mat->data[row + col * mat->nbRow];
}
/*----------------------------------------------------------------------------------*/
void setMatElement(sciMatrix * mat, int row, int col, void * newValue)
{
    mat->data[row + col * mat->nbRow] = newValue;
}
/*----------------------------------------------------------------------------------*/
void changeMatElement(sciMatrix * mat, int row, int col, void * newValue)
{
    if (mat->data[row + col * mat->nbRow] != NULL)
    {
        FREE(mat->data[row + col * mat->nbRow]);
    }
    mat->data[row + col * mat->nbRow] = newValue;
}
/*----------------------------------------------------------------------------------*/
void copyMatElement(      sciMatrix     * mat      ,
                          int             row      ,
                          int             col      ,
                          const void          * copyValue,
                          int             valueSize)
{
    /* copy the value */
    void * newValue = MALLOC(valueSize);
    memcpy(newValue, copyValue, valueSize);

    /* change the current one */
    changeMatElement(mat, row, col, newValue);
}
/*----------------------------------------------------------------------------------*/
int getMatNbRow(const sciMatrix * mat)
{
    return mat->nbRow ;
}
/*----------------------------------------------------------------------------------*/
int getMatNbCol(const sciMatrix * mat)
{
    return mat->nbCol ;
}
/*----------------------------------------------------------------------------------*/
void ** getMatData(const sciMatrix * mat)
{
    return mat->data ;
}
/*----------------------------------------------------------------------------------*/