1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1988
|
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 <cassert> |
|
33 |
3503
|
34 #include <iostream> |
1988
|
35 |
|
36 #include "DiagArray2.h" |
|
37 |
|
38 #include "lo-error.h" |
|
39 |
|
40 // A two-dimensional array with diagonal elements only. |
|
41 |
|
42 #if 0 |
|
43 template <class T> |
|
44 T& |
|
45 DiagArray2<T>::elem (int r, int c) |
|
46 { |
|
47 static T foo (0); |
|
48 return (r == c) ? Array<T>::xelem (r) : foo; |
|
49 } |
|
50 |
|
51 template <class T> |
|
52 T& |
|
53 DiagArray2<T>::checkelem (int r, int c) |
|
54 { |
|
55 static T foo (0); |
|
56 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
57 { |
3928
|
58 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
59 return foo; |
|
60 } |
|
61 return (r == c) ? Array<T>::xelem (r) : foo; |
|
62 } |
|
63 |
|
64 template <class T> |
|
65 T& |
|
66 DiagArray2<T>::operator () (int r, int c) |
|
67 { |
|
68 static T foo (0); |
|
69 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
70 { |
3928
|
71 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
1988
|
72 return foo; |
|
73 } |
|
74 return (r == c) ? Array<T>::xelem (r) : foo; |
|
75 } |
|
76 #endif |
|
77 |
|
78 template <class T> |
|
79 T |
|
80 DiagArray2<T>::elem (int r, int c) const |
|
81 { |
|
82 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
83 } |
|
84 |
|
85 template <class T> |
|
86 T |
|
87 DiagArray2<T>::checkelem (int r, int c) const |
|
88 { |
|
89 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
90 { |
3928
|
91 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
92 return T (); |
1988
|
93 } |
|
94 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
95 } |
|
96 |
|
97 template <class T> |
|
98 T |
|
99 DiagArray2<T>::operator () (int r, int c) const |
|
100 { |
|
101 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
102 { |
3928
|
103 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
104 return T (); |
1988
|
105 } |
|
106 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
107 } |
|
108 |
|
109 template <class T> |
|
110 T& |
|
111 DiagArray2<T>::xelem (int r, int c) |
|
112 { |
|
113 static T foo (0); |
|
114 return (r == c) ? Array<T>::xelem (r) : foo; |
|
115 } |
|
116 |
|
117 template <class T> |
|
118 T |
|
119 DiagArray2<T>::xelem (int r, int c) const |
|
120 { |
|
121 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
122 } |
|
123 |
|
124 template <class T> |
|
125 void |
|
126 DiagArray2<T>::resize (int r, int c) |
|
127 { |
|
128 if (r < 0 || c < 0) |
|
129 { |
|
130 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
131 return; |
|
132 } |
|
133 |
|
134 if (r == dim1 () && c == dim2 ()) |
|
135 return; |
|
136 |
|
137 ArrayRep *old_rep = rep; |
|
138 const T *old_data = data (); |
|
139 int old_len = length (); |
|
140 |
|
141 int new_len = r < c ? r : c; |
|
142 |
|
143 rep = new ArrayRep (new_len); |
|
144 |
|
145 nr = r; |
|
146 nc = c; |
|
147 |
|
148 if (old_data && old_len > 0) |
|
149 { |
|
150 int min_len = old_len < new_len ? old_len : new_len; |
|
151 |
|
152 for (int i = 0; i < min_len; i++) |
|
153 xelem (i, i) = old_data[i]; |
|
154 } |
|
155 |
|
156 if (--old_rep->count <= 0) |
|
157 delete old_rep; |
|
158 } |
|
159 |
|
160 template <class T> |
|
161 void |
|
162 DiagArray2<T>::resize (int r, int c, const T& val) |
|
163 { |
|
164 if (r < 0 || c < 0) |
|
165 { |
|
166 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
167 return; |
|
168 } |
|
169 |
|
170 if (r == dim1 () && c == dim2 ()) |
|
171 return; |
|
172 |
|
173 ArrayRep *old_rep = rep; |
|
174 const T *old_data = data (); |
|
175 int old_len = length (); |
|
176 |
|
177 int new_len = r < c ? r : c; |
|
178 |
|
179 rep = new ArrayRep (new_len); |
|
180 |
|
181 nr = r; |
|
182 nc = c; |
|
183 |
|
184 int min_len = old_len < new_len ? old_len : new_len; |
|
185 |
|
186 if (old_data && old_len > 0) |
|
187 { |
|
188 for (int i = 0; i < min_len; i++) |
|
189 xelem (i, i) = old_data[i]; |
|
190 } |
|
191 |
|
192 for (int i = min_len; i < new_len; i++) |
|
193 xelem (i, i) = val; |
|
194 |
|
195 if (--old_rep->count <= 0) |
|
196 delete old_rep; |
|
197 } |
|
198 |
|
199 /* |
|
200 ;;; Local Variables: *** |
|
201 ;;; mode: C++ *** |
|
202 ;;; End: *** |
|
203 */ |