From 93715c46a99c96d6c866968312691ec9ab0f6a03 Mon Sep 17 00:00:00 2001 From: jcid Date: Sun, 7 Oct 2007 00:36:34 +0200 Subject: Initial revision --- src/klist.c | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/klist.c (limited to 'src/klist.c') diff --git a/src/klist.c b/src/klist.c new file mode 100644 index 00000000..113472b6 --- /dev/null +++ b/src/klist.c @@ -0,0 +1,118 @@ +/* + * File: klist.c + * + * Copyright 2001 Jorge Arellano Cid + * + * This program 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 of the License, or + * (at your option) any later version. + */ + +/* + * A simple ADT for Key-Data pairs + * + * NOTE: this ADT is not perfect. The possibility of holding a Key, after + * its data has been removed, long enough for the key-counter to reset and + * reuse the same key is very low, but EXISTS. So, the responsibility + * remains with the caller. + */ + +#include "klist.h" + + +/* + * Compare function for searching data by its key + */ +static int Klist_node_by_key_cmp(const void *Node, const void *key) +{ + return ((KlistNode_t *)Node)->Key - VOIDP2INT(key); +} + +/* + * Return the data pointer for a given Key (or NULL if not found) + */ +void *a_Klist_get_data(Klist_t *Klist, int Key) +{ + void *aux; + + if (!Klist) + return NULL; + aux = dList_find_sorted(Klist->List, INT2VOIDP(Key), Klist_node_by_key_cmp); + return (aux) ? ((KlistNode_t*)aux)->Data : NULL; +} + +/* + * Insert a data pointer and return a key for it. + */ +int a_Klist_insert(Klist_t **Klist, void *Data) +{ + KlistNode_t *Node; + + if (!*Klist) { + (*Klist) = dNew(Klist_t, 1); + (*Klist)->List = dList_new(32); + (*Klist)->Clean = 1; + (*Klist)->Counter = 0; + } + + /* This avoids repeated keys at the same time */ + do { + if (++((*Klist)->Counter) == 0) { + (*Klist)->Counter = 1; + (*Klist)->Clean = 0; + } + } while (!((*Klist)->Clean) && + a_Klist_get_data((*Klist), (*Klist)->Counter)); + + Node = dNew(KlistNode_t, 1); + Node->Key = (*Klist)->Counter; + Node->Data = Data; + dList_insert_sorted((*Klist)->List, Node, Klist_node_by_key_cmp); + return (*Klist)->Counter; +} + +/* + * Remove data by Key + */ +void a_Klist_remove(Klist_t *Klist, int Key) +{ + void *data; + + data = dList_find_sorted(Klist->List, INT2VOIDP(Key),Klist_node_by_key_cmp); + if (data) { + dList_remove(Klist->List, data); + dFree(data); + } + if (dList_length(Klist->List) == 0) + Klist->Clean = 1; +} + +/* + * Return the number of elements in the Klist + */ +int a_Klist_length(Klist_t *Klist) +{ + return dList_length(Klist->List); +} + +/* + * Free a Klist + */ +void a_Klist_free(Klist_t **KlistPtr) +{ + void *node; + Klist_t *Klist = *KlistPtr; + + if (!Klist) + return; + + while (dList_length(Klist->List) > 0) { + node = dList_nth_data(Klist->List, 0); + dList_remove_fast(Klist->List, 0); + dFree(node); + } + dFree(Klist); + *KlistPtr = NULL; +} + -- cgit v1.2.3