// file: ht_prune_3.cc
//
// isip include files
//
#include "hmm_train.h"
#include "hmm_train_constants.h"

// method: garbage_prune_cc
//
// arguments:
//  Train_Link_list** trace_list : (input) the trace linked list
//  int_4* active : (input) list of active phones
//  int_4 num_act : (input) number of active phones
//  int_4& ndel : (output) the number of traces deleted
//
// return: a logical_1 to indicate status
//
// this method checks all phone-end traces and removes those that are
// below the pruning beam width
//
logical_1 garbage_prune_cc(Train_Link_list** trace_list_a, int_4* active_a,
			   int_4 num_act_a, int_4& ndel_a) {

  // dummy variable
  //
  Train_Trace* tr = (Train_Trace*)NULL;
  Train_Link_node* node = (Train_Link_node*)NULL;

  // memory manager
  //
  Train_Memory_manager* manager = Train_Link_list::get_manager_cc();

  // loop over all active trace lists
  //
  for (int_4 ww = 0; ww < num_act_a; ww++) {
    
    // loop over all traces
    //
    for (Train_Link_node* nd = trace_list_a[active_a[ww]]->get_head_cc();
	 nd != (Train_Link_node*)NULL; nd = nd->get_next_cc()) {
      
      // get the trace in this node
      //
      tr = (Train_Trace*)(nd->get_item_cc());
      
      // check if this trace is marked to be removed
      //
      if (tr->get_ref_count_cc() < (int_4)0) {
	
	// delete the trace
	//
	node = nd->get_prev_cc();
	manager->delete_cc(tr);
	trace_list_a[active_a[ww]]->remove_cc(nd);
	
	// reset node pointer
	//
	nd = node;
	
	// increment deletion count
	//
	ndel_a++;
      }
    }
  }
  
  // exit gracefully
  //
  return ISIP_TRUE;
}
