1988
|
1 // Template array classes -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1996 John W. Eaton |
|
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 |
|
34 #include <iostream.h> |
|
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 { |
|
58 (*current_liboctave_error_handler) ("range error"); |
|
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 { |
|
71 (*current_liboctave_error_handler) ("range error"); |
|
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 { |
|
91 (*current_liboctave_error_handler) ("range error"); |
|
92 T foo; |
|
93 static T *bar = &foo; |
|
94 return foo; |
|
95 } |
|
96 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
97 } |
|
98 |
|
99 template <class T> |
|
100 T |
|
101 DiagArray2<T>::operator () (int r, int c) const |
|
102 { |
|
103 if (r < 0 || c < 0 || r >= nr || c >= nc) |
|
104 { |
|
105 (*current_liboctave_error_handler) ("range error"); |
|
106 T foo; |
|
107 static T *bar = &foo; |
|
108 return foo; |
|
109 } |
|
110 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
111 } |
|
112 |
|
113 template <class T> |
|
114 T& |
|
115 DiagArray2<T>::xelem (int r, int c) |
|
116 { |
|
117 static T foo (0); |
|
118 return (r == c) ? Array<T>::xelem (r) : foo; |
|
119 } |
|
120 |
|
121 template <class T> |
|
122 T |
|
123 DiagArray2<T>::xelem (int r, int c) const |
|
124 { |
|
125 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
126 } |
|
127 |
|
128 template <class T> |
|
129 void |
|
130 DiagArray2<T>::resize (int r, int c) |
|
131 { |
|
132 if (r < 0 || c < 0) |
|
133 { |
|
134 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
135 return; |
|
136 } |
|
137 |
|
138 if (r == dim1 () && c == dim2 ()) |
|
139 return; |
|
140 |
|
141 ArrayRep *old_rep = rep; |
|
142 const T *old_data = data (); |
|
143 int old_len = length (); |
|
144 |
|
145 int new_len = r < c ? r : c; |
|
146 |
|
147 rep = new ArrayRep (new_len); |
|
148 |
|
149 nr = r; |
|
150 nc = c; |
|
151 |
|
152 if (old_data && old_len > 0) |
|
153 { |
|
154 int min_len = old_len < new_len ? old_len : new_len; |
|
155 |
|
156 for (int i = 0; i < min_len; i++) |
|
157 xelem (i, i) = old_data[i]; |
|
158 } |
|
159 |
|
160 if (--old_rep->count <= 0) |
|
161 delete old_rep; |
|
162 } |
|
163 |
|
164 template <class T> |
|
165 void |
|
166 DiagArray2<T>::resize (int r, int c, const T& val) |
|
167 { |
|
168 if (r < 0 || c < 0) |
|
169 { |
|
170 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
171 return; |
|
172 } |
|
173 |
|
174 if (r == dim1 () && c == dim2 ()) |
|
175 return; |
|
176 |
|
177 ArrayRep *old_rep = rep; |
|
178 const T *old_data = data (); |
|
179 int old_len = length (); |
|
180 |
|
181 int new_len = r < c ? r : c; |
|
182 |
|
183 rep = new ArrayRep (new_len); |
|
184 |
|
185 nr = r; |
|
186 nc = c; |
|
187 |
|
188 int min_len = old_len < new_len ? old_len : new_len; |
|
189 |
|
190 if (old_data && old_len > 0) |
|
191 { |
|
192 for (int i = 0; i < min_len; i++) |
|
193 xelem (i, i) = old_data[i]; |
|
194 } |
|
195 |
|
196 for (int i = min_len; i < new_len; i++) |
|
197 xelem (i, i) = val; |
|
198 |
|
199 if (--old_rep->count <= 0) |
|
200 delete old_rep; |
|
201 } |
|
202 |
|
203 /* |
|
204 ;;; Local Variables: *** |
|
205 ;;; mode: C++ *** |
|
206 ;;; page-delimiter: "^/\\*" *** |
|
207 ;;; End: *** |
|
208 */ |