view libinterp/parse-tree/pt-tm-const.cc @ 31607:aac27ad79be6 stable

maint: Re-indent code after switch to using namespace macros. * build-env.h, build-env.in.cc, Cell.h, __betainc__.cc, __eigs__.cc, __ftp__.cc, __ichol__.cc, __ilu__.cc, __isprimelarge__.cc, __magick_read__.cc, __pchip_deriv__.cc, amd.cc, base-text-renderer.cc, base-text-renderer.h, besselj.cc, bitfcns.cc, bsxfun.cc, c-file-ptr-stream.h, call-stack.cc, call-stack.h, ccolamd.cc, cellfun.cc, chol.cc, colamd.cc, dasrt.cc, data.cc, debug.cc, defaults.cc, defaults.h, det.cc, display.cc, display.h, dlmread.cc, dynamic-ld.cc, dynamic-ld.h, ellipj.cc, environment.cc, environment.h, error.cc, error.h, errwarn.h, event-manager.cc, event-manager.h, event-queue.cc, event-queue.h, fcn-info.cc, fcn-info.h, fft.cc, fft2.cc, file-io.cc, filter.cc, find.cc, ft-text-renderer.cc, ft-text-renderer.h, gcd.cc, gl-render.cc, gl-render.h, gl2ps-print.cc, gl2ps-print.h, graphics-toolkit.cc, graphics-toolkit.h, graphics.cc, gsvd.cc, gtk-manager.cc, gtk-manager.h, help.cc, help.h, hook-fcn.cc, hook-fcn.h, input.cc, input.h, interpreter-private.cc, interpreter-private.h, interpreter.cc, interpreter.h, inv.cc, jsondecode.cc, jsonencode.cc, latex-text-renderer.cc, latex-text-renderer.h, load-path.cc, load-path.h, load-save.cc, load-save.h, lookup.cc, ls-hdf5.cc, ls-mat4.cc, ls-mat5.cc, lsode.cc, lu.cc, mappers.cc, matrix_type.cc, max.cc, mex.cc, mexproto.h, mxarray.h, mxtypes.in.h, oct-errno.in.cc, oct-hdf5-types.cc, oct-hist.cc, oct-hist.h, oct-map.cc, oct-map.h, oct-opengl.h, oct-prcstrm.h, oct-process.cc, oct-process.h, oct-stdstrm.h, oct-stream.cc, oct-stream.h, oct-strstrm.h, octave-default-image.h, ordqz.cc, ordschur.cc, pager.cc, pager.h, pinv.cc, pow2.cc, pr-output.cc, psi.cc, qr.cc, quadcc.cc, rand.cc, regexp.cc, settings.cc, settings.h, sighandlers.cc, sighandlers.h, sparse-xpow.cc, sqrtm.cc, stack-frame.cc, stack-frame.h, stream-euler.cc, strfns.cc, svd.cc, syminfo.cc, syminfo.h, symrcm.cc, symrec.cc, symrec.h, symscope.cc, symscope.h, symtab.cc, symtab.h, sysdep.cc, sysdep.h, text-engine.cc, text-engine.h, text-renderer.cc, text-renderer.h, time.cc, toplev.cc, typecast.cc, url-handle-manager.cc, url-handle-manager.h, urlwrite.cc, utils.cc, utils.h, variables.cc, variables.h, xdiv.cc, __delaunayn__.cc, __init_fltk__.cc, __init_gnuplot__.cc, __ode15__.cc, __voronoi__.cc, audioread.cc, convhulln.cc, gzip.cc, cdef-class.cc, cdef-class.h, cdef-fwd.h, cdef-manager.cc, cdef-manager.h, cdef-method.cc, cdef-method.h, cdef-object.cc, cdef-object.h, cdef-package.cc, cdef-package.h, cdef-property.cc, cdef-property.h, cdef-utils.cc, cdef-utils.h, ov-base-diag.cc, ov-base-int.cc, ov-base-mat.cc, ov-base-mat.h, ov-base-scalar.cc, ov-base.cc, ov-base.h, ov-bool-mat.cc, ov-bool-mat.h, ov-bool-sparse.cc, ov-bool.cc, ov-builtin.h, ov-cell.cc, ov-ch-mat.cc, ov-class.cc, ov-class.h, ov-classdef.cc, ov-classdef.h, ov-complex.cc, ov-cx-diag.cc, ov-cx-mat.cc, ov-cx-sparse.cc, ov-dld-fcn.cc, ov-dld-fcn.h, ov-fcn-handle.cc, ov-fcn-handle.h, ov-fcn.h, ov-float.cc, ov-flt-complex.cc, ov-flt-cx-diag.cc, ov-flt-cx-mat.cc, ov-flt-re-diag.cc, ov-flt-re-mat.cc, ov-flt-re-mat.h, ov-intx.h, ov-java.cc, ov-lazy-idx.cc, ov-legacy-range.cc, ov-magic-int.cc, ov-mex-fcn.cc, ov-mex-fcn.h, ov-null-mat.cc, ov-perm.cc, ov-range.cc, ov-re-diag.cc, ov-re-mat.cc, ov-re-mat.h, ov-re-sparse.cc, ov-scalar.cc, ov-str-mat.cc, ov-struct.cc, ov-typeinfo.cc, ov-typeinfo.h, ov-usr-fcn.cc, ov-usr-fcn.h, ov.cc, ov.h, ovl.h, octave.cc, octave.h, op-b-sbm.cc, op-bm-sbm.cc, op-cs-scm.cc, op-fm-fcm.cc, op-fs-fcm.cc, op-s-scm.cc, op-scm-cs.cc, op-scm-s.cc, op-sm-cs.cc, ops.h, anon-fcn-validator.cc, anon-fcn-validator.h, bp-table.cc, bp-table.h, comment-list.cc, comment-list.h, filepos.h, lex.h, oct-lvalue.cc, oct-lvalue.h, parse.h, profiler.cc, profiler.h, pt-anon-scopes.cc, pt-anon-scopes.h, pt-arg-list.cc, pt-arg-list.h, pt-args-block.cc, pt-args-block.h, pt-array-list.cc, pt-array-list.h, pt-assign.cc, pt-assign.h, pt-binop.cc, pt-binop.h, pt-bp.cc, pt-bp.h, pt-cbinop.cc, pt-cbinop.h, pt-cell.cc, pt-cell.h, pt-check.cc, pt-check.h, pt-classdef.cc, pt-classdef.h, pt-cmd.h, pt-colon.cc, pt-colon.h, pt-const.cc, pt-const.h, pt-decl.cc, pt-decl.h, pt-eval.cc, pt-eval.h, pt-except.cc, pt-except.h, pt-exp.cc, pt-exp.h, pt-fcn-handle.cc, pt-fcn-handle.h, pt-id.cc, pt-id.h, pt-idx.cc, pt-idx.h, pt-jump.h, pt-loop.cc, pt-loop.h, pt-mat.cc, pt-mat.h, pt-misc.cc, pt-misc.h, pt-pr-code.cc, pt-pr-code.h, pt-select.cc, pt-select.h, pt-spmd.cc, pt-spmd.h, pt-stmt.cc, pt-stmt.h, pt-tm-const.cc, pt-tm-const.h, pt-unop.cc, pt-unop.h, pt-walk.cc, pt-walk.h, pt.cc, pt.h, token.cc, token.h, Range.cc, Range.h, idx-vector.cc, idx-vector.h, range-fwd.h, CollocWt.cc, CollocWt.h, aepbalance.cc, aepbalance.h, chol.cc, chol.h, gepbalance.cc, gepbalance.h, gsvd.cc, gsvd.h, hess.cc, hess.h, lo-mappers.cc, lo-mappers.h, lo-specfun.cc, lo-specfun.h, lu.cc, lu.h, oct-convn.cc, oct-convn.h, oct-fftw.cc, oct-fftw.h, oct-norm.cc, oct-norm.h, oct-rand.cc, oct-rand.h, oct-spparms.cc, oct-spparms.h, qr.cc, qr.h, qrp.cc, qrp.h, randgamma.cc, randgamma.h, randmtzig.cc, randmtzig.h, randpoisson.cc, randpoisson.h, schur.cc, schur.h, sparse-chol.cc, sparse-chol.h, sparse-lu.cc, sparse-lu.h, sparse-qr.cc, sparse-qr.h, svd.cc, svd.h, child-list.cc, child-list.h, dir-ops.cc, dir-ops.h, file-ops.cc, file-ops.h, file-stat.cc, file-stat.h, lo-sysdep.cc, lo-sysdep.h, lo-sysinfo.cc, lo-sysinfo.h, mach-info.cc, mach-info.h, oct-env.cc, oct-env.h, oct-group.cc, oct-group.h, oct-password.cc, oct-password.h, oct-syscalls.cc, oct-syscalls.h, oct-time.cc, oct-time.h, oct-uname.cc, oct-uname.h, action-container.cc, action-container.h, base-list.h, cmd-edit.cc, cmd-edit.h, cmd-hist.cc, cmd-hist.h, f77-fcn.h, file-info.cc, file-info.h, lo-array-errwarn.cc, lo-array-errwarn.h, lo-hash.cc, lo-hash.h, lo-ieee.h, lo-regexp.cc, lo-regexp.h, lo-utils.cc, lo-utils.h, oct-base64.cc, oct-base64.h, oct-glob.cc, oct-glob.h, oct-inttypes.h, oct-mutex.cc, oct-mutex.h, oct-refcount.h, oct-shlib.cc, oct-shlib.h, oct-sparse.cc, oct-sparse.h, oct-string.h, octave-preserve-stream-state.h, pathsearch.cc, pathsearch.h, quit.cc, quit.h, unwind-prot.cc, unwind-prot.h, url-transfer.cc, url-transfer.h: Re-indent code after switch to using namespace macros.
author Rik <rik@octave.org>
date Thu, 01 Dec 2022 18:02:15 -0800
parents e88a07dec498
children 597f3ee61a48
line wrap: on
line source

