gdsl  1.8
examples/main_hash.c

This is an example of how to use gdsl_hash module.

/*
 * This file is part of the Generic Data Structures Library (GDSL).
 * Copyright (C) 1998-2017 Nicolas Darnis <ndarnis@free.fr>.
 *
 * GDSL 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.
 *
 * GDSL 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 GDSL.  If not, see <http://www.gnu.org/licenses/>. 
 *
 * $RCSfile: main_hash.c,v $
 * $Revision: 1.25 $
 * $Date: 2015/02/17 12:33:16 $
 */


#include <config.h>


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mcheck.h>


#include "gdsl_types.h"
#include "gdsl_hash.h"
#include "_strings.h"


#define SIZE 11 /* Should be prime number !! */


struct _my_struct
{
    int  integer;
    char*string;
};
typedef struct _my_struct* my_struct;

static gdsl_element_t 
my_struct_alloc (void* d)
{
    static int n = 0;

    my_struct e = (my_struct) malloc (sizeof (struct _my_struct));
    if (e == NULL)
    {
        return NULL;
    }

    e->integer = n++;
    e->string = strdup ((char*) d);

    return (gdsl_element_t) e;
}

static void 
my_struct_free (gdsl_element_t e)
{
    my_struct s = (my_struct) e;
    free (s->string);
    free (s);
}

static void
my_struct_printf (gdsl_element_t e, FILE* file, gdsl_location_t location, void* d)
{
    my_struct s = (my_struct) e;
    fprintf (file, "%d:%s ", s->integer, s->string);
}     

const char* 
my_struct_key (gdsl_element_t e)
{
    my_struct s = (my_struct) e;
    return s->string;
}

int main (void)
{
    int         choice;
    gdsl_hash_t ht;
    
    mtrace ();

    ht = gdsl_hash_alloc ("MY HASH TABLE", my_struct_alloc, my_struct_free, my_struct_key, NULL, SIZE);
    if (ht == NULL)
    {
        fprintf (stderr, "%s:%d: %s - gdsl_hash_alloc(): NULL", 
             __FILE__, __LINE__, __FUNCTION__);
        exit (EXIT_FAILURE);
    }

    do
    {
        printf ("\t\tMENU - HASH\n\n");
        printf ("\t1> Insert\n");
        printf ("\t2> Search\n");
        printf ("\t3> Remove\n");
        printf ("\t4> Display\n");
        printf ("\t5> Flush\n");
        printf ("\t6> Fill factor\n");
        printf ("\t7> Dump\n");
        printf ("\t8> XML display\n");
        printf ("\t0> Quit\n\n");
        printf ("\t\tYour choice: ");
        scanf ("%d", &choice);

        switch (choice)
        {
        case 1:
            {
            char nom[50];

            printf ("String: ");
            scanf ("%s", nom);

            if (gdsl_hash_insert (ht, (void*) nom) == NULL)
                {
                printf ("ERROR: Insert failed!\n");
                }
            }
            break;

        case 2:
            {
            char nom[50];
            gdsl_element_t e;

            printf ("String: ");
            scanf ("%s", nom);

            e = gdsl_hash_search (ht, nom);
            if (e == NULL)
                {
                printf ("String '%s' doesn't exist\n", nom);
                }
            else
                {
                printf ("String '%s' found\n", nom);
                }
            }
            break;

        case 3:
            {
            char nom[50];
            gdsl_element_t e;

            printf ("String: ");
            scanf ("%s", nom);

            e = gdsl_hash_remove (ht, nom);
            if (e == NULL)
                {
                printf ("String '%s' doesn't exist\n", nom);
                }
            else
                {
                free_string (e);
                }
            }
            break;

        case 4:
            gdsl_hash_write (ht, my_struct_printf, stdout, " ");
            printf ("\n");
            break;

        case 5:
            gdsl_hash_flush (ht);
            break;

        case 6:
            printf ("Fill factor: %g\n", gdsl_hash_get_fill_factor (ht));
            break;

        case 7:
            gdsl_hash_dump (ht, my_struct_printf, stdout, NULL);
            break;

        case 8:
            gdsl_hash_write_xml (ht, my_struct_printf, stdout, NULL);
            break;
        }
    } 
    while (choice != 0);

    gdsl_hash_free (ht);

    exit (EXIT_SUCCESS);
}