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