// file: $isip/class/search/Trace/trace_05.cc
// version: $Id: trace_05.cc 9146 2003-05-17 19:34:27Z jelinek $
//

// isip include files
//
#include "Trace.h"

// method: setActive
//
// arguments:
//  boolean is_active: (input) defines the trace as active or inactive
//
// return: a boolean value indicating status
//
// this method sets the trace to be either active or inactive
//
boolean Trace::setActive(boolean is_active_a) {

  if (!is_active_a) {
    score_d = INACTIVE_SCORE;
  }
  else {
    if (score_d == INACTIVE_SCORE) {
      score_d = DEF_SCORE;
    }
  }
  return true;
}

// method: setBackPointer
//
// arguments:
//  Trace* back_ptr: (input) the trace to point back to
//
// return: a boolean value indicating status
//
// this method sets the trace's back pointer
//
boolean Trace::setBackPointer(Trace* back_ptr_a) {

  if (back_ptr_d != (Trace*)NULL) {
    back_ptr_d->decrementRefCount();
  }
  
  // set the new back pointer
  //
  back_ptr_d = back_ptr_a;
  if (back_ptr_d != (Trace*)NULL) {
    back_ptr_d->incrementRefCount();
  }

  // exit gracefully
  //
  return true;
}

// method: decrementRefCount
//
// arguments:
//  ulong decrement: (input) the number to decrement by 
//
// return: the reference count after decrementing
//
// this method decrements the reference count
//
ulong Trace::decrementRefCount(ulong decrement_a) {

  if (decrement_a > reference_count_d) {
    reference_count_d = 0;
  }
  else {
    reference_count_d -= decrement_a;
  }

  // exit gracefully
  //
  return reference_count_d;
}

// method: hash
//
// arguments:
//   long capacity: (input) hash table capacity
//
// return: logical error status
//
// hash function for the hypothesis path class
//
long Trace::hash(long capacity_a) const {

  // get the hash index of history
  //
  ulong hash_index = getHistory()->hash(capacity_a);

  // combine it with the hash index of symbol
  //
  ulong index = getSymbol()->getHashIndex();
  hash_index = (hash_index << 5) ^ (hash_index >> 27) ^ index;

  // return the hash index
  //  
  return (hash_index % (capacity_a - 1));

}

  

