2009/CaoCourseTme2: dico.c

File dico.c, 3.5 KB (added by franck, 14 years ago)
Line 
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include "dico.h"
5#include "hash.h"
6
7/**************************************************************
8 Creation d'un dictionnaire possédant 'size' cases
9*************************************************************/
10dico_root_t *dico_create(unsigned int size)
11{
12dico_root_t *root = malloc(sizeof(dico_root_t));
13if(root == NULL) {
14    perror("dico_create");
15    exit(1);
16}
17root->SIZE = size;
18root->HTAB = calloc(size, sizeof(dico_item_t *));
19if(root->HTAB == NULL) {
20    perror("dico_create");
21    exit(1);
22}
23return root;
24}
25
26/**************************************************************
27 Recherche d'un item de cle 'key' dans le dictionaire 'root'
28 - Si l'item existe déjà, on renvoie un pointeur sur cet item.
29 - S'il n'existe pas, on le crée, en initialisant le champs
30   COUNT à la valeur 0.
31**************************************************************/
32dico_item_t *dico_get (dico_root_t *root, char *key)
33{
34unsigned int index;
35dico_item_t *curr;
36dico_item_t *item;
37unsigned int trouve = 0;
38
39if (key)
40    {
41        /* calcul de l'index dans le tableau */
42        index = hashindex(key) % root->SIZE;
43
44        /* recherche de l'item dans sa liste */
45        for (curr = root->HTAB[index] ; curr ; curr = curr->NEXT) {
46                if (strcmp (key, curr->KEY) == 0) { 
47            trouve = 1;
48            item = curr;
49        } 
50    }
51    /* creation d'un item si necessaire */
52    if(trouve == 0) {
53            item = malloc(  sizeof(struct dico_item_t *) + 
54                sizeof(unsigned int) + 
55                strlen (key) + 1);
56        if(item == NULL) perror("dico_get");
57        item->NEXT = root->HTAB[index];
58        item->COUNT = 0;
59        strcpy(item->KEY, key);
60        root->HTAB[index] = item;
61    }
62    return item;
63}
64perror ("dico_get");
65exit (1);
66} 
67
68
69/***********************************************************
70  Création d'un iterateur sur la table de hachage root
71************************************************************/
72dico_iterator_t *dico_iterator(dico_root_t *root)
73{
74dico_iterator_t *iter;
75if ((iter = malloc (sizeof (dico_iterator_t)))) {
76    iter->ROOT = root;
77    return iter;
78}
79perror ("dico_iterator");        /* si malloc rend NULL */
80exit (1);
81}
82
83/*******************************************************
84  Recherche du premier item de la table de hachage
85*******************************************************/
86dico_item_t *dico_first (dico_iterator_t *iter)
87{
88if (iter) {
89        /* recherche de la première liste non nulle */
90        dico_root_t * root = iter->ROOT;
91        for (iter->INDEX = 0;
92             (iter->INDEX < root->SIZE) && (root->HTAB[iter->INDEX] == NULL);
93             iter->INDEX++);
94
95        /* retour de la fonction, attention le dictionnaire peut être vide */
96        if (iter->INDEX >= root->SIZE) return NULL;   
97        else return iter->ITEM = root->HTAB[iter->INDEX];
98}
99return NULL;
100} 
101
102/************************************************
103 * Recherche de l'item suivant dans iterator 
104*************************************************/
105dico_item_t *dico_next (dico_iterator_t *iter)
106{
107if (iter)
108    {
109        /* pointe sur l'item suivant dans la liste et retour si non nul */
110        iter->ITEM = iter->ITEM->NEXT;
111        if (iter->ITEM) return iter->ITEM;
112
113        /* recherche de la prochaine liste non nulle */
114        dico_root_t * root = iter->ROOT;
115        for (iter->INDEX++;
116             (iter->INDEX < root->SIZE) && (root->HTAB[iter->INDEX] == NULL);
117             iter->INDEX++);
118
119        /* retour de la fonction, attention le dictionnaire peut être vide */
120        if (iter->INDEX >= root->SIZE) return NULL;     
121        else return iter->ITEM = root->HTAB[iter->INDEX];
122    }
123    return NULL;
124}
125