////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1996-2022 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 "oct-locbuf.h"
#include "quit.h"

#include "data.h"
#include "defun.h"
#include "error.h"
#include "errwarn.h"
#include "oct-map.h"
#include "ovl.h"
#include "pt-arg-list.h"
#include "pt-bp.h"
#include "pt-eval.h"
#include "pt-exp.h"
#include "pt-mat.h"
#include "pt-tm-const.h"
#include "utils.h"
#include "ov.h"
#include "variables.h"

#include "ov-cx-mat.h"
#include "ov-flt-cx-mat.h"
#include "ov-re-sparse.h"
#include "ov-cx-sparse.h"

OCTAVE_NORETURN static
void
eval_error (const char *msg, const dim_vector& x, const dim_vector& y)
{
  error ("%s (%s vs %s)", msg, x.str ().c_str (), y.str ().c_str ());
}

OCTAVE_BEGIN_NAMESPACE(octave)

void tm_row_const::cellify (void)
{
  bool elt_changed = false;

  for (auto& elt : m_values)
    {
      octave_quit ();

      if (! elt.iscell ())
        {
          elt_changed = true;

          if (elt.isempty ())
            elt = Cell ();
          else
            elt = Cell (elt);
        }
    }

  if (! elt_changed)
    return;

  bool first_elem = true;

  for (const auto& val : m_values)
    {
      octave_quit ();

      dim_vector this_elt_dv = val.dims ();

      if (! this_elt_dv.zero_by_zero ())
        {
          if (first_elem)
            {
              first_elem = false;
              m_dv = this_elt_dv;
            }
          else if (! m_dv.hvcat (this_elt_dv, 1))
            eval_error ("horizontal dimensions mismatch", m_dv, this_elt_dv);
        }
    }
}

