ze-filter  (ze-filter-0.8.0-develop-180218)
ze-bestof-n.c
Go to the documentation of this file.
1 /*
2  *
3  * ze-filter - Mail Server Filter for sendmail
4  *
5  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
6  *
7  * Auteur : Jose Marcio Martins da Cruz
8  * jose.marcio.mc@gmail.org
9  *
10  * Historique :
11  * Creation : Sun Aug 19 22:21:21 CEST 2007
12  *
13  * This program is free software, but with restricted license :
14  *
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * More details about ze-filter license can be found at ze-filter
21  * web site : http://foss.jose-marcio.org
22  */
23 
24 
25 #include <ze-sys.h>
26 #include <ze-filter.h>
27 #include <ze-bestof-n.h>
28 
29 /* ****************************************************************************
30  * *
31  * *
32  **************************************************************************** */
33 static int
34 dblcmp(a, b)
35  const void *a;
36  const void *b;
37 {
38  double *ra, *rb;
39 
40  ra = (double *) a;
41  rb = (double *) b;
42 
43  return (int) (fabs(*ra) - fabs(*rb));
44 }
45 
46 /* ****************************************************************************
47  * *
48  * *
49  **************************************************************************** */
50 bool
51 bestof_init(b, dim, bcmp)
52  bestof_T *b;
53  int dim;
54  bestcomp_F bcmp;
55 {
56  ASSERT(b != NULL);
57 
58  memset(b, 0, sizeof (*b));
59 
60  b->dim = dim;
61  b->n = 0;
62  if (bcmp != NULL)
63  b->bcmp = bcmp;
64  else
65  b->bcmp = dblcmp;
66 
67  return TRUE;
68 }
69 
70 /* ****************************************************************************
71  * *
72  * *
73  **************************************************************************** */
74 bool
76  bestof_T *b;
77  double v;
78 {
79  if (b->n < b->dim)
80  {
81  b->best[b->n++] = v;
82  return TRUE;
83  }
84 
85  qsort(b->best, b->n, sizeof (double), b->bcmp);
86  if (b->bcmp(&v, &b->best[0]) > 0)
87  b->best[0] = v;
88 
89  return TRUE;
90 }
91 
92 /* ****************************************************************************
93  * *
94  * *
95  **************************************************************************** */
96 double
98  bestof_T *b;
99 {
100  int i;
101  double v = 0.;
102 
103  if (b->n <= 0)
104  return 0.;
105 
106  for (i = 0; i < b->n; i++)
107  v += b->best[i];
108 
109  return v / b->n;
110 }
bool bestof_add(bestof_T *b, double v)
Definition: ze-bestof-n.c:75
#define ASSERT(a)
Definition: macros.h:27
double bestof_average(bestof_T *b)
Definition: ze-bestof-n.c:97
int(* bestcomp_F)(const void *, const void *)
Definition: ze-bestof-n.h:28
#define TRUE
Definition: macros.h:157
double best[BOFDIM]
Definition: ze-bestof-n.h:36
bool bestof_init(bestof_T *b, int dim, bestcomp_F bcmp)
Definition: ze-bestof-n.c:51
bestcomp_F bcmp
Definition: ze-bestof-n.h:37