#include "datamat.h"
#include "block.h"
#include "logical.h"

static char *initblockid = "$Id: init_block.c,v 1.5 1996/12/30 22:20:17 galway Exp $";

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

    init_block.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




   Starts a new block with code nblk and row irw by first getting a
   blank block and then by finding the set of columns using Hartigan's
   algorithm.

   L. Galway 30-DEC-1996 */
/*****************************************************************/

extern struct block *alloc_block();
extern int block_count();
extern enum logical is_blkind();
extern void add_row();
extern void add_col();

struct block *init_block(dat,irw,nblk)
struct datamat *dat;int irw,nblk;
{
  struct block *retval;int i;

  /* Allocate space for new block structure */
  retval = alloc_block(dat->nrows,dat->ncols);
  retval->bcode=nblk;

  /* Add the specified row... */
  add_row(irw,retval);
  /*  ... and then add columns which are not block symbols, defining
      new block symbols to be the contents of the added columns */
  for (i=0;i<retval->NC;i++) {
    if (is_blkind(irw,i,dat) == FALSE) {
      add_col(i,retval);retval->blkcd[i] = dat->data[irw][i];
    };
  };

  return(retval);
}
