# generateFilterArray.c

Aller à la documentation de ce fichier.

    00001 /*  This file is part of MED.
    00002  *
    00003  *  COPYRIGHT (C) 1999 - 2012  EDF R&D, CEA/DEN
    00004  *  MED is free software: you can redistribute it and/or modify
    00005  *  it under the terms of the GNU Lesser General Public License as published by
    00006  *  the Free Software Foundation, either version 3 of the License, or
    00007  *  (at your option) any later version.
    00008  *
    00009  *  MED is distributed in the hope that it will be useful,
    00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00012  *  GNU Lesser General Public License for more details.
    00013  *
    00014  *  You should have received a copy of the GNU Lesser General Public License
    00015  *  along with MED.  If not, see <http://www.gnu.org/licenses/>.
    00016  */
    00017
    00018 #include <med.h>
    00019 #define MESGERR 1
    00020 #include "med_utils.h"
    00021 #include <string.h>
    00022
    00023 #include <unistd.h>
    00024 #include <time.h>
    00025 #include <assert.h>
    00026 #include <stdlib.h>
    00027
    00028 static int cmp(const med_int *p1, const med_int *p2) { return *p1-*p2; }
    00029
    00030 med_err generateFilterArray( const med_size nentities, const med_size nvaluesperentity, const med_size nconstituentpervalue,
    00031                              const med_size profilearraysize, const med_int * const profilearray,
    00032                              med_int  * const nentitiesfiltered, med_int **filterarray ) {
    00033
    00034
    00035   med_err    _ret=-1;
    00036   med_int    _nentitiesfiltered=0,_maxfilternentities=0;
    00037   med_int    *_indexarray=NULL;
    00038   int        _i=0;
    00039   struct tm  *_tm ;
    00040   time_t _tt=time(0);
    00041
    00042   _tm = localtime(&_tt);
    00043
    00044
    00045   /*Taille du tableau des numéros d'entités à filtrer */
    00046   if ( profilearraysize ) {
    00047     _maxfilternentities = profilearraysize;
    00048   } else {
    00049     _maxfilternentities = nentities;
    00050   }
    00051
    00052   /* Allocation du tableau de filtre */
    00053
    00054   srandom((*_tm).tm_sec * (*_tm).tm_min );
    00055   _nentitiesfiltered         = 1 + (int) ((float)(_maxfilternentities) * (random() / (RAND_MAX + 1.0)));
    00056   /*       _nentitiesfiltered         = 2; */
    00057
    00058   (*filterarray)         = malloc(_nentitiesfiltered*sizeof(med_int));
    00059
    00060 /*   if ( profilearraysize) { */
    00061 /*     _indexarray     = malloc(_nentitiesfiltered*sizeof(med_int)); */
    00062 /*   } else { */
    00063 /*     _indexarray=(*filterarray); */
    00064 /*   } */
    00065
    00066   _indexarray=(*filterarray);
    00067
    00068   for (_i=0; _i < _nentitiesfiltered; ++_i ) {
    00069     _indexarray[_i] =  1 + (int) ((double)(_maxfilternentities) * (random() / (RAND_MAX + 1.0)));
    00070   }
    00071
    00072   /*N'enlève pas les doublons, mais celà fonctionne*/
    00073   qsort(_indexarray, _nentitiesfiltered, sizeof(med_int), (int(*)(const void *, const void *) ) cmp);
    00074
    00075 /*   for (_i=0; _i < _nentitiesfiltered; ++_i ) { */
    00076 /*     ISCRUTE(_indexarray[_i]); */
    00077 /*   } */
    00078
    00079   /* Cette indirection ne doit jamais être faite car le tableau filtre contient des indices de profils.*/
    00080 /*   if ( profilearraysize) */
    00081 /*     for (_i=0; _i < _nentitiesfiltered; ++_i ) { */
    00082 /*       (*filterarray)[_i] = profilearray[_indexarray[_i]]; */
    00083 /*       ISCRUTE((*filterarray)[_i]); */
    00084 /*     } */
    00085
    00086   *nentitiesfiltered=_nentitiesfiltered;
    00087
    00088   _ret=0;
    00089
    00090  ERROR:
    00091 /*   if ( profilearraysize) free(_indexarray); */
    00092
    00093   return _ret;
    00094
    00095 }