void tm_row_const::init_element (const octave_value& val, bool& first_elem)
{
  std::string this_elt_class_name
    = val.isobject () ? "class" : val.class_name ();

  m_class_name = get_concat_class (m_class_name, this_elt_class_name);

  dim_vector this_elt_dv = val.dims ();

  if (! this_elt_dv.zero_by_zero ())
    {
      m_all_empty = false;

      if (first_elem)
        {
          if (val.isstruct ())
            m_first_elem_is_struct = true;

          first_elem = false;
        }
    }
  else if (val.iscell ())
    first_elem = false;

  m_values.push_back (val);

  if (m_all_strings && ! val.is_string ())
    m_all_strings = false;

  if (m_all_sq_strings && ! val.is_sq_string ())
    m_all_sq_strings = false;

  if (m_all_dq_strings && ! val.is_dq_string ())
    m_all_dq_strings = false;

  if (! m_some_strings && val.is_string ())
    m_some_strings = true;

  if (m_all_real && ! val.isreal ())
    m_all_real = false;

  if (m_all_complex && ! (val.iscomplex () || val.isreal ()))
    m_all_complex = false;

  if (! m_any_cell && val.iscell ())
    m_any_cell = true;

  if (! m_any_sparse && val.issparse ())
    m_any_sparse = true;

  if (! m_any_class && val.isobject ())
    m_any_class = true;

  // Special treatment of sparse matrices to avoid out-of-memory error
  m_all_1x1 = m_all_1x1 && ! val.issparse () && val.numel () == 1;
}

void tm_row_const::init (const tree_argument_list& row, tree_evaluator& tw)
{
  bool first_elem = true;

  for (auto *elt : row)
    {
      octave_quit ();

      octave_value tmp = elt->evaluate (tw);

      if (tmp.is_undefined ())
        error ("undefined element in matrix list");

      if (tmp.is_cs_list ())
        {
          octave_value_list tlst = tmp.list_value ();

          for (octave_idx_type i = 0; i < tlst.length (); i++)
            {
              octave_quit ();

              init_element (tlst(i), first_elem);
            }
        }
      else
        init_element (tmp, first_elem);
    }

  if (m_any_cell && ! m_any_class && ! m_first_elem_is_struct)
    cellify ();

  first_elem = true;

  for (const auto& val : m_values)
    {
      octave_quit ();

      dim_vector this_elt_dv = val.dims ();

      if (! this_elt_dv.zero_by_zero ())
        {
          m_all_empty = false;

          if (first_elem)
            {
              first_elem = false;
              m_dv = this_elt_dv;
            }
          else if ((! m_any_class) && (! m_dv.hvcat (this_elt_dv, 1)))
            eval_error ("horizontal dimensions mismatch", m_dv, this_elt_dv);
        }
    }
}

