/* Distributions.c */
/* Paul Boersma, September 8, 1999 */

#include "Distributions.h"

static void info (I) {
	iam (TableOfReal);
	Melder_information ("Number of distributions = %ld\n"
		"Number of values = %ld",
		my numberOfColumns, my numberOfRows);
}

class_methods (Distributions, TableOfReal)
	class_method (info)
class_methods_end

Distributions Distributions_create (long numberOfRows, long numberOfColumns) {
	Distributions me = new (Distributions);
	if (! me || ! TableOfReal_init (me, numberOfRows, numberOfColumns)) forget (me);
	return me;
}

double Distributions_getProbability (Distributions me, const char *string, long column) {
	long row, rowOfString = 0;
	double total = 0.0;
	if (column < 1 || column > my numberOfColumns) return NUMundefined;
	for (row = 1; row <= my numberOfRows; row ++) {
		total += my data [row] [column];
		if (my rowLabels [row] && strequ (my rowLabels [row], string))
			rowOfString = row;
	}
	if (total <= 0.0) return NUMundefined;
	if (rowOfString == 0) return 0.0;
	return my data [rowOfString] [column] / total;
}

double Distributionses_getMeanAbsoluteDifference (Distributions me, Distributions thee, long column) {
	long row;
	double total = 0.0;
	if (column < 1 || column > my numberOfColumns || column > thy numberOfColumns ||
	    my numberOfRows != thy numberOfRows) return NUMundefined;
	for (row = 1; row <= my numberOfRows; row ++) {
		total += fabs (my data [row] [column] - thy data [row] [column]);
	}
	return total / my numberOfRows;
}

static void unicize (Distributions me) {
	/* Must have been sorted beforehand. */
	long nrow = 0, ifrom = 1, ito, i, j, icol;
	for (i = 1; i <= my numberOfRows; i ++) {
		if (i == my numberOfRows || (my rowLabels [i] == NULL) != (my rowLabels [i + 1] == NULL) ||
		    my rowLabels [i] != NULL && ! strequ (my rowLabels [i], my rowLabels [i + 1]))
		{
			/*
			 * Detected a change.
			 */
			nrow ++;
			ito = i;
			/*
			 * Move row 'ifrom' to 'nrow'. May be the same row.
			 */
			if (ifrom != nrow) {
				Melder_free (my rowLabels [nrow]);
				my rowLabels [nrow] = my rowLabels [ifrom];   /* Surface copy. */
				my rowLabels [ifrom] = NULL;   /* Undangle. */
				for (icol = 1; icol <= my numberOfColumns; icol ++)
					my data [nrow] [icol] = my data [ifrom] [icol];
			}
			/*
			 * Purge rows from 'ifrom'+1 to 'ito'.
			 */
			for (j = ifrom + 1; j <= ito; j ++) {
				Melder_free (my rowLabels [j]);
				for (icol = 1; icol <= my numberOfColumns; icol ++)
					my data [nrow] [icol] += my data [j] [icol];
			}
			ifrom = ito + 1;
		}
	}
	my numberOfRows = nrow;
}

Distributions Distributions_addTwo (Distributions me, Distributions thee) {
	Distributions him = TablesOfReal_append (me, thee);
	if (! him) return NULL;
	TableOfReal_sortByLabel (him, 0, 0);
	unicize (him);
	return him;
}

Distributions Distributions_addMany (Collection me) {
	Distributions thee = TablesOfReal_appendMany (me);
	if (! thee) return NULL;
	TableOfReal_sortByLabel (thee, 0, 0);
	unicize (thee);
	return thee;
}

/* End of file Distributions.c */
