chMatrix.cc

Go to the documentation of this file.
00001 // Matrix manipulations.
00002 /*
00003 
00004 Copyright (C) 1995-2012 John W. Eaton
00005 Copyright (C) 2010 VZLU Prague
00006 
00007 This file is part of Octave.
00008 
00009 Octave is free software; you can redistribute it and/or modify it
00010 under the terms of the GNU General Public License as published by the
00011 Free Software Foundation; either version 3 of the License, or (at your
00012 option) any later version.
00013 
00014 Octave is distributed in the hope that it will be useful, but WITHOUT
00015 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00016 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00017 for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with Octave; see the file COPYING.  If not, see
00021 <http://www.gnu.org/licenses/>.
00022 
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #include <config.h>
00027 #endif
00028 
00029 #include <cstring>
00030 
00031 #include <iostream>
00032 #include <string>
00033 
00034 #include "lo-error.h"
00035 #include "str-vec.h"
00036 #include "mx-base.h"
00037 #include "mx-inlines.cc"
00038 #include "mx-op-defs.h"
00039 
00040 // charMatrix class.
00041 
00042 charMatrix::charMatrix (char c)
00043   : Array<char> ()
00044 {
00045   octave_idx_type nc = 1;
00046   octave_idx_type nr = 1;
00047 
00048   resize (nr, nc);
00049 
00050   elem (0, 0) = c;
00051 }
00052 
00053 charMatrix::charMatrix (const char *s)
00054   : Array<char> ()
00055 {
00056   octave_idx_type nc = s ? strlen (s) : 0;
00057   octave_idx_type nr = s && nc > 0 ? 1 : 0;
00058 
00059   resize (nr, nc);
00060 
00061   for (octave_idx_type i = 0; i < nc; i++)
00062     elem (0, i) = s[i];
00063 }
00064 
00065 charMatrix::charMatrix (const std::string& s)
00066   : Array<char> ()
00067 {
00068   octave_idx_type nc = s.length ();
00069   octave_idx_type nr = nc > 0 ? 1 : 0;
00070 
00071   resize (nr, nc);
00072 
00073   for (octave_idx_type i = 0; i < nc; i++)
00074     elem (0, i) = s[i];
00075 }
00076 
00077 charMatrix::charMatrix (const string_vector& s, char fill_value)
00078   : Array<char> (dim_vector (s.length (), s.max_length ()), fill_value)
00079 {
00080   octave_idx_type nr = rows ();
00081 
00082   for (octave_idx_type i = 0; i < nr; i++)
00083     {
00084       const std::string si = s(i);
00085       octave_idx_type nc = si.length ();
00086       for (octave_idx_type j = 0; j < nc; j++)
00087         elem (i, j) = si[j];
00088     }
00089 }
00090 
00091 bool
00092 charMatrix::operator == (const charMatrix& a) const
00093 {
00094   if (rows () != a.rows () || cols () != a.cols ())
00095     return 0;
00096 
00097   return mx_inline_equal (length (), data (), a.data ());
00098 }
00099 
00100 bool
00101 charMatrix::operator != (const charMatrix& a) const
00102 {
00103   return !(*this == a);
00104 }
00105 
00106 charMatrix&
00107 charMatrix::insert (const char *s, octave_idx_type r, octave_idx_type c)
00108 {
00109   if (s)
00110     {
00111       octave_idx_type s_len = strlen (s);
00112 
00113       if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ())
00114         {
00115           (*current_liboctave_error_handler) ("range error for insert");
00116           return *this;
00117         }
00118 
00119       for (octave_idx_type i = 0; i < s_len; i++)
00120         elem (r, c+i) = s[i];
00121     }
00122   return *this;
00123 }
00124 
00125 charMatrix&
00126 charMatrix::insert (const charMatrix& a, octave_idx_type r, octave_idx_type c)
00127 {
00128   Array<char>::insert (a, r, c);
00129   return *this;
00130 }
00131 
00132 std::string
00133 charMatrix::row_as_string (octave_idx_type r, bool strip_ws) const
00134 {
00135   std::string retval;
00136 
00137   octave_idx_type nr = rows ();
00138   octave_idx_type nc = cols ();
00139 
00140   if (r == 0 && (nr == 0 || nc == 0))
00141     return retval;
00142 
00143   if (r < 0 || r >= nr)
00144     {
00145       (*current_liboctave_error_handler) ("range error for row_as_string");
00146       return retval;
00147     }
00148 
00149   retval.resize (nc, '\0');
00150 
00151   for (octave_idx_type i = 0; i < nc; i++)
00152     retval[i] = elem (r, i);
00153 
00154   if (strip_ws)
00155     {
00156       while (--nc >= 0)
00157         {
00158           char c = retval[nc];
00159           if (c && c != ' ')
00160             break;
00161         }
00162 
00163       retval.resize (nc+1);
00164     }
00165 
00166   return retval;
00167 }
00168 
00169 charMatrix
00170 charMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
00171 {
00172   if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; }
00173   if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; }
00174 
00175   octave_idx_type new_r = r2 - r1 + 1;
00176   octave_idx_type new_c = c2 - c1 + 1;
00177 
00178   charMatrix result (new_r, new_c);
00179 
00180   for (octave_idx_type j = 0; j < new_c; j++)
00181     for (octave_idx_type i = 0; i < new_r; i++)
00182       result.elem (i, j) = elem (r1+i, c1+j);
00183 
00184   return result;
00185 }
00186 
00187 charMatrix
00188 charMatrix::diag (octave_idx_type k) const
00189 {
00190   return Array<char>::diag (k);
00191 }
00192 
00193 // FIXME Do these really belong here?  Maybe they should be
00194 // in a base class?
00195 
00196 boolMatrix
00197 charMatrix::all (int dim) const
00198 {
00199   return do_mx_red_op<bool, char> (*this, dim, mx_inline_all);
00200 }
00201 
00202 boolMatrix
00203 charMatrix::any (int dim) const
00204 {
00205   return do_mx_red_op<bool, char> (*this, dim, mx_inline_any);
00206 }
00207 
00208 MS_CMP_OPS (charMatrix, char)
00209 MS_BOOL_OPS (charMatrix, char)
00210 
00211 SM_CMP_OPS (char, charMatrix)
00212 SM_BOOL_OPS (char, charMatrix)
00213 
00214 MM_CMP_OPS (charMatrix, charMatrix)
00215 MM_BOOL_OPS (charMatrix, charMatrix)
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines