[1022] | 1 | /********************************************************************* |
---|
| 2 | * Copyright 1993, UCAR/Unidata |
---|
| 3 | * See netcdf/COPYRIGHT file for copying and redistribution conditions. |
---|
| 4 | * $Header: /upc/share/CVS/netcdf-3/libncdap3/nchashmap.c,v 1.4 2009/09/23 22:26:08 dmh Exp $ |
---|
| 5 | *********************************************************************/ |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | #include <string.h> |
---|
| 9 | |
---|
| 10 | #include "nchashmap.h" |
---|
| 11 | |
---|
| 12 | static ncelem ncDATANULL = (ncelem)0; |
---|
| 13 | |
---|
| 14 | #ifndef TRUE |
---|
| 15 | #define TRUE 1 |
---|
| 16 | #endif |
---|
| 17 | #ifndef FALSE |
---|
| 18 | #define FALSE 0 |
---|
| 19 | #endif |
---|
| 20 | |
---|
| 21 | #define DEFAULTALLOC 31 |
---|
| 22 | |
---|
| 23 | NChashmap* nchashnew(void) {return nchashnew0(DEFAULTALLOC);} |
---|
| 24 | |
---|
| 25 | NChashmap* nchashnew0(int alloc) |
---|
| 26 | { |
---|
| 27 | NChashmap* hm; |
---|
| 28 | hm = (NChashmap*)malloc(sizeof(NChashmap)); |
---|
| 29 | if(!hm) return NULL; |
---|
| 30 | hm->alloc = alloc; |
---|
| 31 | hm->table = (NClist**)malloc(hm->alloc*sizeof(NClist*)); |
---|
| 32 | if(!hm->table) {free(hm); return NULL;} |
---|
| 33 | memset((void*)hm->table,0,hm->alloc*sizeof(NClist*)); |
---|
| 34 | return hm; |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | int |
---|
| 38 | nchashfree(NChashmap* hm) |
---|
| 39 | { |
---|
| 40 | if(hm) { |
---|
| 41 | int i; |
---|
| 42 | for(i=0;i<hm->alloc;i++) { |
---|
| 43 | if(hm->table[i] != NULL) nclistfree(hm->table[i]); |
---|
| 44 | } |
---|
| 45 | free(hm->table); |
---|
| 46 | free(hm); |
---|
| 47 | } |
---|
| 48 | return TRUE; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /* Insert a <nchashid,ncelem> pair into the table*/ |
---|
| 52 | /* Fail if already there*/ |
---|
| 53 | int |
---|
| 54 | nchashinsert(NChashmap* hm, nchashid hash, ncelem value) |
---|
| 55 | { |
---|
| 56 | int i,offset,len; |
---|
| 57 | NClist* seq; |
---|
| 58 | ncelem* list; |
---|
| 59 | |
---|
| 60 | offset = (hash % hm->alloc); |
---|
| 61 | seq = hm->table[offset]; |
---|
| 62 | if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;} |
---|
| 63 | len = nclistlength(seq); |
---|
| 64 | list = nclistcontents(seq); |
---|
| 65 | for(i=0;i<len;i+=2,list+=2) { |
---|
| 66 | if(*list == hash) return FALSE; |
---|
| 67 | } |
---|
| 68 | nclistpush(seq,(ncelem)hash); |
---|
| 69 | nclistpush(seq,value); |
---|
| 70 | hm->size++; |
---|
| 71 | return TRUE; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /* Insert a <nchashid,ncelem> pair into the table*/ |
---|
| 75 | /* Overwrite if already there*/ |
---|
| 76 | int |
---|
| 77 | nchashreplace(NChashmap* hm, nchashid hash, ncelem value) |
---|
| 78 | { |
---|
| 79 | int i,offset,len; |
---|
| 80 | NClist* seq; |
---|
| 81 | ncelem* list; |
---|
| 82 | |
---|
| 83 | offset = (hash % hm->alloc); |
---|
| 84 | seq = hm->table[offset]; |
---|
| 85 | if(seq == NULL) {seq = nclistnew(); hm->table[offset] = seq;} |
---|
| 86 | len = nclistlength(seq); |
---|
| 87 | list = nclistcontents(seq); |
---|
| 88 | for(i=0;i<len;i+=2,list+=2) { |
---|
| 89 | if(*list == hash) {list[1] = value; return TRUE;} |
---|
| 90 | } |
---|
| 91 | nclistpush(seq,(ncelem)hash); |
---|
| 92 | nclistpush(seq,value); |
---|
| 93 | hm->size++; |
---|
| 94 | return TRUE; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | /* remove a nchashid*/ |
---|
| 98 | /* return TRUE if found, false otherwise*/ |
---|
| 99 | int |
---|
| 100 | nchashremove(NChashmap* hm, nchashid hash) |
---|
| 101 | { |
---|
| 102 | int i,offset,len; |
---|
| 103 | NClist* seq; |
---|
| 104 | ncelem* list; |
---|
| 105 | |
---|
| 106 | offset = (hash % hm->alloc); |
---|
| 107 | seq = hm->table[offset]; |
---|
| 108 | if(seq == NULL) return TRUE; |
---|
| 109 | len = nclistlength(seq); |
---|
| 110 | list = nclistcontents(seq); |
---|
| 111 | for(i=0;i<len;i+=2,list+=2) { |
---|
| 112 | if(*list == hash) { |
---|
| 113 | nclistremove(seq,i+1); |
---|
| 114 | nclistremove(seq,i); |
---|
| 115 | hm->size--; |
---|
| 116 | if(nclistlength(seq) == 0) {nclistfree(seq); hm->table[offset] = NULL;} |
---|
| 117 | return TRUE; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | return FALSE; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | /* lookup a nchashid; return DATANULL if not found*/ |
---|
| 124 | /* (use hashlookup if the possible values include 0)*/ |
---|
| 125 | ncelem |
---|
| 126 | nchashget(NChashmap* hm, nchashid hash) |
---|
| 127 | { |
---|
| 128 | ncelem value; |
---|
| 129 | if(!nchashlookup(hm,hash,&value)) return ncDATANULL; |
---|
| 130 | return value; |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | int |
---|
| 134 | nchashlookup(NChashmap* hm, nchashid hash, ncelem* valuep) |
---|
| 135 | { |
---|
| 136 | int i,offset,len; |
---|
| 137 | NClist* seq; |
---|
| 138 | ncelem* list; |
---|
| 139 | |
---|
| 140 | offset = (hash % hm->alloc); |
---|
| 141 | seq = hm->table[offset]; |
---|
| 142 | if(seq == NULL) return TRUE; |
---|
| 143 | len = nclistlength(seq); |
---|
| 144 | list = nclistcontents(seq); |
---|
| 145 | for(i=0;i<len;i+=2,list+=2) { |
---|
| 146 | if(*list == hash) {if(valuep) {*valuep = list[1]; return TRUE;}} |
---|
| 147 | } |
---|
| 148 | return FALSE; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | /* Return the ith pair; order is completely arbitrary*/ |
---|
| 152 | /* Can be expensive*/ |
---|
| 153 | int |
---|
| 154 | nchashith(NChashmap* hm, int index, nchashid* hashp, ncelem* elemp) |
---|
| 155 | { |
---|
| 156 | int i; |
---|
| 157 | if(hm == NULL) return FALSE; |
---|
| 158 | for(i=0;i<hm->alloc;i++) { |
---|
| 159 | NClist* seq = hm->table[i]; |
---|
| 160 | int len = nclistlength(seq) / 2; |
---|
| 161 | if(len == 0) continue; |
---|
| 162 | if((index - len) < 0) { |
---|
| 163 | if(hashp) *hashp = (nchashid)nclistget(seq,index*2); |
---|
| 164 | if(elemp) *elemp = nclistget(seq,(index*2)+1); |
---|
| 165 | return TRUE; |
---|
| 166 | } |
---|
| 167 | index -= len; |
---|
| 168 | } |
---|
| 169 | return FALSE; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* Return all the keys; order is completely arbitrary*/ |
---|
| 173 | /* Can be expensive*/ |
---|
| 174 | int |
---|
| 175 | nchashkeys(NChashmap* hm, nchashid** keylist) |
---|
| 176 | { |
---|
| 177 | int i,j,index; |
---|
| 178 | nchashid* keys; |
---|
| 179 | if(hm == NULL) return FALSE; |
---|
| 180 | if(hm->size == 0) { |
---|
| 181 | keys = NULL; |
---|
| 182 | } else { |
---|
| 183 | keys = (nchashid*)malloc(sizeof(nchashid)*hm->size); |
---|
| 184 | for(index=0,i=0;i<hm->alloc;i++) { |
---|
| 185 | NClist* seq = hm->table[i]; |
---|
| 186 | for(j=0;j<nclistlength(seq);j+=2) { |
---|
| 187 | keys[index++] = (nchashid)nclistget(seq,j); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | } |
---|
| 191 | if(keylist) {*keylist = keys;} |
---|
| 192 | return TRUE; |
---|
| 193 | } |
---|
| 194 | |
---|