メインページ   クラス階層   構成   ファイル一覧   構成メンバ   ファイルメンバ  

chMatrix.cc

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

Wed Dec 29 11:50:57 2004に生成されました。 doxygen1.2.18
SEO [PR] 爆速!無料ブログ 無料ホームページ開設 無料ライブ放送