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) |
|
49 : MArray2<char> ((s ? 1 : 0), (s ? strlen (s) : 0)) |
|
50 { |
|
51 int nc = cols (); |
|
52 for (int i = 0; i < nc; i++) |
|
53 elem (0, i) = s[i]; |
|
54 } |
|
55 |
1733
|
56 charMatrix::charMatrix (const string& s) |
|
57 : MArray2<char> (1, s.length ()) |
|
58 { |
|
59 int nc = cols (); |
|
60 for (int i = 0; i < nc; i++) |
|
61 elem (0, i) = s[i]; |
|
62 } |
|
63 |
2349
|
64 charMatrix::charMatrix (const string_vector& s) |
2350
|
65 : MArray2<char> (s.length (), s.max_length (), 0) |
2349
|
66 { |
2350
|
67 int nr = rows (); |
2349
|
68 for (int i = 0; i < nr; i++) |
|
69 { |
|
70 int nc = s[i].length (); |
|
71 for (int j = 0; j < nc; j++) |
|
72 elem (i, j) = s[i][j]; |
|
73 } |
|
74 } |
|
75 |
1573
|
76 int |
|
77 charMatrix::operator == (const charMatrix& a) const |
|
78 { |
|
79 if (rows () != a.rows () || cols () != a.cols ()) |
|
80 return 0; |
|
81 |
|
82 return equal (data (), a.data (), length ()); |
|
83 } |
|
84 |
|
85 int |
|
86 charMatrix::operator != (const charMatrix& a) const |
|
87 { |
|
88 return !(*this == a); |
|
89 } |
|
90 |
|
91 charMatrix& |
|
92 charMatrix::insert (const char *s, int r, int c) |
|
93 { |
|
94 if (s) |
|
95 { |
|
96 int s_len = strlen (s); |
|
97 |
|
98 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
99 { |
|
100 (*current_liboctave_error_handler) ("range error for insert"); |
|
101 return *this; |
|
102 } |
|
103 |
|
104 for (int i = 0; i < s_len; i++) |
|
105 elem (r, c+i) = s[i]; |
|
106 } |
|
107 return *this; |
|
108 } |
|
109 |
|
110 charMatrix& |
|
111 charMatrix::insert (const charMatrix& a, int r, int c) |
|
112 { |
|
113 Array2<char>::insert (a, r, c); |
|
114 return *this; |
|
115 } |
|
116 |
1728
|
117 string |
1573
|
118 charMatrix::row_as_string (int r) const |
|
119 { |
|
120 if (r < 0 || r >= rows ()) |
|
121 { |
|
122 (*current_liboctave_error_handler) ("range error for row_as_string"); |
|
123 return 0; |
|
124 } |
|
125 |
|
126 int nc = cols (); |
|
127 |
1728
|
128 string retval (nc, '\0'); |
1573
|
129 |
|
130 for (int i = 0; i < nc; i++) |
|
131 retval[i] = elem (r, i); |
|
132 |
1780
|
133 while (--nc >= 0) |
|
134 if (retval[nc]) |
|
135 break; |
|
136 |
|
137 retval.resize (nc+1); |
|
138 |
1573
|
139 return retval; |
|
140 } |
|
141 |
2255
|
142 charMatrix |
|
143 charMatrix::transpose (void) const |
|
144 { |
|
145 int nr = rows (); |
|
146 int nc = cols (); |
|
147 charMatrix result (nc, nr); |
|
148 if (length () > 0) |
|
149 { |
|
150 for (int j = 0; j < nc; j++) |
|
151 for (int i = 0; i < nr; i++) |
|
152 result.elem (j, i) = elem (i, j); |
|
153 } |
|
154 return result; |
|
155 } |
|
156 |
1573
|
157 /* |
|
158 ;;; Local Variables: *** |
|
159 ;;; mode: C++ *** |
|
160 ;;; End: *** |
|
161 */ |