Skip to content

Instantly share code, notes, and snippets.

@icarofreire
Last active August 29, 2015 14:07
Show Gist options
  • Save icarofreire/0198da67fecc3edbdc24 to your computer and use it in GitHub Desktop.
Save icarofreire/0198da67fecc3edbdc24 to your computer and use it in GitHub Desktop.
Técnica de tabela hash em C com inteiros de 32 bits.
#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max(a,b)((a<b)?(b):(a))
#define min(a,b)((b<a)?(b):(a))
#define dif(a,b)(max(a,b)-min(a,b))
#define rep(i,b) for(i=0;i<b;i++)
#define repi(i,a,b) for(i=a;i<b;i++)
unsigned hashtab[(1<<24)];
#define hash32(x) (((x)*2654435761) >> (32-24))
#define hash(v,k) hashtab[hash32(k)]=v;
#define key(k) (hashtab[hash32(k)])
int main(){
int valor = 154;
int chave_qualquer = 56;
/* \/ Guarda o valor na tabela 'hashtab' atraves de um calculo com o valor da chave. */
hash(valor,chave_qualquer);
printf("valor: %d\nchave: %d\n", valor, chave_qualquer );
/* \/ Obtem o valor que foi guardado na tabela pela chave. */
printf("valor obtido pela chave %d: %d\n", chave_qualquer, key(chave_qualquer) );
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment