1993
|
1 // Matrix manipulations. |
1573
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1573
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
21 |
|
22 */ |
|
23 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include <config.h> |
|
26 #endif |
|
27 |
3503
|
28 #include <iostream> |
1728
|
29 #include <string> |
|
30 |
1573
|
31 #include "lo-error.h" |
2349
|
32 #include "str-vec.h" |
1573
|
33 #include "mx-base.h" |
|
34 #include "mx-inlines.cc" |
|
35 |
|
36 // charMatrix class. |
|
37 |
3189
|
38 charMatrix::charMatrix (char c) |
|
39 : MArray2<char> () |
|
40 { |
5275
|
41 octave_idx_type nc = 1; |
|
42 octave_idx_type nr = 1; |
3189
|
43 |
|
44 resize (nr, nc); |
|
45 |
|
46 elem (0, 0) = c; |
|
47 } |
|
48 |
1573
|
49 charMatrix::charMatrix (const char *s) |
2613
|
50 : MArray2<char> () |
1573
|
51 { |
5275
|
52 octave_idx_type nc = s ? strlen (s) : 0; |
|
53 octave_idx_type nr = s && nc > 0 ? 1 : 0; |
2613
|
54 |
|
55 resize (nr, nc); |
|
56 |
5275
|
57 for (octave_idx_type i = 0; i < nc; i++) |
1573
|
58 elem (0, i) = s[i]; |
|
59 } |
|
60 |
3504
|
61 charMatrix::charMatrix (const std::string& s) |
2613
|
62 : MArray2<char> () |
1733
|
63 { |
5275
|
64 octave_idx_type nc = s.length (); |
|
65 octave_idx_type nr = nc > 0 ? 1 : 0; |
2613
|
66 |
|
67 resize (nr, nc); |
|
68 |
5275
|
69 for (octave_idx_type i = 0; i < nc; i++) |
1733
|
70 elem (0, i) = s[i]; |
|
71 } |
|
72 |
2349
|
73 charMatrix::charMatrix (const string_vector& s) |
2350
|
74 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
75 { |
5275
|
76 octave_idx_type nr = rows (); |
2613
|
77 |
5275
|
78 for (octave_idx_type i = 0; i < nr; i++) |
2349
|
79 { |
5275
|
80 octave_idx_type nc = s[i].length (); |
|
81 for (octave_idx_type j = 0; j < nc; j++) |
2349
|
82 elem (i, j) = s[i][j]; |
|
83 } |
|
84 } |
|
85 |
2384
|
86 bool |
1573
|
87 charMatrix::operator == (const charMatrix& a) const |
|
88 { |
|
89 if (rows () != a.rows () || cols () != a.cols ()) |
|
90 return 0; |
|
91 |
3769
|
92 return mx_inline_equal (data (), a.data (), length ()); |
1573
|
93 } |
|
94 |
2384
|
95 bool |
1573
|
96 charMatrix::operator != (const charMatrix& a) const |
|
97 { |
|
98 return !(*this == a); |
|
99 } |
|
100 |
|
101 charMatrix& |
5275
|
102 charMatrix::insert (const char *s, octave_idx_type r, octave_idx_type c) |
1573
|
103 { |
|
104 if (s) |
|
105 { |
5275
|
106 octave_idx_type s_len = strlen (s); |
1573
|
107 |
|
108 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
109 { |
|
110 (*current_liboctave_error_handler) ("range error for insert"); |
|
111 return *this; |
|
112 } |
|
113 |
5275
|
114 for (octave_idx_type i = 0; i < s_len; i++) |
1573
|
115 elem (r, c+i) = s[i]; |
|
116 } |
|
117 return *this; |
|
118 } |
|
119 |
|
120 charMatrix& |
5275
|
121 charMatrix::insert (const charMatrix& a, octave_idx_type r, octave_idx_type c) |
1573
|
122 { |
|
123 Array2<char>::insert (a, r, c); |
|
124 return *this; |
|
125 } |
|
126 |
3504
|
127 std::string |
5275
|
128 charMatrix::row_as_string (octave_idx_type r, bool strip_ws, bool raw) const |
1573
|
129 { |
3504
|
130 std::string retval; |
2613
|
131 |
5275
|
132 octave_idx_type nr = rows (); |
|
133 octave_idx_type nc = cols (); |
2613
|
134 |
|
135 if (r == 0 && nr == 0 && nc == 0) |
|
136 return retval; |
|
137 |
|
138 if (r < 0 || r >= nr) |
1573
|
139 { |
|
140 (*current_liboctave_error_handler) ("range error for row_as_string"); |
2613
|
141 return retval; |
1573
|
142 } |
|
143 |
2613
|
144 retval.resize (nc, '\0'); |
1573
|
145 |
5275
|
146 for (octave_idx_type i = 0; i < nc; i++) |
1573
|
147 retval[i] = elem (r, i); |
|
148 |
3836
|
149 if (! raw) |
2493
|
150 { |
3836
|
151 if (strip_ws) |
2493
|
152 { |
3836
|
153 while (--nc >= 0) |
|
154 { |
|
155 char c = retval[nc]; |
|
156 if (c && c != ' ') |
|
157 break; |
|
158 } |
2493
|
159 } |
3836
|
160 else |
|
161 { |
|
162 while (--nc >= 0) |
|
163 if (retval[nc]) |
|
164 break; |
|
165 } |
|
166 |
|
167 retval.resize (nc+1); |
2493
|
168 } |
1780
|
169 |
1573
|
170 return retval; |
|
171 } |
|
172 |
2255
|
173 charMatrix |
5275
|
174 charMatrix::extract (octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const |
3189
|
175 { |
5275
|
176 if (r1 > r2) { octave_idx_type tmp = r1; r1 = r2; r2 = tmp; } |
|
177 if (c1 > c2) { octave_idx_type tmp = c1; c1 = c2; c2 = tmp; } |
3189
|
178 |
5275
|
179 octave_idx_type new_r = r2 - r1 + 1; |
|
180 octave_idx_type new_c = c2 - c1 + 1; |
3189
|
181 |
|
182 charMatrix result (new_r, new_c); |
|
183 |
5275
|
184 for (octave_idx_type j = 0; j < new_c; j++) |
|
185 for (octave_idx_type i = 0; i < new_r; i++) |
3189
|
186 result.elem (i, j) = elem (r1+i, c1+j); |
|
187 |
|
188 return result; |
|
189 } |
|
190 |
4015
|
191 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
192 // in a base class? |
3136
|
193 |
4015
|
194 boolMatrix |
|
195 charMatrix::all (int dim) const |
3136
|
196 { |
4015
|
197 MX_ALL_OP (dim); |
3136
|
198 } |
|
199 |
4015
|
200 boolMatrix |
|
201 charMatrix::any (int dim) const |
3136
|
202 { |
4015
|
203 MX_ANY_OP (dim); |
3136
|
204 } |
|
205 |
1573
|
206 /* |
|
207 ;;; Local Variables: *** |
|
208 ;;; mode: C++ *** |
|
209 ;;; End: *** |
|
210 */ |