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 |
4066
|
24 #if defined (__GNUG__) && ! defined (NO_PRAGMA_INTERFACE_IMPLEMENTATION) |
1573
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
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" |
|
39 |
|
40 // charMatrix class. |
|
41 |
3189
|
42 charMatrix::charMatrix (char c) |
|
43 : MArray2<char> () |
|
44 { |
|
45 int nc = 1; |
|
46 int nr = 1; |
|
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 { |
2613
|
56 int nc = s ? strlen (s) : 0; |
|
57 int nr = s && nc > 0 ? 1 : 0; |
|
58 |
|
59 resize (nr, nc); |
|
60 |
1573
|
61 for (int i = 0; i < nc; i++) |
|
62 elem (0, i) = s[i]; |
|
63 } |
|
64 |
3504
|
65 charMatrix::charMatrix (const std::string& s) |
2613
|
66 : MArray2<char> () |
1733
|
67 { |
2613
|
68 int nc = s.length (); |
|
69 int nr = nc > 0 ? 1 : 0; |
|
70 |
|
71 resize (nr, nc); |
|
72 |
1733
|
73 for (int i = 0; i < nc; i++) |
|
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 { |
2350
|
80 int nr = rows (); |
2613
|
81 |
2349
|
82 for (int i = 0; i < nr; i++) |
|
83 { |
|
84 int nc = s[i].length (); |
|
85 for (int j = 0; j < nc; j++) |
|
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& |
|
106 charMatrix::insert (const char *s, int r, int c) |
|
107 { |
|
108 if (s) |
|
109 { |
|
110 int s_len = strlen (s); |
|
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 |
|
118 for (int i = 0; i < s_len; i++) |
|
119 elem (r, c+i) = s[i]; |
|
120 } |
|
121 return *this; |
|
122 } |
|
123 |
|
124 charMatrix& |
|
125 charMatrix::insert (const charMatrix& a, int r, int c) |
|
126 { |
|
127 Array2<char>::insert (a, r, c); |
|
128 return *this; |
|
129 } |
|
130 |
3504
|
131 std::string |
3836
|
132 charMatrix::row_as_string (int r, bool strip_ws, bool raw) const |
1573
|
133 { |
3504
|
134 std::string retval; |
2613
|
135 |
|
136 int nr = rows (); |
|
137 int nc = cols (); |
|
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 |
|
150 for (int i = 0; i < nc; i++) |
|
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 |
3189
|
178 charMatrix::extract (int r1, int c1, int r2, int c2) const |
|
179 { |
|
180 if (r1 > r2) { int tmp = r1; r1 = r2; r2 = tmp; } |
|
181 if (c1 > c2) { int tmp = c1; c1 = c2; c2 = tmp; } |
|
182 |
|
183 int new_r = r2 - r1 + 1; |
|
184 int new_c = c2 - c1 + 1; |
|
185 |
|
186 charMatrix result (new_r, new_c); |
|
187 |
|
188 for (int j = 0; j < new_c; j++) |
|
189 for (int i = 0; i < new_r; i++) |
|
190 result.elem (i, j) = elem (r1+i, c1+j); |
|
191 |
|
192 return result; |
|
193 } |
|
194 |
4015
|
195 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
196 // in a base class? |
3136
|
197 |
4015
|
198 boolMatrix |
|
199 charMatrix::all (int dim) const |
3136
|
200 { |
4015
|
201 MX_ALL_OP (dim); |
3136
|
202 } |
|
203 |
4015
|
204 boolMatrix |
|
205 charMatrix::any (int dim) const |
3136
|
206 { |
4015
|
207 MX_ANY_OP (dim); |
3136
|
208 } |
|
209 |
1573
|
210 /* |
|
211 ;;; Local Variables: *** |
|
212 ;;; mode: C++ *** |
|
213 ;;; End: *** |
|
214 */ |