-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
290 lines (255 loc) · 7.51 KB
/
table.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Ce fichier fait partie d'un projet de programmation donné en Licence 3
* à l'Université de Bordeaux.
*
* Copyright (C) 2014 Adrien Boussicault
*
* This Library 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 2 of the License, or
* (at your option) any later version.
*
* This Library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this Library. If not, see <http://www.gnu.org/licenses/>.
*/
#include "table.h"
#include "outils.h"
#include "fifo.h"
#include "avl.h"
#include <assert.h>
#include <search.h>
#include <stdlib.h>
typedef struct Table_association {
void (*supprimer_cle)(intptr_t cle);
intptr_t (*copier_cle)(const intptr_t cle);
int (*comparer_cle)(const intptr_t cle1, const intptr_t cle2);
intptr_t cle;
intptr_t valeur;
} Table_association ;
struct Table {
int (*comparer_cle)( const intptr_t cle1, const intptr_t cle2 );
intptr_t (*copier_cle)( const intptr_t cle );
void (*supprimer_cle)(intptr_t cle);
struct avl_table * root;
};
intptr_t get_cle( Table_iterateur it ){
const Table_association * asso = ( const Table_association * ) avl_t_cur( &it );
return (const intptr_t) asso->cle;
}
intptr_t get_valeur( Table_iterateur it ){
Table_association * asso = ( Table_association * ) avl_t_cur( &it );
return asso->valeur;
}
Table_association * creer_table_association(
const Table* table, const intptr_t cle, intptr_t valeur
){
Table_association * res = xmalloc(
sizeof( Table_association )
);
if( table->copier_cle && cle ){
res->cle = table->copier_cle( cle );
}else{
res->cle = cle;
}
res->valeur = valeur;
res->supprimer_cle = table->supprimer_cle;
res->copier_cle = table->copier_cle;
res->comparer_cle = table->comparer_cle;
return res;
}
Table_association * copier_table_association( Table_association * asso ){
Table_association * res = xmalloc(
sizeof( Table_association )
);
*res = *asso;
if( asso->copier_cle && asso->cle ){
res->cle = asso->copier_cle( asso->cle );
}
return res;
}
int compare_table_association( const void * pa1, const void * pb1, void* param ){
Table_association * pa = (Table_association *) pa1;
Table_association * pb = (Table_association *) pb1;
if( pa->comparer_cle ){
int r = pa->comparer_cle( pa->cle, pb->cle );
return r;
}else{
if( pa->cle < pb->cle )
return -1;
if( pa->cle > pb->cle )
return 1;
return 0;
}
}
void supprimer_table_association2( void* asso_tmp, void* data ){
Table_association * asso = (Table_association*) asso_tmp;
if( asso->supprimer_cle && asso->cle ){
asso->supprimer_cle( asso->cle );
}
xfree(asso);
}
void supprimer_table_association( Table_association * asso ){
if( asso->supprimer_cle && asso->cle ){
asso->supprimer_cle( asso->cle );
}
xfree(asso);
}
Table* creer_table(
int (*comparer_cle)( const intptr_t cle1, const intptr_t cle2 ),
intptr_t (*copier_cle)( const intptr_t cle ),
void (*supprimer_cle)(intptr_t cle)
){
Table* res = xmalloc( sizeof(Table) );
res->root = avl_create ( compare_table_association, NULL, NULL );
res->supprimer_cle = supprimer_cle;
res->comparer_cle = comparer_cle;
res->copier_cle = copier_cle;
return res;
}
void liberer_table( Table* table ){
assert( table );
avl_destroy ( table->root, supprimer_table_association2 );
xfree( table );
}
void add_table( Table* table, const intptr_t cle, intptr_t valeur ) {
Table_association* asso = creer_table_association(table, cle, valeur);
void* val = avl_probe ( table->root, (void*) asso );
if( val == NULL ){
ERREUR( "Espace insuffisant" );
}
Table_association* asso_tree = *( Table_association** ) val;
if( asso_tree != asso ){
supprimer_table_association( asso );
asso_tree->valeur = valeur;
}
}
intptr_t delete_table( Table* table, intptr_t cle ){
intptr_t valeur = (intptr_t) NULL;
Table_association* asso_tree = NULL;
Table_association* asso = creer_table_association(
table, cle, (intptr_t) NULL
);
void* val = avl_find( table->root, (void*) asso );
if( val ){
asso_tree = ( Table_association* ) val;
valeur = asso_tree->valeur;
}
avl_delete( table->root, (void*) asso );
if(asso_tree){
supprimer_table_association( asso_tree );
}
supprimer_table_association( asso );
return valeur;
}
void pour_toute_cle_valeur_table(
const Table* table,
void (* action)( const intptr_t cle, intptr_t valeur, void* data ),
void* data
){
struct avl_traverser traverser;
void * item;
avl_t_init( &traverser, table->root );
while( (item = avl_t_next( &traverser )) ){
Table_association* asso = (Table_association *) item;
action( asso->cle, asso->valeur, data );
}
}
typedef struct {
void (* supprimer_valeur)(intptr_t valeur );
} data_pour_toute_valeur_table_t;
void action_pour_toute_valeur_table( const intptr_t cle, intptr_t valeur, void* data ){
data_pour_toute_valeur_table_t * d = (data_pour_toute_valeur_table_t*) data;
d->supprimer_valeur( valeur );
}
void pour_toute_valeur_table(
const Table* table,
void (* supprimer_valeur)(intptr_t valeur )
){
data_pour_toute_valeur_table_t data;
data.supprimer_valeur = supprimer_valeur;
pour_toute_cle_valeur_table( table, action_pour_toute_valeur_table, &data );
}
void vider_table( Table* table ){
avl_destroy ( table->root, supprimer_table_association2 );
table->root = avl_create ( compare_table_association, NULL, NULL );
}
typedef struct {
void (*print_cle)( const intptr_t cle );
void (*print_valeur)( const intptr_t valeur );
} data_print_table_t;
void print_association( const intptr_t cle, intptr_t valeur, void* data ){
data_print_table_t* d = (data_print_table_t*) data;
if( d->print_cle ){
d->print_cle( cle );
}else{
printf("%ld", cle );
}
printf(" --> ");
if( d->print_valeur ){
d->print_valeur( valeur );
}else{
printf("%ld", valeur );
}
printf(", ");
}
void print_table(
Table* table, void (*print_cle)( const intptr_t cle ),
void (*print_valeur)( const intptr_t valeur ),
const char* texte_de_fin
){
data_print_table_t data;
data.print_cle = print_cle;
data.print_valeur = print_valeur;
printf( "{ ");
pour_toute_cle_valeur_table( table, print_association, &data );
printf( " }%s", texte_de_fin );
}
Table_iterateur trouver_table( const Table* table, intptr_t cle ){
Table_iterateur it;
Table_association* asso = creer_table_association(
table, cle, (intptr_t) NULL
);
avl_t_find( &it, table->root, (void*) asso );
supprimer_table_association( asso );
return it;
}
Table_iterateur premier_iterateur_table( const Table* table ){
Table_iterateur it;
avl_t_first( &it, table->root );
return it;
}
Table_iterateur dernier_iterateur_table(
const Table_iterateur * iterator, Table* table
){
Table_iterateur it;
avl_t_last( &it, table->root );
return it;
}
int iterateur_est_vide( Table_iterateur iterator ){
return avl_t_is_null( &iterator );
}
Table_iterateur iterateur_suivant_table( Table_iterateur iterateur ){
avl_t_next( &iterateur );
return iterateur;
}
Table_iterateur iterateur_precedent_table( Table_iterateur iterateur ){
avl_t_prev( &iterateur );
return iterateur;
}
int taille_table( Table* t ){
int res = 0;
Table_iterateur it1;
for(
it1 = premier_iterateur_table( t );
! iterateur_est_vide( it1 );
it1 = iterateur_suivant_table( it1 )
){
res += 1;
}
return res;
};