/hashtable.c (bf0d0de0ffabb1bcb8c3e009964cd225decbb1fa) (1828 bytes) (mode 100644) (type blob)
#include <string.h>
#include <stdlib.h>
#include "hashtable.h"
static uint32_t hashfunc(const char *key)
{
// fnv
uint32_t hash = 2166136261;
while(*key)
hash = (hash ^ *key++) * 16777619;
return hash;
}
void ht_init(struct hashtable *h, size_t size)
{
h->count = 0;
h->size = size;
h->bucket = calloc(sizeof(struct hashnode), h->size);
}
void * ht_lookup(struct hashtable *h, const char *name)
{
size_t start = hashfunc(name) & (h->size -1);
size_t idx = (start + 1) & (h->size -1);
for(; idx != start; idx = (idx + 1) & (h->size -1)){
if(h->bucket[idx].name){
if( !strcmp(h->bucket[idx].name, name)){
return h->bucket[idx].val;
}
}else{
break;
}
}
return NULL;
}
static void resize(struct hashtable *h)
{
struct hashtable newht;
ht_init(&newht, h->size*2);
size_t i;
for(i = 0; i != h->size; i++){
if(h->bucket[i].name){
ht_put(&newht, h->bucket[i].name, h->bucket[i].val);
}
}
free(h->bucket);
h->bucket = newht.bucket;
h->size = newht.size;
h->count = newht.count;
}
bool ht_put(struct hashtable *h, const char *name, void *val)
{
size_t start = hashfunc(name) & (h->size-1);
size_t idx = (start + 1) & (h->size-1);
for(; /*idx != start*/; idx = (idx + 1) & (h->size-1)){
if(!h->bucket[idx].name){
h->bucket[idx].name = name;
h->bucket[idx].val = val;
break;
}else if( !strcmp(h->bucket[idx].name, name)){
return false;
}
}
h->count++;
if(h->count*3/2 > h->size){
resize(h);
}
return true;
}
void ht_clear(struct hashtable *h) {
memset(h->bucket, 0, h->size*sizeof(struct hashnode));
h->count = 0;
}
Mode |
Type |
Size |
Ref |
File |
100644 |
blob |
170 |
28f83f8deb57bbc09c713a866975355fd9225827 |
.gitignore |
100644 |
blob |
48 |
3bfb16e6c32903fb8e4db429b412ee6e8ef23a14 |
AUTHORS |
100644 |
blob |
1313 |
4dc80d23e5c4804173171f9e03724a7d9fff66a1 |
LICENSE |
100644 |
blob |
3536 |
f405558b5490a27c3844720ed7a7f3eb17974e48 |
Makefile |
100644 |
blob |
1431 |
176e50b9b24ded89c009136a2386586cacd5fcd2 |
blocks.c |
100644 |
blob |
282 |
7e063255643b647d598dadd7832458b24af8999d |
blocks.h |
100755 |
blob |
391 |
1f49d20bfca22ddee132b62a37c103610d08e211 |
check.sh |
100755 |
blob |
219 |
debf6a860da565f1e9e8d5a8c8a58daba0757ac4 |
fail.sh |
100644 |
blob |
215 |
e2d0c0f230e4387accdf5037eca5ee512c90a8a0 |
funcdecl.c |
100644 |
blob |
236 |
99ca506caaa5e457089f7391b005351fe8a0f2de |
funcdecl.h |
100644 |
blob |
22142 |
56d67f5495ac2e0127252678fa0894d8a789fa18 |
grammar.y |
100644 |
blob |
1828 |
bf0d0de0ffabb1bcb8c3e009964cd225decbb1fa |
hashtable.c |
100644 |
blob |
501 |
2185dc1cb949f6fa3a1b0985b4caaf7769456156 |
hashtable.h |
100644 |
blob |
6097 |
c05ae1913794760883aa087d2b36cba99dbafb50 |
main.c |
100644 |
blob |
23654 |
8deae69c400fa8085249d912e8a53c23c05fa6fe |
misc.c |
100644 |
blob |
4272 |
f9a7cae72786f1959627b7f0d28871737875f760 |
misc.h |
100644 |
blob |
330 |
92a4597d6df5097c58649391541d54f06b3bda1b |
paramlist.c |
100644 |
blob |
256 |
bb253362ef26a0ea881856aebeb8503fb82646fb |
paramlist.h |
100644 |
blob |
1442 |
13257c0e670ee1208e24eba34fcaf87fb55b0fae |
readme.md |
040000 |
tree |
- |
ef7f97c21b043bea53b6506697f035d6cffef30f |
tests |
100644 |
blob |
6131 |
02dd534438811fce01d2026c07e602617bc3a631 |
token.l |
100644 |
blob |
2187 |
973210c307fc9f81122e72fff412139b57461ec3 |
typeandname.c |
100644 |
blob |
914 |
c7c2ecbf5459c7cb80bd2661e6ca78c32a780e10 |
typeandname.h |
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"
Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/vrtc/pjass
Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/vrtc/pjass
Clone this repository using git:
git clone git://git.rocketgit.com/user/vrtc/pjass
You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a
merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main