/***************************************
*                                      *
*   Copyright (c) 1998 Jean-Eric Pin   *
*   All rights reserved.               *
*                                      *
*   TAB = 2 spaces                     *
*                                      *
***************************************/

/*-------------------------------------------------------------------
 * Espace.c    Jean-Eric Pin 07/12/96
 *-------------------------------------------------------------------
 */     

/************************************************************************
 *
 * Estimation de la taille de la table de hachage. On commence par
 * evaluer la taille maximale de la table, compte tenu de la memoire 
 * disponible. On recherche ensuite le plus grand nombre premier
 * inferieur a cette valeur. Pour cela, on prend un crible naif,
 * car les valeurs considerees sont inferieures a 10^6.
 *
 ***********************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <math.h>
#include "Globales.h"
#include "Main.h"
#include "Utilitaires.h"
#include "Espace.h"

extern long contigu;
extern unsigned short NbEtats, NbLettres, TypeSemigroupe;
extern unsigned long TailleElement;
extern char **Messages; 

/****************************************************
*
* EstimationTailleTableDeHachage. OK
*
****************************************************/

unsigned long EstimationTailleTableDeHachage(unsigned long N)  /* retourne un nombre premier > 100 voisin de 10T/9 */
{
  unsigned long n;
  short i, RacineCarree, Premier = 0;

  n = (unsigned long)N * 5;
  if ((n % 2) == 0)
    n++;
  RacineCarree = (short)ceil(sqrt(n));
  while (!Premier)
  {
    n += 2;
    i = 3;
    while (((n % i)!= 0) && (i < RacineCarree))
      i += 2;
    Premier = (i >= RacineCarree);
  }
  n = Max(n, 101);
  return (n);
}

/****************************************************
*
* EstimationTailleMaxi. OK
*
****************************************************/

unsigned long EstimationTailleMaxi(void)
{
  long BorneSup;

#if  SYSTEME == MAC
  short TailleEntreeTable = sizeof(info) + sizeof(ProduitsDG) * NbLettres;
  BorneSup = (contigu - 50000L) / (5 * sizeof(unsigned long) + (TailleEntreeTable + TailleElement + 4));
  printf("%s (< %ld) : ", Messages[M_Upper_bound], BorneSup);
#else
  printf("%s : ", Messages[M_Upper_bound]);  /* Donner une borne superieure */
#endif

  if (scanf("%ld", &BorneSup) != 1)
  {
    printf("scanf error\n");
    exit(1);
  }
  printf("\n");
  return ((unsigned long)(BorneSup));
}