view liboctave/dim-vector.h @ 4543:79df15d4470c

[project @ 2003-10-18 03:53:52 by jwe]
author jwe
date Sat, 18 Oct 2003 03:53:53 +0000
parents 01ee68d18069
children 820323598f4f
line wrap: on
line source

/*

Copyright (C) 2003 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, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/

#if !defined (octave_dim_vector_h)
#define octave_dim_vector_h 1

#if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION)
#pragma interface
#endif

#include <cassert>
#include <string>

#include "lo-sstream.h"

class
dim_vector
{
public:

  dim_vector (void) : ndims (0), dims (0) { }

  dim_vector (int n) : ndims (1), dims (new int [1]) { dims[0] = n; }

  dim_vector (int r, int c)
    : ndims (2), dims (new int [2]) { dims[0] = r; dims[1] = c; }

  dim_vector (int r, int c, int p)
    : ndims (3), dims (new int [3]) { dims[0] = r; dims[1] = c; dims[2] = p; }

  dim_vector (const dim_vector& dv)
    : ndims (dv.ndims)
    {
      if (dv.dims)
	{
	  dims = new int [ndims];

	  for (int i = 0; i < ndims; i++)
	    dims[i] = dv.dims[i];
	}
      else
	dims = 0;
    }

  dim_vector& operator = (const dim_vector& dv)
    {
      if (&dv != this)
	{
	  ndims = dv.ndims;

	  if (dv.dims)
	    {
	      dims = new int [ndims];

	      for (int i = 0; i < ndims; i++)
		dims[i] = dv.dims[i];
	    }
	}

      return *this;
    }

  ~dim_vector (void) { delete [] dims; }

  int length (void) const { return ndims; }

  int& elem (int i)
    {
      if (i >= ndims)
	resize (i+1);

      return dims[i];
    }

  int elem (int i) const { return i < ndims ? dims[i] : -1; }

  int& operator () (int i) { return elem (i); }

  int operator () (int i) const { return elem (i); }

  void resize (int n)
    {
      if (n > ndims)
	{
	  int *new_dims = new int [n];

	  for (int i = 0; i < ndims; i++)
	    new_dims[i] = dims[i];

	  for (int i = ndims; i < n; i++)
	    new_dims[i] = 0;

	  delete [] dims;

	  dims = new_dims;

	  ndims = n;
	}
      else
	ndims = n;
    }

  std::string str (void) const
    {
      OSSTREAM buf;

      for (int i = 0; i < ndims; i++)
	{
	  buf << dims[i];

	  if (i < ndims - 1)
	    buf << "x";
	}

      buf << OSSTREAM_ENDS;

      std::string retval = OSSTREAM_STR (buf);

      OSSTREAM_FREEZE (buf);

      return retval;
    }

  bool all_zero (void) const
    {
      bool retval = true;

      for (int i = 0; i < ndims; i++)
	{
	  if (dims[i] != 0)
	    {
	      retval = false;
	      break;
	    }
	}

      return retval;
    }

private:

  int ndims;
  int *dims;
};

static inline bool
operator == (const dim_vector& a, const dim_vector& b)
{
  bool retval = true;

  int a_len = a.length ();
  int b_len = b.length ();

  if (a_len != b_len)
    retval = false;
  else
    {
      for (int i = 0; i < a_len; i++)
	{
	  if (a(i) != b(i))
	    {
	      retval = false;
	      break;
	    }
	}
    }

  return retval;
}

static inline bool
operator != (const dim_vector& a, const dim_vector& b)
{
  return ! operator == (a, b);
}

#endif

/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/