GNU Octave  4.0.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-2015 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 
42 bool
44 {
45  if (rows () != a.rows () || cols () != a.cols ())
46  return 0;
47 
48  return mx_inline_equal (length (), data (), a.data ());
49 }
50 
51 bool
53 {
54  return !(*this == a);
55 }
56 
59 {
60  if (s)
61  {
62  octave_idx_type s_len = strlen (s);
63 
64  if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ())
65  {
66  (*current_liboctave_error_handler) ("range error for insert");
67  return *this;
68  }
69 
70  for (octave_idx_type i = 0; i < s_len; i++)
71  elem (r, c+i) = s[i];
72  }
73  return *this;
74 }
75 
78 {
79  Array<char>::insert (a, r, c);
80  return *this;
81 }
82 
83 std::string
85 {
86  std::string retval;
87 
88  octave_idx_type nr = rows ();
89  octave_idx_type nc = cols ();
90 
91  if (r == 0 && (nr == 0 || nc == 0))
92  return retval;
93 
94  if (r < 0 || r >= nr)
95  {
96  (*current_liboctave_error_handler) ("range error for row_as_string");
97  return retval;
98  }
99 
100  retval.resize (nc, '\0');
101 
102  for (octave_idx_type i = 0; i < nc; i++)
103  retval[i] = elem (r, i);
104 
105  if (strip_ws)
106  {
107  while (--nc >= 0)
108  {
109  char c = retval[nc];
110  if (c && c != ' ')
111  break;
112  }
113 
114  retval.resize (nc+1);
115  }
116 
117  return retval;
118 }
119 
123 {
124  if (r1 > r2) { std::swap (r1, r2); }
125  if (c1 > c2) { std::swap (c1, c2); }
126 
127  octave_idx_type new_r = r2 - r1 + 1;
128  octave_idx_type new_c = c2 - c1 + 1;
129 
130  charMatrix result (new_r, new_c);
131 
132  for (octave_idx_type j = 0; j < new_c; j++)
133  for (octave_idx_type i = 0; i < new_r; i++)
134  result.elem (i, j) = elem (r1+i, c1+j);
135 
136  return result;
137 }
138 
141 
142 SM_CMP_OPS (char, charMatrix)
143 SM_BOOL_OPS (char, charMatrix)
144 
145 MM_CMP_OPS (charMatrix, charMatrix)
146 MM_BOOL_OPS (charMatrix, charMatrix)
bool operator==(const charMatrix &a) const
Definition: chMatrix.cc:43
#define MM_BOOL_OPS(M1, M2)
Definition: mx-op-defs.h:210
#define SM_BOOL_OPS(S, M)
Definition: mx-op-defs.h:167
char & elem(octave_idx_type n)
Definition: Array.h:380
#define MS_CMP_OPS(M, S)
Definition: mx-op-defs.h:107
std::string row_as_string(octave_idx_type, bool strip_ws=false) const
Definition: chMatrix.cc:84
octave_idx_type rows(void) const
Definition: Array.h:313
Array< T > & insert(const Array< T > &a, const Array< octave_idx_type > &idx)
Insert an array into another at a specified position.
Definition: Array.cc:1591
charMatrix & insert(const char *s, octave_idx_type r, octave_idx_type c)
Definition: chMatrix.cc:58
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type r2
bool operator!=(const charMatrix &a) const
Definition: chMatrix.cc:52
const char * data(void) const
Definition: Array.h:479
octave_idx_type length(void) const
Number of elements in the array.
Definition: Array.h:267
charMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
Definition: chMatrix.cc:121
#define MS_BOOL_OPS(M, S)
Definition: mx-op-defs.h:124
#define MM_CMP_OPS(M1, M2)
Definition: mx-op-defs.h:193
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
bool mx_inline_equal(size_t n, const T1 *x, const T2 *y)
Definition: mx-inlines.cc:441
octave_idx_type cols(void) const
Definition: Array.h:321
#define SM_CMP_OPS(S, M)
Definition: mx-op-defs.h:150