/* WIDE AREA INFORMATION SERVER SOFTWARE:
   No guarantees or restrictions.  See the readme file for the full standard
   disclaimer.
*/

/* Hash Table routines
 * for strings based on Common Lisp interface functions.
 *  -brewster
 */

/* This hashtable will never grow, rather it just adds entries to the buckets.
 * 
 * The contents are all in one contiguous block so that the contents can be 
 * sorted.  This will cause problems because of the reallocs needed.
 *
 */

/* Change log:
 * $Log:	dochash.c,v $
 * Revision 1.4  92/08/12  09:34:53  autumn
 * cleaned up and documented
 * 
 * Revision 1.3  92/07/14  09:40:39  autumn
 * Working version
 * 
 * Revision 1.2  92/07/02  16:08:35  autumn
 * *** empty log message ***
 * 
 * Revision 1.1  92/07/01  18:51:32  autumn
 * Initial revision
 * 
 * Revision 1.9  92/05/06  17:27:38  jonathan
 * Added cast for htable->contents malloc.
 * 
 * Revision 1.8  92/02/21  11:06:53  jonathan
 * Added header, RCSIdent.
 * 
 * Revision 1.7  92/02/16  12:37:57  jonathan
 * Changed bzero's to memset's.
 * 
 * Revision 1.6  92/02/12  13:20:08  jonathan
 * Added "$Log" so RCS will put the log message in the header
 * 
*/

#ifndef lint
static char *RCSid = "$Header: /afs/net.mit.edu/project/z3950/autumn/techinfo/search_engine/ir/RCS/dochash.c,v 1.4 92/08/12 09:34:53 autumn Exp Locker: autumn $";
#endif

#include "cdialect.h"
#include "cutil.h"
#include "dochash.h"
#include <string.h>
#include "panic.h"

#define ALIGNMENT_SIZE 4 /* the word size to align elements of the contents on */


doc_hashtable*
make_doc_hashtable (number_of_buckets, number_of_elements, element_size)
     long number_of_buckets;
     long number_of_elements;
     long element_size;
{
  doc_hashtable *htable = (doc_hashtable *)s_malloc(sizeof(doc_hashtable));

  if(number_of_buckets > 0)
    htable->number_of_buckets = number_of_buckets;
  else
    htable->number_of_buckets = DEFAULT_NUMBER_OF_BUCKETS;

  htable->buckets_mask = htable->number_of_buckets - 1;

  if(element_size > 0){
    htable->element_size = element_size;
  }
  else
    return(NULL);
  
  htable->number_of_elements = 0;

  if(number_of_elements > 0)
    htable->max_number_of_elements = number_of_elements;
  else
    htable->max_number_of_elements = DEFAULT_NUMBER_OF_ELEMENTS;

  htable->buckets
    = (long*)s_malloc(sizeof(long) * htable->number_of_buckets);
  /* this depends on -1L to be made up of -1 characters */
  memset(htable->buckets, -1, sizeof(long) * htable->number_of_buckets);

  htable->contents 
    = (doc_hash_entry*)s_malloc(htable->max_number_of_elements * 
			    htable->element_size);
  return(htable);
}


/*----------------------------------------------------------------- */

/* returns a doc_hashcode for a string.  
 * key is a string that is assumed to be downcased (if appropriate),
 * function is a number from 0 to 20 that are fairly independent doc_hash 
 *    functions.
 */


/*===============================*
 *===  Doc_Hashing Functions  ===*
 *===============================*/


/*___________________________________________________________________________*/

/* Date: Fri, 13 Dec 91 16:28:53 EST
   From: Henry Massalin <henry@cs.columbia.edu> */

#define reg	register

/*** find another numerical fomula. ***/

long
doc_hash(s)
reg     long	s;
{
  reg   long    ia = 171;
  reg   long    ic = 11213;
  reg   long    im = 53125;

return((long)((s * ia + ic) % im));


}
/*
#define spec_l_mangle(x,highbit,mask) \
  ((((x) & ((highbit) - 1)) << 1) ^ ((x) & (highbit) ? (mask << 1 | 1) : 0))

I'll come back to this 

*/

