#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

/*Size of the hashtable*/
#define TABLE_SIZE 250000
#define MAXSIZE 30

//Declaration de liste chainee de chaines de caracteres
typedef struct cell {
  char val[MAXSIZE];
  struct cell * suiv;
} Cellule, *Ptcellule, *Liste;


typedef struct salle{
  char* region;
  int affiliate_id;
  char* affiliate_name;
}Salle;

typedef struct athlete{
  int id_athlete;
  char* lastname;
  char* firstname;
  char gender;
  int age;
  char* weight;
  char* height;
  int overallscore;
  int overallrank;
  int score;
  char* scoredisplay;
  int rank;
  Salle* salle;
}Athlete; 

typedef Athlete* AllAthletes;

//déclaration de la table hashage 
typedef Liste Hashtable[TABLE_SIZE];

//initialisation de la table de hachage
void init_ht(Hashtable ht)
{
  for(int i=0;i<TABLE_SIZE;i++)
    {
      ht[i]=NULL;
    }
}

//Hash fonction 
int asciis(char *word)
{
  int i=0;
  int h=0;
  while(word[i]!='\0')
    {
      h=h+(word[i]-96);
      i++;
    }
  return h;
}

int hash(char *word)
{
  return (asciis(word) % TABLE_SIZE);
}

/*Update of the hashtable : add the given word in the table!*/
void update_ht(char *word, Hashtable ht)
{
  ajout_alphab(&ht[hash(word)],word);
}

/*Load the file in the internal structure ht */
void load_ht(FILE *fp, Hashtable ht)
{
  char word[MAXSIZE];
  fscanf(fp,"%s",word);

  while(!feof(fp))
    {
      ajout_alphab(&ht[hash(word)],word);
      fscanf(fp,"%s",word);
    }
}

void ajout_alphab(Liste *pl, char mot[MAXSIZE])
{
    if ( (*pl==NULL) || (strcmp(mot, (*pl)->val) < 0) ) { // empty list or mo\
t < head => new head                                                          
        ajout_tete(pl,mot);
    }
    else
    {
        if (strcmp(mot, (*pl)->val) > 0) // mot > head => add in next's       
        {
            ajout_alphab(&(*pl)->suiv,mot);
        } // else mot already in list                                         
    }
}

int main (int argc, char *argv[])
{
  if (argc < 2) { // text file is missing ?                                     
    fprintf(stderr, "usage: hash <file_name>\n");
  }
  else {
    FILE *fp;
    fp=fopen(argv[1], "r");
    if (fp==NULL) {
      fprintf(stderr, "no such file, or unreachable: %s\n", argv[1]);
    }
    
    else {

  Hashtable mylist;
  init_ht(mylist);
  load_ht(fp,mylist);
    }
    
  return 0;
  }
}
