1993
|
1 // Template array classes |
1559
|
2 /* |
|
3 |
2847
|
4 Copyright (C) 1996, 1997 John W. Eaton |
1559
|
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 |
1906
|
24 #include "Array-flags.h" |
1559
|
25 #include "idx-vector.h" |
|
26 #include "lo-error.h" |
|
27 |
|
28 template <class T> |
|
29 void |
1619
|
30 Array<T>::clear_index (void) |
1559
|
31 { |
|
32 delete [] idx; |
|
33 idx = 0; |
|
34 idx_count = 0; |
|
35 } |
|
36 |
|
37 template <class T> |
|
38 void |
3513
|
39 Array<T>::set_index (const idx_vector& idx_arg) |
1559
|
40 { |
|
41 if (! idx) |
|
42 idx = new idx_vector [max_indices]; |
|
43 |
|
44 if (idx_count < max_indices) |
|
45 { |
3513
|
46 idx[idx_count++] = idx_arg; |
1559
|
47 } |
|
48 else |
|
49 { |
|
50 (*current_liboctave_error_handler) |
|
51 ("invalid number of indices specified"); |
1619
|
52 |
1559
|
53 clear_index (); |
|
54 } |
|
55 } |
|
56 |
|
57 template <class T> |
|
58 Array<T> |
|
59 Array<T>::value (void) |
|
60 { |
|
61 Array<T> retval; |
|
62 |
3512
|
63 retval = index (idx[0]); |
2381
|
64 |
|
65 clear_index (); |
|
66 |
|
67 return retval; |
|
68 } |
|
69 |
|
70 template <class T> |
|
71 Array<T> |
4461
|
72 Array<T>::index (idx_vector& idx_arg, int resize_ok, const T& rfv) const |
2381
|
73 { |
|
74 Array<T> retval; |
|
75 |
1559
|
76 int len = length (); |
|
77 |
3933
|
78 int n = idx_arg.freeze (len, "vector", resize_ok); |
1559
|
79 |
3512
|
80 if (idx_arg) |
1559
|
81 { |
3512
|
82 if (idx_arg.is_colon_equiv (len)) |
1559
|
83 { |
|
84 retval = *this; |
|
85 } |
|
86 else if (n == 0) |
|
87 { |
|
88 retval.resize (0); |
|
89 } |
|
90 else if (len == 1 && n > 1 |
3512
|
91 && idx_arg.one_zero_only () |
|
92 && idx_arg.ones_count () == n) |
1559
|
93 { |
|
94 retval.resize (n, elem (0)); |
|
95 } |
|
96 else |
|
97 { |
|
98 retval.resize (n); |
|
99 |
|
100 for (int i = 0; i < n; i++) |
|
101 { |
3512
|
102 int ii = idx_arg.elem (i); |
4409
|
103 if (ii >= len) |
4461
|
104 retval.elem (i) = rfv; |
3933
|
105 else |
|
106 retval.elem (i) = elem (ii); |
1559
|
107 } |
|
108 } |
|
109 } |
2381
|
110 |
1559
|
111 // idx_vector::freeze() printed an error message for us. |
|
112 |
|
113 return retval; |
|
114 } |
|
115 |
|
116 template <class T> |
|
117 void |
3512
|
118 Array<T>::maybe_delete_elements (idx_vector& idx_arg) |
1559
|
119 { |
|
120 int len = length (); |
|
121 |
|
122 if (len == 0) |
|
123 return; |
|
124 |
3512
|
125 if (idx_arg.is_colon_equiv (len, 1)) |
1559
|
126 resize (0); |
|
127 else |
|
128 { |
3512
|
129 int num_to_delete = idx_arg.length (len); |
1559
|
130 |
|
131 if (num_to_delete != 0) |
|
132 { |
2245
|
133 int new_len = len; |
|
134 |
|
135 int iidx = 0; |
|
136 |
|
137 for (int i = 0; i < len; i++) |
3512
|
138 if (i == idx_arg.elem (iidx)) |
2245
|
139 { |
|
140 iidx++; |
|
141 new_len--; |
2918
|
142 |
|
143 if (iidx == num_to_delete) |
|
144 break; |
2245
|
145 } |
|
146 |
1559
|
147 if (new_len > 0) |
|
148 { |
|
149 T *new_data = new T [new_len]; |
|
150 |
|
151 int ii = 0; |
2245
|
152 iidx = 0; |
1559
|
153 for (int i = 0; i < len; i++) |
|
154 { |
3512
|
155 if (iidx < num_to_delete && i == idx_arg.elem (iidx)) |
1559
|
156 iidx++; |
|
157 else |
|
158 { |
|
159 new_data[ii] = elem (i); |
|
160 ii++; |
|
161 } |
|
162 } |
|
163 |
|
164 if (--rep->count <= 0) |
|
165 delete rep; |
|
166 |
4054
|
167 rep = new typename Array<T>::ArrayRep (new_data, new_len); |
1694
|
168 |
1559
|
169 set_max_indices (1); |
|
170 } |
|
171 else |
|
172 (*current_liboctave_error_handler) |
1634
|
173 ("A(idx) = []: index out of range"); |
1559
|
174 } |
|
175 } |
|
176 } |
|
177 |
2014
|
178 // ??? FIXME ??? -- this does not handle assignment of empty vectors |
1559
|
179 // to delete elements. Should it? |
|
180 |
|
181 template <class LT, class RT> |
|
182 int |
4461
|
183 assign (Array<LT>& lhs, const Array<RT>& rhs, const LT& rfv) |
1559
|
184 { |
|
185 int retval = 1; |
|
186 |
|
187 idx_vector *tmp = lhs.get_idx (); |
|
188 |
3512
|
189 idx_vector lhs_idx = tmp[0]; |
1559
|
190 |
|
191 int lhs_len = lhs.length (); |
|
192 int rhs_len = rhs.length (); |
|
193 |
4461
|
194 int n = lhs_idx.freeze (lhs_len, "vector", true, liboctave_wrore_flag); |
1559
|
195 |
|
196 if (n != 0) |
|
197 { |
4461
|
198 if (rhs_len == n || rhs_len == 1) |
1559
|
199 { |
3512
|
200 int max_idx = lhs_idx.max () + 1; |
1559
|
201 if (max_idx > lhs_len) |
4461
|
202 lhs.resize (max_idx, rfv); |
1559
|
203 } |
|
204 |
|
205 if (rhs_len == n) |
|
206 { |
|
207 for (int i = 0; i < n; i++) |
|
208 { |
3512
|
209 int ii = lhs_idx.elem (i); |
1559
|
210 lhs.elem (ii) = rhs.elem (i); |
|
211 } |
|
212 } |
|
213 else if (rhs_len == 1) |
|
214 { |
|
215 RT scalar = rhs.elem (0); |
|
216 |
|
217 for (int i = 0; i < n; i++) |
|
218 { |
3512
|
219 int ii = lhs_idx.elem (i); |
1559
|
220 lhs.elem (ii) = scalar; |
|
221 } |
|
222 } |
|
223 else |
|
224 { |
|
225 (*current_liboctave_error_handler) |
|
226 ("A(I) = X: X must be a scalar or a vector with same length as I"); |
|
227 |
|
228 retval = 0; |
|
229 } |
|
230 } |
3512
|
231 else if (lhs_idx.is_colon ()) |
2112
|
232 { |
|
233 if (lhs_len == 0) |
|
234 { |
|
235 lhs.resize (rhs_len); |
|
236 |
|
237 for (int i = 0; i < rhs_len; i++) |
|
238 lhs.elem (i) = rhs.elem (i); |
|
239 } |
|
240 else |
|
241 (*current_liboctave_error_handler) |
|
242 ("A(:) = X: A must be the same size as X"); |
|
243 } |
3177
|
244 else if (! (rhs_len == 1 || rhs_len == 0)) |
1559
|
245 { |
3177
|
246 (*current_liboctave_error_handler) |
|
247 ("A([]) = X: X must also be an empty matrix or a scalar"); |
|
248 |
|
249 retval = 0; |
1559
|
250 } |
|
251 |
|
252 lhs.clear_index (); |
|
253 |
|
254 return retval; |
|
255 } |
|
256 |
|
257 /* |
|
258 ;;; Local Variables: *** |
|
259 ;;; mode: C++ *** |
|
260 ;;; End: *** |
|
261 */ |