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