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