summaryrefslogtreecommitdiff
path: root/lib/str_ht.c
blob: 6278026a919e20323a37bd0025d41af08b5b3e45 (plain)
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
/*
 * str_ht.c
 *
 * Single (char *) hash table
 *
 * Copyright (c) 1988, 89, 90, 91, 92, 93 Miguel Santana
 * Copyright (c) 1995, 96, 97, 98 Akim Demaille, Miguel Santana
 */

/*
 * This file is part of a2ps.
 * 
 * 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; see the file COPYING.  If not, write to
 * the Free Software Foundation, 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * $Id: str_ht.c,v 1.1.1.1.2.1 2007/12/29 01:58:23 mhatta Exp $
 */

#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, 1);
  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;
}

/*
 * Free the whole structure
 */
void
string_htable_free (struct string_htable * table)
{
  hash_free (table, (hash_map_func_t) free);
  free (table);
}

/*
 *  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);
  free (entries);
}

/*
 * 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);
}