519
|
1 // idx-vector.cc -*- C++ -*- |
1
|
2 /* |
|
3 |
1009
|
4 Copyright (C) 1992, 1993, 1994, 1995 John W. Eaton |
1
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
21 |
|
22 */ |
|
23 |
1297
|
24 #if defined (__GNUG__) |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
240
|
28 #ifdef HAVE_CONFIG_H |
1192
|
29 #include <config.h> |
1
|
30 #endif |
|
31 |
1343
|
32 #include <cstdlib> |
|
33 |
164
|
34 #include <iostream.h> |
|
35 |
1352
|
36 #include "Range.h" |
453
|
37 #include "dMatrix.h" |
1352
|
38 |
|
39 #include "error.h" |
1
|
40 #include "idx-vector.h" |
164
|
41 #include "user-prefs.h" |
1
|
42 #include "utils.h" |
|
43 |
|
44 idx_vector::idx_vector (const idx_vector& a) |
|
45 { |
1135
|
46 data = 0; |
191
|
47 initialized = a.initialized; |
|
48 |
1
|
49 len = a.len; |
|
50 if (len > 0) |
|
51 { |
|
52 data = new int [len]; |
|
53 for (int i = 0; i < len; i++) |
|
54 data[i] = a.data[i]; |
|
55 |
|
56 num_zeros = a.num_zeros; |
|
57 num_ones = a.num_ones; |
|
58 one_zero = a.one_zero; |
|
59 |
|
60 max_val = a.max_val; |
|
61 min_val = a.min_val; |
|
62 } |
|
63 } |
|
64 |
|
65 static inline int |
|
66 tree_to_mat_idx (double x) |
|
67 { |
|
68 if (x > 0) |
|
69 return ((int) (x + 0.5) - 1); |
|
70 else |
|
71 return ((int) (x - 0.5) - 1); |
|
72 } |
|
73 |
164
|
74 idx_vector::idx_vector (const Matrix& m, int do_ftn_idx, |
526
|
75 const char *rc, int z_len) |
1
|
76 { |
1135
|
77 data = 0; |
191
|
78 initialized = 0; |
|
79 |
1
|
80 int nr = m.rows (); |
|
81 int nc = m.columns (); |
|
82 |
|
83 if (nr == 0 || nc == 0) |
|
84 { |
|
85 len = 0; |
|
86 num_zeros = 0; |
|
87 num_ones = 0; |
|
88 one_zero = 0; |
191
|
89 initialized = 1; |
1
|
90 return; |
|
91 } |
|
92 else if (nr > 1 && nc > 1 && do_ftn_idx) |
|
93 { |
240
|
94 const double *cop_out = m.data (); |
1
|
95 len = nr * nc; |
|
96 data = new int [len]; |
|
97 for (int i = 0; i < len; i++) |
|
98 data[i] = tree_to_mat_idx (*cop_out++); |
|
99 } |
|
100 else if (nr == 1 && nc > 0) |
|
101 { |
|
102 len = nc; |
|
103 data = new int [len]; |
|
104 for (int i = 0; i < len; i++) |
|
105 data[i] = tree_to_mat_idx (m.elem (0, i)); |
|
106 } |
|
107 else if (nc == 1 && nr > 0) |
|
108 { |
|
109 len = nr; |
|
110 data = new int [len]; |
|
111 for (int i = 0; i < len; i++) |
|
112 data[i] = tree_to_mat_idx (m.elem (i, 0)); |
|
113 } |
|
114 else |
|
115 { |
240
|
116 ::error ("invalid matrix used as index"); |
191
|
117 return; |
1
|
118 } |
|
119 |
|
120 init_state (rc, z_len); |
|
121 } |
|
122 |
|
123 idx_vector::idx_vector (const Range& r) |
|
124 { |
1135
|
125 data = 0; |
191
|
126 initialized = 0; |
|
127 |
1
|
128 len = r.nelem (); |
|
129 |
191
|
130 if (len < 0) |
|
131 { |
240
|
132 ::error ("invalid range used as index"); |
191
|
133 return; |
|
134 } |
|
135 else if (len == 0) |
|
136 { |
|
137 num_zeros = 0; |
|
138 num_ones = 0; |
|
139 one_zero = 0; |
|
140 initialized = 1; |
|
141 return; |
|
142 } |
1
|
143 |
|
144 double b = r.base (); |
|
145 double step = r.inc (); |
|
146 |
|
147 data = new int [len]; |
|
148 |
|
149 for (int i = 0; i < len; i++) |
|
150 { |
|
151 double val = b + i * step; |
|
152 data[i] = tree_to_mat_idx (val); |
|
153 } |
|
154 |
|
155 init_state (); |
|
156 } |
|
157 |
|
158 idx_vector& |
|
159 idx_vector::operator = (const idx_vector& a) |
|
160 { |
|
161 if (this != &a) |
|
162 { |
191
|
163 initialized = a.initialized; |
|
164 |
1
|
165 delete [] data; |
|
166 len = a.len; |
|
167 data = new int [len]; |
|
168 for (int i = 0; i < len; i++) |
|
169 data[i] = a.data[i]; |
|
170 |
|
171 num_zeros = a.num_zeros; |
|
172 num_ones = a.num_ones; |
|
173 one_zero = a.one_zero; |
|
174 |
|
175 max_val = a.max_val; |
|
176 min_val = a.min_val; |
|
177 } |
|
178 return *this; |
|
179 } |
|
180 |
|
181 void |
526
|
182 idx_vector::init_state (const char *rc, int z_len) |
1
|
183 { |
|
184 one_zero = 1; |
|
185 num_zeros = 0; |
|
186 num_ones = 0; |
|
187 |
|
188 min_val = max_val = data[0]; |
|
189 |
|
190 int i = 0; |
|
191 do |
|
192 { |
|
193 if (data[i] == -1) |
|
194 num_zeros++; |
|
195 else if (data[i] == 0) |
|
196 num_ones++; |
|
197 |
|
198 if (one_zero && data[i] != -1 && data[i] != 0) |
|
199 one_zero = 0; |
|
200 |
|
201 if (data[i] > max_val) |
|
202 max_val = data[i]; |
|
203 |
|
204 if (data[i] < min_val) |
|
205 min_val = data[i]; |
|
206 } |
|
207 while (++i < len); |
|
208 |
|
209 if (one_zero && z_len == len) |
|
210 { |
1130
|
211 if (num_ones != len || user_pref.prefer_zero_one_indexing) |
1
|
212 convert_one_zero_to_idx (); |
|
213 } |
|
214 else if (min_val < 0) |
|
215 { |
240
|
216 ::error ("%s index %d out of range", rc, min_val+1); |
191
|
217 initialized = 0; |
|
218 return; |
1
|
219 } |
942
|
220 #if 0 |
1358
|
221 // Checking max index against size won't work right here unless we |
|
222 // also look at resize on range error, and we have to do that later |
|
223 // on anyway. |
942
|
224 |
852
|
225 else if (max_val >= z_len) |
|
226 { |
|
227 ::error ("%s index %d out of range", rc, max_val+1); |
|
228 initialized = 0; |
|
229 return; |
|
230 } |
942
|
231 #endif |
191
|
232 |
|
233 initialized = 1; |
1
|
234 } |
|
235 |
|
236 void |
|
237 idx_vector::convert_one_zero_to_idx (void) |
|
238 { |
|
239 if (num_ones == 0) |
|
240 { |
|
241 len = 0; |
|
242 max_val = 0; |
|
243 min_val = 0; |
|
244 delete [] data; |
1130
|
245 data = 0; |
1
|
246 } |
|
247 else |
|
248 { |
|
249 assert (num_ones + num_zeros == len); |
|
250 |
|
251 int *new_data = new int [num_ones]; |
|
252 int count = 0; |
|
253 for (int i = 0; i < len; i++) |
|
254 if (data[i] == 0) |
|
255 new_data[count++] = i; |
|
256 |
|
257 delete [] data; |
|
258 len = num_ones; |
|
259 data = new_data; |
|
260 |
|
261 min_val = max_val = data[0]; |
|
262 |
1321
|
263 int i = 0; |
1
|
264 do |
|
265 { |
|
266 if (data[i] > max_val) |
|
267 max_val = data[i]; |
|
268 |
|
269 if (data[i] < min_val) |
|
270 min_val = data[i]; |
|
271 } |
|
272 while (++i < len); |
|
273 } |
|
274 } |
|
275 |
209
|
276 static inline int |
|
277 intcmp (int *i, int *j) |
|
278 { |
|
279 return (*i - *j); |
|
280 } |
|
281 |
227
|
282 int |
|
283 idx_vector::checkelem (int n) const |
|
284 { |
|
285 if (n < 0 || n >= len) |
|
286 { |
240
|
287 ::error ("idx-vector: index out of range"); |
227
|
288 return 0; |
|
289 } |
|
290 |
|
291 return elem (n); |
|
292 } |
|
293 |
209
|
294 void |
|
295 idx_vector::sort (void) |
|
296 { |
|
297 qsort ((void *) data, len, sizeof (int), |
526
|
298 (int (*)(const void*, const void*)) intcmp); |
209
|
299 } |
|
300 |
417
|
301 void |
|
302 idx_vector::sort_uniq (void) |
|
303 { |
|
304 if (len > 0) |
|
305 { |
|
306 sort (); |
|
307 |
|
308 int *new_data = new int [len]; |
|
309 new_data[0] = data[0]; |
|
310 int k = 0; |
|
311 for (int i = 1; i < len; i++) |
|
312 { |
|
313 if (data[i] != new_data[k]) |
|
314 { |
|
315 k++; |
|
316 new_data[k] = data[i]; |
|
317 } |
|
318 } |
|
319 delete [] data; |
|
320 len = k+1; |
|
321 data = new_data; |
|
322 } |
|
323 } |
|
324 |
434
|
325 void |
|
326 idx_vector::shorten (int n) |
|
327 { |
|
328 if (n > 0 && n <= len) |
|
329 len = n; |
|
330 else |
|
331 panic_impossible (); |
|
332 } |
|
333 |
1
|
334 ostream& |
|
335 operator << (ostream& os, const idx_vector& a) |
|
336 { |
|
337 for (int i = 0; i < a.len; i++) |
|
338 os << a.data[i] << "\n"; |
|
339 return os; |
|
340 } |
|
341 |
|
342 /* |
|
343 ;;; Local Variables: *** |
|
344 ;;; mode: C++ *** |
|
345 ;;; page-delimiter: "^/\\*" *** |
|
346 ;;; End: *** |
|
347 */ |