view libinterp/corefcn/dispatch.h @ 19010:3fb030666878 draft default tip dspies

Added special-case logical-indexing function * logical-index.h (New file) : Logical-indexing function. May be called on octave_value types via call_bool_index * nz-iterators.h : Add base-class nz_iterator for iterator types. Array has template bool for whether to internally store row-col or compute on the fly Add skip_ahead method which skips forward to the next nonzero after its argument Add flat_index for computing octave_idx_type index of current position (with assertion failure in the case of overflow) Move is_zero to separate file * ov-base-diag.cc, ov-base-mat.cc, ov-base-sparse.cc, ov-perm.cc (do_index_op): Add call to call_bool_index in logical-index.h * Array.h : Move forward-declaration for array_iterator to separate header file * dim-vector.cc (dim_max): Refers to idx-bounds.h (max_idx) * array-iter-decl.h (New file): Header file for forward declaration of array-iterator * direction.h : Add constants fdirc and bdirc to avoid having to reconstruct them * dv-utils.h, dv-utils.cc (New files) : Utility functions for querying and constructing dim-vectors * idx-bounds.h (New file) : Utility constants and functions for determining whether things will overflow the maximum allowed bounds * interp-idx.h (New function : to_flat_idx) : Converts row-col pair to linear index of octave_idx_type * is-zero.h (New file) : Function for determining whether an element is zero * logical-index.tst : Add tests for correct return-value dimensions and large sparse matrix behavior
author David Spies <dnspies@gmail.com>
date Fri, 25 Jul 2014 13:39:31 -0600
parents 80ca3b05d77c
children
line wrap: on
line source

/*

Copyright (C) 2014 David Spies

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
<http://www.gnu.org/licenses/>.

*/

#if !defined (octave_dispatch_h)
#define octave_dispatch_h 1

#include <string>

#include "gripes.h"
#include "oct-obj.h"

// This function takes a templated functor as a template-template argument
// and calls it back with the matrix-type corresponding to arg.
//
// This is handy when you wish to write a templated function for dealing
// with many different matrix types and you don't want to explicitly have
// to list out all the different types your function can deal with and
// how to call it with each one.
//
// It is expected that the functor operator() takes two arguments:
//
// 1. The value being unwrapped from arg
// and
// 2. A struct containing any other information the function needs
// (ie other arguments, nargin, nargout etc.)
//
// The return value is an octave_value_list containing the return value
// for the function
//
// In addition to the above two parameters, the dispatch function itself
// also takes a string argument which is the name of the function being
// called.  This argument is only used for error-reporting
//
// It is highly recommended you always use this function as you can be
// sure dispatch will throw a compiler exception if you forget to handle
// a particular type.
//
// For an example of how to call dispatch, see the "find" function in
// libinterp/corefcn/find.cc
template<template<typename > class fun, typename Inf>
octave_value_list
dispatch (const octave_value& arg, const Inf& info, const std::string& funname)
{
  octave_value_list retval;

  if (arg.is_bool_type ())
    {
      if (arg.is_sparse_type ())
        {
          SparseBoolMatrix v = arg.sparse_bool_matrix_value ();

          if (! error_state)
            retval = fun<SparseBoolMatrix> () (v, info);
        }
      else
        {
          boolNDArray v = arg.bool_array_value ();

          if (! error_state)
            retval = fun<boolNDArray> () (v, info);
        }
    }
  else if (arg.is_integer_type ())
    {
#define DO_INT_BRANCH(INTT) \
      if (arg.is_ ## INTT ## _type ()) \
        { \
          INTT ## NDArray v = arg.INTT ## _array_value (); \
          \
          if (! error_state) \
            retval = fun<INTT ## NDArray> () (v, info);\
        } else

      DO_INT_BRANCH (int8)
      DO_INT_BRANCH (int16)
      DO_INT_BRANCH (int32)
      DO_INT_BRANCH (int64)
      DO_INT_BRANCH (uint8)
      DO_INT_BRANCH (uint16)
      DO_INT_BRANCH (uint32)
      DO_INT_BRANCH (uint64)
        panic_impossible ();
#undef DO_INT_BRANCH
    }
  else if (arg.is_sparse_type ())
    {
      if (arg.is_real_type ())
        {
          SparseMatrix v = arg.sparse_matrix_value ();

          if (! error_state)
            retval = fun<SparseMatrix> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          SparseComplexMatrix v = arg.sparse_complex_matrix_value ();

          if (! error_state)
            retval = fun<SparseComplexMatrix> () (v, info);
        }
      else
        gripe_wrong_type_arg (funname, arg);
    }
  else if (arg.is_diag_matrix ())
    {
      if (arg.is_real_type ())
        {
          DiagMatrix v = arg.diag_matrix_value ();
          if (! error_state)
            retval = fun<DiagMatrix> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          ComplexDiagMatrix v = arg.complex_diag_matrix_value ();
          if (! error_state)
            retval = fun<ComplexDiagMatrix> () (v, info);
        }
    }
  else if (arg.is_perm_matrix ())
    {
      PermMatrix v = arg.perm_matrix_value ();

      if (! error_state)
        retval = fun<PermMatrix> () (v, info);
    }
  else if (arg.is_string ())
    {
      charNDArray v = arg.char_array_value ();

      if (! error_state)
        retval = fun<charNDArray> () (v, info);
    }
  else if (arg.is_single_type ())
    {
      if (arg.is_real_type ())
        {
          FloatNDArray v = arg.float_array_value ();

          if (! error_state)
            retval = fun<FloatNDArray> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          FloatComplexNDArray v = arg.float_complex_array_value ();

          if (! error_state)
            retval = fun<FloatComplexNDArray> () (v, info);
        }
    }
  else if (arg.is_real_type ())
    {
      NDArray v = arg.array_value ();

      if (! error_state)
        retval = fun<NDArray> () (v, info);
    }
  else if (arg.is_complex_type ())
    {
      ComplexNDArray v = arg.complex_array_value ();

      if (! error_state)
        retval = fun<ComplexNDArray> () (v, info);
    }
  else
    gripe_wrong_type_arg (funname, arg);

  return retval;
}

#endif