octave_value tm_const::concat (char string_fill_char) const
{
  if (m_tm_rows.empty ())
    return Matrix ();

  // Try to speed up the common cases.

  std::string result_type = m_class_name;

  if (m_any_class)
    return class_concat ();
  else if (result_type == "double")
    {
      if (m_any_sparse)
        {
          if (m_all_real)
            return sparse_array_concat<SparseMatrix> ();
          else
            return sparse_array_concat<SparseComplexMatrix> ();
        }
      else
        {
          if (m_all_real)
            return array_concat<NDArray> ();
          else
            return array_concat<ComplexNDArray> ();
        }
    }
  else if (result_type == "single")
    {
      if (m_all_real)
        return array_concat<FloatNDArray> ();
      else
        return array_concat<FloatComplexNDArray> ();
    }
  else if (result_type == "char")
    {
      if (! m_all_strings)
        warn_implicit_conversion ("Octave:num-to-str",
                                  "numeric", result_type);
      else
        maybe_warn_string_concat (m_all_dq_strings, m_all_sq_strings);

      return char_array_concat (string_fill_char);
    }
  else if (result_type == "logical")
    {
      if (m_any_sparse)
        return sparse_array_concat<SparseBoolMatrix> ();
      else
        return array_concat<boolNDArray> ();
    }
  else if (result_type == "int8")
    return array_concat<int8NDArray> ();
  else if (result_type == "int16")
    return array_concat<int16NDArray> ();
  else if (result_type == "int32")
    return array_concat<int32NDArray> ();
  else if (result_type == "int64")
    return array_concat<int64NDArray> ();
  else if (result_type == "uint8")
    return array_concat<uint8NDArray> ();
  else if (result_type == "uint16")
    return array_concat<uint16NDArray> ();
  else if (result_type == "uint32")
    return array_concat<uint32NDArray> ();
  else if (result_type == "uint64")
    return array_concat<uint64NDArray> ();
  else if (result_type == "cell")
    return array_concat<Cell> ();
  else if (result_type == "struct")
    {
      if (m_all_1x1)
        return map_concat<octave_scalar_map> ();
      else
        return map_concat<octave_map> ();
    }
  else
    return generic_concat ();
}

void tm_const::init (const tree_matrix& tm)
{
  bool first_elem = true;
  bool first_elem_is_struct = false;

  // Just eval and figure out if what we have is complex or all strings.
  // We can't check columns until we know that this is a numeric matrix --
  // collections of strings can have elements of different lengths.

  for (const auto *elt : tm)
    {
      octave_quit ();

      tm_row_const row (*elt, m_evaluator);

      if (first_elem)
        {
          first_elem_is_struct = row.first_elem_struct_p ();

          first_elem = false;
        }

      if (row.empty ())
        continue;

      if (m_all_strings && ! row.all_strings_p ())
        m_all_strings = false;

      if (m_all_sq_strings && ! row.all_sq_strings_p ())
        m_all_sq_strings = false;

      if (m_all_dq_strings && ! row.all_dq_strings_p ())
        m_all_dq_strings = false;

      if (! m_some_strings && row.some_strings_p ())
        m_some_strings = true;

      if (m_all_real && ! row.all_real_p ())
        m_all_real = false;

      if (m_all_complex && ! row.all_complex_p ())
        m_all_complex = false;

      if (m_all_empty && ! row.all_empty_p ())
        m_all_empty = false;

      if (! m_any_cell && row.any_cell_p ())
        m_any_cell = true;

      if (! m_any_sparse && row.any_sparse_p ())
        m_any_sparse = true;

      if (! m_any_class && row.any_class_p ())
        m_any_class = true;

      m_all_1x1 = m_all_1x1 && row.all_1x1_p ();

      m_tm_rows.push_back (row);
    }

  if (m_any_cell && ! m_any_class && ! first_elem_is_struct)
    {
      for (auto& elt : m_tm_rows)
        {
          octave_quit ();

          elt.cellify ();
        }
    }

  first_elem = true;

  for (const auto& elt : m_tm_rows)
    {
      octave_quit ();

      octave_idx_type this_elt_nr = elt.rows ();
      octave_idx_type this_elt_nc = elt.cols ();

      std::string this_elt_class_name = elt.class_name ();
      m_class_name = get_concat_class (m_class_name, this_elt_class_name);

      dim_vector this_elt_dv = elt.dims ();

      m_all_empty = false;

      if (first_elem)
        {
          first_elem = false;

          m_dv = this_elt_dv;
        }
      else if (m_all_strings && m_dv.ndims () == 2
               && this_elt_dv.ndims () == 2)
        {
          // This is Octave's specialty.
          // Character matrices support rows of unequal length.
          if (m_dv.any_zero ())
            {
              // Empty existing element (bug #52542).
              // Replace empty element with non-empty one.
              m_dv = this_elt_dv;
            }
          else
            {
              if (this_elt_nc > cols ())
                m_dv(1) = this_elt_nc;
              m_dv(0) += this_elt_nr;
            }
        }
      else if ((! m_any_class) && (! m_dv.hvcat (this_elt_dv, 0)))
        eval_error ("vertical dimensions mismatch", m_dv, this_elt_dv);
    }
}

octave_value tm_const::char_array_concat (char string_fill_char) const
{
  char type = (m_all_dq_strings ? '"' : '\'');

  charNDArray result (m_dv, string_fill_char);

  array_concat_internal<charNDArray> (result);

  return octave_value (result, type);
}

