view libinterp/corefcn/Cell.cc @ 32660:f53ac65ffba6

maint: New method rwdata() as clearer alternative to fortran_vec(). * etc/NEWS.10.md: Announce new method and recommend its use in future code. * doc/interpreter/external.txi: Update Octave manual to use rwdata. * liboctave/array/Array.h (rwdata): New function prototype. * liboctave/array/Array.h (fortran_vec): Make inline function that calls rwdata(). * liboctave/array/DiagArray2.h (rwdata): New function prototype. * liboctave/array/DiagArray2.h (fortran_vec): Make inline function that calls rwdata(). * examples/code/fortrandemo.cc, libgui/graphics/QtHandlesUtils.cc, libinterp/corefcn/Cell.cc, libinterp/corefcn/__dsearchn__.cc, libinterp/corefcn/__ichol__.cc, libinterp/corefcn/__ilu__.cc, libinterp/corefcn/__lin_interpn__.cc, libinterp/corefcn/__magick_read__.cc, libinterp/corefcn/__pchip_deriv__.cc, libinterp/corefcn/amd.cc, libinterp/corefcn/data.cc, libinterp/corefcn/dot.cc, libinterp/corefcn/ellipj.cc, libinterp/corefcn/filter.cc, libinterp/corefcn/gcd.cc, libinterp/corefcn/gl-render.cc, libinterp/corefcn/graphics.cc, libinterp/corefcn/graphics.in.h, libinterp/corefcn/kron.cc, libinterp/corefcn/ls-mat4.cc, libinterp/corefcn/ls-mat5.cc, libinterp/corefcn/mex.cc, libinterp/corefcn/oct-map.cc, libinterp/corefcn/oct-stream.cc, libinterp/corefcn/ordqz.cc, libinterp/corefcn/ordschur.cc, libinterp/corefcn/perms.cc, libinterp/corefcn/psi.cc, libinterp/corefcn/quadcc.cc, libinterp/corefcn/qz.cc, libinterp/corefcn/rand.cc, libinterp/corefcn/sqrtm.cc, libinterp/corefcn/strfind.cc, libinterp/corefcn/symrcm.cc, libinterp/corefcn/tril.cc, libinterp/corefcn/typecast.cc, libinterp/corefcn/xdiv.cc, libinterp/dldfcn/__delaunayn__.cc, libinterp/dldfcn/__glpk__.cc, libinterp/dldfcn/__ode15__.cc, libinterp/dldfcn/__voronoi__.cc, libinterp/dldfcn/audioread.cc, libinterp/dldfcn/convhulln.cc, libinterp/octave-value/ov-base-int.cc, libinterp/octave-value/ov-bool-mat.cc, libinterp/octave-value/ov-ch-mat.cc, libinterp/octave-value/ov-cx-diag.cc, libinterp/octave-value/ov-cx-mat.cc, libinterp/octave-value/ov-flt-cx-diag.cc, libinterp/octave-value/ov-flt-cx-mat.cc, libinterp/octave-value/ov-flt-re-diag.cc, libinterp/octave-value/ov-flt-re-mat.cc, libinterp/octave-value/ov-intx.h, libinterp/octave-value/ov-java.cc, libinterp/octave-value/ov-perm.cc, libinterp/octave-value/ov-re-diag.cc, libinterp/octave-value/ov-re-mat.cc, libinterp/octave-value/ov-str-mat.cc, liboctave/array/Array-base.cc, liboctave/array/Array-util.cc, liboctave/array/CColVector.cc, liboctave/array/CDiagMatrix.cc, liboctave/array/CMatrix.cc, liboctave/array/CNDArray.cc, liboctave/array/CRowVector.cc, liboctave/array/CSparse.cc, liboctave/array/DiagArray2.h, liboctave/array/MArray.cc, liboctave/array/PermMatrix.cc, liboctave/array/Range.h, liboctave/array/Sparse.cc, liboctave/array/boolSparse.cc, liboctave/array/dColVector.cc, liboctave/array/dDiagMatrix.cc, liboctave/array/dMatrix.cc, liboctave/array/dNDArray.cc, liboctave/array/dRowVector.cc, liboctave/array/dSparse.cc, liboctave/array/fCColVector.cc, liboctave/array/fCDiagMatrix.cc, liboctave/array/fCMatrix.cc, liboctave/array/fCNDArray.cc, liboctave/array/fCRowVector.cc, liboctave/array/fColVector.cc, liboctave/array/fMatrix.cc, liboctave/array/fNDArray.cc, liboctave/array/fRowVector.cc, liboctave/array/idx-vector.cc, liboctave/numeric/CollocWt.cc, liboctave/numeric/DASPK.cc, liboctave/numeric/DASRT.cc, liboctave/numeric/DASSL.cc, liboctave/numeric/EIG.cc, liboctave/numeric/LSODE.cc, liboctave/numeric/Quad.cc, liboctave/numeric/aepbalance.cc, liboctave/numeric/bsxfun-defs.cc, liboctave/numeric/chol.cc, liboctave/numeric/eigs-base.cc, liboctave/numeric/fEIG.cc, liboctave/numeric/gepbalance.cc, liboctave/numeric/gsvd.cc, liboctave/numeric/hess.cc, liboctave/numeric/lu.cc, liboctave/numeric/oct-convn.cc, liboctave/numeric/oct-rand.cc, liboctave/numeric/qr.cc, liboctave/numeric/qrp.cc, liboctave/numeric/schur.cc, liboctave/numeric/sparse-dmsolve.cc, liboctave/numeric/sparse-lu.cc, liboctave/numeric/sparse-qr.cc, liboctave/numeric/svd.cc, liboctave/operators/mx-inlines.cc, liboctave/operators/mx-op-defs.h, liboctave/util/oct-base64.cc, liboctave/util/oct-binmap.h, liboctave/util/str-vec.cc: Replace calls to fortran_vec() with calls to rwdata().
author Rik <rik@octave.org>
date Wed, 27 Dec 2023 16:55:14 -0800
parents 2e484f9f1f18
children
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1999-2024 The Octave Project Developers
//
// See the file COPYRIGHT.md in the top-level directory of this
// distribution or <https://octave.org/copyright/>.
//
// 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 3 of the License, 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, see
// <https://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////

