BasicAlgos.c

Go to the documentation of this file.
00001 /*------------------------------------------------------------------------*/
00002 /* file: BasicAlgos.c                                                     */
00003 /* Copyright INRIA 2006                                                   */
00004 /* Authors : Fabrice Leray, Jean-Baptiste Silvy                           */
00005 /* desc : Several basic algorithms which can be used in several places in */
00006 /*        the code.                                                       */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include "BasicAlgos.h"
00010 #include "MALLOC.h"
00011 #include "core_math.h"
00012 
00013 /*------------------------------------------------------------------------*/
00014 double sciFindStPosMin( double x[], int n )
00015 {
00016   double min ;
00017   int i ;
00018 
00019   if ( n <= 0 )
00020   {
00021     return -1.0 ;
00022   }
00023 
00024   min = x[0] ;
00025 
00026   for ( i = 1 ; i < n ; i++ )
00027   {
00028     if ( x[i] > 0.0 && x[i] < min )
00029     {
00030       min = x[i] ;
00031     }
00032   }
00033 
00034   /* if we have found at least one positive value in x, min strictly positive */
00035   return min ;
00036 }
00037 /*------------------------------------------------------------------------*/
00038 void MaxiInd( double vect[], integer n, integer * ind, double maxi )
00039 {
00040   integer i ;
00041   if ( *ind+1 < n )
00042   {
00043     for ( i = *ind+1 ; i < n ; i++ )
00044     {
00045       if ( vect[i] >= maxi)
00046       {
00047         *ind=i;
00048         return ;
00049       }
00050     }
00051   }
00052 }
00053 /*------------------------------------------------------------------------*/
00054 int C2F(entier2d)( integer * n, double * dx,integer * s )
00055 {
00056   integer ix;
00057   for (ix = *n -1 ; ix >= 0; --ix) { dx[ix] = (double) s[ix]; }
00058   return 0;
00059 }  
00060 /*------------------------------------------------------------------------*/
00061 int checkMonotony( double vector[], int nbElement )
00062 {
00063   int i ;
00064   if( vector[1] >= vector[0] )
00065   {
00066     /* might be incresing */
00067     for ( i = 1 ; i < nbElement - 1 ; i++ )
00068     {
00069       if ( vector[i+1] < vector[i] )
00070       {
00071         /* not increasing */
00072         return 0 ;
00073       }
00074     }
00075     return 1 ;
00076   }
00077   else
00078   {
00079     /* might be decreasing */
00080     for ( i = 1 ; i < nbElement - 1 ; i++ )
00081     {
00082       if ( vector[i+1] > vector[i] )
00083       {
00084         /* not decreasing */
00085         return 0 ;
00086       }
00087     }
00088     return -1 ;
00089 
00090   }
00091   return 0 ;
00092 
00093 }
00094 /*------------------------------------------------------------------------*/
00095 void doubleArrayCopy( double dest[], const double src[], int nbElement )
00096 {
00097   memcpy( dest, src, nbElement * sizeof(double) ) ;
00098 }
00099 /*------------------------------------------------------------------------*/
00100 void intArrayCopy( int dest[], const int src[], int nbElement )
00101 {
00102   memcpy( dest, src, nbElement * sizeof(int) ) ;
00103 }
00104 /*------------------------------------------------------------------------*/
00105 void stringArrayCopy( char * dest[], char * src[], int nbElement )
00106 {
00107   int i ;
00108   for ( i = 0 ; i < nbElement ; i++ )
00109   {
00110     int elemSize = strlen( src[i] ) + 1 ;
00111     FREE( dest[i] ) ;
00112 
00113     dest[i] = MALLOC( elemSize * sizeof(char) ) ;
00114 
00115     if ( dest[i] == NULL )
00116     {
00117       destroyStringArray( dest, nbElement ) ;
00118       return ;
00119     }
00120 
00121     strcpy( dest[i], src[i] ) ;
00122   }
00123 }
00124 /*------------------------------------------------------------------------*/
00125 void setDoubleArraySingleValue( double dest[], double value, int nbElement )
00126 {
00127   int i ;
00128   for ( i = 0 ; i < nbElement ; i++ )
00129   {
00130     dest[i] = value ;
00131   }
00132 }
00133 /*------------------------------------------------------------------------*/
00134 double * createNewArrayFromSource( int destSize, const double src[], int srcSize )
00135 {
00136   int i ;
00137   int endCopy = Min( destSize, srcSize ) ;
00138   /* create new array */
00139   double * dest = MALLOC( destSize * sizeof(double) ) ;
00140 
00141   if ( dest == NULL )
00142   {
00143     return NULL ;
00144   }
00145 
00146   /* copy the element which needs to be copied */
00147   memcpy( dest, src, endCopy * sizeof( double ) ) ;
00148 
00149   for ( i = endCopy ; i < destSize ; i++ )
00150   {
00151     dest[i] = 0.0 ;
00152   }
00153 
00154   return dest ;
00155 
00156 }
00157 /*------------------------------------------------------------------------*/
00158 void destroyStringArray( char * src[], int nbStrings )
00159 {
00160   int i ;
00161   for ( i = 0 ; i < nbStrings ; i++ )
00162   {
00163     FREE( src[i] ) ;
00164     src[i] = NULL ;
00165   }
00166   FREE( src ) ;
00167 }
00168 /*-----------------------------------------------------------------------------------*/
00169 double * createDoubleArrayCopy( const double src[], int nbElement )
00170 {
00171   double * res = MALLOC( nbElement * sizeof(double) ) ;
00172 
00173   if ( res == NULL )
00174   {
00175     return NULL ;
00176   }
00177 
00178   memcpy( res, src, nbElement * sizeof(double) ) ;
00179 
00180   return res ;
00181 }
00182 /*-----------------------------------------------------------------------------------*/
00183 int * createIntArrayCopy( const int src[], int nbElement )
00184 {
00185   int * res = MALLOC( nbElement * sizeof(int) ) ;
00186   
00187   if ( res == NULL )
00188   {
00189     return NULL ;
00190   }
00191   
00192   memcpy( res, src, nbElement * sizeof(int) ) ;
00193 
00194   return res ;
00195 }
00196 /*-----------------------------------------------------------------------------------*/
00197 char ** createStringArrayCopy( char * src[], int nbElement )
00198 {
00199   int i ;
00200   char ** res = MALLOC( nbElement * sizeof(char *) ) ;
00201 
00202   if ( res == NULL )
00203   {
00204     return NULL ;
00205   }
00206 
00207   for ( i = 0 ; i < nbElement; i++ )
00208   {
00209     res[i] = NULL ;
00210   }
00211 
00212   stringArrayCopy( res, src, nbElement ) ;
00213 
00214   return res ;
00215 
00216 }
00217 /*-----------------------------------------------------------------------------------*/

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