Path: blob/devel/elmergrid/src/metis-5.1.0/GKlib/htable.c
3206 views
/*1* Copyright 2004, Regents of the University of Minnesota2*3* This file contains routines for manipulating a direct-access hash table4*5* Started 3/22/046* George7*8*/910#include <GKlib.h>1112/******************************************************************************13* This function creates the hash-table14*******************************************************************************/15gk_HTable_t *HTable_Create(int nelements)16{17gk_HTable_t *htable;1819htable = gk_malloc(sizeof(gk_HTable_t), "HTable_Create: htable");20htable->harray = gk_ikvmalloc(nelements, "HTable_Create: harray");21htable->nelements = nelements;2223HTable_Reset(htable);2425return htable;26}272829/******************************************************************************30* This function resets the data-structures associated with the hash-table31*******************************************************************************/32void HTable_Reset(gk_HTable_t *htable)33{34int i;3536for (i=0; i<htable->nelements; i++)37htable->harray[i].key = HTABLE_EMPTY;38htable->htsize = 0;3940}4142/******************************************************************************43* This function resizes the hash-table44*******************************************************************************/45void HTable_Resize(gk_HTable_t *htable, int nelements)46{47int i, old_nelements;48gk_ikv_t *old_harray;4950old_nelements = htable->nelements;51old_harray = htable->harray;5253/* prepare larger hash */54htable->nelements = nelements;55htable->htsize = 0;56htable->harray = gk_ikvmalloc(nelements, "HTable_Resize: harray");57for (i=0; i<nelements; i++)58htable->harray[i].key = HTABLE_EMPTY;5960/* reassign the values */61for (i=0; i<old_nelements; i++)62if (old_harray[i].key != HTABLE_EMPTY)63HTable_Insert(htable, old_harray[i].key, old_harray[i].val);6465/* remove old harray */66gk_free((void **)&old_harray, LTERM);67}686970/******************************************************************************71* This function inserts a key-value pair in the array72*******************************************************************************/73void HTable_Insert(gk_HTable_t *htable, int key, int val)74{75int i, first;7677if (htable->htsize > htable->nelements/2)78HTable_Resize(htable, 2*htable->nelements);7980first = HTable_HFunction(htable->nelements, key);8182for (i=first; i<htable->nelements; i++) {83if (htable->harray[i].key == HTABLE_EMPTY || htable->harray[i].key == HTABLE_DELETED) {84htable->harray[i].key = key;85htable->harray[i].val = val;86htable->htsize++;87return;88}89}9091for (i=0; i<first; i++) {92if (htable->harray[i].key == HTABLE_EMPTY || htable->harray[i].key == HTABLE_DELETED) {93htable->harray[i].key = key;94htable->harray[i].val = val;95htable->htsize++;96return;97}98}99100}101102103/******************************************************************************104* This function deletes key from the htable105*******************************************************************************/106void HTable_Delete(gk_HTable_t *htable, int key)107{108int i, first;109110first = HTable_HFunction(htable->nelements, key);111112for (i=first; i<htable->nelements; i++) {113if (htable->harray[i].key == key) {114htable->harray[i].key = HTABLE_DELETED;115htable->htsize--;116return;117}118}119120for (i=0; i<first; i++) {121if (htable->harray[i].key == key) {122htable->harray[i].key = HTABLE_DELETED;123htable->htsize--;124return;125}126}127128}129130131/******************************************************************************132* This function returns the data associated with the key in the hastable133*******************************************************************************/134int HTable_Search(gk_HTable_t *htable, int key)135{136int i, first;137138first = HTable_HFunction(htable->nelements, key);139140for (i=first; i<htable->nelements; i++) {141if (htable->harray[i].key == key)142return htable->harray[i].val;143else if (htable->harray[i].key == HTABLE_EMPTY)144return -1;145}146147for (i=0; i<first; i++) {148if (htable->harray[i].key == key)149return htable->harray[i].val;150else if (htable->harray[i].key == HTABLE_EMPTY)151return -1;152}153154return -1;155}156157158/******************************************************************************159* This function returns the next key/val160*******************************************************************************/161int HTable_GetNext(gk_HTable_t *htable, int key, int *r_val, int type)162{163int i;164static int first, last;165166if (type == HTABLE_FIRST)167first = last = HTable_HFunction(htable->nelements, key);168169if (first > last) {170for (i=first; i<htable->nelements; i++) {171if (htable->harray[i].key == key) {172*r_val = htable->harray[i].val;173first = i+1;174return 1;175}176else if (htable->harray[i].key == HTABLE_EMPTY)177return -1;178}179first = 0;180}181182for (i=first; i<last; i++) {183if (htable->harray[i].key == key) {184*r_val = htable->harray[i].val;185first = i+1;186return 1;187}188else if (htable->harray[i].key == HTABLE_EMPTY)189return -1;190}191192return -1;193}194195196/******************************************************************************197* This function returns the data associated with the key in the hastable198*******************************************************************************/199int HTable_SearchAndDelete(gk_HTable_t *htable, int key)200{201int i, first;202203first = HTable_HFunction(htable->nelements, key);204205for (i=first; i<htable->nelements; i++) {206if (htable->harray[i].key == key) {207htable->harray[i].key = HTABLE_DELETED;208htable->htsize--;209return htable->harray[i].val;210}211else if (htable->harray[i].key == HTABLE_EMPTY)212gk_errexit(SIGERR, "HTable_SearchAndDelete: Failed to find the key!\n");213}214215for (i=0; i<first; i++) {216if (htable->harray[i].key == key) {217htable->harray[i].key = HTABLE_DELETED;218htable->htsize--;219return htable->harray[i].val;220}221else if (htable->harray[i].key == HTABLE_EMPTY)222gk_errexit(SIGERR, "HTable_SearchAndDelete: Failed to find the key!\n");223}224225return -1;226227}228229230231/******************************************************************************232* This function destroys the data structures associated with the hash-table233*******************************************************************************/234void HTable_Destroy(gk_HTable_t *htable)235{236gk_free((void **)&htable->harray, &htable, LTERM);237}238239240/******************************************************************************241* This is the hash-function. Based on multiplication242*******************************************************************************/243int HTable_HFunction(int nelements, int key)244{245return (int)(key%nelements);246}247248249