#if defined (HAVE_CONFIG_H)
#  include "config.h"
#endif

#include "idx-vector.h"

#include "Cell.h"
#include "error.h"
#include "errwarn.h"
#include "ovl.h"

Cell::Cell (const octave_value_list& ovl)
  : Array<octave_value> (ovl.cell_value ())
{ }

Cell::Cell (const string_vector& sv, bool trim)
  : Array<octave_value> ()
{
  octave_idx_type n = sv.numel ();

  if (n > 0)
    {
      resize (dim_vector (n, 1));

      for (octave_idx_type i = 0; i < n; i++)
        {
          std::string s = sv[i];

          if (trim)
            {
              std::size_t pos = s.find_last_not_of (' ');

              s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
            }

          elem (i, 0) = s;
        }
    }
}

Cell::Cell (const std::list<std::string>& sl)
  : Array<octave_value> ()
{
  octave_idx_type n = sl.size ();

  if (n > 0)
    {
      resize (dim_vector (n, 1));

      octave_value *dst = rwdata ();
      auto p = sl.begin ();

      for (octave_idx_type i = 0; i < n; i++)
        dst[i] = *p++;
    }
}

Cell::Cell (const Array<std::string>& sa)
  : Array<octave_value> (sa.dims ())
{
  octave_idx_type n = sa.numel ();

  octave_value *dst = rwdata ();
  const std::string *src = sa.data ();

  for (octave_idx_type i = 0; i < n; i++)
    dst[i] = src[i];
}

// Set size to DV, filling with [].  Then fill with as many elements of
// SV as possible.

Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim)
  : Array<octave_value> (dv, Matrix ())
{
  octave_idx_type n = sv.numel ();

  if (n > 0)
    {
      octave_idx_type m = numel ();

      octave_idx_type len = (n > m ? m : n);

      for (octave_idx_type i = 0; i < len; i++)
        {
          std::string s = sv[i];

          if (trim)
            {
              std::size_t pos = s.find_last_not_of (' ');

              s = (pos == std::string::npos) ? "" : s.substr (0, pos+1);
            }

          elem(i) = s;
        }
    }
}

bool
Cell::iscellstr () const
{
  bool retval = true;

  octave_idx_type n = numel ();

  for (octave_idx_type i = 0; i < n; i++)
    {
      if (! elem(i).is_string ())
        {
          retval = false;
          break;
        }
    }

  return retval;
}

Array<std::string>
Cell::cellstr_value () const
{
  Array<std::string> retval (dims ());

  octave_idx_type n = numel ();

  for (octave_idx_type i = 0; i < n; i++)
    retval.xelem (i) = elem (i).string_value ();

  return retval;
}

string_vector
Cell::string_vector_value () const
{
  octave_idx_type n = numel ();

  string_vector retval (n);

  for (octave_idx_type i = 0; i < n; i++)
    retval.xelem (i) = elem (i).string_value ();

  return retval;
}

