#include "scproblem.h"


SCProblem* scproblem_new(int rows,int columns) {

    SCProblem* p=NULL;

    /*Check parameters*/
    assert(rows>0 && columns>0);
    
    /*Allocate memory*/
    p = (SCProblem*)malloc(sizeof(SCProblem));
    assert(p!=NULL);
    
    /*Allocate data inluding one spare row*/
    p->data = (unsigned char*)malloc((rows+1)*columns);
    assert(p->data!=NULL);
    /*Clear all data*/
    memset(p->data,0,(rows+1)*columns);
    
    /*Allocate vector of column costs*/
    p->columnCosts = (unsigned int*)malloc(columns*sizeof(unsigned int));
    assert(p->columnCosts!=NULL);
    /*Clear all costs*/
    memset(p->columnCosts,0,columns);
    
    
    /*Set dimensions*/
    p->rows=rows;
    p->columns=columns;
    
    /*Return value*/
    return p;
}


void scproblem_delete(SCProblem* m) {
    
    /*Check parameters*/
    if (m==NULL) return;
    
    /*Free data if possible*/
    if (m->data!=NULL) free(m->data);
    if (m->columnCosts!=NULL) free(m->columnCosts);
    

    /*Free SCProblem*/
    free(m);
}


void scproblem_set(SCProblem* m,int row,int column,unsigned char value) {

    unsigned char* p=NULL;

    /*Check parameters and conditions*/
    assert (m!=NULL &&  m->data!=NULL);
    assert (row>=0 && column>=0 && row<m->rows && column<m->columns);
    
    /*Set the value*/
    p=m->data;
    *(p+row*m->columns+column)=value;
    
}

void scproblem_spare_row_set(SCProblem* m,int column,unsigned char value) {

    unsigned char* p=NULL;

    /*Check parameters and conditions*/
    assert (m!=NULL && m->data!=NULL);
    assert (column>=0 && column<m->columns);

    p=m->data;
    *(p+(m->rows-1)*m->columns+column)=value;
}


void scproblem_set_cost(SCProblem* m,int column,unsigned int cost) {
    
    /*Check parameters and conditions*/
    assert (m!=NULL && m->columnCosts!=NULL);
    assert (column>=0 && column < m->columns);
    
    /*Set cost*/
    *(m->columnCosts+column)=cost;
    
}


void scproblem_print(SCProblem* m) {
    if (m!=NULL) printf ("SCProblem\n");
}
