// file: dec_clean_4.cc
//

// isip include files
//
#include "decoder.h"
#include "decoder_constants.h"
 
// method: active_instances_cc
//
// arguments: none
//
// return: logical_1 indicating status
//
// this method cleans up all the instances in the instance table that
// have not been active for the specified time
//
logical_1 Decoder::active_instances_cc() {

  // do this only if needed
  //
  if (frame_d % DEC_FRAMES_OUTNUM == (int_4)0) {

    // dummy variables
    //
    Instance* inst = (Instance*)NULL;
    Hash_cell** hcells = (Hash_cell**)NULL;
    Hash_cell* hcell = (Hash_cell*)NULL;
    Hash_cell* pcell = (Hash_cell*)NULL;
    int_4 hsize = (int_4)0;
    
    // loop over the instance hash-table
    //
    hcells = inst_table_d->get_cells_cc();
    hsize = inst_table_d->get_size_cc();
    for (int_4 k = 0; k < hsize; k++) {
      pcell = hcells[k];
      for (Hash_cell* cell = pcell; cell != (Hash_cell*)NULL;
	   cell = hcell) {
	
	// get the instance here
	//
	hcell = cell->get_next_cc();
	inst = (Instance*)(cell->get_item_cc());

	// if this instance is ready for removal
	//
	if (inst->active_cc(frame_d, DEC_FRAMES_OUTNUM) == ISIP_FALSE) {

	  // check if this is the head of the list
	  //
	  if (cell == hcells[k]) {
	    hcells[k] = hcell;
	    pcell = hcell;
	  }

	  // otherwise update the list
	  //
	  else {
	    pcell->set_next_cc(hcell);
	  }
	  
	  // remove this instance
	  //
	  manager_d->delete_cc(inst);
	  manager_d->delete_cc(cell);

	} // end if ready to remove

	// otherwise update the previous cell value
	//
	else {
	  pcell = cell;
	}
      } // end for all cells
    } // end for k
  } // end if it is time

  // exit gracefully
  //
  return ISIP_TRUE;
}
