/* This file is part of the Neper software package. */
/* Copyright (C) 2003-2014, Romain Quey. */
/* See the COPYING file in the top-level directory. */

#include"net_filter_.h"

void
net_filter (struct IN In, struct TESR *pTesr)
{
  int i, j, dim, rptqty, status, qty;
  int **rptpos = NULL;
  char** list = NULL;

  ut_string_separate (In.filter, NEUT_SEP_DEP, &list, &qty);

  for (i = 0; i < qty; i++)
  {
    // Size filter
    if (!strncmp (list[i], "size", 4))
    {
      ut_print_message (0, 2, "Applying filter `%s'...\n", list[i]);

      status = sscanf (list[i], "size(%d)", &dim);

      if (status != 1)
	dim = (*pTesr).Dim - 1;

      do
      {
	net_tesr_filter_cell_find (*pTesr, dim, &rptqty, &rptpos);

	if (rptqty > 0)
	{
	  net_tesr_filter_cell_remove (pTesr, dim, rptqty, rptpos);

	  ut_free_2d_int (rptpos, rptqty);
	  rptpos = NULL;
	}

	ut_print_message (0, 3, "%d point%s filtered.\n",
			  rptqty, (rptqty > 1) ? "s" : "");
      }
      while (rptqty > 0);
    }
    else if (!strncmp (list[i], "noise", 5))
    {
      ut_print_message (0, 2, "Applying filter `%s'...\n", list[i]);

      for (j = pow (3, (*pTesr).Dim) - 1; j >= 0; j--)
      {
	rptqty = net_tesr_filter_noise (pTesr, 0, j, INT_MAX);

	if (rptqty >= 0)
	  ut_print_message (0, 3, "%d point%s filtered.\n",
			    rptqty, (rptqty > 1) ? "s" : "");
	else
	{
	  rptqty = 0;
	  ut_print_message (0, 3, "%d point%s filtered.\n",
			    rptqty, (rptqty > 1) ? "s" : "");
	  break;
	}
      }
    }
    else
      ut_print_message (2, 3, "Expression `%s' could not be processed.\n", list[i]);
  }

  return;
}