octave_value tm_const::class_concat (void) const
{
  octave_value retval;

  octave_value_list rows (m_tm_rows.size (), octave_value ());

  octave_idx_type j = 0;
  for (const auto& tmrc : m_tm_rows)
    {
      octave_quit ();

      if (tmrc.length () == 1)
        rows(j++) = *(tmrc.begin ());
      else
        {
          octave_value_list row (tmrc.length (), octave_value ());

          octave_idx_type i = 0;
          for (const auto& elt : tmrc)
            row(i++) = elt;

          rows(j++) = do_class_concat (row, "horzcat", 1);
        }
    }

  if (rows.length () == 1)
    retval = rows(0);
  else
    retval = do_class_concat (rows, "vertcat", 0);

  return retval;
}

octave_value tm_const::generic_concat (void) const
{
  // The line below might seem crazy, since we take a copy of the
  // first argument, resize it to be empty and then resize it to be
  // full.  This is done since it means that there is no recopying of
  // data, as would happen if we used a single resize.  It should be
  // noted that resize operation is also significantly slower than the
  // cat_op function, so it makes sense to have an empty matrix and
  // copy all data.
  //
  // We might also start with a empty octave_value using
  //
  //   ctmp = type_info::lookup_type (tmp.begin() -> begin() -> type_name());
  //
  // and then directly resize.  However, for some types there might be
  // some additional setup needed, and so this should be avoided.

  octave_value ctmp;

  // Find the first non-empty object

  if (m_any_sparse)
    {
      // Start with sparse matrix to avoid issues memory issues with
      // things like [ones(1,4),sprandn(1e8,4,1e-4)]

      if (m_all_real)
        ctmp = octave_sparse_matrix ().resize (m_dv);
      else
        ctmp = octave_sparse_complex_matrix ().resize (m_dv);
    }
  else
    {
      for (const auto& row : m_tm_rows)
        {
          octave_quit ();

          for (const auto& elt : row)
            {
              octave_quit ();

              ctmp = elt;

              if (! ctmp.all_zero_dims ())
                goto found_non_empty;
            }
        }

      ctmp = (*(m_tm_rows.begin () -> begin ()));

    found_non_empty:

      if (! m_all_empty)
        ctmp = ctmp.resize (dim_vector (0, 0)).resize (m_dv);
    }

  // Now, extract the values from the individual elements and insert
  // them in the result matrix.

  interpreter& interp = m_evaluator.get_interpreter ();

  type_info& ti = interp.get_type_info ();

  int dv_len = m_dv.ndims ();
  octave_idx_type ntmp = (dv_len > 1 ? dv_len : 2);
  Array<octave_idx_type> ra_idx (dim_vector (ntmp, 1), 0);

  for (const auto& row : m_tm_rows)
    {
      octave_quit ();

      for (const auto& elt : row)
        {
          octave_quit ();

          if (elt.isempty ())
            continue;

          ctmp = cat_op (ti, ctmp, elt, ra_idx);

          ra_idx (1) += elt.columns ();
        }

      ra_idx (0) += row.rows ();
      ra_idx (1) = 0;
    }

  octave_value retval = ctmp;

  // If some elements are strings, force the result to be a string.

  if (m_some_strings && ! retval.is_string ())
    retval = retval.convert_to_str ();

  return retval;
}

// The result is passed as a parameter to this function so that the
// char_array_concat function can create the array externally.
// Otherwise, we would need a specialization of this function for
// character arrays just to handle string_fill_char.

template <typename TYPE>
void tm_const::array_concat_internal (TYPE& result) const
{
  octave_idx_type r = 0;
  octave_idx_type c = 0;

  for (const auto& row : m_tm_rows)
    {
      // Skip empty arrays to allow looser rules.
      if (row.dims ().any_zero ())
        continue;

      for (const auto& elt : row)
        {
          octave_quit ();

          TYPE ra = octave_value_extract<TYPE> (elt);

          // Skip empty arrays to allow looser rules.

          if (! ra.isempty ())
            {
              result.insert (ra, r, c);

              c += ra.columns ();
            }
        }

      r += row.rows ();
      c = 0;
    }
}

template <typename TYPE>
TYPE tm_const::array_concat (void) const
{
  typedef typename TYPE::element_type ELT_T;

  if (m_dv.any_zero ())
    return TYPE (m_dv);

  if (m_tm_rows.size () == 1)
    {
      // If possible, forward the operation to liboctave.
      // Single row.
      const tm_row_const& row = m_tm_rows.front ();
      if (! (equal_types<ELT_T, char>::value
             || equal_types<ELT_T, octave_value>::value)
          && row.all_1x1_p ())
        {
          // Optimize all scalars case.
          TYPE result (m_dv);
          panic_unless (static_cast<std::size_t> (result.numel ())
                        == row.length ());
          octave_idx_type i = 0;
          for (const auto& elt : row)
            result(i++) = octave_value_extract<ELT_T> (elt);

          return result;
        }

      octave_idx_type ncols = row.length ();
      octave_idx_type i = 0;
      OCTAVE_LOCAL_BUFFER (TYPE, array_list, ncols);

      for (const auto& elt : row)
        {
          octave_quit ();

          array_list[i++] = octave_value_extract<TYPE> (elt);
        }

      return TYPE::cat (-2, ncols, array_list);
    }
  else
    {
      TYPE result (m_dv);
      array_concat_internal<TYPE> (result);
      return result;
    }
}

