#include "datamat.h"

static char *make_datamatid = "$Id: make_datamat.c,v 1.3 1996/12/15 18:50:32 galway Exp $";

extern struct categval *find_vals();

/* ***************************************************************** */
/* 


    make_datamat.c

    Copyright (C) 2004  Lionel A. Galway

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    For a copy of the GNU General Public License write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA




   Takes a data matrix of values, with labels, etc. and sets up a
   complete data structure, including block value indicators and 
   lists of unique values of column values.

   L. Galway 16-AUG-1996
/* ***************************************************************** */

struct datamat *make_datamat(data,nr,nc,rlabs,clabs)
int **data,nr,nc;char **rlabs,**clabs;
{
  struct datamat *retval; int i,j;

  /*  Allocate space */
  retval = (struct datamat *)malloc(sizeof(struct datamat));

  /*  Plug in values for data, matrix dimensions, labels */
  retval->nrows = nr; retval->ncols = nc;
  retval->rowlabs = rlabs; retval->collabs = clabs;
  retval->data = data;

  /*  Allocate and initialize block indicators */
  retval->blkind=(int **)malloc(retval->nrows*sizeof(int*));
  for (i=0;i<retval->nrows;i++) {
    retval->blkind[i]=(int *)malloc(retval->ncols * sizeof(int));
    for (j=0;j<retval->ncols;j++) retval->blkind[i][j] = -1;
  };

  /*  Allocate storage and compute unique column values */
  retval->colvals = 
    (struct categval **)malloc(retval->ncols * sizeof(struct categval *));
  for (i=0;i<retval->ncols;i++) retval->colvals[i]=find_vals(retval,i);
		 
  return(retval);
}