Cell
Cell::index (const octave_value_list& idx_arg, bool resize_ok) const
{
  Cell retval;

  octave_idx_type n = idx_arg.length ();

  // If we catch an indexing error in index_vector, we flag an error
  // in index k.  Ensure it is the right value before each idx_vector
  // call.  Same variable as used in for loop in default case.

  octave_idx_type k = 0;

  try
    {
      switch (n)
        {
        case 0:
          warn_empty_index ("cell array");
          retval = *this;
          break;

        case 1:
          {
            octave::idx_vector i = idx_arg(0).index_vector ();

            retval = Array<octave_value>::index (i, resize_ok, Matrix ());
          }
          break;

        case 2:
          {
            octave::idx_vector i = idx_arg(0).index_vector ();

            k = 1;
            octave::idx_vector j = idx_arg(1).index_vector ();

            retval = Array<octave_value>::index (i, j, resize_ok, Matrix ());
          }
          break;

        default:
          {
            Array<octave::idx_vector> iv (dim_vector (n, 1));

            for (k = 0; k < n; k++)
              iv(k) = idx_arg(k).index_vector ();

            retval = Array<octave_value>::index (iv, resize_ok, Matrix ());
          }
          break;
        }
    }
  catch (octave::index_exception& ie)
    {
      // Rethrow to allow more info to be reported later.
      ie.set_pos_if_unset (n, k+1);
      throw;
    }

  return retval;
}

/*
%% This behavior is required for Matlab compatibility.
%!shared a
%! a = {"foo", "bar"};
%!assert (a(), a)
%!error <invalid empty index expression> a{}
*/

void
Cell::assign (const octave_value_list& idx_arg, const Cell& rhs,
              const octave_value& fill_val)

{
  octave_idx_type len = idx_arg.length ();

  Array<octave::idx_vector> ra_idx (dim_vector (len, 1));

  for (octave_idx_type i = 0; i < len; i++)
    {
      try
        {
          ra_idx(i) = idx_arg(i).index_vector ();
        }
      catch (octave::index_exception& ie)
        {
          // Rethrow to allow more info to be reported later.
          ie.set_pos (len, i+1);
          throw;
        }
    }

  Array<octave_value>::assign (ra_idx, rhs, fill_val);
}

void
Cell::delete_elements (const octave_value_list& idx_arg)

{
  octave_idx_type len = idx_arg.length ();

  Array<octave::idx_vector> ra_idx (dim_vector (len, 1));

  for (octave_idx_type i = 0; i < len; i++)
    try
      {
        ra_idx.xelem (i) = idx_arg(i).index_vector ();
      }
    catch (octave::index_exception& ie)
      {
        // Rethrow to allow more info to be reported later.
        ie.set_pos (len, i+1);
        throw;
      }

  Array<octave_value>::delete_elements (ra_idx);
}

octave_idx_type
Cell::nnz () const
{
  err_wrong_type_arg ("nnz", "cell array");
}

/*
%!error <wrong type argument 'cell array'> nnz ({0, 1, 2})
%!error <wrong type argument 'cell array'> nnz (cell ())
%!error <wrong type argument 'cell array'> nnz ({"foo", "bar"})
*/

Cell
Cell::column (octave_idx_type i) const
{
  Cell retval;

  if (ndims () > 2)
    error ("Cell::column: requires 2-D cell array");

  if (i < 0 || i >= cols ())
    error ("invalid column selection");

  octave_idx_type nr = rows ();

  retval.resize (dim_vector (nr, 1));

  for (octave_idx_type j = 0; j < nr; j++)
    retval.xelem (j) = elem (j, i);

  return retval;
}

Cell
Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx)
{
  return insert (rb, ra_idx);
}

Cell&
Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c)
{
  Array<octave_value>::insert (a, r, c);
  return *this;
}

Cell&
Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx)
{
  Array<octave_value>::insert (a, ra_idx);
  return *this;
}

Cell
Cell::map (ctype_mapper fcn) const
{
  Cell retval (dims ());
  octave_value *r = retval.rwdata ();

  const octave_value *p = data ();

  for (octave_idx_type i = 0; i < numel (); i++)
    r[i] = ((p++)->*fcn) ();

  return retval;
}

octave_value
Cell::resize_fill_value () const
{
  static octave_value rfv = octave_value (Matrix ());
  return rfv;
}

Cell
Cell::diag (octave_idx_type k) const
{
  return Array<octave_value>::diag (k);
}

Cell
Cell::diag (octave_idx_type m, octave_idx_type n) const
{
  return Array<octave_value>::diag (m, n);
}