hash.c revision d46ee884c41b808b239563b1978468aae12e33a2
/*
* hash.c: chained hash tables
*
* Reference: Your favorite introductory book on algorithms
*
* Copyright (C) 2000 Bjorn Reese and Daniel Veillard.
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
* MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS AND
* CONTRIBUTORS ACCEPT NO RESPONSIBILITY IN ANY CONCEIVABLE MANNER.
*
* Author: breese@users.sourceforge.net
*/
#define IN_LIBXML
#include "libxml.h"
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlerror.h>
#define MAX_HASH_LEN 8
/* #define DEBUG_GROW */
/*
* A single entry in the hash table
*/
typedef struct _xmlHashEntry xmlHashEntry;
typedef xmlHashEntry *xmlHashEntryPtr;
struct _xmlHashEntry {
struct _xmlHashEntry *next;
void *payload;
int valid;
};
/*
* The entire hash table
*/
struct _xmlHashTable {
struct _xmlHashEntry *table;
int size;
int nbElems;
};
/*
* xmlHashComputeKey:
* Calculate the hash key
*/
static unsigned long
unsigned long value = 0L;
char ch;
}
}
}
}
}
}
}
static unsigned long
unsigned long value = 0L;
char ch;
else
}
}
}
}
}
}
}
}
}
}
}
}
}
/**
* xmlHashCreate:
* @size: the size of the hash table
*
* Create a new xmlHashTablePtr.
*
* Returns the newly created object, or NULL if an error occured.
*/
xmlHashCreate(int size) {
if (size <= 0)
size = 256;
if (table) {
return(table);
}
}
return(NULL);
}
/**
* xmlHashCreateDict:
* @size: the size of the hash table
* @dict: a dictionary to use for the hash
*
* Create a new xmlHashTablePtr which will use @dict as the internal dictionary
*
* Returns the newly created object, or NULL if an error occured.
*/
}
return(table);
}
/**
* xmlHashGrow:
* @table: the hash table
* @size: the new size of the hash table
*
* resize the hash table
*
* Returns 0 in case of success, -1 in case of failure
*/
static int
unsigned long key;
int oldsize, i;
struct _xmlHashEntry *oldtable;
#ifdef DEBUG_GROW
unsigned long nbElem = 0;
#endif
return(-1);
if (size < 8)
return(-1);
return(-1);
return(-1);
return(-1);
}
/* If the two loops are merged, there would be situations where
a new entry needs to allocated and data copied into it from
the main table. So instead, we run through the array twice, first
copying all the elements in the main array (where we can't get
conflicts) and then the rest, so we only free (and don't allocate)
*/
for (i = 0; i < oldsize; i++) {
continue;
}
for (i = 0; i < oldsize; i++) {
while (iter) {
/*
* put back the entry in the new table
*/
} else {
}
#ifdef DEBUG_GROW
nbElem++;
#endif
}
}
#ifdef DEBUG_GROW
#endif
return(0);
}
/**
* xmlHashFree:
* @table: the hash table
* @f: the deallocator function for items in the hash
*
* Free the hash @table and its contents. The userdata is
* deallocated with @f if provided.
*/
void
int i;
int inside_table = 0;
int nbElems;
return;
continue;
inside_table = 1;
while (iter) {
}
if (!inside_table)
nbElems--;
inside_table = 0;
}
inside_table = 0;
}
}
}
/**
* xmlHashAddEntry:
* @table: the hash table
* @name: the name of the userdata
* @userdata: a pointer to the userdata
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the @name. Duplicate names generate errors.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
}
/**
* xmlHashAddEntry2:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @userdata: a pointer to the userdata
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the (@name, @name2) tuple. Duplicate tuples generate errors.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
}
/**
* xmlHashUpdateEntry:
* @table: the hash table
* @name: the name of the userdata
* @userdata: a pointer to the userdata
* @f: the deallocator function for replaced item (if any)
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the @name. Existing entry for this @name will be removed
* and freed with @f if found.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
void *userdata, xmlHashDeallocator f) {
}
/**
* xmlHashUpdateEntry2:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @userdata: a pointer to the userdata
* @f: the deallocator function for replaced item (if any)
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the (@name, @name2) tuple. Existing entry for this tuple will
* be removed and freed with @f if found.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
xmlHashDeallocator f) {
}
/**
* xmlHashLookup:
* @table: the hash table
* @name: the name of the userdata
*
* Find the userdata specified by the @name.
*
* Returns the pointer to the userdata
*/
void *
}
/**
* xmlHashLookup2:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
*
* Find the userdata specified by the (@name, @name2) tuple.
*
* Returns the pointer to the userdata
*/
void *
}
/**
* xmlHashQLookup:
* @table: the hash table
* @prefix: the prefix of the userdata
* @name: the name of the userdata
*
* Find the userdata specified by the QName @prefix:@name/@name.
*
* Returns the pointer to the userdata
*/
void *
}
/**
* xmlHashQLookup2:
* @table: the hash table
* @prefix: the prefix of the userdata
* @name: the name of the userdata
* @prefix2: the second prefix of the userdata
* @name2: a second name of the userdata
*
* Find the userdata specified by the QNames tuple
*
* Returns the pointer to the userdata
*/
void *
}
/**
* xmlHashAddEntry3:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @name3: a third name of the userdata
* @userdata: a pointer to the userdata
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the tuple (@name, @name2, @name3). Duplicate entries generate
* errors.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
void *userdata) {
return(-1);
/*
* If using a dict internalize if needed
*/
return(-1);
}
return(-1);
}
return(-1);
}
}
/*
* Check for duplicate and insertion location.
*/
} else {
return(-1);
len++;
}
return(-1);
} else {
return(-1);
len++;
}
return(-1);
}
}
} else {
return(-1);
}
} else {
}
if (len > MAX_HASH_LEN)
return(0);
}
/**
* xmlHashUpdateEntry3:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @name3: a third name of the userdata
* @userdata: a pointer to the userdata
* @f: the deallocator function for replaced item (if any)
*
* Add the @userdata to the hash @table. This can later be retrieved
* by using the tuple (@name, @name2, @name3). Existing entry for this tuple
* will be removed and freed with @f if found.
*
* Returns 0 the addition succeeded and -1 in case of error.
*/
int
void *userdata, xmlHashDeallocator f) {
unsigned long key;
return(-1);
/*
* If using a dict internalize if needed
*/
return(-1);
}
return(-1);
}
return(-1);
}
}
/*
* Check for duplicate and insertion location.
*/
} else {
if (f)
return(0);
}
}
if (f)
return(0);
}
} else {
if (f)
return(0);
}
}
if (f)
return(0);
}
}
}
} else {
return(-1);
}
} else {
}
}
return(0);
}
/**
* xmlHashLookup3:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @name3: a third name of the userdata
*
* Find the userdata specified by the (@name, @name2, @name3) tuple.
*
* Returns the a pointer to the userdata
*/
void *
unsigned long key;
return(NULL);
return(NULL);
return(NULL);
}
}
}
return(NULL);
}
/**
* xmlHashQLookup3:
* @table: the hash table
* @prefix: the prefix of the userdata
* @name: the name of the userdata
* @prefix2: the second prefix of the userdata
* @name2: a second name of the userdata
* @prefix3: the third prefix of the userdata
* @name3: a third name of the userdata
*
* Find the userdata specified by the (@name, @name2, @name3) tuple.
*
* Returns the a pointer to the userdata
*/
void *
unsigned long key;
return(NULL);
return(NULL);
return(NULL);
}
return(NULL);
}
typedef struct {
void *data;
} stubData;
static void
}
/**
* xmlHashScan:
* @table: the hash table
* @f: the scanner function for items in the hash
* @data: extra data passed to f
*
* Scan the hash @table and applied @f to each value.
*/
void
stubdata.hashscanner = f;
}
/**
* xmlHashScanFull:
* @table: the hash table
* @f: the scanner function for items in the hash
* @data: extra data passed to f
*
* Scan the hash @table and applied @f to each value.
*/
void
int i;
return;
if (f == NULL)
return;
continue;
while (iter) {
}
}
}
}
/**
* xmlHashScan3:
* @table: the hash table
* @name: the name of the userdata or NULL
* @name2: a second name of the userdata or NULL
* @name3: a third name of the userdata or NULL
* @f: the scanner function for items in the hash
* @data: extra data passed to f
*
* Scan the hash @table and applied @f to each value matching
* (@name, @name2, @name3) tuple. If one of the names is null,
* the comparison is considered to match.
*/
void
xmlHashScanner f, void *data) {
(xmlHashScannerFull) f, data);
}
/**
* xmlHashScanFull3:
* @table: the hash table
* @name: the name of the userdata or NULL
* @name2: a second name of the userdata or NULL
* @name3: a third name of the userdata or NULL
* @f: the scanner function for items in the hash
* @data: extra data passed to f
*
* Scan the hash @table and applied @f to each value matching
* (@name, @name2, @name3) tuple. If one of the names is null,
* the comparison is considered to match.
*/
void
xmlHashScannerFull f, void *data) {
int i;
return;
if (f == NULL)
return;
continue;
while (iter) {
}
}
}
}
}
/**
* xmlHashCopy:
* @table: the hash table
* @f: the copier function for items in the hash
*
* Scan the hash @table and applied @f to each value.
*
* Returns the new table or NULL in case of error.
*/
int i;
return(NULL);
if (f == NULL)
return(NULL);
continue;
while (iter) {
}
}
}
return(ret);
}
/**
* xmlHashSize:
* @table: the hash table
*
* Query the number of elements installed in the hash @table.
*
* Returns the number of elements in the hash table or
* -1 in case of error
*/
int
return(-1);
}
/**
* xmlHashRemoveEntry:
* @table: the hash table
* @name: the name of the userdata
* @f: the deallocator function for removed item (if any)
*
* Find the userdata specified by the @name and remove
* it from the hash @table. Existing userdata for this tuple will be removed
* and freed with @f.
*
* Returns 0 if the removal succeeded and -1 in case of error or not found.
*/
xmlHashDeallocator f) {
}
/**
* xmlHashRemoveEntry2:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @f: the deallocator function for removed item (if any)
*
* Find the userdata specified by the (@name, @name2) tuple and remove
* it from the hash @table. Existing userdata for this tuple will be removed
* and freed with @f.
*
* Returns 0 if the removal succeeded and -1 in case of error or not found.
*/
int
}
/**
* xmlHashRemoveEntry3:
* @table: the hash table
* @name: the name of the userdata
* @name2: a second name of the userdata
* @name3: a third name of the userdata
* @f: the deallocator function for removed item (if any)
*
* Find the userdata specified by the (@name, @name2, @name3) tuple and remove
* it from the hash @table. Existing userdata for this tuple will be removed
* and freed with @f.
*
* Returns 0 if the removal succeeded and -1 in case of error or not found.
*/
int
unsigned long key;
return(-1);
return(-1);
} else {
}
if(prev) {
} else {
} else {
}
}
return(0);
}
}
return(-1);
}
}
#define bottom_hash
#include "elfgcchack.h"