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 |
4192
|
24 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1988
|
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 template <class T> |
|
43 T |
|
44 DiagArray2<T>::elem (int r, int c) const |
|
45 { |
|
46 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
47 } |
|
48 |
|
49 template <class T> |
|
50 T |
|
51 DiagArray2<T>::checkelem (int r, int c) const |
|
52 { |
4645
|
53 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
54 { |
3928
|
55 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
56 return T (); |
1988
|
57 } |
|
58 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
59 } |
|
60 |
|
61 template <class T> |
|
62 T |
|
63 DiagArray2<T>::operator () (int r, int c) const |
|
64 { |
4645
|
65 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
66 { |
3928
|
67 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
68 return T (); |
1988
|
69 } |
|
70 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
71 } |
|
72 |
|
73 template <class T> |
|
74 T& |
|
75 DiagArray2<T>::xelem (int r, int c) |
|
76 { |
|
77 static T foo (0); |
|
78 return (r == c) ? Array<T>::xelem (r) : foo; |
|
79 } |
|
80 |
|
81 template <class T> |
|
82 T |
|
83 DiagArray2<T>::xelem (int r, int c) const |
|
84 { |
|
85 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
86 } |
|
87 |
|
88 template <class T> |
|
89 void |
|
90 DiagArray2<T>::resize (int r, int c) |
|
91 { |
|
92 if (r < 0 || c < 0) |
|
93 { |
|
94 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
95 return; |
|
96 } |
|
97 |
4645
|
98 if (r == this->dim1 () && c == this->dim2 ()) |
1988
|
99 return; |
|
100 |
4323
|
101 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
4645
|
102 const T *old_data = this->data (); |
|
103 int old_len = this->length (); |
1988
|
104 |
|
105 int new_len = r < c ? r : c; |
|
106 |
4323
|
107 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
108 |
4645
|
109 this->dimensions = dim_vector (r, c); |
1988
|
110 |
|
111 if (old_data && old_len > 0) |
|
112 { |
|
113 int min_len = old_len < new_len ? old_len : new_len; |
|
114 |
|
115 for (int i = 0; i < min_len; i++) |
|
116 xelem (i, i) = old_data[i]; |
|
117 } |
|
118 |
|
119 if (--old_rep->count <= 0) |
|
120 delete old_rep; |
|
121 } |
|
122 |
|
123 template <class T> |
|
124 void |
|
125 DiagArray2<T>::resize (int r, int c, const T& val) |
|
126 { |
|
127 if (r < 0 || c < 0) |
|
128 { |
|
129 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
130 return; |
|
131 } |
|
132 |
4645
|
133 if (r == this->dim1 () && c == this->dim2 ()) |
1988
|
134 return; |
|
135 |
4323
|
136 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
4645
|
137 const T *old_data = this->data (); |
|
138 int old_len = this->length (); |
1988
|
139 |
|
140 int new_len = r < c ? r : c; |
|
141 |
4323
|
142 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
143 |
4645
|
144 this->dimensions = dim_vector (r, c); |
1988
|
145 |
|
146 int min_len = old_len < new_len ? old_len : new_len; |
|
147 |
|
148 if (old_data && old_len > 0) |
|
149 { |
|
150 for (int i = 0; i < min_len; i++) |
|
151 xelem (i, i) = old_data[i]; |
|
152 } |
|
153 |
|
154 for (int i = min_len; i < new_len; i++) |
|
155 xelem (i, i) = val; |
|
156 |
|
157 if (--old_rep->count <= 0) |
|
158 delete old_rep; |
|
159 } |
|
160 |
|
161 /* |
|
162 ;;; Local Variables: *** |
|
163 ;;; mode: C++ *** |
|
164 ;;; End: *** |
|
165 */ |