2010-06-03 21:24:43 +08:00
|
|
|
/*********************************************************************
|
2012-08-20 05:12:35 +08:00
|
|
|
* Copyright 2010, UCAR/Unidata
|
2010-06-03 21:24:43 +08:00
|
|
|
* See netcdf/COPYRIGHT file for copying and redistribution conditions.
|
2012-08-20 05:12:35 +08:00
|
|
|
* $Header$
|
2010-06-03 21:24:43 +08:00
|
|
|
*********************************************************************/
|
2012-08-20 05:12:35 +08:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "nchashmap.h"
|
|
|
|
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE 1
|
|
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define DEFAULTALLOC 31
|
|
|
|
|
|
|
|
NChashmap* nchashnew(void) {return nchashnew0(DEFAULTALLOC);}
|
|
|
|
|
2014-03-11 02:09:36 +08:00
|
|
|
NChashmap* nchashnew0(size_t alloc)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
|
|
|
NChashmap* hm;
|
2012-08-20 05:12:35 +08:00
|
|
|
if(sizeof(nchashid) != sizeof(void*)){
|
|
|
|
fprintf(stderr,"nchashmap: sizeof(nchashid) != sizeof(void*)");
|
|
|
|
abort();
|
|
|
|
}
|
2010-06-03 21:24:43 +08:00
|
|
|
hm = (NChashmap*)malloc(sizeof(NChashmap));
|
|
|
|
if(!hm) return NULL;
|
|
|
|
hm->alloc = alloc;
|
|
|
|
hm->table = (NClist**)malloc(hm->alloc*sizeof(NClist*));
|
|
|
|
if(!hm->table) {free(hm); return NULL;}
|
|
|
|
memset((void*)hm->table,0,hm->alloc*sizeof(NClist*));
|
|
|
|
return hm;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
nchashfree(NChashmap* hm)
|
|
|
|
{
|
|
|
|
if(hm) {
|
|
|
|
int i;
|
|
|
|
for(i=0;i<hm->alloc;i++) {
|
|
|
|
if(hm->table[i] != NULL) nclistfree(hm->table[i]);
|
|
|
|
}
|
|
|
|
free(hm->table);
|
|
|
|
free(hm);
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-20 05:12:35 +08:00
|
|
|
/* Insert a <nchashid,void*> pair into the table*/
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Fail if already there*/
|
|
|
|
int
|
2012-08-20 05:12:35 +08:00
|
|
|
nchashinsert(NChashmap* hm, nchashid hash, void* value)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-03-11 02:09:36 +08:00
|
|
|
int i;
|
|
|
|
size_t offset,len;
|
2010-06-03 21:24:43 +08:00
|
|
|
NClist* seq;
|
2012-08-20 05:12:35 +08:00
|
|
|
void** list;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
offset = (hash % hm->alloc);
|
|
|
|
seq = hm->table[offset];
|
|
|
|
if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;}
|
|
|
|
len = nclistlength(seq);
|
|
|
|
list = nclistcontents(seq);
|
|
|
|
for(i=0;i<len;i+=2,list+=2) {
|
2012-08-20 05:12:35 +08:00
|
|
|
if(hash==(nchashid)(*list)) return FALSE;
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2012-08-20 05:12:35 +08:00
|
|
|
nclistpush(seq,(void*)hash);
|
2010-06-03 21:24:43 +08:00
|
|
|
nclistpush(seq,value);
|
|
|
|
hm->size++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2012-08-20 05:12:35 +08:00
|
|
|
/* Insert a <nchashid,void*> pair into the table*/
|
2010-06-03 21:24:43 +08:00
|
|
|
/* Overwrite if already there*/
|
|
|
|
int
|
2012-08-20 05:12:35 +08:00
|
|
|
nchashreplace(NChashmap* hm, nchashid hash, void* value)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-03-11 02:09:36 +08:00
|
|
|
int i;
|
|
|
|
size_t offset,len;
|
2010-06-03 21:24:43 +08:00
|
|
|
NClist* seq;
|
2012-08-20 05:12:35 +08:00
|
|
|
void** list;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
offset = (hash % hm->alloc);
|
|
|
|
seq = hm->table[offset];
|
|
|
|
if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;}
|
|
|
|
len = nclistlength(seq);
|
|
|
|
list = nclistcontents(seq);
|
|
|
|
for(i=0;i<len;i+=2,list+=2) {
|
2012-08-20 05:12:35 +08:00
|
|
|
if(hash==(nchashid)(*list)) {list[1] = value; return TRUE;}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
2012-08-20 05:12:35 +08:00
|
|
|
nclistpush(seq,(void*)hash);
|
2010-06-03 21:24:43 +08:00
|
|
|
nclistpush(seq,value);
|
|
|
|
hm->size++;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* remove a nchashid*/
|
|
|
|
/* return TRUE if found, false otherwise*/
|
|
|
|
int
|
|
|
|
nchashremove(NChashmap* hm, nchashid hash)
|
|
|
|
{
|
2014-03-11 02:09:36 +08:00
|
|
|
size_t i;
|
|
|
|
size_t offset,len;
|
2010-06-03 21:24:43 +08:00
|
|
|
NClist* seq;
|
2012-08-20 05:12:35 +08:00
|
|
|
void** list;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
offset = (hash % hm->alloc);
|
|
|
|
seq = hm->table[offset];
|
|
|
|
if(seq == NULL) return TRUE;
|
|
|
|
len = nclistlength(seq);
|
|
|
|
list = nclistcontents(seq);
|
|
|
|
for(i=0;i<len;i+=2,list+=2) {
|
2012-08-20 05:12:35 +08:00
|
|
|
if(hash==(nchashid)(*list)) {
|
2014-03-11 02:09:36 +08:00
|
|
|
nclistremove(seq,(i+1));
|
2010-06-03 21:24:43 +08:00
|
|
|
nclistremove(seq,i);
|
|
|
|
hm->size--;
|
|
|
|
if(nclistlength(seq) == 0) {nclistfree(seq); hm->table[offset] = NULL;}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* lookup a nchashid; return DATANULL if not found*/
|
|
|
|
/* (use hashlookup if the possible values include 0)*/
|
2012-08-20 05:12:35 +08:00
|
|
|
void*
|
2010-06-03 21:24:43 +08:00
|
|
|
nchashget(NChashmap* hm, nchashid hash)
|
|
|
|
{
|
2012-08-20 05:12:35 +08:00
|
|
|
void* value;
|
|
|
|
if(!nchashlookup(hm,hash,&value)) return NULL;
|
2010-06-03 21:24:43 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2012-08-20 05:12:35 +08:00
|
|
|
nchashlookup(NChashmap* hm, nchashid hash, void** valuep)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-03-11 02:09:36 +08:00
|
|
|
int i;
|
|
|
|
size_t offset,len;
|
2010-06-03 21:24:43 +08:00
|
|
|
NClist* seq;
|
2012-08-20 05:12:35 +08:00
|
|
|
void** list;
|
2010-06-03 21:24:43 +08:00
|
|
|
|
|
|
|
offset = (hash % hm->alloc);
|
|
|
|
seq = hm->table[offset];
|
|
|
|
if(seq == NULL) return TRUE;
|
|
|
|
len = nclistlength(seq);
|
|
|
|
list = nclistcontents(seq);
|
|
|
|
for(i=0;i<len;i+=2,list+=2) {
|
2012-08-20 05:12:35 +08:00
|
|
|
if(hash==(nchashid)(*list)) {if(valuep) {*valuep = list[1]; return TRUE;}}
|
2010-06-03 21:24:43 +08:00
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the ith pair; order is completely arbitrary*/
|
|
|
|
/* Can be expensive*/
|
|
|
|
int
|
2014-03-11 00:01:44 +08:00
|
|
|
nchashith(NChashmap* hm, int index, nchashid* hashp, void** elemp)
|
2010-06-03 21:24:43 +08:00
|
|
|
{
|
2014-03-11 00:01:44 +08:00
|
|
|
int i;
|
2010-06-03 21:24:43 +08:00
|
|
|
if(hm == NULL) return FALSE;
|
|
|
|
for(i=0;i<hm->alloc;i++) {
|
|
|
|
NClist* seq = hm->table[i];
|
2014-03-11 00:01:44 +08:00
|
|
|
int len = nclistlength(seq) / 2;
|
2010-06-03 21:24:43 +08:00
|
|
|
if(len == 0) continue;
|
|
|
|
if((index - len) < 0) {
|
|
|
|
if(hashp) *hashp = (nchashid)nclistget(seq,index*2);
|
|
|
|
if(elemp) *elemp = nclistget(seq,(index*2)+1);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
index -= len;
|
|
|
|
}
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return all the keys; order is completely arbitrary*/
|
|
|
|
/* Can be expensive*/
|
|
|
|
int
|
|
|
|
nchashkeys(NChashmap* hm, nchashid** keylist)
|
|
|
|
{
|
2014-03-11 00:01:44 +08:00
|
|
|
int i,j,index;
|
2010-06-03 21:24:43 +08:00
|
|
|
nchashid* keys;
|
|
|
|
if(hm == NULL) return FALSE;
|
|
|
|
if(hm->size == 0) {
|
|
|
|
keys = NULL;
|
|
|
|
} else {
|
|
|
|
keys = (nchashid*)malloc(sizeof(nchashid)*hm->size);
|
|
|
|
for(index=0,i=0;i<hm->alloc;i++) {
|
|
|
|
NClist* seq = hm->table[i];
|
|
|
|
for(j=0;j<nclistlength(seq);j+=2) {
|
|
|
|
keys[index++] = (nchashid)nclistget(seq,j);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(keylist) {*keylist = keys;}
|
2013-08-06 04:36:33 +08:00
|
|
|
else {free(keys);}
|
|
|
|
|
2010-06-03 21:24:43 +08:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|