#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "EspaceD.h"
#include "TestAlloc.h"

EspaceD::EspaceD(int dx,int dy) {
  this->dx = dx;
  this->dy = dy;
  p =(double **) calloc(dy,sizeof(double *));
  testAllocAndExit(p,"new EspaceD(int,int)");
  if ( p ) {
    for ( int i = 0 ; i < dy ; i++ ) {
      p[i] =(double *) calloc(dx+1,sizeof(double));
      testAllocAndExit(p[i],"new EspaceD(int,int)",i); } }
}

EspaceD::~EspaceD(void) {
  for ( int i = 0 ; i < dy ; i++ )
    free(p[i]) ;
  free(p) ;
}

void EspaceD::init(double v) {
  for ( int i = 0 ; i < dy ; i++ )
    for ( int j = 0 ; j < dx+1 ; j++ )
      p[i][j] = v ;
}

double EspaceD::chercheAltitude(int x,int y) {
  return(p[y][x]) ;
}

void EspaceD::sauveAltitude(double altitude,int x,int y) {
  p[y][x] = altitude ;
}

long EspaceD::getUsedMemory(void) {
  long cpt = sizeof(EspaceD);
  cpt += dy*(dx+1)*sizeof(double) ;
  cpt += dy*sizeof(double *) ;
  return(cpt);
}

int EspaceD::getUsedHandles(void) {
  int cpt = 1;
  cpt += dy ;
  cpt++ ;
  return(cpt);
}

