/*
 * The Spar Library - modular math parser
 * Copyright (C) 2000,2001 Davide Angelocola <davide178@inwind.it>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 *
 */

#include <spar/sl_vector.h>
#include <spar/sl_util.h>

#include <stdarg.h>


sl_vector *
sl_vector_new (const sl_size m)
{
  sl_assert (m > 0);

  {
    sl_vector *new;
    new = sl_malloc (sizeof (sl_vector *));

    new->dim = m;
    new->data = sl_malloc (sizeof (double) * m);

    return new;
  }
}

sl_vector *
sl_vector_new_from (const sl_size m, double first, ...)
{
  va_list ap;
  double item;
  sl_size i;
  sl_vector *new;

  sl_assert (m > 0);

  va_start (ap, first);
  new = sl_malloc (sizeof (sl_vector *));
  new->dim = m;
  new->data = sl_calloc (m, sizeof (double));
  new->data[0] = first;

  for (i = 1; i < m; i++)
    {
      item = va_arg (ap, double);
      new->data[i] = item;
    }

  va_end (ap);

  return new;
}


int
sl_vector_delete (sl_vector * v)
{
  sl_assert (v != NULL);
  sl_free (v->data, sizeof (double) * v->dim);
  sl_free (v, sizeof (v));

  return SL_SUCCESS;
}

sl_vector *
sl_vector_change_dim (sl_vector * v, const sl_size s)
{
  sl_assert (v != NULL);
  sl_assert (s > 0);

  if (v->dim == s)
    return v;
  else
    {
      v->dim = s;
      v->data = sl_realloc (v->data, sizeof (double) * s);

      return v;
    }
}


syntax highlighted by Code2HTML, v. 0.9.1