// ftable.h
#ifndef LOOKUP_H
#define LOOKUP_H

#define LINK_DHSVM

#define NUM_ROWS 12
#define NUM_OUTFLOWS 4

#include <iostream>
#include <cstring>
#include <fstream>
#include <cassert>
#include <cmath>
#include <new>

using namespace std;

typedef struct {
  int numRows;
  int numOutflows;
  float key[NUM_ROWS];
  float surfArea[NUM_ROWS];
  float volume[NUM_ROWS];
  float outflow[NUM_ROWS][NUM_OUTFLOWS];
} TABLE;

typedef struct {
  float key;
  float surfArea;
  float volume;
  float outflow[NUM_OUTFLOWS];
  float outVol[NUM_OUTFLOWS];
} RESULTS;


class Ftable
{
public:
  // constructors & intializers
  Ftable();
  int readTableInfo(  char multFileName[], char tableFileName[], Ftable *ftab, 
                      int num_ftab );
  RESULTS Ftable::route( float invol, float dT );
  int adjustTable();
  int adjustTable( float multKey, float multArea, float multVol, float multOut[] );
  
private:  
  int allocateTables( int numTypes );
  //int adjustTable();
  int getDemand();
  void solve( float oint, float dT);
  RESULTS interpolate( float time );
  void endOfdT(RESULTS *r);
  float solveODE( int i, float vol, float inflow, float dT, float *slope, float *ratio );
  inline float timeToVol( float volStart, float volEnd, float slope, float ratio );
  void getFlowVols( int i, float volume, float time, float totalOutVol, float outVol[] );  
  inline float getMeanVol( int i, float meanQ );

  static float totalDemand[NUM_ROWS];
  static TABLE *table;
  static TABLE tbl;
  
  int d_type;
  float d_mult[NUM_OUTFLOWS+3];
  float d_volume;
  int d_baseRow;

  // test
  TABLE *d_base;  // pointer to appropriate base ftable
};

static Ftable *ftab;

Ftable * buildFtables( char multFileName[], char tableFileName[], int *num_ftables );
int getNumFtables(char multFileName[]);
//float route_segment( int seg_id, float ivol, float dT );


#endif

