#include <malloc.h>
+#include <math.h>
#include <stdio.h>
#include "alloc.h"
#include "debug.h"
#include "format.h"
+#include "tabular.h"
#include "storage.h"
/* global variables */
#define DEFAULT_STORAGE_SIZE 10
-double *storage = NULL;
-
-int storage_size = -1;
+tab_t *storage = NULL;
/* storage functions */
-void memory (int nb)
+int memory (int nb)
{
- int i, l;
- double *tmp = NULL;
- if (nb != storage_size) {
- l = (nb < storage_size) ? nb : storage_size;
- tmp = (double *) callocordie (nb, sizeof (double));
- for (i = 0; i < l; i++) {
- tmp[i] = storage[i];
- }
- if (storage != NULL) {
- free (storage);
- }
- storage = tmp;
- storage_size = nb;
+ if ((nb != -1) && (nb != size_tab (storage))) {
+ storage = resize_tab (storage, nb);
+ }
+ if (nb == -1) {
+ memory (DEFAULT_STORAGE_SIZE);
}
+ return size_tab (storage);
}
-double store (int index, double value)
+double store (int id, double value)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- storage[index - 1] = value;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
- }
- return value;
+ return set_tab (storage, id, value);
}
-double recall (int index)
+double recall (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1];
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
- }
- return 0;
+ return get_tab (storage, id);
}
-double increase (int index)
+double increase (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1]++;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ double val = get_tab (storage, id);
+ if (!isnan (val)) {
+ set_tab (storage, id, ++val);
}
- return 0;
+ return val;
}
-double decrease (int index)
+double decrease (int id)
{
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- if ((index > 0) && (index <= storage_size)) {
- return storage[index - 1]--;
- } else {
- VERBOSE (WARNING, fprintf (stdout, "invalid index (%d) [%d, %d]\n", index, (storage_size) ? 1 : 0, storage_size));
+ double val = get_tab (storage, id);
+ if (!isnan (val)) {
+ set_tab (storage, id, --val);
}
- return 0;
+ return val;
}
void display (void)
{
- int i;
- if (storage_size == -1) {
+ if (storage == NULL) {
memory (DEFAULT_STORAGE_SIZE);
}
- fprintf (stdout, "storage:");
- for (i = 0; i < storage_size; i++) {
- fprintf (stdout, " ");
- fprintf (stdout, minform, storage[i]);
+ int i, n = size_tab (storage);
+ printf ("storage:");
+ for (i = 0; i < n; i++) {
+ printf (" ");
+ printl (get_tab (storage, i + 1));
}
- fprintf (stdout, "\n");
+ printf ("\n");
}
void clear ()
{
- int i;
- for (i = 0; i < storage_size; i++) {
- storage[i] = 0;
+ if (storage == NULL) {
+ memory (DEFAULT_STORAGE_SIZE);
+ }
+ int i, n = size_tab (storage);
+ for (i = 0; i < n; i++) {
+ set_tab (storage, i, 0);
}
}