template <typename TYPE>
TYPE tm_const::sparse_array_concat (void) const
{
  if (m_dv.any_zero ())
    return TYPE (m_dv);

  // Sparse matrices require preallocation for efficient indexing; besides,
  // only horizontal concatenation can be efficiently handled by indexing.
  // So we just cat all rows through liboctave, then cat the final column.
  octave_idx_type nrows = m_tm_rows.size ();
  octave_idx_type j = 0;
  OCTAVE_LOCAL_BUFFER (TYPE, sparse_row_list, nrows);
  for (const auto& row : m_tm_rows)
    {
      octave_idx_type ncols = row.length ();
      octave_idx_type i = 0;
      OCTAVE_LOCAL_BUFFER (TYPE, sparse_list, ncols);

      for (auto& elt : row)
        {
          octave_quit ();

          sparse_list[i] = octave_value_extract<TYPE> (elt);
          i++;
        }

      TYPE stmp = TYPE::cat (-2, ncols, sparse_list);
      sparse_row_list[j] = stmp;
      j++;
    }

  return TYPE::cat (-1, nrows, sparse_row_list);
}

template <typename MAP>
octave_map tm_const::map_concat (void) const
{
  if (m_dv.any_zero ())
    return octave_map (m_dv);

  octave_idx_type nrows = m_tm_rows.size ();
  octave_idx_type j = 0;
  OCTAVE_LOCAL_BUFFER (octave_map, map_row_list, nrows);
  for (const auto& row : m_tm_rows)
    {
      octave_idx_type ncols = row.length ();
      octave_idx_type i = 0;
      OCTAVE_LOCAL_BUFFER (MAP, map_list, ncols);

      for (auto& elt : row)
        {
          octave_quit ();

          map_list[i] = octave_value_extract<MAP> (elt);
          i++;
        }

      octave_map mtmp = octave_map::cat (-2, ncols, map_list);
      map_row_list[j] = mtmp;
      j++;
    }

  return octave_map::cat (-1, nrows, map_row_list);
}

OCTAVE_END_NAMESPACE(octave)

