|
// Written by retoor@molodetz.nl
|
|
|
|
// This source code provides the implementation of a simple hash table using separate chaining for collision resolution. It contains
|
|
// functions to hash, retrieve, and insert entries into the hash table.
|
|
|
|
// Imported library: The code uses the non-standard library "rmalloc.h".
|
|
|
|
/* MIT License
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
The above copyright notice and this permission notice shall be included in all
|
|
copies or substantial portions of the Software.
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
SOFTWARE.
|
|
*/
|
|
|
|
#ifndef RHASHTABLE_H
|
|
#define RHASHTABLE_H
|
|
|
|
#include "rmalloc.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define HASHSIZE 101
|
|
|
|
typedef struct rnlist {
|
|
struct rnlist *next;
|
|
char *name;
|
|
char *defn;
|
|
} rnlist;
|
|
|
|
static rnlist *rhashtab[HASHSIZE];
|
|
|
|
unsigned rhash(char *s) {
|
|
unsigned hashval = 0;
|
|
while (*s != '\0') {
|
|
hashval = *s + 31 * hashval;
|
|
s++;
|
|
}
|
|
return hashval % HASHSIZE;
|
|
}
|
|
|
|
rnlist *rlget(char *s) {
|
|
rnlist *np;
|
|
for (np = rhashtab[rhash(s)]; np != NULL; np = np->next) {
|
|
if (strcmp(s, np->name) == 0)
|
|
return np;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
char *rget(char *s) {
|
|
rnlist *np = rlget(s);
|
|
return np ? np->defn : NULL;
|
|
}
|
|
|
|
struct rnlist *rset(char *name, char *defn) {
|
|
struct rnlist *np = NULL;
|
|
unsigned hashval;
|
|
|
|
if ((rlget(name)) == NULL) {
|
|
np = (struct rnlist *)malloc(sizeof(*np));
|
|
if (np == NULL || (np->name = strdup(name)) == NULL)
|
|
return NULL;
|
|
hashval = rhash(name);
|
|
np->next = rhashtab[hashval];
|
|
rhashtab[hashval] = np;
|
|
} else {
|
|
if (np->defn) {
|
|
free(np->defn);
|
|
}
|
|
np->defn = NULL;
|
|
}
|
|
if ((np->defn = strdup(defn)) == NULL)
|
|
return NULL;
|
|
return np;
|
|
}
|
|
#endif |