RE: hashtable corrupting data???



>hello.
>
>I'm trying to store datas in a g_hashtable. As soon as i do the g_hash_table_insert, my data 
>seems to be corrupted. I've attached the program.
>
>ps: by the way, what would be a good hashfunction to hash 128bits keys ?
>
>thanx
>
>Antoine

Sorry, I just noticed that the attachment disapeared.....
The programm follows....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>

typedef long Position[4];

typedef struct {
  char depart;
  char arrivee;
} Coup;

typedef struct {
  Position pos_depart;
  int taille;
  Coup *coup;
} Fermeture;



void
position_set_from_array(Position *this, const char no_case[10]);
void
position_dump(Position *this);

void
coup_dump(Coup *this);


/* 
 * fermeture 
 */
Fermeture*
fermeture_new_from_array(const char no_case[10]) {
  Fermeture *this = (Fermeture*)malloc(sizeof(Fermeture));
  if (!this)
    return NULL;
  position_set_from_array(&this->pos_depart, no_case);
  this->taille = 0;
  this->coup = NULL;
  return this;
}

void
fermeture_dump(Fermeture *this) {
  int no_coup;
  position_dump(&this->pos_depart);
  printf("%d\n",this->taille);
  for (no_coup=0; no_coup < this->taille; no_coup++)
    coup_dump(&this->coup[no_coup]);
}

/* 
 * position 
 */
Position*
position_new_from_array(char no_case[10]) {
  Position *this = (Position*)malloc(sizeof(Position));
  position_set_from_array(this, no_case);
  return this;
}

void
position_set_from_array(Position *this, const char no_case[10]) {
  int i;
  memset((void *)this,0, sizeof(Position));
  for (i=0; i<10; i++) {
    int idx = no_case[i] / 32;
    int no_bit = no_case[i] - 32 * idx;
    *this[idx] = (*this[idx]) | (1<<no_bit); 
  }
}

void
position_dump(Position *this) {
  int i, j;
  for (i=0; i<4; i++)
    for (j=0; j<32; j++)
      if ((*this[i]) & (1<<j))
        printf("%d ", i * 32 + j);
  printf("\n");
}


gint        
pos_compare (gconstpointer a, gconstpointer b) {
  printf("in pos_compare\n");
  return !memcmp(a, b, sizeof(Position));
}

guint
pos_hash (gconstpointer a) {
  guint hash = 1;
  printf("in pos_hash\n"); 
/*   hash = g_int_hash(a); */
  return hash;
}

/*
 * coup
 */
void
coup_dump(Coup *this) {
  printf("%d -> %d\n",this->depart, this->arrivee); 
}

int 
main(int argc, char **argv) {  
  GHashTable *table;
  Fermeture *f;
  Position *p;
  char pos_h0_[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; 
  char pos_0_[] =  {0, 1, 2, 3, 4, 5, 6, 7, 8, 10}; 
  char pos_h3_[] = {111, 112, 113, 114, 115, 116, 117, 118, 119, 120}; 
  
  table = g_hash_table_new((GHashFunc)pos_hash, (GCompareFunc)pos_compare);
  f = fermeture_new_from_array(pos_h0_);
  printf("fermeture created\n");
  fermeture_dump(f);
  printf("\n");

/*   g_hash_table_insert(table, &f->pos_depart, f);  */
  printf("fermeture inserted\n");
  fermeture_dump(f);
  printf("\n");

  p = position_new_from_array(pos_h0_);
  f = g_hash_table_lookup(table, p);
  printf("fermeture looked up\n");
  ( f ? fermeture_dump(f) : printf("not found\n"));
  printf("\n");
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]