/*
## test concatenation with all zero matrices
%!assert ([ "" 65*ones(1,10) ], "AAAAAAAAAA")
%!assert ([ 65*ones(1,10) "" ], "AAAAAAAAAA")

%!test
%! c = {"foo"; "bar"; "bazoloa"};
%! assert ([c; "a"; "bc"; "def"], {"foo"; "bar"; "bazoloa"; "a"; "bc"; "def"});

%!assert (class ([int64(1), int64(1)]), "int64")
%!assert (class ([int64(1), int32(1)]), "int64")
%!assert (class ([int64(1), int16(1)]), "int64")
%!assert (class ([int64(1), int8(1)]), "int64")
%!assert (class ([int64(1), uint64(1)]), "int64")
%!assert (class ([int64(1), uint32(1)]), "int64")
%!assert (class ([int64(1), uint16(1)]), "int64")
%!assert (class ([int64(1), uint8(1)]), "int64")
%!assert (class ([int64(1), single(1)]), "int64")
%!assert (class ([int64(1), double(1)]), "int64")
%!assert (class ([int64(1), cell(1)]), "cell")
%!assert (class ([int64(1), true]), "int64")
%!assert (class ([int64(1), "a"]), "char")

%!assert (class ([int32(1), int64(1)]), "int32")
%!assert (class ([int32(1), int32(1)]), "int32")
%!assert (class ([int32(1), int16(1)]), "int32")
%!assert (class ([int32(1), int8(1)]), "int32")
%!assert (class ([int32(1), uint64(1)]), "int32")
%!assert (class ([int32(1), uint32(1)]), "int32")
%!assert (class ([int32(1), uint16(1)]), "int32")
%!assert (class ([int32(1), uint8(1)]), "int32")
%!assert (class ([int32(1), single(1)]), "int32")
%!assert (class ([int32(1), double(1)]), "int32")
%!assert (class ([int32(1), cell(1)]), "cell")
%!assert (class ([int32(1), true]), "int32")
%!assert (class ([int32(1), "a"]), "char")

%!assert (class ([int16(1), int64(1)]), "int16")
%!assert (class ([int16(1), int32(1)]), "int16")
%!assert (class ([int16(1), int16(1)]), "int16")
%!assert (class ([int16(1), int8(1)]), "int16")
%!assert (class ([int16(1), uint64(1)]), "int16")
%!assert (class ([int16(1), uint32(1)]), "int16")
%!assert (class ([int16(1), uint16(1)]), "int16")
%!assert (class ([int16(1), uint8(1)]), "int16")
%!assert (class ([int16(1), single(1)]), "int16")
%!assert (class ([int16(1), double(1)]), "int16")
%!assert (class ([int16(1), cell(1)]), "cell")
%!assert (class ([int16(1), true]), "int16")
%!assert (class ([int16(1), "a"]), "char")

%!assert (class ([int8(1), int64(1)]), "int8")
%!assert (class ([int8(1), int32(1)]), "int8")
%!assert (class ([int8(1), int16(1)]), "int8")
%!assert (class ([int8(1), int8(1)]), "int8")
%!assert (class ([int8(1), uint64(1)]), "int8")
%!assert (class ([int8(1), uint32(1)]), "int8")
%!assert (class ([int8(1), uint16(1)]), "int8")
%!assert (class ([int8(1), uint8(1)]), "int8")
%!assert (class ([int8(1), single(1)]), "int8")
%!assert (class ([int8(1), double(1)]), "int8")
%!assert (class ([int8(1), cell(1)]), "cell")
%!assert (class ([int8(1), true]), "int8")
%!assert (class ([int8(1), "a"]), "char")

%!assert (class ([uint64(1), int64(1)]), "uint64")
%!assert (class ([uint64(1), int32(1)]), "uint64")
%!assert (class ([uint64(1), int16(1)]), "uint64")
%!assert (class ([uint64(1), int8(1)]), "uint64")
%!assert (class ([uint64(1), uint64(1)]), "uint64")
%!assert (class ([uint64(1), uint32(1)]), "uint64")
%!assert (class ([uint64(1), uint16(1)]), "uint64")
%!assert (class ([uint64(1), uint8(1)]), "uint64")
%!assert (class ([uint64(1), single(1)]), "uint64")
%!assert (class ([uint64(1), double(1)]), "uint64")
%!assert (class ([uint64(1), cell(1)]), "cell")
%!assert (class ([uint64(1), true]), "uint64")
%!assert (class ([uint64(1), "a"]), "char")

%!assert (class ([uint32(1), int64(1)]), "uint32")
%!assert (class ([uint32(1), int32(1)]), "uint32")
%!assert (class ([uint32(1), int16(1)]), "uint32")
%!assert (class ([uint32(1), int8(1)]), "uint32")
%!assert (class ([uint32(1), uint64(1)]), "uint32")
%!assert (class ([uint32(1), uint32(1)]), "uint32")
%!assert (class ([uint32(1), uint16(1)]), "uint32")
%!assert (class ([uint32(1), uint8(1)]), "uint32")
%!assert (class ([uint32(1), single(1)]), "uint32")
%!assert (class ([uint32(1), double(1)]), "uint32")
%!assert (class ([uint32(1), cell(1)]), "cell")
%!assert (class ([uint32(1), true]), "uint32")
%!assert (class ([uint32(1), "a"]), "char")

%!assert (class ([uint16(1), int64(1)]), "uint16")
%!assert (class ([uint16(1), int32(1)]), "uint16")
%!assert (class ([uint16(1), int16(1)]), "uint16")
%!assert (class ([uint16(1), int8(1)]), "uint16")
%!assert (class ([uint16(1), uint64(1)]), "uint16")
%!assert (class ([uint16(1), uint32(1)]), "uint16")
%!assert (class ([uint16(1), uint16(1)]), "uint16")
%!assert (class ([uint16(1), uint8(1)]), "uint16")
%!assert (class ([uint16(1), single(1)]), "uint16")
%!assert (class ([uint16(1), double(1)]), "uint16")
%!assert (class ([uint16(1), cell(1)]), "cell")
%!assert (class ([uint16(1), true]), "uint16")
%!assert (class ([uint16(1), "a"]), "char")

%!assert (class ([uint8(1), int64(1)]), "uint8")
%!assert (class ([uint8(1), int32(1)]), "uint8")
%!assert (class ([uint8(1), int16(1)]), "uint8")
%!assert (class ([uint8(1), int8(1)]), "uint8")
%!assert (class ([uint8(1), uint64(1)]), "uint8")
%!assert (class ([uint8(1), uint32(1)]), "uint8")
%!assert (class ([uint8(1), uint16(1)]), "uint8")
%!assert (class ([uint8(1), uint8(1)]), "uint8")
%!assert (class ([uint8(1), single(1)]), "uint8")
%!assert (class ([uint8(1), double(1)]), "uint8")
%!assert (class ([uint8(1), cell(1)]), "cell")
%!assert (class ([uint8(1), true]), "uint8")
%!assert (class ([uint8(1), "a"]), "char")

%!assert (class ([single(1), int64(1)]), "int64")
%!assert (class ([single(1), int32(1)]), "int32")
%!assert (class ([single(1), int16(1)]), "int16")
%!assert (class ([single(1), int8(1)]), "int8")
%!assert (class ([single(1), uint64(1)]), "uint64")
%!assert (class ([single(1), uint32(1)]), "uint32")
%!assert (class ([single(1), uint16(1)]), "uint16")
%!assert (class ([single(1), uint8(1)]), "uint8")
%!assert (class ([single(1), single(1)]), "single")
%!assert (class ([single(1), double(1)]), "single")
%!assert (class ([single(1), cell(1)]), "cell")
%!assert (class ([single(1), true]), "single")
%!assert (class ([single(1), "a"]), "char")

%!assert (class ([double(1), int64(1)]), "int64")
%!assert (class ([double(1), int32(1)]), "int32")
%!assert (class ([double(1), int16(1)]), "int16")
%!assert (class ([double(1), int8(1)]), "int8")
%!assert (class ([double(1), uint64(1)]), "uint64")
%!assert (class ([double(1), uint32(1)]), "uint32")
%!assert (class ([double(1), uint16(1)]), "uint16")
%!assert (class ([double(1), uint8(1)]), "uint8")
%!assert (class ([double(1), single(1)]), "single")
%!assert (class ([double(1), double(1)]), "double")
%!assert (class ([double(1), cell(1)]), "cell")
%!assert (class ([double(1), true]), "double")
%!assert (class ([double(1), "a"]), "char")

%!assert (class ([cell(1), int64(1)]), "cell")
%!assert (class ([cell(1), int32(1)]), "cell")
%!assert (class ([cell(1), int16(1)]), "cell")
%!assert (class ([cell(1), int8(1)]), "cell")
%!assert (class ([cell(1), uint64(1)]), "cell")
%!assert (class ([cell(1), uint32(1)]), "cell")
%!assert (class ([cell(1), uint16(1)]), "cell")
%!assert (class ([cell(1), uint8(1)]), "cell")
%!assert (class ([cell(1), single(1)]), "cell")
%!assert (class ([cell(1), double(1)]), "cell")
%!assert (class ([cell(1), cell(1)]), "cell")
%!assert (class ([cell(1), true]), "cell")
%!assert (class ([cell(1), "a"]), "cell")

%!assert (class ([true, int64(1)]), "int64")
%!assert (class ([true, int32(1)]), "int32")
%!assert (class ([true, int16(1)]), "int16")
%!assert (class ([true, int8(1)]), "int8")
%!assert (class ([true, uint64(1)]), "uint64")
%!assert (class ([true, uint32(1)]), "uint32")
%!assert (class ([true, uint16(1)]), "uint16")
%!assert (class ([true, uint8(1)]), "uint8")
%!assert (class ([true, single(1)]), "single")
%!assert (class ([true, double(1)]), "double")
%!assert (class ([true, cell(1)]), "cell")
%!assert (class ([true, true]), "logical")
%!assert (class ([true, "a"]), "char")

%!assert (class (["a", int64(1)]), "char")
%!assert (class (["a", int32(1)]), "char")
%!assert (class (["a", int16(1)]), "char")
%!assert (class (["a", int8(1)]), "char")
%!assert (class (["a", int64(1)]), "char")
%!assert (class (["a", int32(1)]), "char")
%!assert (class (["a", int16(1)]), "char")
%!assert (class (["a", int8(1)]), "char")
%!assert (class (["a", single(1)]), "char")
%!assert (class (["a", double(1)]), "char")
%!assert (class (["a", cell(1)]), "cell")
%!assert (class (["a", true]), "char")
%!assert (class (["a", "a"]), "char")

%!assert (class ([cell(1), struct("foo", "bar")]), "cell")
%!error [struct("foo", "bar"), cell(1)]

%!test <*39041> assert (class ([cell(0), struct()]), "cell")
%!test <51086> assert (class ([struct(), cell(0)]), "struct")

%!assert ([,1], 1)
%!assert ([1,], 1)
%!assert ([,1,], 1)
%!assert ([,1,;;], 1)
%!assert ([,1,;,;], 1)

%!assert ([1,1], ones (1, 2))
%!assert ([,1,1], ones (1, 2))
%!assert ([1,1,], ones (1, 2))
%!assert ([,1,1,], ones (1, 2))
%!assert ([,1,1,;;], ones (1, 2))
%!assert ([,1,1,;,;], ones (1, 2))
%!assert ([,;,1,1], ones (1, 2))

%!assert ([1;1], ones (2, 1))
%!assert ([1,;1], ones (2, 1))
%!assert ([1,;,;1], ones (2, 1))

%!error eval ("[,,]")
%!error eval ("[,,;,]")
%!error eval ("[,;,,;,]")

%!assert (isnull ([,]))
%!assert (isnull ([;]))
%!assert (isnull ([;;]))
%!assert (isnull ([;,;]))
%!assert (isnull ([,;,;,]))

## Undefined elements.
%!function my_undef ()
%!endfunction
%!
%!shared es
%! es = struct ("a", {});
%!
%!assert <*58695> ([1; es.a; 3], [1; 3])
%!test <*58695>
%! fail ("undefined element in matrix list", "[1; my_undef(), 3]");
%!
%!assert <*58695> ([es.a; es.a; 3], 3)
%!test <*58695>
%! fail ("undefined element in matrix list", "[my_undef(); my_undef(); 3]")
*/