root/libswish3/trunk/src/libswish3/hash.c

Revision 2166, 3.1 kB (checked in by karpet, 2 months ago)

store codepoints instead of start_byte (since we no longer need the latter); optimize the context storage a little to reduce number of malloc/frees

Line 
1 /*
2  * This file is part of libswish3
3  * Copyright (C) 2007 Peter Karman
4  *
5  *  libswish3 is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  libswish3 is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with libswish3; if not, write to the Free Software
17  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18 */
19
20 /* wrappers to common functions in libxml2 hash */
21
22 #include <stdlib.h>
23
24 #include "libswish3.h"
25
26 extern int SWISH_DEBUG;
27
28 static void free_hashval(
29     void *val,
30     xmlChar *key
31 );
32 static void merge_hashes(
33     xmlChar *value,
34     xmlHashTablePtr hash1,
35     xmlChar *key
36 );
37
38 static void
39 free_hashval(
40     void *val,
41     xmlChar *key
42 )
43 {
44     swish_xfree(val);
45 }
46
47 int
48 swish_hash_add(
49     xmlHashTablePtr hash,
50     xmlChar *key,
51     void *value
52 )
53 {
54     int ret;
55     ret = xmlHashAddEntry(hash, key, value);
56     if (ret == -1)
57         SWISH_CROAK("xmlHashAddEntry for %s failed", key);
58
59     return ret;
60 }
61
62 int
63 swish_hash_exists_or_add(
64     xmlHashTablePtr hash,
65     xmlChar *key,
66     xmlChar *value
67 )
68 {
69     if (!swish_hash_exists(hash, key)) {
70         return swish_hash_add(hash, key, swish_xstrdup( value ));
71     }
72     else {
73         return 1;
74     }
75 }
76
77 void
78 swish_hash_free(
79     xmlHashTablePtr hash
80 )
81 {
82     xmlHashFree(hash, (xmlHashDeallocator)free_hashval);
83 }
84
85 int
86 swish_hash_replace(
87     xmlHashTablePtr hash,
88     xmlChar *key,
89     void *value
90 )
91 {
92     int ret;
93     ret =
94         xmlHashUpdateEntry(hash, key, value, (xmlHashDeallocator) free_hashval);
95     if (ret == -1)
96         SWISH_CROAK("xmlHashUpdateEntry for %s failed", key);
97
98     return ret;
99 }
100
101 int
102 swish_hash_delete(
103     xmlHashTablePtr hash,
104     xmlChar *key
105 )
106 {
107     int ret;
108     ret = xmlHashRemoveEntry(hash, key, (xmlHashDeallocator) free_hashval);
109     if (ret == -1)
110         SWISH_CROAK("xmlHashRemoveEntry for %s failed", key);
111
112     return ret;
113 }
114
115 boolean
116 swish_hash_exists(
117     xmlHashTablePtr hash,
118     xmlChar *key
119 )
120 {
121     return xmlHashLookup(hash, key) ? 1 : 0;
122 }
123
124 void *
125 swish_hash_fetch(
126     xmlHashTablePtr hash,
127     xmlChar *key
128 )
129 {
130     return xmlHashLookup(hash, key);
131 }
132
133 xmlHashTablePtr
134 swish_init_hash(
135     int size
136 )
137 {
138     xmlHashTablePtr h;
139
140     h = xmlHashCreate(size);
141     if (h == NULL) {
142         SWISH_CROAK("error creating hash of size %d", size);
143         return NULL; // never get here
144     }
145
146     return h;
147 }
148
149 static void
150 merge_hashes(
151     xmlChar *value,
152     xmlHashTablePtr hash1,
153     xmlChar *key
154 )
155 {
156     if (swish_hash_exists(hash1, key)) {
157         swish_hash_replace(hash1, key, swish_xstrdup(value));
158     }
159     else {
160         swish_hash_add(hash1, key, swish_xstrdup(value));
161     }
162 }
163
164 void
165 swish_hash_merge(
166     xmlHashTablePtr hash1,
167     xmlHashTablePtr hash2
168 )
169 {
170     xmlHashScan(hash2, (xmlHashScanner) merge_hashes, hash1);
171 }
Note: See TracBrowser for help on using the browser.