// file: train_memory_manager.h
//

// make sure definitions are only made once
//
#ifndef __ISIP_TRAIN_MEMORY_MANAGER
#define __ISIP_TRAIN_MEMORY_MANAGER

// isip include files
//
#ifndef __ISIP_INTEGRAL
#include <integral.h>
#endif

// forward declaration of classes
//
class Train_Trace;
class Train_Lex_node;
class Train_Lattice_node;
class Train_Hash_cell;

#ifndef __ISIP_TRAIN_LINK_NODE
#include <train_link_node.h>
#endif

// Train_Memory_manager: a class that handles memory allocation and deletion
// for traces and link list nodes
//
class Train_Memory_manager {
  
  //---------------------------------------------------------------------------
  //
  // protected data
  //
  //---------------------------------------------------------------------------
protected:
  
  // pointer to a list of free node pointers and the count of node
  // pointers left on the free node list
  //
  Train_Link_node* node_list_d;
  int_4 node_count_d;
  
  // pointer to a list of free traces and the count of traces left on
  // the free trace list
  //
  Train_Link_node* trace_list_d;
  int_4 trace_count_d;

  // pointer to a list of lexical tree nodes
  //
  Train_Link_node* lex_list_d;
  int_4 lex_count_d;
  
  // pointer to a list of lattice nodes
  //
  Train_Link_node* lat_list_d;
  int_4 lat_count_d;
  
  // pointer to a list of hash table cells
  //
  Train_Link_node* hash_list_d;
  int_4 hash_count_d;
  
  // the number of nodes or traces to allocate if one of the free
  // lists becomes empty
  //
  int_4 node_grow_size_d;
  int_4 trace_grow_size_d;  
  int_4 lex_grow_size_d;
  int_4 lat_grow_size_d;
  int_4 hash_grow_size_d;
  
  //---------------------------------------------------------------------------
  //
  // public methods
  //
  //---------------------------------------------------------------------------
public:

  // required methods
  //
  char_1* name_cc();
  volatile void error_handler_cc(char_1* method_name, char_1* message);
  logical_1 debug_cc(FILE* fp, char_1* message);
  int_4 size_cc();
  
  // destructors/constructors
  //
  ~Train_Memory_manager();  
  Train_Memory_manager();                                         // default
  Train_Memory_manager(int_4 grow_size, int_4 node_size);         // overloaded
  Train_Memory_manager(const Train_Memory_manager& manager);            // copy

  // methods that take memory units off of the free lists and give
  // them to the user
  //
  Train_Link_node* new_node_cc();
  Train_Trace* new_trace_cc();
  Train_Lex_node* new_lex_cc();
  Train_Lattice_node* new_lat_cc();
  Train_Hash_cell* new_hash_cc();

  // methods that put memory units back on the free lists
  //
  logical_1 delete_cc(Train_Link_node* node);  
  logical_1 delete_cc(Train_Trace* trace);
  logical_1 delete_cc(Train_Lex_node* lxn);
  logical_1 delete_cc(Train_Lattice_node* ltn);
  logical_1 delete_cc(Train_Hash_cell* hash);

  // set methods
  //
  logical_1 set_node_grow_size_cc(int_4 size);
  logical_1 set_trace_grow_size_cc(int_4 size);
  logical_1 set_lex_grow_size_cc(int_4 size);
  logical_1 set_lat_grow_size_cc(int_4 size);
  logical_1 set_hash_grow_size_cc(int_4 size);

  // get methods
  //
  int_4 get_node_grow_size_cc();
  int_4 get_trace_grow_size_cc();
  int_4 get_lex_grow_size_cc();
  int_4 get_lat_grow_size_cc();
  int_4 get_hash_grow_size_cc();

  // count methods
  //
  int_4 get_node_count_cc();
  int_4 get_trace_count_cc();
  int_4 get_lex_count_cc();
  int_4 get_lat_count_cc();
  int_4 get_hash_count_cc();
  
  //---------------------------------------------------------------------------
  //
  // private methods
  //
  //---------------------------------------------------------------------------
private:

  // methods to allocate more memory for the free traces list and the
  // free nodes list
  //
  logical_1 grow_node_cc();
  logical_1 grow_trace_cc();  
  logical_1 grow_lex_cc();  
  logical_1 grow_lat_cc();  
  logical_1 grow_hash_cc();  
  
};

// end of file
// 
#endif
