GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
chMatrix.cc
Go to the documentation of this file.
1 // Matrix manipulations.
2 /*
3 
4 Copyright (C) 1995-2013 John W. Eaton
5 Copyright (C) 2010 VZLU Prague
6 
7 This file is part of Octave.
8 
9 Octave is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 3 of the License, or (at your
12 option) any later version.
13 
14 Octave is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with Octave; see the file COPYING. If not, see
21 <http://www.gnu.org/licenses/>.
22 
23 */
24 
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 
29 #include <cstring>
30 
31 #include <iostream>
32 #include <string>
33 
34 #include "lo-error.h"
35 #include "str-vec.h"
36 #include "mx-base.h"
37 #include "mx-inlines.cc"
38 #include "mx-op-defs.h"
39 
40 // charMatrix class.
41 
43  : Array<char> ()
44 {
45  octave_idx_type nc = 1;
46  octave_idx_type nr = 1;
47 
48  resize (nr, nc);
49 
50  elem (0, 0) = c;
51 }
52 
53 charMatrix::charMatrix (const char *s)
54  : Array<char> ()
55 {
56  octave_idx_type nc = s ? strlen (s) : 0;
57  octave_idx_type nr = s && nc > 0 ? 1 : 0;
58 
59  resize (nr, nc);
60 
61  for (octave_idx_type i = 0; i < nc; i++)
62  elem (0, i) = s[i];
63 }
64 
65 charMatrix::charMatrix (const std::string& s)
66  : Array<char> ()
67 {
68  octave_idx_type nc = s.length ();
69  octave_idx_type nr = nc > 0 ? 1 : 0;
70 
71  resize (nr, nc);
72 
73  for (octave_idx_type i = 0; i < nc; i++)
74  elem (0, i) = s[i];
75 }
76 
77 charMatrix::charMatrix (const string_vector& s, char fill_value)
78  : Array<char> (dim_vector (s.length (), s.max_length ()), fill_value)
79 {
80  octave_idx_type nr = rows ();
81 
82  for (octave_idx_type i = 0; i < nr; i++)
83  {
84  const std::string si = s(i);
85  octave_idx_type nc = si.length ();
86  for (octave_idx_type j = 0; j < nc; j++)
87  elem (i, j) = si[j];
88  }
89 }
90 
91 bool
93 {
94  if (rows () != a.rows () || cols () != a.cols ())
95  return 0;
96 
97  return mx_inline_equal (length (), data (), a.data ());
98 }
99 
100 bool
102 {
103  return !(*this == a);
104 }
105 
106 charMatrix&
108 {
109  if (s)
110  {
111  octave_idx_type s_len = strlen (s);
112 
113  if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ())
114  {
115  (*current_liboctave_error_handler) ("range error for insert");
116  return *this;
117  }
118 
119  for (octave_idx_type i = 0; i < s_len; i++)
120  elem (r, c+i) = s[i];
121  }
122  return *this;
123 }
124 
125 charMatrix&
127 {
128  Array<char>::insert (a, r, c);
129  return *this;
130 }
131 
132 std::string
134 {
135  std::string retval;
136 
137  octave_idx_type nr = rows ();
138  octave_idx_type nc = cols ();
139 
140  if (r == 0 && (nr == 0 || nc == 0))
141  return retval;
142 
143  if (r < 0 || r >= nr)
144  {
145  (*current_liboctave_error_handler) ("range error for row_as_string");
146  return retval;
147  }
148 
149  retval.resize (nc, '\0');
150 
151  for (octave_idx_type i = 0; i < nc; i++)
152  retval[i] = elem (r, i);
153 
154  if (strip_ws)
155  {
156  while (--nc >= 0)
157  {
158  char c = retval[nc];
159  if (c && c != ' ')
160  break;
161  }
162 
163  retval.resize (nc+1);
164  }
165 
166  return retval;
167 }
168 
172 {
173  if (r1 > r2) { std::swap (r1, r2); }
174  if (c1 > c2) { std::swap (c1, c2); }
175 
176  octave_idx_type new_r = r2 - r1 + 1;
177  octave_idx_type new_c = c2 - c1 + 1;
178 
179  charMatrix result (new_r, new_c);
180 
181  for (octave_idx_type j = 0; j < new_c; j++)
182  for (octave_idx_type i = 0; i < new_r; i++)
183  result.elem (i, j) = elem (r1+i, c1+j);
184 
185  return result;
186 }
187 
190 {
191  return Array<char>::diag (k);
192 }
193 
194 // FIXME: Do these really belong here? Maybe they should be in a base class?
195 
197 charMatrix::all (int dim) const
198 {
199  return do_mx_red_op<bool, char> (*this, dim, mx_inline_all);
200 }
201 
203 charMatrix::any (int dim) const
204 {
205  return do_mx_red_op<bool, char> (*this, dim, mx_inline_any);
206 }
207 
208 MS_CMP_OPS (charMatrix, char)
209 MS_BOOL_OPS (charMatrix, char)
210 
211 SM_CMP_OPS (char, charMatrix)
212 SM_BOOL_OPS (char, charMatrix)
213 
214 MM_CMP_OPS (charMatrix, charMatrix)
215 MM_BOOL_OPS (charMatrix, charMatrix)