/*___________________________________________________________________________*/

#define DOC_HASHTABLE_GROW_FACTOR 4

doc_hash_entry *put_doc_hash(key, htable, element)
     long key;
     doc_hashtable *htable;
     doc_hash_entry *element;
{
  /* allocate the entry out of the contents area, then add the entry to 
     the head of the bucket list. */

  /* old long bucket_number = doc_hash_code(key, 0) & htable->buckets_mask; */
  long bucket_number = doc_hash((long)key) & htable->buckets_mask;
  long old_bucket_value =  (htable->buckets)[bucket_number];
  long new_bucket_value = htable->number_of_elements++;
  doc_hash_entry *entry_header;

/*  printf("key: %ld", key);
    printf("bucket_number: %d", bucket_number);  */


  if(new_bucket_value >= htable->max_number_of_elements){
    long new_size = 
      DOC_HASHTABLE_GROW_FACTOR * htable->max_number_of_elements * 
	htable->element_size;
	
    waislog(WLOG_LOW, WLOG_INFO, 
	    "Expanding doc_hashtable from %ld bytes to %ld bytes address %ld...",
	   htable->max_number_of_elements * htable->element_size ,
	   new_size, htable->contents);
    htable->max_number_of_elements *= DOC_HASHTABLE_GROW_FACTOR;
    htable->contents = (doc_hash_entry*)s_realloc(htable->contents, new_size);
    if(NULL == htable->contents)
      panic("Out of virtual memory.");
  }
  entry_header = &(htable->contents)[new_bucket_value];
  memcpy((char *)entry_header, (char *)element, htable->element_size);
/*   printf("Writing key: %ld into index %ld\n", key, new_bucket_value); */
  entry_header->key =  key;
  entry_header->next = old_bucket_value;


  (htable->buckets)[bucket_number] = new_bucket_value;
  return(entry_header);
}

/*----------------------------------------------------------------- */

/* returns a pointer to the element stored or NULL if none. */
doc_hash_entry *get_doc_hash (key, htable)
     long key;
     doc_hashtable *htable;
{
  /* this looks up the value in the bucket and puts it at the head of the 
     bucket. */

  /* old long bucket_number = doc_hash_code(key, 0) & htable->buckets_mask; */
  long bucket_number = doc_hash((unsigned char*)key) & htable->buckets_mask;
  long old_bucket_value =  (htable->buckets)[bucket_number];

  long next_contents_index = old_bucket_value;

  while(next_contents_index != -1){
    /* keep looking down the list for the right value */
    if((htable->contents)[next_contents_index].key == key)
  /* found it */
      return(&(htable->contents)[next_contents_index]);
    else
      next_contents_index = (htable->contents)[next_contents_index].next;
  }
  return(NULL);
}


/*----------------------------------------------------------------- */

boolean clr_doc_hashtable (htable)
     doc_hashtable *htable;
{    
  htable->number_of_elements = 0;
  memset(htable->buckets, -1, sizeof(long) * htable->number_of_buckets);
  return(true);
}

/*----------------------------------------------------------------- */

/* removes contents and free's memory for the hastable.   
   returns true if successful, false otherwise */
boolean free_doc_hashtable(htable)
     doc_hashtable *htable;
{
  s_free(htable->contents);
  s_free(htable->buckets);
  s_free(htable);
  return(true);
}

/*----------------------------------------------------------------- */

/* returns the number of elements in the doc_hashtable */
long doc_hashtable_count(htable)
     doc_hashtable *htable;
{
  return(htable->number_of_elements);
}

/*----------------------------------------------------------------- */


static int doc_hash_entry_compare _AP((doc_hash_entry*i,doc_hash_entry* j));

static int doc_hash_entry_compare(i,j)
doc_hash_entry *i;
doc_hash_entry *j;
{
  if (i->weight > j->weight)
    return(1);
  else if (i->weight > j->weight)
    return(-1);
  else return(0);
}


boolean sort_doc_hashtable(htable)
     doc_hashtable *htable;
{
  qsort(htable->contents,
	htable->number_of_elements,
	(size_t)sizeof(doc_hash_entry),
	doc_hash_entry_compare);
  return(true);
}

