Mercurial > hg > octave-nkf
annotate liboctave/chMatrix.cc @ 12283:0664a7ddd944 release-3-4-x
README.MacOS: Spelling corrections.
author | Ben Abbott <bpabbott@mac.com> |
---|---|
date | Fri, 28 Jan 2011 22:27:17 -0500 |
parents | 12df7854fa7c |
children | 0c86ae6f7c34 |
rev | line source |
---|---|
1993 | 1 // Matrix manipulations. |
1573 | 2 /* |
3 | |
11523 | 4 Copyright (C) 1995-2011 John W. Eaton |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10362
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
1573 | 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 | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
1573 | 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 | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
1573 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
7048 | 29 #include <cstring> |
30 | |
3503 | 31 #include <iostream> |
1728 | 32 #include <string> |
33 | |
1573 | 34 #include "lo-error.h" |
2349 | 35 #include "str-vec.h" |
1573 | 36 #include "mx-base.h" |
37 #include "mx-inlines.cc" | |
8774
b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
Jaroslav Hajek <highegg@gmail.com>
parents:
8750
diff
changeset
|
38 #include "mx-op-defs.h" |
1573 | 39 |
40 // charMatrix class. | |
41 | |
3189 | 42 charMatrix::charMatrix (char c) |
10351
5150ceb4dbb4
base charMatrix and boolMatrix on Array<char>
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
43 : Array<char> () |
3189 | 44 { |
5275 | 45 octave_idx_type nc = 1; |
46 octave_idx_type nr = 1; | |
3189 | 47 |
48 resize (nr, nc); | |
49 | |
50 elem (0, 0) = c; | |
51 } | |
52 | |
1573 | 53 charMatrix::charMatrix (const char *s) |
10351
5150ceb4dbb4
base charMatrix and boolMatrix on Array<char>
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
54 : Array<char> () |
1573 | 55 { |
5275 | 56 octave_idx_type nc = s ? strlen (s) : 0; |
57 octave_idx_type nr = s && nc > 0 ? 1 : 0; | |
2613 | 58 |
59 resize (nr, nc); | |
60 | |
5275 | 61 for (octave_idx_type i = 0; i < nc; i++) |
1573 | 62 elem (0, i) = s[i]; |
63 } | |
64 | |
3504 | 65 charMatrix::charMatrix (const std::string& s) |
10351
5150ceb4dbb4
base charMatrix and boolMatrix on Array<char>
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
66 : Array<char> () |
1733 | 67 { |
5275 | 68 octave_idx_type nc = s.length (); |
69 octave_idx_type nr = nc > 0 ? 1 : 0; | |
2613 | 70 |
71 resize (nr, nc); | |
72 | |
5275 | 73 for (octave_idx_type i = 0; i < nc; i++) |
1733 | 74 elem (0, i) = s[i]; |
75 } | |
76 | |
2349 | 77 charMatrix::charMatrix (const string_vector& s) |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
78 : Array<char> (dim_vector (s.length (), s.max_length ()), 0) |
2349 | 79 { |
5275 | 80 octave_idx_type nr = rows (); |
2613 | 81 |
5275 | 82 for (octave_idx_type i = 0; i < nr; i++) |
2349 | 83 { |
9370
4ff6f8efdda2
fix slow cellstr -> char matrix conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
84 const std::string si = s(i); |
4ff6f8efdda2
fix slow cellstr -> char matrix conversions
Jaroslav Hajek <highegg@gmail.com>
parents:
9227
diff
changeset
|
85 octave_idx_type nc = si.length (); |
5275 | 86 for (octave_idx_type j = 0; j < nc; j++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
87 elem (i, j) = si[j]; |
2349 | 88 } |
89 } | |
90 | |
2384 | 91 bool |
1573 | 92 charMatrix::operator == (const charMatrix& a) const |
93 { | |
94 if (rows () != a.rows () || cols () != a.cols ()) | |
95 return 0; | |
96 | |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9370
diff
changeset
|
97 return mx_inline_equal (length (), data (), a.data ()); |
1573 | 98 } |
99 | |
2384 | 100 bool |
1573 | 101 charMatrix::operator != (const charMatrix& a) const |
102 { | |
103 return !(*this == a); | |
104 } | |
105 | |
106 charMatrix& | |
5275 | 107 charMatrix::insert (const char *s, octave_idx_type r, octave_idx_type c) |
1573 | 108 { |
109 if (s) | |
110 { | |
5275 | 111 octave_idx_type s_len = strlen (s); |
1573 | 112 |
113 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) | |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
114 { |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
115 (*current_liboctave_error_handler) ("range error for insert"); |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
116 return *this; |
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
117 } |
1573 | 118 |
5275 | 119 for (octave_idx_type i = 0; i < s_len; i++) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
120 elem (r, c+i) = s[i]; |
1573 | 121 } |
122 return *this; | |
123 } | |
124 | |
125 charMatrix& | |
5275 | 126 charMatrix::insert (const charMatrix& a, octave_idx_type r, octave_idx_type c) |
1573 | 127 { |
10351
5150ceb4dbb4
base charMatrix and boolMatrix on Array<char>
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
128 Array<char>::insert (a, r, c); |
1573 | 129 return *this; |
130 } | |
131 | |
3504 | 132 std::string |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11570
diff
changeset
|
133 charMatrix::row_as_string (octave_idx_type r, bool strip_ws) const |
1573 | 134 { |
3504 | 135 std::string retval; |
2613 | 136 |
5275 | 137 octave_idx_type nr = rows (); |
138 octave_idx_type nc = cols (); | |
2613 | 139 |
140 if (r == 0 && nr == 0 && nc == 0) | |
141 return retval; | |
142 | |
143 if (r < 0 || r >= nr) | |
1573 | 144 { |
145 (*current_liboctave_error_handler) ("range error for row_as_string"); | |
2613 | 146 return retval; |
1573 | 147 } |
148 | |
2613 | 149 retval.resize (nc, '\0'); |
1573 | 150 |
5275 | 151 for (octave_idx_type i = 0; i < nc; i++) |
1573 | 152 retval[i] = elem (r, i); |
153 | |
11265
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10521
diff
changeset
|
154 if (strip_ws) |
2493 | 155 { |
11265
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10521
diff
changeset
|
156 while (--nc >= 0) |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
157 { |
11265
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10521
diff
changeset
|
158 char c = retval[nc]; |
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10521
diff
changeset
|
159 if (c && c != ' ') |
a117dc8ea1b9
charMatrix::row_as_string: never strip trailing nul characters
John W. Eaton <jwe@octave.org>
parents:
10521
diff
changeset
|
160 break; |
10314
07ebe522dac2
untabify liboctave C++ sources
John W. Eaton <jwe@octave.org>
parents:
10267
diff
changeset
|
161 } |
3836 | 162 |
163 retval.resize (nc+1); | |
2493 | 164 } |
1780 | 165 |
1573 | 166 return retval; |
167 } | |
168 | |
2255 | 169 charMatrix |
5275 | 170 charMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
3189 | 171 { |
5275 | 172 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
173 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
3189 | 174 |
5275 | 175 octave_idx_type new_r = r2 - r1 + 1; |
176 octave_idx_type new_c = c2 - c1 + 1; | |
3189 | 177 |
178 charMatrix result (new_r, new_c); | |
179 | |
5275 | 180 for (octave_idx_type j = 0; j < new_c; j++) |
181 for (octave_idx_type i = 0; i < new_r; i++) | |
3189 | 182 result.elem (i, j) = elem (r1+i, c1+j); |
183 | |
184 return result; | |
185 } | |
186 | |
6979 | 187 charMatrix |
188 charMatrix::diag (octave_idx_type k) const | |
189 { | |
10351
5150ceb4dbb4
base charMatrix and boolMatrix on Array<char>
Jaroslav Hajek <highegg@gmail.com>
parents:
10314
diff
changeset
|
190 return Array<char>::diag (k); |
6979 | 191 } |
192 | |
5775 | 193 // FIXME Do these really belong here? Maybe they should be |
4015 | 194 // in a base class? |
3136 | 195 |
4015 | 196 boolMatrix |
197 charMatrix::all (int dim) const | |
3136 | 198 { |
10362
b47ab50a6aa8
simplify appliers in mx-inlines.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10351
diff
changeset
|
199 return do_mx_red_op<bool, char> (*this, dim, mx_inline_all); |
3136 | 200 } |
201 | |
4015 | 202 boolMatrix |
203 charMatrix::any (int dim) const | |
3136 | 204 { |
10362
b47ab50a6aa8
simplify appliers in mx-inlines.cc
Jaroslav Hajek <highegg@gmail.com>
parents:
10351
diff
changeset
|
205 return do_mx_red_op<bool, char> (*this, dim, mx_inline_any); |
3136 | 206 } |
207 | |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
208 MS_CMP_OPS (charMatrix, char) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9370
diff
changeset
|
209 MS_BOOL_OPS (charMatrix, char) |
6456 | 210 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
211 SM_CMP_OPS (char, charMatrix) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9370
diff
changeset
|
212 SM_BOOL_OPS (char, charMatrix) |
6456 | 213 |
9578
7dafdb8b062f
refactor comparison ops implementations
Jaroslav Hajek <highegg@gmail.com>
parents:
9550
diff
changeset
|
214 MM_CMP_OPS (charMatrix, charMatrix) |
9550
3d6a9aea2aea
refactor binary & bool ops in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
9370
diff
changeset
|
215 MM_BOOL_OPS (charMatrix, charMatrix) |