353 | | 1 #include <stdio.h> |
354 | | 2 #include <string.h> |
355 | | 3 #include <stdlib.h> |
356 | | 4 #include "hte.h" |
357 | | 5 |
358 | | 6 /* |
359 | | 7 * Calcul de l'index de hachage de Donald Knuth |
360 | | 8 * Elle produit un nombre entier à partir d'une chaine de caractères. |
361 | | 9 */ |
362 | | 10 unsigned hte_hash (char *key) |
363 | | 11 { |
364 | | 12 char *c = key; |
365 | | 13 unsigned h; |
366 | | 14 for (h = 0; *c; c++) |
367 | | 15 { |
368 | | 16 h += (h ^ (h >> 1)) + 314159 * (unsigned char) *c; |
369 | | 17 while (h >= 516595003) |
370 | | 18 h -= 516595003; |
371 | | 19 } |
372 | | 20 return h; |
373 | | 21 } |
374 | | 22 |
375 | | 23 |
376 | | 24 /* |
377 | | 25 * Création d'un dictionnaire vide |
378 | | 26 */ |
379 | | 27 hte_root_t *hte_create (size_t nb_item) |
380 | | 28 { |
381 | | 29 hte_root_t *root; |
382 | | 30 unsigned i, nb_list; |
383 | | 31 static unsigned primes[] = { /*suite croissante de nombres premiers */ |
384 | | 32 101, 149, 223, 347, 499, 727, 1163, 1697, 2503, 3989, 5839, 8543, |
385 | | 33 12503, 20143, 29483, 43151, 63179, 92503, 101747, 148961, 218107, |
386 | | 34 319313, 514243, 752891, 1212551, 1613873, 2599153, 3805463, |
387 | | 35 5571523, 8157241, 11943011, -1 |
388 | | 36 }; |
389 | | 37 |
390 | | 38 /* détermination de la taille réelle du dictionnaire */ |
391 | | 39 for (i = 0; primes[i] < nb_item; i++); |
392 | | 40 nb_list = primes[i]; |
393 | | 41 if (nb_item != -1) |
394 | | 42 { |
395 | | 43 /* allocation et initialisation du dictionnaire */ |
396 | | 44 if ((root = malloc (sizeof (hte_root_t)))) |
397 | | 45 { |
398 | | 46 root->NB_LIST = nb_list; |
399 | | 47 if ((root->LIST = calloc (nb_list, sizeof (hte_item_t *)))) |
400 | | 48 return root; |
401 | | 49 } |
402 | | 50 } |
403 | | 51 fprintf (stderr, "hte_create: not enough memory\n"); |
404 | | 52 exit (1); |
405 | | 53 } |
| 352 | #include <stdio.h> |
| 353 | #include <string.h> |
| 354 | #include <stdlib.h> |
| 355 | #include "hte.h" |
| 356 | }}} |
| 357 | {{{ |
| 358 | /* |
| 359 | * Calcul de l'index de hachage de Donald Knuth |
| 360 | * Elle produit un nombre entier à partir d'une chaine de caractères. |
| 361 | */ |
| 362 | unsigned hte_hash (char *key) |
| 363 | { |
| 364 | char *c = key; |
| 365 | unsigned h; |
| 366 | for (h = 0; *c; c++) |
| 367 | { |
| 368 | h += (h ^ (h >> 1)) + 314159 * (unsigned char) *c; |
| 369 | while (h >= 516595003) |
| 370 | h -= 516595003; |
| 371 | } |
| 372 | return h; |
| 373 | } |
| 374 | }}} |
| 375 | {{{ |
| 376 | /* |
| 377 | * Création d'un dictionnaire vide |
| 378 | */ |
| 379 | hte_root_t *hte_create (size_t nb_item) |
| 380 | { |
| 381 | hte_root_t *root; |
| 382 | unsigned i, nb_list; |
| 383 | static unsigned primes[] = { /*suite croissante de nombres premiers */ |
| 384 | 101, 149, 223, 347, 499, 727, 1163, 1697, 2503, 3989, 5839, 8543, |
| 385 | 12503, 20143, 29483, 43151, 63179, 92503, 101747, 148961, 218107, |
| 386 | 319313, 514243, 752891, 1212551, 1613873, 2599153, 3805463, |
| 387 | 5571523, 8157241, 11943011, -1 |
| 388 | }; |
| 389 | |
| 390 | /* détermination de la taille réelle du dictionnaire */ |
| 391 | for (i = 0; primes[i] < nb_item; i++); |
| 392 | nb_list = primes[i]; |
| 393 | if (nb_item != -1) |
| 394 | { |
| 395 | /* allocation et initialisation du dictionnaire */ |
| 396 | if ((root = malloc (sizeof (hte_root_t)))) |
| 397 | { |
| 398 | root->NB_LIST = nb_list; |
| 399 | if ((root->LIST = calloc (nb_list, sizeof (hte_item_t *)))) |
| 400 | return root; |
| 401 | } |
| 402 | } |
| 403 | fprintf (stderr, "hte_create: not enough memory\n"); |
| 404 | exit (1); |
| 405 | } |
412 | | 1 #include "hte.h" |
413 | | 2 #include <stdio.h> |
414 | | 3 #include <string.h> |
415 | | 4 #include <stdlib.h> |
416 | | 5 |
417 | | 6 /* |
418 | | 7 * structure définissant un item du dictionnaire |
419 | | 8 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
420 | | 9 * permettant de chainer les items ayant le meme index de hachage |
421 | | 10 */ |
422 | | 11 struct hte_item_s |
423 | | 12 { |
424 | | 13 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
425 | | 14 char KEY[]; /* tableau flexible contenant la clé */ |
426 | | 15 }; |
427 | | 16 |
428 | | 17 |
429 | | 18 unsigned dejavu (char *key) |
430 | | 19 { |
431 | | 20 unsigned index; |
432 | | 21 hte_item_t *curr; |
433 | | 22 static hte_root_t *root_namealloc; |
434 | | 23 |
435 | | 24 if (key) |
436 | | 25 { |
437 | | 26 /* création du dictionnaire la première fois */ |
438 | | 27 if (root_namealloc == NULL) |
439 | | 28 root_namealloc = hte_create (MAX_NAMEALLOC); |
440 | | 29 |
441 | | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
442 | | 31 index = hte_hash (key) % root_namealloc->NB_LIST; |
443 | | 32 |
444 | | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
445 | | 34 for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
446 | | 35 { |
447 | | 36 if ((int) curr == 0x4d52) |
448 | | 37 fprintf (stderr, |
449 | | 38 "------------------------------------------------------------ ARG1 \n"); |
450 | | 39 if (strcmp (curr->KEY, key) == 0) |
451 | | 40 return 1; |
452 | | 41 } |
453 | | 42 |
454 | | 43 /* création d'une nouvelle entrée */ |
455 | | 44 curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
456 | | 45 if (curr) |
457 | | 46 { |
458 | | 47 if ((int) curr == 0x4d52) |
459 | | 48 fprintf (stderr, |
460 | | 49 "------------------------------------------------------------ ARG2 \n"); |
461 | | 50 strcpy (curr->KEY, key); |
462 | | 51 curr->NEXT = root_namealloc->LIST[index]; |
463 | | 52 if ((int) curr == 0x4d52) |
464 | | 53 fprintf (stderr, |
465 | | 54 "------------------------------------------------------------ ARG3 \n"); |
466 | | 55 root_namealloc->LIST[index] = curr; |
467 | | 56 return 0; |
468 | | 57 } |
469 | | 58 } |
470 | | 59 perror ("dejavu"); |
471 | | 60 exit (1); |
472 | | 61 } |
| 412 | #include "hte.h" |
| 413 | #include <stdio.h> |
| 414 | #include <string.h> |
| 415 | #include <stdlib.h> |
| 416 | }}} |
| 417 | {{{ |
| 418 | /* |
| 419 | * structure définissant un item du dictionnaire |
| 420 | * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 421 | * permettant de chainer les items ayant le meme index de hachage |
| 422 | */ |
| 423 | struct hte_item_s |
| 424 | { |
| 425 | struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 426 | char KEY[]; /* tableau flexible contenant la clé */ |
| 427 | }; |
| 428 | }}} |
| 429 | {{{ |
| 430 | unsigned dejavu (char *key) |
| 431 | { |
| 432 | unsigned index; |
| 433 | hte_item_t *curr; |
| 434 | static hte_root_t *root_namealloc; |
| 435 | |
| 436 | if (key) |
| 437 | { |
| 438 | /* création du dictionnaire la première fois */ |
| 439 | if (root_namealloc == NULL) |
| 440 | root_namealloc = hte_create (MAX_NAMEALLOC); |
| 441 | |
| 442 | /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 443 | index = hte_hash (key) % root_namealloc->NB_LIST; |
| 444 | |
| 445 | /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 446 | for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
| 447 | if (strcmp (curr->KEY, key) == 0) |
| 448 | return 1; |
| 449 | |
| 450 | /* création d'une nouvelle entrée */ |
| 451 | curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
| 452 | if (curr) |
| 453 | { |
| 454 | strcpy (curr->KEY, key); |
| 455 | curr->NEXT = root_namealloc->LIST[index]; |
| 456 | root_namealloc->LIST[index] = curr; |
| 457 | return 0; |
| 458 | } |
| 459 | } |
| 460 | perror ("dejavu"); |
| 461 | exit (1); |
| 462 | } |
480 | | 1 #include <stdio.h> |
481 | | 2 #include <string.h> |
482 | | 3 #include <stdlib.h> |
483 | | 4 #include "hte.h" |
484 | | 5 |
485 | | 6 |
486 | | 7 /* |
487 | | 8 * structure définissant un item du dictionnaire |
488 | | 9 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
489 | | 10 * permettant de chainer les items ayant le meme index de hachage |
490 | | 11 */ |
491 | | 12 struct hte_item_s |
492 | | 13 { |
493 | | 14 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
494 | | 15 hte_data_t *DATA; /* valeur associée à l'item */ |
495 | | 16 char KEY[]; /* tableau flexible contenant la clé */ |
496 | | 17 }; |
497 | | 18 |
498 | | 19 |
499 | | 20 /* |
500 | | 21 * Recherche d'un item de clé key dans le dictionaire root |
501 | | 22 */ |
502 | | 23 hte_data_t *hte_get (hte_root_t * root, char *key) |
503 | | 24 { |
504 | | 25 int index; |
505 | | 26 hte_item_t *curr; |
506 | | 27 |
507 | | 28 if (key) |
508 | | 29 { |
509 | | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
510 | | 31 index = hte_hash (key) % root->NB_LIST; |
511 | | 32 |
512 | | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
513 | | 34 for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
514 | | 35 if (strcmp (key, curr->KEY) == 0) |
515 | | 36 return curr->DATA; |
516 | | 37 |
517 | | 38 return NULL; |
518 | | 39 } |
519 | | 40 perror ("hte_get"); |
520 | | 41 exit (1); |
521 | | 42 } |
522 | | 43 |
523 | | 44 /* |
524 | | 45 * Recherche d'un item de clé key dans le dictionnaire root |
525 | | 46 * avec création en cas d'absence |
526 | | 47 * et affectation d'une nouvelle donnée data |
527 | | 48 */ |
528 | | 49 void hte_add (hte_root_t * root, char *key, hte_data_t * data) |
529 | | 50 { |
530 | | 51 int index; |
531 | | 52 hte_item_t *curr; |
532 | | 53 |
533 | | 54 if (key) |
534 | | 55 { |
535 | | 56 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
536 | | 57 index = hte_hash (key) % root->NB_LIST; |
537 | | 58 |
538 | | 59 /* recherche et l'item dans sa liste et sortie si trouvé */ |
539 | | 60 for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
540 | | 61 { |
541 | | 62 if (strcmp (curr->KEY, key) == 0) |
542 | | 63 { |
543 | | 64 curr->DATA = data; |
544 | | 65 return; |
545 | | 66 } |
546 | | 67 } |
547 | | 68 |
548 | | 69 /* création d'une nouvelle entrée */ |
549 | | 70 curr = malloc (sizeof (struct hte_item_t *) + sizeof (void *) + strlen (key) + 1); |
550 | | 71 if (curr) |
551 | | 72 { |
552 | | 73 strcpy (curr->KEY, key); |
553 | | 74 curr->NEXT = root->LIST[index]; |
554 | | 75 root->LIST[index] = curr; |
555 | | 76 curr->DATA = data; |
556 | | 77 return; |
557 | | 78 } |
558 | | 79 } |
559 | | 80 perror ("hte_add"); |
560 | | 81 exit (1); |
561 | | 82 } |
| 470 | #include <stdio.h> |
| 471 | #include <string.h> |
| 472 | #include <stdlib.h> |
| 473 | #include "hte.h" |
| 474 | }}} |
| 475 | {{{ |
| 476 | /* |
| 477 | * structure définissant un item du dictionnaire |
| 478 | * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 479 | * permettant de chainer les items ayant le meme index de hachage |
| 480 | */ |
| 481 | struct hte_item_s |
| 482 | { |
| 483 | struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 484 | hte_data_t *DATA; /* valeur associée à l'item */ |
| 485 | char KEY[]; /* tableau flexible contenant la clé */ |
| 486 | }; |
| 487 | }}} |
| 488 | {{{ |
| 489 | /* |
| 490 | * Recherche d'un item de clé key dans le dictionaire root |
| 491 | */ |
| 492 | hte_data_t *hte_get (hte_root_t * root, char *key) |
| 493 | { |
| 494 | int index; |
| 495 | hte_item_t *curr; |
| 496 | |
| 497 | if (key) |
| 498 | { |
| 499 | /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 500 | index = hte_hash (key) % root->NB_LIST; |
| 501 | |
| 502 | /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 503 | for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
| 504 | if (strcmp (key, curr->KEY) == 0) |
| 505 | return curr->DATA; |
| 506 | |
| 507 | return NULL; |
| 508 | } |
| 509 | perror ("hte_get"); |
| 510 | exit (1); |
| 511 | } |
| 512 | }}} |
| 513 | {{{ |
| 514 | /* |
| 515 | * Recherche d'un item de clé key dans le dictionnaire root |
| 516 | * avec création en cas d'absence |
| 517 | * et affectation d'une nouvelle donnée data |
| 518 | */ |
| 519 | void hte_add (hte_root_t * root, char *key, hte_data_t * data) |
| 520 | { |
| 521 | int index; |
| 522 | hte_item_t *curr; |
| 523 | |
| 524 | if (key) |
| 525 | { |
| 526 | /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 527 | index = hte_hash (key) % root->NB_LIST; |
| 528 | |
| 529 | /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 530 | for (curr = root->LIST[index]; curr; curr = curr->NEXT) |
| 531 | { |
| 532 | if (strcmp (curr->KEY, key) == 0) |
| 533 | { |
| 534 | curr->DATA = data; |
| 535 | return; |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | /* création d'une nouvelle entrée */ |
| 540 | curr = malloc (sizeof (struct hte_item_t *) + sizeof (void *) + strlen (key) + 1); |
| 541 | if (curr) |
| 542 | { |
| 543 | strcpy (curr->KEY, key); |
| 544 | curr->NEXT = root->LIST[index]; |
| 545 | root->LIST[index] = curr; |
| 546 | curr->DATA = data; |
| 547 | return; |
| 548 | } |
| 549 | } |
| 550 | perror ("hte_add"); |
| 551 | exit (1); |
| 552 | } |
568 | | 1 #include "hte.h" |
569 | | 2 #include <stdio.h> |
570 | | 3 #include <string.h> |
571 | | 4 #include <stdlib.h> |
572 | | 5 |
573 | | 6 /* |
574 | | 7 * structure définissant un item du dictionnaire |
575 | | 8 * Un item est formé d'un couple (clé,valeur) plus un pointeur |
576 | | 9 * permettant de chainer les items ayant le meme index de hachage |
577 | | 10 */ |
578 | | 11 struct hte_item_s |
579 | | 12 { |
580 | | 13 struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
581 | | 14 char KEY[]; /* tableau flexible contenant la clé */ |
582 | | 15 }; |
583 | | 16 |
584 | | 17 |
585 | | 18 char *namealloc (char *key) |
586 | | 19 { |
587 | | 20 int index; |
588 | | 21 hte_item_t *curr; |
589 | | 22 static hte_root_t *root_namealloc; |
590 | | 23 |
591 | | 24 if (key) |
592 | | 25 { |
593 | | 26 /* création du dictionnaire la première fois */ |
594 | | 27 if (root_namealloc == NULL) |
595 | | 28 root_namealloc = hte_create (MAX_NAMEALLOC); |
596 | | 29 |
597 | | 30 /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
598 | | 31 index = hte_hash (key) % root_namealloc->NB_LIST; |
599 | | 32 |
600 | | 33 /* recherche et l'item dans sa liste et sortie si trouvé */ |
601 | | 34 for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
602 | | 35 { |
603 | | 36 if (strcmp (curr->KEY, key) == 0) |
604 | | 37 return curr->KEY; |
605 | | 38 } |
606 | | 39 |
607 | | 40 /* création d'une nouvelle entrée */ |
608 | | 41 curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
609 | | 42 if (curr) |
610 | | 43 { |
611 | | 44 strcpy (curr->KEY, key); |
612 | | 45 curr->NEXT = root_namealloc->LIST[index]; |
613 | | 46 root_namealloc->LIST[index] = curr; |
614 | | 47 return curr->KEY; |
615 | | 48 } |
616 | | 49 } |
617 | | 50 perror ("namealloc"); |
618 | | 51 exit (1); |
619 | | 52 } |
620 | | }}} |
| 559 | #include "hte.h" |
| 560 | #include <stdio.h> |
| 561 | #include <string.h> |
| 562 | #include <stdlib.h> |
| 563 | }}} |
| 564 | {{{ |
| 565 | /* |
| 566 | * structure définissant un item du dictionnaire |
| 567 | * Un item est formé d'un couple (clé,valeur) plus un pointeur |
| 568 | * permettant de chainer les items ayant le meme index de hachage |
| 569 | */ |
| 570 | struct hte_item_s |
| 571 | { |
| 572 | struct hte_item_s *NEXT; /* pointeur sur l'item suivant */ |
| 573 | char KEY[]; /* tableau flexible contenant la clé */ |
| 574 | }; |
| 575 | }}} |
| 576 | {{{ |
| 577 | char *namealloc (char *key) |
| 578 | { |
| 579 | int index; |
| 580 | hte_item_t *curr; |
| 581 | static hte_root_t *root_namealloc; |
| 582 | |
| 583 | if (key) |
| 584 | { |
| 585 | /* création du dictionnaire la première fois */ |
| 586 | if (root_namealloc == NULL) |
| 587 | root_namealloc = hte_create (MAX_NAMEALLOC); |
| 588 | |
| 589 | /* calcul de l'index pour trouver la liste ou devrait être l'item */ |
| 590 | index = hte_hash (key) % root_namealloc->NB_LIST; |
| 591 | |
| 592 | /* recherche et l'item dans sa liste et sortie si trouvé */ |
| 593 | for (curr = root_namealloc->LIST[index]; curr; curr = curr->NEXT) |
| 594 | { |
| 595 | if (strcmp (curr->KEY, key) == 0) |
| 596 | return curr->KEY; |
| 597 | } |
| 598 | |
| 599 | /* création d'une nouvelle entrée */ |
| 600 | curr = malloc (sizeof (struct hte_item_t *) + strlen (key) + 1); |
| 601 | if (curr) |
| 602 | { |
| 603 | strcpy (curr->KEY, key); |
| 604 | curr->NEXT = root_namealloc->LIST[index]; |
| 605 | root_namealloc->LIST[index] = curr; |
| 606 | return curr->KEY; |
| 607 | } |
| 608 | } |
| 609 | perror ("namealloc"); |
| 610 | exit (1); |
| 611 | } |
| 612 | }}} |