
/*
 * Copyright (1987) Jeff Elman.  University of California, San Diego
 * This software may be redistributed without charge; this notice
 * should be preserved.
 */

#include <stdio.h>
#include "defs.h"

/*
 * concatenate the hidden units to the input, so that they function
 * as context for the next cycle
 */
buildin(context) 
	int 	context;
{
	extern	int debug;
	extern	float **layerp;
	extern	float *nodep;
	extern	int *layer_descp;
	extern	float *cunits;
	extern	float decays[];
	extern	int numin;
	extern	int numout;
	extern	int num_hidden;		/* total number hidden units */
	register int i;
	register int j;
	float	**lp;
	float	*blp;
	float	*cup;
	extern	int nlayers;
	extern	int numtotal;
	int	*ldp;
	int	offset;

	/* 
	 * we copy the hidden units into the context units, then
	 * copy the context units into the end of the 1st layer.
	 */
	offset = 0;
	if (context == 1)
		offset += num_hidden;
	blp = *layerp;
	blp += *layer_descp - offset;
	cup = cunits;
	/*
	 * Copy in the hidden units if necessary;  we loop nlayer-2 times, 
	 * since we omit the first layer and the last
	 */
	if (context == 1) {
		if (debug==99)
			fprintf(stdout, "COPYING:\t");
		for (i=0,j=0,ldp=(layer_descp+1), lp=(layerp+1); 
				i < (nlayers-2); i++, ldp++, lp++) { 
			float *l;
			l = *lp;
			for (j=0; j < *ldp; j++) {
				*cup = *l + (decays[j] * *cup);
				*blp = *cup;
				if (debug==99)
					fprintf(stdout, "(%d)%f\t", j, *blp);
				blp++;
				l++;
			}
				if (debug==99)
					fprintf(stdout, "\n");
		}
	}
}
