Mercurial > hg > octave-nkf
annotate liboctave/chMatrix.cc @ 8774:b756ce0002db
split implementation and interface in mx-op-defs and MArray-defs
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 17 Feb 2009 08:38:00 +0100 |
parents | 8af4ba6b4216 |
children | eb63fbe60fab |
rev | line source |
---|---|
1993 | 1 // Matrix manipulations. |
1573 | 2 /* |
3 | |
7017 | 4 Copyright (C) 1995, 1996, 1997, 1998, 2000, 2001, 2002, 2004, 2005, |
5 2006, 2007 John W. Eaton | |
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) |
43 : MArray2<char> () | |
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) |
2613 | 54 : MArray2<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) |
2613 | 66 : MArray2<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) |
2350 | 78 : MArray2<char> (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 { |
5275 | 84 octave_idx_type nc = s[i].length (); |
85 for (octave_idx_type j = 0; j < nc; j++) | |
2349 | 86 elem (i, j) = s[i][j]; |
87 } | |
88 } | |
89 | |
2384 | 90 bool |
1573 | 91 charMatrix::operator == (const charMatrix& a) const |
92 { | |
93 if (rows () != a.rows () || cols () != a.cols ()) | |
94 return 0; | |
95 | |
3769 | 96 return mx_inline_equal (data (), a.data (), length ()); |
1573 | 97 } |
98 | |
2384 | 99 bool |
1573 | 100 charMatrix::operator != (const charMatrix& a) const |
101 { | |
102 return !(*this == a); | |
103 } | |
104 | |
105 charMatrix& | |
5275 | 106 charMatrix::insert (const char *s, octave_idx_type r, octave_idx_type c) |
1573 | 107 { |
108 if (s) | |
109 { | |
5275 | 110 octave_idx_type s_len = strlen (s); |
1573 | 111 |
112 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) | |
113 { | |
114 (*current_liboctave_error_handler) ("range error for insert"); | |
115 return *this; | |
116 } | |
117 | |
5275 | 118 for (octave_idx_type i = 0; i < s_len; i++) |
1573 | 119 elem (r, c+i) = s[i]; |
120 } | |
121 return *this; | |
122 } | |
123 | |
124 charMatrix& | |
5275 | 125 charMatrix::insert (const charMatrix& a, octave_idx_type r, octave_idx_type c) |
1573 | 126 { |
127 Array2<char>::insert (a, r, c); | |
128 return *this; | |
129 } | |
130 | |
3504 | 131 std::string |
5275 | 132 charMatrix::row_as_string (octave_idx_type r, bool strip_ws, bool raw) const |
1573 | 133 { |
3504 | 134 std::string retval; |
2613 | 135 |
5275 | 136 octave_idx_type nr = rows (); |
137 octave_idx_type nc = cols (); | |
2613 | 138 |
139 if (r == 0 && nr == 0 && nc == 0) | |
140 return retval; | |
141 | |
142 if (r < 0 || r >= nr) | |
1573 | 143 { |
144 (*current_liboctave_error_handler) ("range error for row_as_string"); | |
2613 | 145 return retval; |
1573 | 146 } |
147 | |
2613 | 148 retval.resize (nc, '\0'); |
1573 | 149 |
5275 | 150 for (octave_idx_type i = 0; i < nc; i++) |
1573 | 151 retval[i] = elem (r, i); |
152 | |
3836 | 153 if (! raw) |
2493 | 154 { |
3836 | 155 if (strip_ws) |
2493 | 156 { |
3836 | 157 while (--nc >= 0) |
158 { | |
159 char c = retval[nc]; | |
160 if (c && c != ' ') | |
161 break; | |
162 } | |
2493 | 163 } |
3836 | 164 else |
165 { | |
166 while (--nc >= 0) | |
167 if (retval[nc]) | |
168 break; | |
169 } | |
170 | |
171 retval.resize (nc+1); | |
2493 | 172 } |
1780 | 173 |
1573 | 174 return retval; |
175 } | |
176 | |
2255 | 177 charMatrix |
5275 | 178 charMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
3189 | 179 { |
5275 | 180 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
181 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } | |
3189 | 182 |
5275 | 183 octave_idx_type new_r = r2 - r1 + 1; |
184 octave_idx_type new_c = c2 - c1 + 1; | |
3189 | 185 |
186 charMatrix result (new_r, new_c); | |
187 | |
5275 | 188 for (octave_idx_type j = 0; j < new_c; j++) |
189 for (octave_idx_type i = 0; i < new_r; i++) | |
3189 | 190 result.elem (i, j) = elem (r1+i, c1+j); |
191 | |
192 return result; | |
193 } | |
194 | |
6979 | 195 charMatrix |
196 charMatrix::diag (octave_idx_type k) const | |
197 { | |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7048
diff
changeset
|
198 return MArray2<char>::diag (k); |
6979 | 199 } |
200 | |
5775 | 201 // FIXME Do these really belong here? Maybe they should be |
4015 | 202 // in a base class? |
3136 | 203 |
4015 | 204 boolMatrix |
205 charMatrix::all (int dim) const | |
3136 | 206 { |
8750
8af4ba6b4216
use new reduction ops for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
207 return do_mx_red_op<boolMatrix> (*this, dim, mx_inline_all); |
3136 | 208 } |
209 | |
4015 | 210 boolMatrix |
211 charMatrix::any (int dim) const | |
3136 | 212 { |
8750
8af4ba6b4216
use new reduction ops for char matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
7620
diff
changeset
|
213 return do_mx_red_op<boolMatrix> (*this, dim, mx_inline_any); |
3136 | 214 } |
215 | |
6456 | 216 MS_CMP_OPS(charMatrix, , char, ) |
217 MS_BOOL_OPS(charMatrix, char, 0) | |
218 | |
219 SM_CMP_OPS(char, , charMatrix, ) | |
220 SM_BOOL_OPS(char, charMatrix, 0) | |
221 | |
222 MM_CMP_OPS(charMatrix, , charMatrix, ) | |
223 MM_BOOL_OPS(charMatrix, charMatrix, 0) | |
224 | |
1573 | 225 /* |
226 ;;; Local Variables: *** | |
227 ;;; mode: C++ *** | |
228 ;;; End: *** | |
229 */ |