Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * hash.c: chained hash tables |
| 3 | * |
| 4 | * Reference: Your favorite introductory book on algorithms |
| 5 | * |
| 6 | * Copyright (C) 2000 Bjorn Reese and Daniel Veillard. |
| 7 | * |
| 8 | * Permission to use, copy, modify, and distribute this software for any |
| 9 | * purpose with or without fee is hereby granted, provided that the above |
| 10 | * copyright notice and this permission notice appear in all copies. |
| 11 | * |
| 12 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED |
| 13 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF |
| 14 | * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND |
| 15 | * CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER. |
| 16 | * |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 17 | * Author: breese@users.sourceforge.net |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Daniel Veillard | 34ce8be | 2002-03-18 19:37:11 +0000 | [diff] [blame] | 20 | #define IN_LIBXML |
Bjorn Reese | 70a9da5 | 2001-04-21 16:57:29 +0000 | [diff] [blame] | 21 | #include "libxml.h" |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 22 | |
| 23 | #include <string.h> |
| 24 | #include <libxml/hash.h> |
| 25 | #include <libxml/xmlmemory.h> |
| 26 | #include <libxml/parser.h> |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 27 | #include <libxml/xmlerror.h> |
Daniel Veillard | 3c01b1d | 2001-10-17 15:58:35 +0000 | [diff] [blame] | 28 | #include <libxml/globals.h> |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 29 | |
| 30 | #define MAX_HASH_LEN 8 |
| 31 | |
| 32 | /* #define DEBUG_GROW */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * A single entry in the hash table |
| 36 | */ |
| 37 | typedef struct _xmlHashEntry xmlHashEntry; |
| 38 | typedef xmlHashEntry *xmlHashEntryPtr; |
| 39 | struct _xmlHashEntry { |
| 40 | struct _xmlHashEntry *next; |
| 41 | xmlChar *name; |
| 42 | xmlChar *name2; |
| 43 | xmlChar *name3; |
| 44 | void *payload; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 45 | int valid; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | /* |
| 49 | * The entire hash table |
| 50 | */ |
| 51 | struct _xmlHashTable { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 52 | struct _xmlHashEntry *table; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 53 | int size; |
| 54 | int nbElems; |
| 55 | }; |
| 56 | |
| 57 | /* |
| 58 | * xmlHashComputeKey: |
| 59 | * Calculate the hash key |
| 60 | */ |
| 61 | static unsigned long |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 62 | xmlHashComputeKey(xmlHashTablePtr table, const xmlChar *name, |
| 63 | const xmlChar *name2, const xmlChar *name3) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 64 | unsigned long value = 0L; |
| 65 | char ch; |
| 66 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 67 | if (name != NULL) { |
| 68 | value += 30 * (*name); |
| 69 | while ((ch = *name++) != 0) { |
Daniel Veillard | 5f7f991 | 2002-06-17 17:03:00 +0000 | [diff] [blame] | 70 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 71 | } |
| 72 | } |
| 73 | if (name2 != NULL) { |
| 74 | while ((ch = *name2++) != 0) { |
Daniel Veillard | 5f7f991 | 2002-06-17 17:03:00 +0000 | [diff] [blame] | 75 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | if (name3 != NULL) { |
| 79 | while ((ch = *name3++) != 0) { |
Daniel Veillard | 5f7f991 | 2002-06-17 17:03:00 +0000 | [diff] [blame] | 80 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 81 | } |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 82 | } |
| 83 | return (value % table->size); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * xmlHashCreate: |
| 88 | * @size: the size of the hash table |
| 89 | * |
| 90 | * Create a new xmlHashTablePtr. |
| 91 | * |
| 92 | * Returns the newly created object, or NULL if an error occured. |
| 93 | */ |
| 94 | xmlHashTablePtr |
| 95 | xmlHashCreate(int size) { |
| 96 | xmlHashTablePtr table; |
| 97 | |
| 98 | if (size <= 0) |
| 99 | size = 256; |
| 100 | |
| 101 | table = xmlMalloc(sizeof(xmlHashTable)); |
| 102 | if (table) { |
| 103 | table->size = size; |
| 104 | table->nbElems = 0; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 105 | table->table = xmlMalloc(size * sizeof(xmlHashEntry)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 106 | if (table->table) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 107 | memset(table->table, 0, size * sizeof(xmlHashEntry)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 108 | return(table); |
| 109 | } |
| 110 | xmlFree(table); |
| 111 | } |
| 112 | return(NULL); |
| 113 | } |
| 114 | |
| 115 | /** |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 116 | * xmlHashGrow: |
| 117 | * @table: the hash table |
| 118 | * @size: the new size of the hash table |
| 119 | * |
| 120 | * resize the hash table |
| 121 | * |
| 122 | * Returns 0 in case of success, -1 in case of failure |
| 123 | */ |
Daniel Veillard | dab4cb3 | 2001-04-20 13:03:48 +0000 | [diff] [blame] | 124 | static int |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 125 | xmlHashGrow(xmlHashTablePtr table, int size) { |
| 126 | unsigned long key; |
| 127 | int oldsize, i; |
| 128 | xmlHashEntryPtr iter, next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 129 | struct _xmlHashEntry *oldtable; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 130 | #ifdef DEBUG_GROW |
| 131 | unsigned long nbElem = 0; |
| 132 | #endif |
| 133 | |
| 134 | if (table == NULL) |
| 135 | return(-1); |
| 136 | if (size < 8) |
| 137 | return(-1); |
| 138 | if (size > 8 * 2048) |
| 139 | return(-1); |
| 140 | |
| 141 | oldsize = table->size; |
| 142 | oldtable = table->table; |
| 143 | if (oldtable == NULL) |
| 144 | return(-1); |
| 145 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 146 | table->table = xmlMalloc(size * sizeof(xmlHashEntry)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 147 | if (table->table == NULL) { |
| 148 | table->table = oldtable; |
| 149 | return(-1); |
| 150 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 151 | memset(table->table, 0, size * sizeof(xmlHashEntry)); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 152 | table->size = size; |
| 153 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 154 | /* If the two loops are merged, there would be situations where |
| 155 | a new entry needs to allocated and data copied into it from |
| 156 | the main table. So instead, we run through the array twice, first |
| 157 | copying all the elements in the main array (where we can't get |
| 158 | conflicts) and then the rest, so we only free (and don't allocate) |
| 159 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 160 | for (i = 0; i < oldsize; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 161 | if (oldtable[i].valid == 0) |
| 162 | continue; |
| 163 | key = xmlHashComputeKey(table, oldtable[i].name, oldtable[i].name2, |
| 164 | oldtable[i].name3); |
| 165 | memcpy(&(table->table[key]), &(oldtable[i]), sizeof(xmlHashEntry)); |
| 166 | table->table[key].next = NULL; |
| 167 | } |
| 168 | |
| 169 | for (i = 0; i < oldsize; i++) { |
| 170 | iter = oldtable[i].next; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 171 | while (iter) { |
| 172 | next = iter->next; |
| 173 | |
| 174 | /* |
| 175 | * put back the entry in the new table |
| 176 | */ |
| 177 | |
| 178 | key = xmlHashComputeKey(table, iter->name, iter->name2, |
| 179 | iter->name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 180 | if (table->table[key].valid == 0) { |
| 181 | memcpy(&(table->table[key]), iter, sizeof(xmlHashEntry)); |
| 182 | table->table[key].next = NULL; |
| 183 | xmlFree(iter); |
| 184 | } else { |
| 185 | iter->next = table->table[key].next; |
| 186 | table->table[key].next = iter; |
| 187 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 188 | |
| 189 | #ifdef DEBUG_GROW |
| 190 | nbElem++; |
| 191 | #endif |
| 192 | |
| 193 | iter = next; |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | xmlFree(oldtable); |
| 198 | |
| 199 | #ifdef DEBUG_GROW |
| 200 | xmlGenericError(xmlGenericErrorContext, |
| 201 | "xmlHashGrow : from %d to %d, %d elems\n", oldsize, size, nbElem); |
| 202 | #endif |
| 203 | |
| 204 | return(0); |
| 205 | } |
| 206 | |
| 207 | /** |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 208 | * xmlHashFree: |
| 209 | * @table: the hash table |
| 210 | * @f: the deallocator function for items in the hash |
| 211 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 212 | * Free the hash @table and its contents. The userdata is |
| 213 | * deallocated with @f if provided. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 214 | */ |
| 215 | void |
| 216 | xmlHashFree(xmlHashTablePtr table, xmlHashDeallocator f) { |
| 217 | int i; |
| 218 | xmlHashEntryPtr iter; |
| 219 | xmlHashEntryPtr next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 220 | int inside_table = 0; |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame^] | 221 | int nbElems; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 222 | |
| 223 | if (table == NULL) |
| 224 | return; |
| 225 | if (table->table) { |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame^] | 226 | nbElems = table->nbElems; |
| 227 | for(i = 0; (i < table->size) && (nbElems > 0); i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 228 | iter = &(table->table[i]); |
| 229 | if (iter->valid == 0) |
| 230 | continue; |
| 231 | inside_table = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 232 | while (iter) { |
| 233 | next = iter->next; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 234 | if (f) |
| 235 | f(iter->payload, iter->name); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 236 | if (iter->name) |
| 237 | xmlFree(iter->name); |
| 238 | if (iter->name2) |
| 239 | xmlFree(iter->name2); |
| 240 | if (iter->name3) |
| 241 | xmlFree(iter->name3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 242 | iter->payload = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 243 | if (!inside_table) |
| 244 | xmlFree(iter); |
Daniel Veillard | 6155d8a | 2003-08-19 15:01:28 +0000 | [diff] [blame^] | 245 | nbElems--; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 246 | inside_table = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 247 | iter = next; |
| 248 | } |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 249 | inside_table = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 250 | } |
| 251 | xmlFree(table->table); |
| 252 | } |
| 253 | xmlFree(table); |
| 254 | } |
| 255 | |
| 256 | /** |
| 257 | * xmlHashAddEntry: |
| 258 | * @table: the hash table |
| 259 | * @name: the name of the userdata |
| 260 | * @userdata: a pointer to the userdata |
| 261 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 262 | * Add the @userdata to the hash @table. This can later be retrieved |
| 263 | * by using the @name. Duplicate names generate errors. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 264 | * |
| 265 | * Returns 0 the addition succeeded and -1 in case of error. |
| 266 | */ |
| 267 | int |
| 268 | xmlHashAddEntry(xmlHashTablePtr table, const xmlChar *name, void *userdata) { |
| 269 | return(xmlHashAddEntry3(table, name, NULL, NULL, userdata)); |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * xmlHashAddEntry2: |
| 274 | * @table: the hash table |
| 275 | * @name: the name of the userdata |
| 276 | * @name2: a second name of the userdata |
| 277 | * @userdata: a pointer to the userdata |
| 278 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 279 | * Add the @userdata to the hash @table. This can later be retrieved |
| 280 | * by using the (@name, @name2) tuple. Duplicate tuples generate errors. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 281 | * |
| 282 | * Returns 0 the addition succeeded and -1 in case of error. |
| 283 | */ |
| 284 | int |
| 285 | xmlHashAddEntry2(xmlHashTablePtr table, const xmlChar *name, |
| 286 | const xmlChar *name2, void *userdata) { |
| 287 | return(xmlHashAddEntry3(table, name, name2, NULL, userdata)); |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * xmlHashUpdateEntry: |
| 292 | * @table: the hash table |
| 293 | * @name: the name of the userdata |
| 294 | * @userdata: a pointer to the userdata |
| 295 | * @f: the deallocator function for replaced item (if any) |
| 296 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 297 | * Add the @userdata to the hash @table. This can later be retrieved |
| 298 | * by using the @name. Existing entry for this @name will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 299 | * and freed with @f if found. |
| 300 | * |
| 301 | * Returns 0 the addition succeeded and -1 in case of error. |
| 302 | */ |
| 303 | int |
| 304 | xmlHashUpdateEntry(xmlHashTablePtr table, const xmlChar *name, |
| 305 | void *userdata, xmlHashDeallocator f) { |
| 306 | return(xmlHashUpdateEntry3(table, name, NULL, NULL, userdata, f)); |
| 307 | } |
| 308 | |
| 309 | /** |
| 310 | * xmlHashUpdateEntry2: |
| 311 | * @table: the hash table |
| 312 | * @name: the name of the userdata |
| 313 | * @name2: a second name of the userdata |
| 314 | * @userdata: a pointer to the userdata |
| 315 | * @f: the deallocator function for replaced item (if any) |
| 316 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 317 | * Add the @userdata to the hash @table. This can later be retrieved |
| 318 | * by using the (@name, @name2) tuple. Existing entry for this tuple will |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 319 | * be removed and freed with @f if found. |
| 320 | * |
| 321 | * Returns 0 the addition succeeded and -1 in case of error. |
| 322 | */ |
| 323 | int |
| 324 | xmlHashUpdateEntry2(xmlHashTablePtr table, const xmlChar *name, |
| 325 | const xmlChar *name2, void *userdata, |
| 326 | xmlHashDeallocator f) { |
| 327 | return(xmlHashUpdateEntry3(table, name, name2, NULL, userdata, f)); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * xmlHashLookup: |
| 332 | * @table: the hash table |
| 333 | * @name: the name of the userdata |
| 334 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 335 | * Find the userdata specified by the @name. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 336 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 337 | * Returns the pointer to the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 338 | */ |
| 339 | void * |
| 340 | xmlHashLookup(xmlHashTablePtr table, const xmlChar *name) { |
| 341 | return(xmlHashLookup3(table, name, NULL, NULL)); |
| 342 | } |
| 343 | |
| 344 | /** |
| 345 | * xmlHashLookup2: |
| 346 | * @table: the hash table |
| 347 | * @name: the name of the userdata |
| 348 | * @name2: a second name of the userdata |
| 349 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 350 | * Find the userdata specified by the (@name, @name2) tuple. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 351 | * |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 352 | * Returns the pointer to the userdata |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 353 | */ |
| 354 | void * |
| 355 | xmlHashLookup2(xmlHashTablePtr table, const xmlChar *name, |
| 356 | const xmlChar *name2) { |
| 357 | return(xmlHashLookup3(table, name, name2, NULL)); |
| 358 | } |
| 359 | |
| 360 | /** |
| 361 | * xmlHashAddEntry3: |
| 362 | * @table: the hash table |
| 363 | * @name: the name of the userdata |
| 364 | * @name2: a second name of the userdata |
| 365 | * @name3: a third name of the userdata |
| 366 | * @userdata: a pointer to the userdata |
| 367 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 368 | * Add the @userdata to the hash @table. This can later be retrieved |
| 369 | * by using the tuple (@name, @name2, @name3). Duplicate entries generate |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 370 | * errors. |
| 371 | * |
| 372 | * Returns 0 the addition succeeded and -1 in case of error. |
| 373 | */ |
| 374 | int |
| 375 | xmlHashAddEntry3(xmlHashTablePtr table, const xmlChar *name, |
| 376 | const xmlChar *name2, const xmlChar *name3, |
| 377 | void *userdata) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 378 | unsigned long key, len = 0; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 379 | xmlHashEntryPtr entry; |
| 380 | xmlHashEntryPtr insert; |
| 381 | |
| 382 | if ((table == NULL) || name == NULL) |
| 383 | return(-1); |
| 384 | |
| 385 | /* |
| 386 | * Check for duplicate and insertion location. |
| 387 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 388 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 389 | if (table->table[key].valid == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 390 | insert = NULL; |
| 391 | } else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 392 | for (insert = &(table->table[key]); insert->next != NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 393 | insert = insert->next) { |
| 394 | if ((xmlStrEqual(insert->name, name)) && |
| 395 | (xmlStrEqual(insert->name2, name2)) && |
| 396 | (xmlStrEqual(insert->name3, name3))) |
| 397 | return(-1); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 398 | len++; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 399 | } |
| 400 | if ((xmlStrEqual(insert->name, name)) && |
| 401 | (xmlStrEqual(insert->name2, name2)) && |
| 402 | (xmlStrEqual(insert->name3, name3))) |
| 403 | return(-1); |
| 404 | } |
| 405 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 406 | if (insert == NULL) { |
| 407 | entry = &(table->table[key]); |
| 408 | } else { |
| 409 | entry = xmlMalloc(sizeof(xmlHashEntry)); |
| 410 | if (entry == NULL) |
| 411 | return(-1); |
| 412 | } |
| 413 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 414 | entry->name = xmlStrdup(name); |
| 415 | entry->name2 = xmlStrdup(name2); |
| 416 | entry->name3 = xmlStrdup(name3); |
| 417 | entry->payload = userdata; |
| 418 | entry->next = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 419 | entry->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 420 | |
| 421 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 422 | if (insert != NULL) |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 423 | insert->next = entry; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 424 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 425 | table->nbElems++; |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 426 | |
| 427 | if (len > MAX_HASH_LEN) |
| 428 | xmlHashGrow(table, MAX_HASH_LEN * table->size); |
| 429 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 430 | return(0); |
| 431 | } |
| 432 | |
| 433 | /** |
| 434 | * xmlHashUpdateEntry3: |
| 435 | * @table: the hash table |
| 436 | * @name: the name of the userdata |
| 437 | * @name2: a second name of the userdata |
| 438 | * @name3: a third name of the userdata |
| 439 | * @userdata: a pointer to the userdata |
| 440 | * @f: the deallocator function for replaced item (if any) |
| 441 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 442 | * Add the @userdata to the hash @table. This can later be retrieved |
| 443 | * by using the tuple (@name, @name2, @name3). Existing entry for this tuple |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 444 | * will be removed and freed with @f if found. |
| 445 | * |
| 446 | * Returns 0 the addition succeeded and -1 in case of error. |
| 447 | */ |
| 448 | int |
| 449 | xmlHashUpdateEntry3(xmlHashTablePtr table, const xmlChar *name, |
| 450 | const xmlChar *name2, const xmlChar *name3, |
| 451 | void *userdata, xmlHashDeallocator f) { |
| 452 | unsigned long key; |
| 453 | xmlHashEntryPtr entry; |
| 454 | xmlHashEntryPtr insert; |
| 455 | |
| 456 | if ((table == NULL) || name == NULL) |
| 457 | return(-1); |
| 458 | |
| 459 | /* |
| 460 | * Check for duplicate and insertion location. |
| 461 | */ |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 462 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 463 | if (table->table[key].valid == 0) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 464 | insert = NULL; |
| 465 | } else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 466 | for (insert = &(table->table[key]); insert->next != NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 467 | insert = insert->next) { |
| 468 | if ((xmlStrEqual(insert->name, name)) && |
| 469 | (xmlStrEqual(insert->name2, name2)) && |
| 470 | (xmlStrEqual(insert->name3, name3))) { |
| 471 | if (f) |
| 472 | f(insert->payload, insert->name); |
| 473 | insert->payload = userdata; |
| 474 | return(0); |
| 475 | } |
| 476 | } |
| 477 | if ((xmlStrEqual(insert->name, name)) && |
| 478 | (xmlStrEqual(insert->name2, name2)) && |
| 479 | (xmlStrEqual(insert->name3, name3))) { |
| 480 | if (f) |
| 481 | f(insert->payload, insert->name); |
| 482 | insert->payload = userdata; |
| 483 | return(0); |
| 484 | } |
| 485 | } |
| 486 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 487 | if (insert == NULL) { |
| 488 | entry = &(table->table[key]); |
| 489 | } else { |
| 490 | entry = xmlMalloc(sizeof(xmlHashEntry)); |
| 491 | if (entry == NULL) |
| 492 | return(-1); |
| 493 | } |
| 494 | |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 495 | entry->name = xmlStrdup(name); |
| 496 | entry->name2 = xmlStrdup(name2); |
| 497 | entry->name3 = xmlStrdup(name3); |
| 498 | entry->payload = userdata; |
| 499 | entry->next = NULL; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 500 | entry->valid = 1; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 501 | table->nbElems++; |
| 502 | |
| 503 | |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 504 | if (insert != NULL) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 505 | insert->next = entry; |
| 506 | } |
| 507 | return(0); |
| 508 | } |
| 509 | |
| 510 | /** |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 511 | * xmlHashLookup3: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 512 | * @table: the hash table |
| 513 | * @name: the name of the userdata |
| 514 | * @name2: a second name of the userdata |
| 515 | * @name3: a third name of the userdata |
| 516 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 517 | * Find the userdata specified by the (@name, @name2, @name3) tuple. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 518 | * |
| 519 | * Returns the a pointer to the userdata |
| 520 | */ |
| 521 | void * |
| 522 | xmlHashLookup3(xmlHashTablePtr table, const xmlChar *name, |
| 523 | const xmlChar *name2, const xmlChar *name3) { |
| 524 | unsigned long key; |
| 525 | xmlHashEntryPtr entry; |
| 526 | |
| 527 | if (table == NULL) |
| 528 | return(NULL); |
| 529 | if (name == NULL) |
| 530 | return(NULL); |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 531 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 532 | if (table->table[key].valid == 0) |
| 533 | return(NULL); |
| 534 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 535 | if ((xmlStrEqual(entry->name, name)) && |
| 536 | (xmlStrEqual(entry->name2, name2)) && |
| 537 | (xmlStrEqual(entry->name3, name3))) |
| 538 | return(entry->payload); |
| 539 | } |
| 540 | return(NULL); |
| 541 | } |
| 542 | |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 543 | typedef struct { |
| 544 | xmlHashScanner hashscanner; |
| 545 | void *data; |
| 546 | } stubData; |
| 547 | |
| 548 | static void |
| 549 | stubHashScannerFull (void *payload, void *data, const xmlChar *name, |
Daniel Veillard | aeb258a | 2002-09-13 14:48:12 +0000 | [diff] [blame] | 550 | const xmlChar *name2 ATTRIBUTE_UNUSED, |
| 551 | const xmlChar *name3 ATTRIBUTE_UNUSED) { |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 552 | stubData *stubdata = (stubData *) data; |
| 553 | stubdata->hashscanner (payload, stubdata->data, (xmlChar *) name); |
| 554 | } |
| 555 | |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 556 | /** |
| 557 | * xmlHashScan: |
| 558 | * @table: the hash table |
| 559 | * @f: the scanner function for items in the hash |
| 560 | * @data: extra data passed to f |
| 561 | * |
| 562 | * Scan the hash @table and applied @f to each value. |
| 563 | */ |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 564 | void |
| 565 | xmlHashScan(xmlHashTablePtr table, xmlHashScanner f, void *data) { |
Daniel Veillard | 153120c | 2002-06-18 07:58:35 +0000 | [diff] [blame] | 566 | stubData stubdata; |
| 567 | stubdata.data = data; |
| 568 | stubdata.hashscanner = f; |
| 569 | xmlHashScanFull (table, stubHashScannerFull, &stubdata); |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /** |
| 573 | * xmlHashScanFull: |
| 574 | * @table: the hash table |
| 575 | * @f: the scanner function for items in the hash |
| 576 | * @data: extra data passed to f |
| 577 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 578 | * Scan the hash @table and applied @f to each value. |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 579 | */ |
| 580 | void |
| 581 | xmlHashScanFull(xmlHashTablePtr table, xmlHashScannerFull f, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 582 | int i; |
| 583 | xmlHashEntryPtr iter; |
| 584 | xmlHashEntryPtr next; |
| 585 | |
| 586 | if (table == NULL) |
| 587 | return; |
| 588 | if (f == NULL) |
| 589 | return; |
| 590 | |
| 591 | if (table->table) { |
| 592 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 593 | if (table->table[i].valid == 0) |
| 594 | continue; |
| 595 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 596 | while (iter) { |
| 597 | next = iter->next; |
| 598 | if (f) |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 599 | f(iter->payload, data, iter->name, |
| 600 | iter->name2, iter->name3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 601 | iter = next; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | /** |
| 608 | * xmlHashScan3: |
| 609 | * @table: the hash table |
| 610 | * @name: the name of the userdata or NULL |
| 611 | * @name2: a second name of the userdata or NULL |
| 612 | * @name3: a third name of the userdata or NULL |
| 613 | * @f: the scanner function for items in the hash |
| 614 | * @data: extra data passed to f |
| 615 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 616 | * Scan the hash @table and applied @f to each value matching |
| 617 | * (@name, @name2, @name3) tuple. If one of the names is null, |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 618 | * the comparison is considered to match. |
| 619 | */ |
| 620 | void |
| 621 | xmlHashScan3(xmlHashTablePtr table, const xmlChar *name, |
| 622 | const xmlChar *name2, const xmlChar *name3, |
| 623 | xmlHashScanner f, void *data) { |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 624 | xmlHashScanFull3 (table, name, name2, name3, |
| 625 | (xmlHashScannerFull) f, data); |
| 626 | } |
| 627 | |
| 628 | /** |
| 629 | * xmlHashScanFull3: |
| 630 | * @table: the hash table |
| 631 | * @name: the name of the userdata or NULL |
| 632 | * @name2: a second name of the userdata or NULL |
| 633 | * @name3: a third name of the userdata or NULL |
| 634 | * @f: the scanner function for items in the hash |
| 635 | * @data: extra data passed to f |
| 636 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 637 | * Scan the hash @table and applied @f to each value matching |
| 638 | * (@name, @name2, @name3) tuple. If one of the names is null, |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 639 | * the comparison is considered to match. |
| 640 | */ |
| 641 | void |
| 642 | xmlHashScanFull3(xmlHashTablePtr table, const xmlChar *name, |
| 643 | const xmlChar *name2, const xmlChar *name3, |
| 644 | xmlHashScannerFull f, void *data) { |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 645 | int i; |
| 646 | xmlHashEntryPtr iter; |
| 647 | xmlHashEntryPtr next; |
| 648 | |
| 649 | if (table == NULL) |
| 650 | return; |
| 651 | if (f == NULL) |
| 652 | return; |
| 653 | |
| 654 | if (table->table) { |
| 655 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 656 | if (table->table[i].valid == 0) |
| 657 | continue; |
| 658 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 659 | while (iter) { |
| 660 | next = iter->next; |
| 661 | if (((name == NULL) || (xmlStrEqual(name, iter->name))) && |
| 662 | ((name2 == NULL) || (xmlStrEqual(name2, iter->name2))) && |
| 663 | ((name3 == NULL) || (xmlStrEqual(name3, iter->name3)))) { |
Thomas Broyer | e812624 | 2001-07-22 03:54:15 +0000 | [diff] [blame] | 664 | f(iter->payload, data, iter->name, |
| 665 | iter->name2, iter->name3); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 666 | } |
| 667 | iter = next; |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | /** |
| 674 | * xmlHashCopy: |
| 675 | * @table: the hash table |
| 676 | * @f: the copier function for items in the hash |
| 677 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 678 | * Scan the hash @table and applied @f to each value. |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 679 | * |
| 680 | * Returns the new table or NULL in case of error. |
| 681 | */ |
| 682 | xmlHashTablePtr |
| 683 | xmlHashCopy(xmlHashTablePtr table, xmlHashCopier f) { |
| 684 | int i; |
| 685 | xmlHashEntryPtr iter; |
| 686 | xmlHashEntryPtr next; |
| 687 | xmlHashTablePtr ret; |
| 688 | |
| 689 | if (table == NULL) |
| 690 | return(NULL); |
| 691 | if (f == NULL) |
| 692 | return(NULL); |
| 693 | |
| 694 | ret = xmlHashCreate(table->size); |
| 695 | if (table->table) { |
| 696 | for(i = 0; i < table->size; i++) { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 697 | if (table->table[i].valid == 0) |
| 698 | continue; |
| 699 | iter = &(table->table[i]); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 700 | while (iter) { |
| 701 | next = iter->next; |
| 702 | xmlHashAddEntry3(ret, iter->name, iter->name2, |
| 703 | iter->name3, f(iter->payload, iter->name)); |
| 704 | iter = next; |
| 705 | } |
| 706 | } |
| 707 | } |
| 708 | ret->nbElems = table->nbElems; |
| 709 | return(ret); |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * xmlHashSize: |
| 714 | * @table: the hash table |
| 715 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 716 | * Query the number of elements installed in the hash @table. |
Daniel Veillard | 5e2dace | 2001-07-18 19:30:27 +0000 | [diff] [blame] | 717 | * |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 718 | * Returns the number of elements in the hash table or |
| 719 | * -1 in case of error |
| 720 | */ |
| 721 | int |
| 722 | xmlHashSize(xmlHashTablePtr table) { |
| 723 | if (table == NULL) |
| 724 | return(-1); |
| 725 | return(table->nbElems); |
| 726 | } |
| 727 | |
| 728 | /** |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 729 | * xmlHashRemoveEntry: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 730 | * @table: the hash table |
| 731 | * @name: the name of the userdata |
| 732 | * @f: the deallocator function for removed item (if any) |
| 733 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 734 | * Find the userdata specified by the @name and remove |
| 735 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 736 | * and freed with @f. |
| 737 | * |
| 738 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 739 | */ |
| 740 | int xmlHashRemoveEntry(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 741 | xmlHashDeallocator f) { |
| 742 | return(xmlHashRemoveEntry3(table, name, NULL, NULL, f)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | /** |
Daniel Veillard | f69bb4b | 2001-05-19 13:24:56 +0000 | [diff] [blame] | 746 | * xmlHashRemoveEntry2: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 747 | * @table: the hash table |
| 748 | * @name: the name of the userdata |
| 749 | * @name2: a second name of the userdata |
| 750 | * @f: the deallocator function for removed item (if any) |
| 751 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 752 | * Find the userdata specified by the (@name, @name2) tuple and remove |
| 753 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 754 | * and freed with @f. |
| 755 | * |
| 756 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 757 | */ |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 758 | int |
| 759 | xmlHashRemoveEntry2(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 760 | const xmlChar *name2, xmlHashDeallocator f) { |
| 761 | return(xmlHashRemoveEntry3(table, name, name2, NULL, f)); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 765 | * xmlHashRemoveEntry3: |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 766 | * @table: the hash table |
| 767 | * @name: the name of the userdata |
| 768 | * @name2: a second name of the userdata |
| 769 | * @name3: a third name of the userdata |
| 770 | * @f: the deallocator function for removed item (if any) |
| 771 | * |
Daniel Veillard | cbaf399 | 2001-12-31 16:16:02 +0000 | [diff] [blame] | 772 | * Find the userdata specified by the (@name, @name2, @name3) tuple and remove |
| 773 | * it from the hash @table. Existing userdata for this tuple will be removed |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 774 | * and freed with @f. |
| 775 | * |
| 776 | * Returns 0 if the removal succeeded and -1 in case of error or not found. |
| 777 | */ |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 778 | int |
| 779 | xmlHashRemoveEntry3(xmlHashTablePtr table, const xmlChar *name, |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 780 | const xmlChar *name2, const xmlChar *name3, xmlHashDeallocator f) { |
| 781 | unsigned long key; |
| 782 | xmlHashEntryPtr entry; |
| 783 | xmlHashEntryPtr prev = NULL; |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 784 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 785 | if (table == NULL || name == NULL) |
| 786 | return(-1); |
Owen Taylor | 3473f88 | 2001-02-23 17:55:21 +0000 | [diff] [blame] | 787 | |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 788 | key = xmlHashComputeKey(table, name, name2, name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 789 | if (table->table[key].valid == 0) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 790 | return(-1); |
| 791 | } else { |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 792 | for (entry = &(table->table[key]); entry != NULL; entry = entry->next) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 793 | if (xmlStrEqual(entry->name, name) && |
| 794 | xmlStrEqual(entry->name2, name2) && |
| 795 | xmlStrEqual(entry->name3, name3)) { |
| 796 | if(f) |
| 797 | f(entry->payload, entry->name); |
| 798 | entry->payload = NULL; |
| 799 | if(entry->name) |
| 800 | xmlFree(entry->name); |
| 801 | if(entry->name2) |
| 802 | xmlFree(entry->name2); |
| 803 | if(entry->name3) |
| 804 | xmlFree(entry->name3); |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 805 | if(prev) { |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 806 | prev->next = entry->next; |
Daniel Veillard | fdc9156 | 2002-07-01 21:52:03 +0000 | [diff] [blame] | 807 | xmlFree(entry); |
| 808 | } else { |
| 809 | if (entry->next == NULL) { |
| 810 | entry->valid = 0; |
| 811 | } else { |
| 812 | entry = entry->next; |
| 813 | memcpy(&(table->table[key]), entry, sizeof(xmlHashEntry)); |
| 814 | xmlFree(entry); |
| 815 | } |
| 816 | } |
Daniel Veillard | a10efa8 | 2001-04-18 13:09:01 +0000 | [diff] [blame] | 817 | table->nbElems--; |
| 818 | return(0); |
| 819 | } |
| 820 | prev = entry; |
| 821 | } |
| 822 | return(-1); |
| 823 | } |
Daniel Veillard | 9e7160d | 2001-03-18 23:17:47 +0000 | [diff] [blame] | 824 | } |
| 825 | |