help-octave
[Top][All Lists]
Advanced

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

Re: [octave] file i/o performance


From: Søren Hauberg
Subject: Re: [octave] file i/o performance
Date: Wed, 25 May 2005 17:27:43 +0200
User-agent: Mozilla Thunderbird 1.0.2 (X11/20050404)

Keith Goodman wrote:
[snip]
I would like a compiled version of strcmp. I use that a lot. I've
never used strtok---I thought the function name was a typo.
I had parts of strcmp implemented in C++, so I just finished it. For the tests I have performed this is about 40 times faster than the version shipped with Octave. Use it if you like...

/Søren

P.S. I'll send this to address@hidden as well
/*
Copyright (C) 2005 Søren Hauberg

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 of the License, 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; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

Author: Søren Hauberg <hauberg at gmail dot com>

2005-05-25 Søren Hauberg <hauberg at gmail dot com>
* Initial revision

*/

#include <octave/oct.h>
#include <octave/Cell.h>
#include <string>

using namespace std;

DEFUN_DLD(strcmp, args, ,
"-*- texinfo -*-\n\
@deftypefn {Function File} {} strcmp (@var{s1}, @var{s2})\n\
Compares two strings, returning 1 if they are the same, and 0 otherwise.\n\
\n\
@strong{Caution:}  For compatibility with @sc{Matlab}, Octave's strcmp\n\
function returns 1 if the strings are equal, and 0 otherwise.  This is\n\
just the opposite of the corresponding C library function.\n\
@end deftypefn\n\
")
{
        
        if (args.length() != 2) 
        {
                usage("strcmp (S, T)");
                return octave_value();
        } 

        if (args(0).is_string() && args(1).is_string()) 
        {
                const string s1 = args(0).string_value();
                const string s2 = args(1).string_value();
                return octave_value(s1 == s2);
        
        } else if ((args(0).is_string() && args(1).is_cell()) ||
                   (args(0).is_cell() && args(1).is_string()))
        {
                string str;
                Cell cell;
                if ( args(0).is_string() ) {
                        str  = args(0).string_value();
                        cell = args(1).cell_value();
                } else {
                        str  = args(1).string_value();
                        cell = args(0).cell_value();
                }
                //const dim_vector size = cell.dimensions;
                boolNDArray output(cell.dimensions);
                for (int i=0; i < cell.length(); i++) {
                        if (cell(i).is_string()) {
                                output(i) = (cell(i).string_value() == str);
                        } else {
                                error("Input cell may only contain strings.\n");
                                return octave_value();
                        }
                }
                return octave_value(output);
                
        } else if (args(0).is_cell() && args(1).is_cell()) 
        {
                const Cell cell1 = args(0).cell_value();
                const Cell cell2 = args(1).cell_value();
                const dim_vector size1 = cell1.dimensions;
                const dim_vector size2 = cell2.dimensions;
                if (size1 != size2) {
                        error("If two cell arrays are given, they must be of 
the same size.\n");
                        return octave_value();
                }
                
                boolNDArray output(size1);
                for (int i = 0; i < cell1.length(); i++) {
                        if (cell1(i).is_string() && cell2(i).is_string()) {
                                const string str1 = cell1(i).string_value();
                                const string str2 = cell2(i).string_value();
                                output(i) = (str1 == str2);
                        } else {
                                error("Input cell may only contain strings.\n");
                                return octave_value();
                        }
                }
                return octave_value(output);
                
        } else 
        {
                error("First and second arguments must be strings or cell 
arrays containing strings.\n");
                return octave_value();
        }
        
}


reply via email to

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