sciMatrix.c

Go to the documentation of this file.
00001 /*-------------------------------------------------------------------------------------------*/
00002 /* COPYRIGHT INRIA 2006                                                                      */
00003 /* File    : PointerMatrix.c                                                                 */
00004 /* Authors : Jean-Baptiste Silvy 2006-xxxx                                                   */
00005 /* Desc.   : Allocation and deletion and modifications of matrices of pointers               */
00006 /*           The matrix is stored by colmuns like in Scilab.                                 */
00007 /*           These matrices acan be used as generic matrices since they used void * pointers */
00008 /*-------------------------------------------------------------------------------------------*/
00009 
00010 #include "sciMatrix.h"
00011 #include "string.h"
00012 #include "MALLOC.h"
00013 #include <stdlib.h>
00014 
00015 /*-------------------------------------------------------------------------------------------*/
00016 sciMatrix * emptyMatrix( void )
00017 {
00018   sciMatrix * newMat ;
00019   newMat = MALLOC( sizeof(sciMatrix) ) ;
00020   newMat->data  = NULL ;
00021   newMat->nbCol = 0    ;
00022   newMat->nbRow = 0    ;
00023   
00024   return newMat ;
00025 }
00026 /*-------------------------------------------------------------------------------------------*/
00027 sciMatrix * newMatrix( int nbRow, int nbCol )
00028 {
00029   int i ;
00030   /* create the matrix */
00031   sciMatrix * newMat = emptyMatrix() ;
00032   
00033   /* allocate the data */
00034   newMat->data  = MALLOC( (nbRow * nbCol)*sizeof(void *) ) ;
00035   newMat->nbRow = nbRow ;
00036   newMat->nbCol = nbCol ;
00037 
00038   /* initialize to NULL */
00039   for ( i = 0 ; i < nbRow * nbCol ; i++ )
00040   {
00041     newMat->data[i] = NULL ;
00042   }
00043 
00044   return newMat ;
00045 }
00046 /*-------------------------------------------------------------------------------------------*/
00047 sciMatrix * newCompleteMatrix( void ** dataMat, int nbRow, int nbCol )
00048 {
00049   /* create the matrix */
00050   sciMatrix * newMat = emptyMatrix() ;
00051   
00052   newMat->data  = dataMat ;
00053   newMat->nbRow = nbRow   ;
00054   newMat->nbCol = nbCol   ;
00055   
00056   return newMat ;
00057 }
00058 /*-------------------------------------------------------------------------------------------*/
00059 void deleteMatrix( sciMatrix * mat )
00060 {
00061   int i ;
00062   for ( i = 0 ; i < mat->nbRow * mat->nbCol ; i++ )
00063   {
00064     FREE( mat->data[i] ) ;
00065     mat->data[i] = NULL ;
00066   }
00067   FREE( mat->data ) ;
00068   mat->data = NULL ;
00069 
00070   mat->nbCol = 0 ;
00071   mat->nbRow = 0 ;
00072 
00073   FREE( mat ) ;
00074 }
00075 /*-------------------------------------------------------------------------------------------*/
00076 void desallocateMatrix( sciMatrix * mat )
00077 {
00078   mat->nbCol = 0 ;
00079   mat->nbRow = 0 ;
00080   mat->data  = NULL ;
00081   FREE( mat ) ;
00082 }
00083 /*-------------------------------------------------------------------------------------------*/
00084 void * getMatElement( const sciMatrix * mat, int row, int col )
00085 {
00086   /* the matrix is stored by column like in scilab */
00087   
00088   return mat->data[row + col * mat->nbRow] ;
00089 }
00090 /*-------------------------------------------------------------------------------------------*/
00091 void setMatElement( sciMatrix * mat, int row, int col, void * newValue )
00092 {
00093   mat->data[row + col * mat->nbRow] = newValue ;
00094 }
00095 /*-------------------------------------------------------------------------------------------*/
00096 void changeMatElement( sciMatrix * mat, int row, int col, void * newValue )
00097 {
00098   if ( mat->data[row + col * mat->nbRow] != NULL )
00099   {
00100     FREE( mat->data[row + col * mat->nbRow] ) ;
00101   }
00102   mat->data[row + col * mat->nbRow] = newValue ;
00103 }
00104 /*-------------------------------------------------------------------------------------------*/
00105 void copyMatElement(       sciMatrix     * mat      ,
00106                            int             row      ,
00107                            int             col      , 
00108                      const void          * copyValue,
00109                            int             valueSize )
00110 {
00111   /* copy the value */
00112   void * newValue = MALLOC( valueSize ) ;
00113   memcpy( newValue, copyValue, valueSize ) ;
00114 
00115   /* change the current one */
00116   changeMatElement( mat, row, col, newValue ) ;
00117 }
00118 /*-------------------------------------------------------------------------------------------*/
00119 int getMatNbRow( const sciMatrix * mat ) { return mat->nbRow ; }
00120 /*-------------------------------------------------------------------------------------------*/
00121 int getMatNbCol( const sciMatrix * mat ) { return mat->nbCol ; }
00122 /*-------------------------------------------------------------------------------------------*/
00123 void ** getMatData( const sciMatrix * mat ) { return mat->data ; }
00124 /*-------------------------------------------------------------------------------------------*/

Generated on Sun Mar 4 15:03:53 2007 for Scilab [trunk] by  doxygen 1.5.1