glitch in the license text detected by hyazinthe, thank you!
[oweals/gnunet.git] / src / testbed / testbed_api_sd.c
index cbafff08fe15baabcf747027efd16ae4b5d67cd7..88647bf84375369a70651263f4ce376dd4057e0e 100644 (file)
@@ -1,27 +1,22 @@
 /*
       This file is part of GNUnet
-      (C) 2008--2013 Christian Grothoff (and other contributing authors)
+      Copyright (C) 2008--2013 GNUnet e.V.
 
-      GNUnet is free software; you can redistribute it and/or modify
-      it under the terms of the GNU General Public License as published
-      by the Free Software Foundation; either version 3, or (at your
-      option) any later version.
+      GNUnet is free software: you can redistribute it and/or modify it
+      under the terms of the GNU Affero General Public License as published
+      by the Free Software Foundation, either version 3 of the License,
+      or (at your option) any later version.
 
       GNUnet is distributed in the hope that it will be useful, but
       WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-      General Public License for more details.
-
-      You should have received a copy of the GNU General Public License
-      along with GNUnet; see the file COPYING.  If not, write to the
-      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-      Boston, MA 02111-1307, USA.
+      Affero General Public License for more details.
  */
 
 /**
  * @file testbed/testbed_api_sd.c
  * @brief functions to calculate standard deviation
- * @author Sree Harsha Totakura <sreeharsha@totakura.in> 
+ * @author Sree Harsha Totakura <sreeharsha@totakura.in>
  */
 
 #include "platform.h"
@@ -109,7 +104,7 @@ GNUNET_TESTBED_SD_init_ (unsigned int max_cnt)
   struct SDHandle *h;
 
   GNUNET_assert (1 < max_cnt);
-  h = GNUNET_malloc (sizeof (struct SDHandle));
+  h = GNUNET_new (struct SDHandle);
   h->max_cnt = max_cnt;
   return h;
 }
@@ -159,7 +154,7 @@ GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
   }
   GNUNET_assert (h->cnt < h->max_cnt);
   if (NULL == entry)
-    entry = GNUNET_malloc (sizeof (struct SDEntry));
+    entry = GNUNET_new (struct SDEntry);
   entry->amount = amount;
   GNUNET_CONTAINER_DLL_insert_tail (h->head, h->tail, entry);
   h->sum += amount;
@@ -173,33 +168,40 @@ GNUNET_TESTBED_SD_add_data_ (struct SDHandle *h, unsigned int amount)
 
 
 /**
- * Returns the factor by which the given amount differs from the standard deviation
+ * Calculates the factor by which the given amount differs
  *
  * @param h the SDhandle
  * @param amount the value for which the deviation is returned
-
- * @return the deviation from the average; GNUNET_SYSERR if the deviation cannot
- *           be calculated OR 0 if the given amount is less than or equal to the
- *           average; a maximum of 4 is returned for deviations equal to or
- *           larger than 4
+ * @param factor the factor by which the given amont differs
+ * @return GNUNET_SYSERR if the deviation cannot
+ *   be calculated; GNUNET_OK if the deviation is returned through factor
  */
 int
-GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount)
+GNUNET_TESTBED_SD_deviation_factor_ (struct SDHandle *h, unsigned int amount,
+                                     int *factor)
 {
   double diff;
-  unsigned int n;
+  int f;
+  int n;
 
   if (h->cnt < 2)
     return GNUNET_SYSERR;
   if (((float) amount) > h->avg)
+  {
     diff = ((float) amount) - h->avg;
+    f = 1;
+  }
   else
-    return 0;                   //diff = h->avg - ((float) amount);
+  {
+    diff = h->avg - ((float) amount);
+    f = -1;
+  }
   diff *= diff;
   for (n = 1; n < 4; n++)
     if (diff < (((double) (n * n)) * h->vr))
       break;
-  return n;
+  *factor = f * n;
+  return GNUNET_OK;
 }
 
 /* end of testbed_api_sd.c */