octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ version of regexprep.cc


From: David Bateman
Subject: Re: C++ version of regexprep.cc
Date: Tue, 02 May 2006 13:25:54 +0200
User-agent: Thunderbird 1.5 (Windows/20051201)

Paul Kienzle wrote:
David,

octave now goes quickly through the regular expression portion of the code.

I haven't yet confirmed that the results are consistent with matlab.

The next portion involves for loops such as the following:

  tag = cell(number_of_tags,4);
  for i=1:number_of_tags
   tag{i,1} = xml(tag_start(i):tag_end(i))
  end

which for 10000 tags is slow.

Are there octave routines for splitting/joining strings into cells
which are fast?

- Paul

Paul,

Hey, I'm on holidays at the moment, and so have a little time. What about the attached implementation of mat2cell? With this you should be able to repalce the above code with

tag = cell(number_of_tags,4);
tag{:,1} = mat2cell (xml, 1, tag_end - tag_start);

John, do you want mat2cell committed as well?

Regards
David



/*

Copyright (C) 2006 David Bateman

This program 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.

This program 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 this program; see the file COPYING.  If not, write to the Free
Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

In addition to the terms of the GPL, you are permitted to link
this program with any Open Source program, as defined by the
Open Source Initiative (www.opensource.org)

*/

#include <octave/config.h>
#include <octave/oct.h>
#include <octave/Cell.h>
#include <octave/ov-str-mat.h>
#include <octave/ov-colon.h>

DEFUN_DLD (mat2cell, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Loadable Function} address@hidden =} num2cell (@var{a}, @var{m}, 
@var{n})\n\
@deftypefnx {Loadable Function} address@hidden =} num2cell (@var{a}, @var{d1}, 
@var{d2}, @dots{})\n\
@deftypefnx {Loadable Function} address@hidden =} num2cell (@var{a}, @var{r})\n\
Converts the matrix @var{a} to a cell array @var{b}. If @var{a} is 2-D, then\n\
@code{sum (@var{m}) == size (@var{a}, 1)} and  @code{sum (@var{n}) ==\n\
size (@var{a}, 2)}. Similarly, if @var{a} in multi-dimensional and the\n\
number of dimensional arguments is equal to the dimensions of @var{a}.\n\
\n\
Given a single dimensional argument @var{r}, the other dimensional\n\
arguments are assumed to equal @code{size (@var{a},@var{i})}.\n\
@end deftypefn\n\
@seealso{num2cell,cell2mat}")
{
  int nargin = args.length();
  octave_value retval;

  if (nargin < 2)
    usage ("mat2cell");
  else
    {
      dim_vector dv = args(0).dims();
      dim_vector new_dv;
      new_dv.resize(dv.length());
      
      if (nargin > 2)
        {
          if (nargin - 1 != dv.length())
            error ("mat2cell: Incorrect number of dimensions");
          else
            {
              for (octave_idx_type j = 0; j < dv.length(); j++)
                {
                  ColumnVector darg = ColumnVector (args(j+1).vector_value 
                                                    (false, true));

                  double sumdarg = 0.;
                  for (octave_idx_type i = 0; i < darg.length(); i++)
                    {
                      if (darg(i) >= 0)
                        sumdarg += darg(i);
                      else
                        {
                          error ("mat2cell: invalid dimensional argument");
                          break;
                        }
                    }

                  if (sumdarg != dv(j))
                    error ("mat2cell: inconsistent dimensions");

                  if (error_state)
                    break;

                  new_dv(j) = darg.length();
                }
            }

          if (! error_state)
            {
              octave_value_list lst (new_dv.length(), octave_value());
              Cell ret (new_dv);
              octave_idx_type nel = new_dv.numel();
              octave_idx_type ntot = 1;

              for (int j = 0; j < new_dv.length()-1; j++)
                ntot *= new_dv(j);

              for (octave_idx_type i = 0; i <  nel; i++)
                {
                  octave_idx_type n = ntot;
                  octave_idx_type ii = i;
                  for (octave_idx_type j =  new_dv.length() - 1;  j >= 0; j--)
                    {
                      ColumnVector d = ColumnVector (args(j+1).vector_value 
                                                     (false, true));

                      double idx = 0.;
                      for (octave_idx_type k = 0; k < ii/n; k++)
                        idx += d(k);
                      lst (j) = Range(idx + 1, idx + d(ii/n));
                      ii = ii % n;
                      if (j != 0)
                        n /= new_dv(j-1);
                    }
                  ret(i) = args(0).do_index_op(lst, 0);
                  if (error_state)
                    break;
                }
          
              if (!error_state)
                retval = ret;
            }
        }
      else
        {
          ColumnVector d = ColumnVector (args(1).vector_value 
                                         (false, true));

          double sumd = 0.;
          for (octave_idx_type i = 0; i < d.length(); i++)
            {
              if (d(i) >= 0)
                sumd += d(i);
              else
                {
                  error ("mat2cell: invalid dimensional argument");
                  break;
                }
            }

          if (sumd != dv(0))
            error ("mat2cell: inconsistent dimensions");

          new_dv(0) = d.length();
          for (octave_idx_type i = 1; i < dv.length(); i++)
            new_dv(i) = 1;

          if (! error_state)
            {
              octave_value_list lst (new_dv.length(), octave_value());
              Cell ret (new_dv);

              for (octave_idx_type i = 1; i < new_dv.length(); i++)
                lst (i) = Range (1., static_cast<double>(dv(i)));
              
              double idx = 0.;
              for (octave_idx_type i = 0; i <  new_dv(0); i++)
                {
                  lst(0) = Range(idx + 1., idx + d(i));
                  ret(i) = args(0).do_index_op(lst, 0);
                  idx += d(i);
                  if (error_state)
                    break;
                }
          
              if (!error_state)
                retval = ret;
            }
        }
    }

  return retval;
}
          
/*
;;; Local Variables: ***
;;; mode: C++ ***
;;; End: ***
*/

reply via email to

[Prev in Thread] Current Thread [Next in Thread]