1993
|
1 // Matrix manipulations. |
1573
|
2 /* |
|
3 |
1882
|
4 Copyright (C) 1996 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 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
|
28 #ifdef HAVE_CONFIG_H |
|
29 #include <config.h> |
|
30 #endif |
|
31 |
|
32 #include <cstdio> |
|
33 #include <cstring> |
|
34 |
1728
|
35 #include <string> |
|
36 |
1573
|
37 #include <iostream.h> |
|
38 |
|
39 // #include <sys/types.h> // XXX FIXME XXX |
|
40 |
|
41 #include "lo-error.h" |
2349
|
42 #include "str-vec.h" |
1573
|
43 #include "mx-base.h" |
|
44 #include "mx-inlines.cc" |
|
45 |
|
46 // charMatrix class. |
|
47 |
|
48 charMatrix::charMatrix (const char *s) |
2613
|
49 : MArray2<char> () |
1573
|
50 { |
2613
|
51 int nc = s ? strlen (s) : 0; |
|
52 int nr = s && nc > 0 ? 1 : 0; |
|
53 |
|
54 resize (nr, nc); |
|
55 |
1573
|
56 for (int i = 0; i < nc; i++) |
|
57 elem (0, i) = s[i]; |
|
58 } |
|
59 |
1733
|
60 charMatrix::charMatrix (const string& s) |
2613
|
61 : MArray2<char> () |
1733
|
62 { |
2613
|
63 int nc = s.length (); |
|
64 int nr = nc > 0 ? 1 : 0; |
|
65 |
|
66 resize (nr, nc); |
|
67 |
1733
|
68 for (int i = 0; i < nc; i++) |
|
69 elem (0, i) = s[i]; |
|
70 } |
|
71 |
2349
|
72 charMatrix::charMatrix (const string_vector& s) |
2350
|
73 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
74 { |
2350
|
75 int nr = rows (); |
2613
|
76 |
2349
|
77 for (int i = 0; i < nr; i++) |
|
78 { |
|
79 int nc = s[i].length (); |
|
80 for (int j = 0; j < nc; j++) |
|
81 elem (i, j) = s[i][j]; |
|
82 } |
|
83 } |
|
84 |
2384
|
85 bool |
1573
|
86 charMatrix::operator == (const charMatrix& a) const |
|
87 { |
|
88 if (rows () != a.rows () || cols () != a.cols ()) |
|
89 return 0; |
|
90 |
|
91 return equal (data (), a.data (), length ()); |
|
92 } |
|
93 |
2384
|
94 bool |
1573
|
95 charMatrix::operator != (const charMatrix& a) const |
|
96 { |
|
97 return !(*this == a); |
|
98 } |
|
99 |
|
100 charMatrix& |
|
101 charMatrix::insert (const char *s, int r, int c) |
|
102 { |
|
103 if (s) |
|
104 { |
|
105 int s_len = strlen (s); |
|
106 |
|
107 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
108 { |
|
109 (*current_liboctave_error_handler) ("range error for insert"); |
|
110 return *this; |
|
111 } |
|
112 |
|
113 for (int i = 0; i < s_len; i++) |
|
114 elem (r, c+i) = s[i]; |
|
115 } |
|
116 return *this; |
|
117 } |
|
118 |
|
119 charMatrix& |
|
120 charMatrix::insert (const charMatrix& a, int r, int c) |
|
121 { |
|
122 Array2<char>::insert (a, r, c); |
|
123 return *this; |
|
124 } |
|
125 |
1728
|
126 string |
2493
|
127 charMatrix::row_as_string (int r, bool strip_ws = false) const |
1573
|
128 { |
2613
|
129 string retval; |
|
130 |
|
131 int nr = rows (); |
|
132 int nc = cols (); |
|
133 |
|
134 if (r == 0 && nr == 0 && nc == 0) |
|
135 return retval; |
|
136 |
|
137 if (r < 0 || r >= nr) |
1573
|
138 { |
|
139 (*current_liboctave_error_handler) ("range error for row_as_string"); |
2613
|
140 return retval; |
1573
|
141 } |
|
142 |
2613
|
143 retval.resize (nc, '\0'); |
1573
|
144 |
|
145 for (int i = 0; i < nc; i++) |
|
146 retval[i] = elem (r, i); |
|
147 |
2493
|
148 if (strip_ws) |
|
149 { |
|
150 while (--nc >= 0) |
|
151 { |
|
152 char c = retval[nc]; |
|
153 if (c && c != ' ') |
|
154 break; |
|
155 } |
|
156 } |
|
157 else |
|
158 { |
|
159 while (--nc >= 0) |
|
160 if (retval[nc]) |
|
161 break; |
|
162 } |
1780
|
163 |
|
164 retval.resize (nc+1); |
|
165 |
1573
|
166 return retval; |
|
167 } |
|
168 |
2255
|
169 charMatrix |
|
170 charMatrix::transpose (void) const |
|
171 { |
|
172 int nr = rows (); |
|
173 int nc = cols (); |
|
174 charMatrix result (nc, nr); |
|
175 if (length () > 0) |
|
176 { |
|
177 for (int j = 0; j < nc; j++) |
|
178 for (int i = 0; i < nr; i++) |
|
179 result.elem (j, i) = elem (i, j); |
|
180 } |
|
181 return result; |
|
182 } |
|
183 |
1573
|
184 /* |
|
185 ;;; Local Variables: *** |
|
186 ;;; mode: C++ *** |
|
187 ;;; End: *** |
|
188 */ |