view src/tc-inlines.h @ 1:78fd87e624cb

[project @ 1993-08-08 01:13:40 by jwe] Initial revision
author jwe
date Sun, 08 Aug 1993 01:13:40 +0000
parents
children 7849db4b6dbc
line wrap: on
line source

// tc-inlines.cc                                          -*- C++ -*-
/*

Copyright (C) 1992, 1993 John W. Eaton

This file is part of Octave.

Octave 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, or (at your option) any
later version.

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

You should have received a copy of the GNU General Public License
along with Octave; see the file COPYING.  If not, write to the Free
Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

// Just a coupla more helper functions.

static inline int
tree_to_mat_idx (double x)
{
  if (x > 0)
    return ((int) (x + 0.5) - 1);
  else
    return ((int) (x - 0.5) - 1);
}

static inline int
range_max_check (int i, int imax)
{
  i++;
  if (i > imax)
    {
      error ("matrix index = %d exceeds maximum dimension = %d", i, imax);
      jump_to_top_level ();
    }
}

static inline int
range_max_check (int i, int j, int nr, int nc)
{
  i++;
  if (i > nr)
    {
      error ("matrix row index = %d exceeds maximum row dimension = %d",
	     i, nr);
      jump_to_top_level ();
    }

  j++;
  if (j > nc)
    {
      error ("matrix column index = %d exceeds maximum column dimension = %d",
	     j, nc); 
      jump_to_top_level ();
    }
}

static inline int
indexed_assign_conforms (int lhs_nr, int lhs_nc, int rhs_nr, int rhs_nc)
{
  return (lhs_nr == rhs_nr && lhs_nc == rhs_nc);
}

static inline int
is_zero_one (const Range& r)
{
  double b = r.base ();
  double l = r.limit ();
  return (NINT (b) == 0 && NINT (l) == 1 && r.nelem () == 2);
}

static inline void
index_check (int i, char *rc)
{
  if (i < 0)
    {
      error ("invalid %s index = %d", rc, i+1);
      jump_to_top_level ();
    }
}

static inline void
index_check (const Range& r, int& max_val, char *rc)
{
  double b = r.base ();
  int ib = tree_to_mat_idx (b);

  if (r.nelem () < 1)
    {
      error ("range invalid as %s index", rc);
      jump_to_top_level ();
    }

  if (ib < 0)
    {
      error ("invalid %s index = %d", rc, ib+1);
      jump_to_top_level ();
    }

  double lim = r.limit ();
  max_val = tree_to_mat_idx (lim);
}

static inline void
index_check (const Range& r, char *rc)
{
  int max_val;
  index_check (r, max_val, rc);
}

static inline int
fortran_row (int i, int nr)
{
  int r;
  r = i % nr;
  if (r == 0)
    r = nr;
  return r;
}

static inline int
fortran_column (int i, int nr)
{
  int c;
  int r;
  r = fortran_row (i, nr);
  c = (i - r) / nr + 1;
  return c;
}

static inline int
valid_scalar_indices (tree_constant *args, int nargs)
{
  int valid = args != NULL_TREE_CONST
    && ((nargs == 3 && args[2].valid_as_scalar_index ()
	 && args[1].valid_as_scalar_index ())
	|| (nargs == 2 && args[1].valid_as_scalar_index ()));

  return valid;
}

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; page-delimiter: "^/\\*" ***
;;; End: ***
*/