MED fichier
generateFilterArray.c
Aller à la documentation de ce fichier.
1/* This file is part of MED.
2 *
3 * COPYRIGHT (C) 1999 - 2025 EDF R&D, CEA/DEN
4 * MED is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * MED is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with MED. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include <med.h>
19#define MESGERR 1
20#include "med_utils.h"
21#include <string.h>
22
23#include <unistd.h>
24#include <time.h>
25#include <assert.h>
26#include <stdlib.h>
27
28static int cmp(const med_int *p1, const med_int *p2) { return *p1-*p2; }
29
30med_err generateFilterArray( const med_size nentities, const med_size nvaluesperentity, const med_size nconstituentpervalue,
31 const med_size profilearraysize, const med_int * const profilearray,
32 med_int * const nentitiesfiltered, med_int **filterarray ) {
33
34
35 med_err _ret=-1;
36 med_int _nentitiesfiltered=0,_maxfilternentities=0;
37 med_int *_indexarray=NULL;
38 int _i=0;
39 struct tm *_tm ;
40 time_t _tt=time(0);
41
42 _tm = localtime(&_tt);
43
44
45 /*Taille du tableau des numéros d'entités à filtrer */
46 /* ISCRUTE(profilearraysize); */
47 if ( profilearraysize ) {
48 _maxfilternentities = profilearraysize;
49 } else {
50 _maxfilternentities = nentities;
51 }
52 /* ISCRUTE(_maxfilternentities); */
53 /* Allocation du tableau de filtre */
54
55 srandom((*_tm).tm_sec * (*_tm).tm_min );
56 _nentitiesfiltered = 1 + (int) ((float)(_maxfilternentities) * (random() / (RAND_MAX + 1.0)));
57 /* _nentitiesfiltered = 2; */
58
59 (*filterarray) = malloc(_nentitiesfiltered*sizeof(med_int));
60
61/* if ( profilearraysize) { */
62/* _indexarray = malloc(_nentitiesfiltered*sizeof(med_int)); */
63/* } else { */
64/* _indexarray=(*filterarray); */
65/* } */
66
67 _indexarray=(*filterarray);
68
69 for (_i=0; _i < _nentitiesfiltered; ++_i ) {
70 _indexarray[_i] = 1 + (int) ((double)(_maxfilternentities) * (random() / (RAND_MAX + 1.0)));
71 }
72
73 /*N'enlève pas les doublons, mais celà fonctionne*/
74 qsort(_indexarray, _nentitiesfiltered, sizeof(med_int), (int(*)(const void *, const void *) ) cmp);
75
76/* for (_i=0; _i < _nentitiesfiltered; ++_i ) { */
77/* ISCRUTE(_indexarray[_i]); */
78/* } */
79
80 /* Cette indirection ne doit jamais être faite car le tableau filtre contient des indices de profils.*/
81/* if ( profilearraysize) */
82/* for (_i=0; _i < _nentitiesfiltered; ++_i ) { */
83/* (*filterarray)[_i] = profilearray[_indexarray[_i]]; */
84/* ISCRUTE((*filterarray)[_i]); */
85/* } */
86
87 *nentitiesfiltered=_nentitiesfiltered;
88
89 _ret=0;
90
91 ERROR:
92/* if ( profilearraysize) free(_indexarray); */
93
94 return _ret;
95
96}
med_err generateFilterArray(const med_size nentities, const med_size nvaluesperentity, const med_size nconstituentpervalue, const med_size profilearraysize, const med_int *const profilearray, med_int *const nentitiesfiltered, med_int **filterarray)
static int cmp(const med_int *p1, const med_int *p2)