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" |
|
42 #include "mx-base.h" |
|
43 #include "mx-inlines.cc" |
|
44 |
|
45 // charMatrix class. |
|
46 |
|
47 charMatrix::charMatrix (const char *s) |
|
48 : MArray2<char> ((s ? 1 : 0), (s ? strlen (s) : 0)) |
|
49 { |
|
50 int nc = cols (); |
|
51 for (int i = 0; i < nc; i++) |
|
52 elem (0, i) = s[i]; |
|
53 } |
|
54 |
1733
|
55 charMatrix::charMatrix (const string& s) |
|
56 : MArray2<char> (1, s.length ()) |
|
57 { |
|
58 int nc = cols (); |
|
59 for (int i = 0; i < nc; i++) |
|
60 elem (0, i) = s[i]; |
|
61 } |
|
62 |
1573
|
63 int |
|
64 charMatrix::operator == (const charMatrix& a) const |
|
65 { |
|
66 if (rows () != a.rows () || cols () != a.cols ()) |
|
67 return 0; |
|
68 |
|
69 return equal (data (), a.data (), length ()); |
|
70 } |
|
71 |
|
72 int |
|
73 charMatrix::operator != (const charMatrix& a) const |
|
74 { |
|
75 return !(*this == a); |
|
76 } |
|
77 |
|
78 charMatrix& |
|
79 charMatrix::insert (const char *s, int r, int c) |
|
80 { |
|
81 if (s) |
|
82 { |
|
83 int s_len = strlen (s); |
|
84 |
|
85 if (r < 0 || r >= rows () || c < 0 || c + s_len - 1 > cols ()) |
|
86 { |
|
87 (*current_liboctave_error_handler) ("range error for insert"); |
|
88 return *this; |
|
89 } |
|
90 |
|
91 for (int i = 0; i < s_len; i++) |
|
92 elem (r, c+i) = s[i]; |
|
93 } |
|
94 return *this; |
|
95 } |
|
96 |
|
97 charMatrix& |
|
98 charMatrix::insert (const charMatrix& a, int r, int c) |
|
99 { |
|
100 Array2<char>::insert (a, r, c); |
|
101 return *this; |
|
102 } |
|
103 |
1728
|
104 string |
1573
|
105 charMatrix::row_as_string (int r) const |
|
106 { |
|
107 if (r < 0 || r >= rows ()) |
|
108 { |
|
109 (*current_liboctave_error_handler) ("range error for row_as_string"); |
|
110 return 0; |
|
111 } |
|
112 |
|
113 int nc = cols (); |
|
114 |
1728
|
115 string retval (nc, '\0'); |
1573
|
116 |
|
117 for (int i = 0; i < nc; i++) |
|
118 retval[i] = elem (r, i); |
|
119 |
1780
|
120 while (--nc >= 0) |
|
121 if (retval[nc]) |
|
122 break; |
|
123 |
|
124 retval.resize (nc+1); |
|
125 |
1573
|
126 return retval; |
|
127 } |
|
128 |
|
129 /* |
|
130 ;;; Local Variables: *** |
|
131 ;;; mode: C++ *** |
|
132 ;;; End: *** |
|
133 */ |