/*----------------------------------------------------------------- */
/* print routines */


static void print_content_element(index, htable)
     long index;
     doc_hashtable *htable;
{  
  printf("  Index: %ld, key: '%s', Next index %ld\n", 
	 index, (htable->contents[index]).key, 
	 (htable->contents[index]).next);
}

void print_doc_bucket(bucket, htable)
     long bucket;
     doc_hashtable *htable;
{
  long next_contents_index = (htable->buckets)[bucket];

  printf(" Bucket: %ld\n", bucket);
  while(next_contents_index != -1){
    doc_hash_entry * entry_header = &(htable->contents)[next_contents_index];
    print_content_element(next_contents_index, htable);
    next_contents_index = entry_header->next;
  }
  return;
}

long doc_bucket_length(bucket, htable)
     long bucket;
     doc_hashtable *htable;
{
  long next_contents_index = (htable->buckets)[bucket];
  long answer = 0;
  while(next_contents_index != -1){
    answer++;
    next_contents_index = ((htable->contents)[next_contents_index]).next;
  }
  return(answer);
}

static int doc_bucket_length_compare _AP((long*i,long* j));

static int doc_bucket_length_compare(i,j)
long *i;
long *j;
{
  return((*j) - (*i));
}

void analyze_doc_hashtable_distribution(htable)
     doc_hashtable * htable;
{
  long count = 0;
  long max_length = 128;
  long *bucket_length_array = (long *)malloc(sizeof(long) * max_length);
  memset((char*)bucket_length_array, 0, (sizeof(long) * max_length));

  waislog(WLOG_LOW, WLOG_INFO, "--Doc_Hashtable Analysis--");
  waislog(WLOG_LOW, WLOG_INFO, "Number of buckets: %ld",
	  htable->number_of_buckets);
  waislog(WLOG_LOW, WLOG_INFO, "Number of allocated elements %ld",
	  htable->number_of_elements);
  waislog(WLOG_LOW, WLOG_INFO, "Max number of elements %ld",
	  htable->max_number_of_elements);

  while(count < htable->number_of_buckets){
    long length = doc_bucket_length(count, htable);
    if(length > max_length){  /* too small, start again */
      max_length *= 2;
      free((char*)bucket_length_array);
      bucket_length_array = (long *)malloc(sizeof(long) * max_length);
      memset((char*)bucket_length_array, 0, (sizeof(long) * max_length));
      count = 0;
    }
    else{
      bucket_length_array[length]++;
      count++;
    }
  }
  /* print the buckets */
  for(count = 0; count < max_length; count++){
    if(bucket_length_array[count] > 0){
      waislog(WLOG_LOW, WLOG_INFO, "Length %4ld, number of buckets: %5ld.",
	      count, bucket_length_array[count]);
    }
  }
  free((char*)bucket_length_array);
}

void print_doc_hashtable(htable)
     doc_hashtable * htable;
{
  long count;
  printf("Number of buckets: %ld\n",  htable->number_of_buckets);
  printf("Bucket mask %ld\n", htable->buckets_mask);
  printf("Element Size %ld\n", htable->element_size);
  printf("Number of allocated elements %ld\n", htable->number_of_elements);
  printf("Max number of elements %ld\n", htable->max_number_of_elements);
  
  printf("\nContents:\n");
  for(count = 0; count < htable->number_of_elements; count++){
    print_content_element(count, htable);	
  }
  printf("\nBuckets:\n");
  for(count = 0; count < htable->number_of_buckets; count++){
    if((htable->buckets)[count] != -1)
      print_bucket(count, htable);	
  }
} 

/* for saber trials:

foo(){
doc_hashtable *htable;
doc_hash_entry entry;
htable = make_doc_hashtable(0, 0, sizeof(doc_hash_entry));
print_doc_hashtable(htable);
put_doc_hash("food", htable, &entry);
print_doc_hashtable(htable);
*get_doc_hash("food", htable);
put_doc_hash("food", htable, &entry);
print_doc_hashtable(htable);
}
*/
