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