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