// file: $isip/class/algo/Math/math_04.cc
// version: $Id: math_04.cc 6907 2001-05-02 16:43:54Z duncan $
//

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

// method: sofSize
//
// arguments: none
//
// return: size of object
//
// this method return  the size of the object
//
long Math::sofSize() const {

  // start with enough space for the type
  //
  long bytes = 0;

  // add the size of the algorithm and function
  //
  bytes += ALGO_MAP.elementSofSize();

  // add the size of num, specification vector, constant and debug
  // level
  //
  bytes += num_operands_d.sofSize();
  bytes += function_d.sofSize();
  bytes += operation_d.sofSize();
  bytes += weight_d.sofSize();
  bytes += const_d.sofSize();
  bytes += debug_level_d.sofSize();

  // add the space required for the implementation name
  //
  bytes += IMPL_MAP.elementSofSize();

  // return the size
  //
  return bytes;
}

// method: write
//
// arguments:
//  Sof& sof: (input) sof file object
//  long tag: (input) sof object instance tag
//  const String& name: (input) sof object instance name
//
// return: a boolean value indicating status
//
// this method has the object write itself to an Sof file
//
boolean Math::write(Sof& sof_a, long tag_a, const String& name_a) const {

  // declare local variables
  //   
  long obj_size = 0;

  // write the instance of the object into the Sof file
  //   
  if (sof_a.isText()) {
    obj_size = Sof::ANY_SIZE;
  }
  else {
    obj_size = sofSize();
  }
  
  // put the object into the sof file's index
  //
  if (!sof_a.put(name_a, tag_a, obj_size)) {
    return false;
  }

  // exit gracefully
  //
  return writeData(sof_a);
}

// method: writeData
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method has the object writes itself from an Sof file. it assumes
// that the Sof file is already positioned correctly.
//
boolean Math::writeData(Sof& sof_a, const String& pname_a) const {

  // declare local variables
  //
  boolean status = false;
  
  // write a start string if necessary
  //
  sof_a.writeLabelPrefix(pname_a);

  // write the algorithm type
  //
  ALGO_MAP.writeElementData(sof_a, PARAM_ALGORITHM, (long)algorithm_d);
  
  // check known algorithms
  //
  if (algorithm_d == FUNCTION_CALCULATOR) {
    status = writeDataCommon(sof_a, pname_a);
  }
  else {
    return Error::handle(name(), L"writeData", ERR_UNKALG,
			 __FILE__, __LINE__);
  }
  
  // put an end string if necessary
  //
  sof_a.writeLabelSuffix(pname_a);
  
  // exit gracefully
  //
  return status;
}

// method: writeDataCommon
//
// arguments:
//  Sof& sof: (input) sof file object
//  const String& pname: (input) parameter name
//
// return: a boolean value indicating status
//
// this method has been implemented for FUNCTION_CALCULATOR algorithm
// and writes data to a file for several algorithms that share the
// same parameters.
//
boolean Math::writeDataCommon(Sof& sof_a, const String& pname_a) const {

  // write the implementation type
  //
  IMPL_MAP.writeElementData(sof_a, PARAM_IMPLEMENTATION,
			    (long)implementation_d);
  // write the number of combinations
  //
  num_operands_d.writeData(sof_a, PARAM_NUMBER);

  // write the constant
  //
  const_d.writeData(sof_a, PARAM_CONST);

  // write function and operation. for text mode, we create and write
  // out a String. for binary mode we stay with the VectorLong
  //
  if (sof_a.isText()) {
    String str;
    getFunction(str);
    str.writeData(sof_a, PARAM_FUNCTION);

    getOperation(str);
    str.writeData(sof_a, PARAM_OPERATION);
  }
  else {
    function_d.writeData(sof_a, PARAM_FUNCTION);
    operation_d.writeData(sof_a, PARAM_OPERATION);
  }

  // write weight
  //
  weight_d.writeData(sof_a, PARAM_WEIGHT);  
  
  // write the debug level
  //
  debug_level_d.writeData(sof_a, PARAM_DBGL);

  // exit gracefully
  //
  return true;
}
