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
|
/* str_ht.c - single (char *) hash table
Copyright 1988-2017 Free Software Foundation, Inc.
This program 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, or (at your option)
any later version.
This program 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 program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
#include <config.h>
#include "system.h"
/* Hack! */
#define hash_table_s string_htable
#include "str_ht.h"
#include "hashtab.h"
/************************************************************************
* hash tables with one char * field *
************************************************************************/
/*
* Basic routines
*/
static unsigned long
string_hash_1 (char *string)
{
return_STRING_HASH_1 (string);
}
static unsigned long
string_hash_2 (char *string)
{
return_STRING_HASH_2 (string);
}
static int
string_hash_cmp (char *x, char *y)
{
return_STRING_COMPARE (x, y);
}
/*
* For sorting them in alpha order
*/
static int
string_hash_qcmp (char **x, char **y)
{
return_STRING_COMPARE (*x, *y);
}
/*
* Create the structure that stores the list of strings
*/
struct string_htable *
string_htable_new (void)
{
struct string_htable * res;
res = XMALLOC (struct string_htable);
hash_init (res, 8,
(hash_func_t) string_hash_1,
(hash_func_t) string_hash_2,
(hash_cmp_func_t) string_hash_cmp);
return res;
}
/*
* Add a string, with your own allocation for them.
*/
void
string_htable_add (struct string_htable * table, const char * key)
{
if (!hash_find_item (table, key))
hash_insert (table, xstrdup(key));
}
/*
* Get the value associated to KEY in TABLE
* Return NULL upon error (this means that it is not
* valid to enter NULL as a value)
*/
char *
string_htable_get (struct string_htable * table, const char * key)
{
return (char *) hash_find_item (table, key);
}
/*
* Mostly for debbuging
*/
void
string_htable_self_print (struct string_htable * table, FILE * stream)
{
int i;
char ** entries;
entries = (char **)
hash_dump (table, NULL,
(hash_cmp_func_t) string_hash_qcmp);
for (i = 0 ; entries[i] ; i++)
fprintf (stream, "%s\n", entries[i]);
putc ('\n', stream);
}
/*
* Dump in a vector
*/
char **
string_htable_dump_sorted (struct string_htable * table)
{
return (char **) hash_dump (table, NULL, (qsort_cmp_t) string_hash_qcmp);
}
|