#include "pcache.h"
-int PData_invariant(const PData *my) {
+static int PData_invariant(const PData *my) {
int retVal;
retVal = my != NULL
&& my->name != NULL
&& my->idx != NULL
- && (my->type == PInt || my->type == PString || my->type == PIntArray || my->type == PStringArray);
+ && (my->type == PInt);
return retVal;
}
-PData *newPInt(const char *name, const char *idx, unsigned long int value) {
+static PData *newPInt(const char *name, const char *idx, unsigned long int value) {
PData *my = malloc(sizeof(PData));
my->name = malloc(strlen(name)+1);
return my;
}
-void delPData(PData *my) {
+static void delPData(PData *my) {
if (my != NULL) {
switch (my->type) {
case PInt:
}
}
-static int keyCompare(const PData *my, const PData *data) {
+static int compare(const PData *my, const char *name, const char *idx) {
int retVal;
- assert(PData_invariant(my));
- assert(PData_invariant(data));
-
- retVal = strcmp(my->name, data->name);
+ retVal = strcmp(my->idx, idx);
if (retVal == 0) {
- retVal = strcmp(my->idx, data->idx);
+ retVal = strcmp(my->name, name);
}
return retVal < 0 ? -1 : retVal > 0 ? 1 : 0;
}
-PCache *insert(PCache *my, PData *data) {
- assert(PData_invariant(data));
-
+static PCache *insert(PCache *my, const char *name, const char *idx, unsigned long int value) {
if (my == NULL) {
my = malloc(sizeof(PCache));
my->r = NULL;
my->l = NULL;
- my->data = data;
+ my->data = newPInt(name, idx, value);
} else {
- switch (keyCompare(my->data, data)) {
+ switch (compare(my->data, name, idx)) {
case 0:
delPData(my->data);
- my->data = data;
+ my->data = newPInt(name, idx, value);
break;
case -1:
- my->l = insert(my->l, data);
+ my->l = insert(my->l, name, idx, value);
break;
case 1:
- my->r = insert(my->r, data);
+ my->r = insert(my->r, name, idx, value);
break;
}
}
return my;
}
-PData *find(const PCache *my, const PData *data) {
+static PData *find(const PCache *my, const char *name, const char *idx) {
PData *retVal;
- assert(PData_invariant(data));
-
if (my == NULL) {
retVal = NULL;
} else {
- switch (keyCompare(my->data, data)) {
+ switch (compare(my->data, name, idx)) {
case 0:
retVal = my->data;
break;
case -1:
- retVal = find(my->l, data);
+ retVal = find(my->l, name, idx);
break;
case 1:
- retVal = find(my->r, data);
+ retVal = find(my->r, name, idx);
break;
}
}
int PCache_storeInt(PCache **my, const char *name, const char *idx, unsigned long int value) {
- *my = insert(*my, newPInt(name, idx, value));
+ *my = insert(*my, name, idx, value);
return 0;
}
int retVal;
const PData *data;
- data = find(my, newPInt(name, idx, 0));
+ data = find(my, name, idx);
if (data == NULL) {
*value = 0;
retVal = -1;