// file: ht_hash_0.cc
//

// isip include files
//
#include "hash_table.h"
#include "hash_table_constants.h"
 
// method: hash_insert_cc
//
// arguments:
//  Hash_cell* cell : (input) the Hash_cell to be copied and inserted
//                    in the hash table
//
// return: a Hash_cell* pointer to the Hash_cell containing this entry
//
// this method inserts an entry for the given Hash_cell in the hash
// table
//
Hash_cell* Hash_table::hash_insert_cc(Hash_cell* cell_a) {

  // check for empty table. create a default sized table if so.
  //
  if (cells_d == (Hash_cell**)NULL) {
    set_size_cc(HASH_TABLE_SIZE);
  }
  
  // index variable
  //
  int_4 index = (int_4)0;
  Hash_cell* cell = (Hash_cell*)NULL;
  
  if (mode_d == HASH_TABLE_STRING_DRIVEN) {

    char_1* str = (char_1*)NULL;
    
    // the string to hash
    //
    cell_a->get_key_cc(str);
    
    // make sure this string is not already hashed
    //
    cell = hash_lookup_cc(str, index);
    if (cell == (Hash_cell*)NULL) {
      
      // the input trace needs to be added to the table
      //
      cell = cell_a;
      
      // insert this cell in the table
      //
      cell->set_next_cc(cells_d[index]);
      cells_d[index] = cell;
    }
    
    // otherwise output error message and exit program
    //
    else {
      fprintf(stdout, "%s\n", str);
      error_handler_cc((char_1*)"hash_insert_cc",
		       (char_1*)"duplicate hashing of cell - each hash string must be unique");
      exit(ISIP_PROTO_ERROR);
    }
  }
  else {
    int_4* vec = (int_4*)NULL;
    int_4 vec_size = 0;
    
    // vector
    //
    cell_a->get_key_cc(vec, vec_size);
    
    // make sure this string is not already hashed
    //
    cell = hash_lookup_cc(vec, vec_size, index);
    if (cell == (Hash_cell*)NULL) {
      
      // the input trace needs to be added to the table
      //
      cell = cell_a;
      
      // insert this cell in the table
      //
      cell->set_next_cc(cells_d[index]);
      cells_d[index] = cell;
    }
    
    // otherwise output error message and exit program
    //
    else {
      error_handler_cc((char_1*)"hash_insert_cc",
		       (char_1*)"duplicate hashing of cell - each hash string must be unique");
      exit(ISIP_PROTO_ERROR);
    }
  }
  
  // exit gracefully
  //
  return cell;
}



