1993
|
1 // Template array classes |
1988
|
2 /* |
|
3 |
7017
|
4 Copyright (C) 1996, 1997, 1999, 2000, 2002, 2003, 2004, 2005, 2007 |
|
5 John W. Eaton |
1988
|
6 |
|
7 This file is part of Octave. |
|
8 |
|
9 Octave is free software; you can redistribute it and/or modify it |
|
10 under the terms of the GNU General Public License as published by the |
7016
|
11 Free Software Foundation; either version 3 of the License, or (at your |
|
12 option) any later version. |
1988
|
13 |
|
14 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
17 for more details. |
|
18 |
|
19 You should have received a copy of the GNU General Public License |
7016
|
20 along with Octave; see the file COPYING. If not, see |
|
21 <http://www.gnu.org/licenses/>. |
1988
|
22 |
|
23 */ |
|
24 |
|
25 #ifdef HAVE_CONFIG_H |
|
26 #include <config.h> |
|
27 #endif |
|
28 |
|
29 #include <cassert> |
|
30 |
3503
|
31 #include <iostream> |
1988
|
32 |
|
33 #include "DiagArray2.h" |
|
34 |
|
35 #include "lo-error.h" |
|
36 |
|
37 // A two-dimensional array with diagonal elements only. |
|
38 |
|
39 template <class T> |
|
40 T |
5275
|
41 DiagArray2<T>::elem (octave_idx_type r, octave_idx_type c) const |
1988
|
42 { |
|
43 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
44 } |
|
45 |
|
46 template <class T> |
|
47 T |
5275
|
48 DiagArray2<T>::checkelem (octave_idx_type r, octave_idx_type c) const |
1988
|
49 { |
4645
|
50 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
51 { |
3928
|
52 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
53 return T (); |
1988
|
54 } |
|
55 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
56 } |
|
57 |
|
58 template <class T> |
|
59 T |
5275
|
60 DiagArray2<T>::operator () (octave_idx_type r, octave_idx_type c) const |
1988
|
61 { |
4645
|
62 if (r < 0 || c < 0 || r >= this->dim1 () || c >= this->dim2 ()) |
1988
|
63 { |
3928
|
64 (*current_liboctave_error_handler) ("range error in DiagArray2"); |
3333
|
65 return T (); |
1988
|
66 } |
|
67 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
68 } |
|
69 |
|
70 template <class T> |
|
71 T& |
5275
|
72 DiagArray2<T>::xelem (octave_idx_type r, octave_idx_type c) |
1988
|
73 { |
|
74 static T foo (0); |
|
75 return (r == c) ? Array<T>::xelem (r) : foo; |
|
76 } |
|
77 |
|
78 template <class T> |
|
79 T |
5275
|
80 DiagArray2<T>::xelem (octave_idx_type r, octave_idx_type c) const |
1988
|
81 { |
|
82 return (r == c) ? Array<T>::xelem (r) : T (0); |
|
83 } |
|
84 |
|
85 template <class T> |
|
86 void |
5275
|
87 DiagArray2<T>::resize (octave_idx_type r, octave_idx_type c) |
1988
|
88 { |
|
89 if (r < 0 || c < 0) |
|
90 { |
|
91 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
92 return; |
|
93 } |
|
94 |
4645
|
95 if (r == this->dim1 () && c == this->dim2 ()) |
1988
|
96 return; |
|
97 |
4323
|
98 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
4645
|
99 const T *old_data = this->data (); |
5275
|
100 octave_idx_type old_len = this->length (); |
1988
|
101 |
5275
|
102 octave_idx_type new_len = r < c ? r : c; |
1988
|
103 |
4323
|
104 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
105 |
4645
|
106 this->dimensions = dim_vector (r, c); |
1988
|
107 |
|
108 if (old_data && old_len > 0) |
|
109 { |
5275
|
110 octave_idx_type min_len = old_len < new_len ? old_len : new_len; |
1988
|
111 |
5275
|
112 for (octave_idx_type i = 0; i < min_len; i++) |
1988
|
113 xelem (i, i) = old_data[i]; |
|
114 } |
|
115 |
|
116 if (--old_rep->count <= 0) |
|
117 delete old_rep; |
|
118 } |
|
119 |
|
120 template <class T> |
|
121 void |
5275
|
122 DiagArray2<T>::resize (octave_idx_type r, octave_idx_type c, const T& val) |
1988
|
123 { |
|
124 if (r < 0 || c < 0) |
|
125 { |
|
126 (*current_liboctave_error_handler) ("can't resize to negative dimensions"); |
|
127 return; |
|
128 } |
|
129 |
4645
|
130 if (r == this->dim1 () && c == this->dim2 ()) |
1988
|
131 return; |
|
132 |
4323
|
133 typename Array<T>::ArrayRep *old_rep = Array<T>::rep; |
4645
|
134 const T *old_data = this->data (); |
5275
|
135 octave_idx_type old_len = this->length (); |
1988
|
136 |
5275
|
137 octave_idx_type new_len = r < c ? r : c; |
1988
|
138 |
4323
|
139 Array<T>::rep = new typename Array<T>::ArrayRep (new_len); |
1988
|
140 |
4645
|
141 this->dimensions = dim_vector (r, c); |
1988
|
142 |
5275
|
143 octave_idx_type min_len = old_len < new_len ? old_len : new_len; |
1988
|
144 |
|
145 if (old_data && old_len > 0) |
|
146 { |
5275
|
147 for (octave_idx_type i = 0; i < min_len; i++) |
1988
|
148 xelem (i, i) = old_data[i]; |
|
149 } |
|
150 |
5275
|
151 for (octave_idx_type i = min_len; i < new_len; i++) |
1988
|
152 xelem (i, i) = val; |
|
153 |
|
154 if (--old_rep->count <= 0) |
|
155 delete old_rep; |
|
156 } |
|
157 |
|
158 /* |
|
159 ;;; Local Variables: *** |
|
160 ;;; mode: C++ *** |
|
161 ;;; End: *** |
|
162 */ |