Dillo v3.1.1-119-g140d9ebd
Loading...
Searching...
No Matches
klist.c
Go to the documentation of this file.
1/*
2 * File: klist.c
3 *
4 * Copyright 2001-2007 Jorge Arellano Cid <jcid@dillo.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 */
11
21#include "klist.h"
22
23
27static int Klist_node_by_key_cmp(const void *Node, const void *key)
28{
29 return ((KlistNode_t *)Node)->Key - VOIDP2INT(key);
30}
31
35static int Klist_node_by_node_cmp(const void *Node1, const void *Node2)
36{
37 return ((KlistNode_t *)Node1)->Key - ((KlistNode_t *)Node2)->Key;
38}
39
43void *a_Klist_get_data(Klist_t *Klist, int Key)
44{
45 void *aux;
46
47 if (!Klist)
48 return NULL;
50 return (aux) ? ((KlistNode_t*)aux)->Data : NULL;
51}
52
56int a_Klist_insert(Klist_t **Klist, void *Data)
57{
58 KlistNode_t *Node;
59
60 if (!*Klist) {
61 (*Klist) = dNew(Klist_t, 1);
62 (*Klist)->List = dList_new(32);
63 (*Klist)->Clean = 1;
64 (*Klist)->Counter = 0;
65 }
66
67 /* This avoids repeated keys at the same time */
68 do {
69 if (++((*Klist)->Counter) == 0) {
70 (*Klist)->Counter = 1;
71 (*Klist)->Clean = 0;
72 }
73 } while (!((*Klist)->Clean) &&
74 a_Klist_get_data((*Klist), (*Klist)->Counter));
75
76 Node = dNew(KlistNode_t, 1);
77 Node->Key = (*Klist)->Counter;
78 Node->Data = Data;
79 dList_insert_sorted((*Klist)->List, Node, Klist_node_by_node_cmp);
80 return (*Klist)->Counter;
81}
82
86void a_Klist_remove(Klist_t *Klist, int Key)
87{
88 void *data;
89
91 if (data) {
92 dList_remove(Klist->List, data);
93 dFree(data);
94 }
95 if (dList_length(Klist->List) == 0)
96 Klist->Clean = 1;
97}
98
103{
104 return dList_length(Klist->List);
105}
106
110void a_Klist_free(Klist_t **KlistPtr)
111{
112 void *node;
113 Klist_t *Klist = *KlistPtr;
114
115 if (!Klist)
116 return;
117
118 while (dList_length(Klist->List) > 0) {
119 node = dList_nth_data(Klist->List, 0);
120 dList_remove_fast(Klist->List, node);
121 dFree(node);
122 }
123 dList_free(Klist->List);
124 dFree(Klist);
125 *KlistPtr = NULL;
126}
127
void dList_insert_sorted(Dlist *lp, void *data, dCompareFunc func)
Insert an element into a sorted list.
Definition dlib.c:769
void dFree(void *mem)
Definition dlib.c:68
Dlist * dList_new(int size)
Create a new empty list.
Definition dlib.c:548
int dList_length(Dlist *lp)
For completing the ADT.
Definition dlib.c:613
void * dList_nth_data(Dlist *lp, int n0)
Return the nth data item, NULL when not found or 'n0' is out of range.
Definition dlib.c:662
void dList_remove_fast(Dlist *lp, const void *data)
Remove a data item without preserving order.
Definition dlib.c:623
void * dList_find_sorted(Dlist *lp, const void *data, dCompareFunc func)
Search a sorted list.
Definition dlib.c:796
void dList_free(Dlist *lp)
Free a list (not its elements)
Definition dlib.c:564
void dList_remove(Dlist *lp, const void *data)
Definition dlib.c:641
#define VOIDP2INT(p)
Definition dlib.h:43
#define INT2VOIDP(i)
Definition dlib.h:44
#define dNew(type, count)
Definition dlib.h:49
void a_Klist_remove(Klist_t *Klist, int Key)
Remove data by Key.
Definition klist.c:86
static int Klist_node_by_node_cmp(const void *Node1, const void *Node2)
Compare function for searching data by node.
Definition klist.c:35
static int Klist_node_by_key_cmp(const void *Node, const void *key)
Compare function for searching data by its key.
Definition klist.c:27
void * a_Klist_get_data(Klist_t *Klist, int Key)
Return the data pointer for a given Key (or NULL if not found)
Definition klist.c:43
void a_Klist_free(Klist_t **KlistPtr)
Free a Klist.
Definition klist.c:110
int a_Klist_insert(Klist_t **Klist, void *Data)
Insert a data pointer and return a key for it.
Definition klist.c:56
int a_Klist_length(Klist_t *Klist)
Return the number of elements in the Klist.
Definition klist.c:102
void * Data
data reference
Definition klist.h:13
int Key
primary key
Definition klist.h:12
int Clean
check flag
Definition klist.h:18
Dlist * List
Definition klist.h:17