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 |
453
|
36 #include "dMatrix.h" |
164
|
37 #include "Range.h" |
1
|
38 #include "idx-vector.h" |
164
|
39 #include "user-prefs.h" |
1
|
40 #include "error.h" |
|
41 #include "utils.h" |
|
42 |
|
43 idx_vector::idx_vector (const idx_vector& a) |
|
44 { |
1135
|
45 data = 0; |
191
|
46 initialized = a.initialized; |
|
47 |
1
|
48 len = a.len; |
|
49 if (len > 0) |
|
50 { |
|
51 data = new int [len]; |
|
52 for (int i = 0; i < len; i++) |
|
53 data[i] = a.data[i]; |
|
54 |
|
55 num_zeros = a.num_zeros; |
|
56 num_ones = a.num_ones; |
|
57 one_zero = a.one_zero; |
|
58 |
|
59 max_val = a.max_val; |
|
60 min_val = a.min_val; |
|
61 } |
|
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 { |
1135
|
76 data = 0; |
191
|
77 initialized = 0; |
|
78 |
1
|
79 int nr = m.rows (); |
|
80 int nc = m.columns (); |
|
81 |
|
82 if (nr == 0 || nc == 0) |
|
83 { |
|
84 len = 0; |
|
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 { |
1135
|
124 data = 0; |
191
|
125 initialized = 0; |
|
126 |
1
|
127 len = r.nelem (); |
|
128 |
191
|
129 if (len < 0) |
|
130 { |
240
|
131 ::error ("invalid range used as index"); |
191
|
132 return; |
|
133 } |
|
134 else if (len == 0) |
|
135 { |
|
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 { |
1130
|
210 if (num_ones != len || user_pref.prefer_zero_one_indexing) |
1
|
211 convert_one_zero_to_idx (); |
|
212 } |
|
213 else if (min_val < 0) |
|
214 { |
240
|
215 ::error ("%s index %d out of range", rc, min_val+1); |
191
|
216 initialized = 0; |
|
217 return; |
1
|
218 } |
942
|
219 #if 0 |
|
220 // Checking max index against size won't work right here unless we |
|
221 // also look at resize on range error, and we have to do that later |
|
222 // on anyway. |
|
223 |
852
|
224 else if (max_val >= z_len) |
|
225 { |
|
226 ::error ("%s index %d out of range", rc, max_val+1); |
|
227 initialized = 0; |
|
228 return; |
|
229 } |
942
|
230 #endif |
191
|
231 |
|
232 initialized = 1; |
1
|
233 } |
|
234 |
|
235 void |
|
236 idx_vector::convert_one_zero_to_idx (void) |
|
237 { |
|
238 if (num_ones == 0) |
|
239 { |
|
240 len = 0; |
|
241 max_val = 0; |
|
242 min_val = 0; |
|
243 delete [] data; |
1130
|
244 data = 0; |
1
|
245 } |
|
246 else |
|
247 { |
|
248 assert (num_ones + num_zeros == len); |
|
249 |
|
250 int *new_data = new int [num_ones]; |
|
251 int count = 0; |
|
252 for (int i = 0; i < len; i++) |
|
253 if (data[i] == 0) |
|
254 new_data[count++] = i; |
|
255 |
|
256 delete [] data; |
|
257 len = num_ones; |
|
258 data = new_data; |
|
259 |
|
260 min_val = max_val = data[0]; |
|
261 |
1321
|
262 int i = 0; |
1
|
263 do |
|
264 { |
|
265 if (data[i] > max_val) |
|
266 max_val = data[i]; |
|
267 |
|
268 if (data[i] < min_val) |
|
269 min_val = data[i]; |
|
270 } |
|
271 while (++i < len); |
|
272 } |
|
273 } |
|
274 |
209
|
275 static inline int |
|
276 intcmp (int *i, int *j) |
|
277 { |
|
278 return (*i - *j); |
|
279 } |
|
280 |
227
|
281 int |
|
282 idx_vector::checkelem (int n) const |
|
283 { |
|
284 if (n < 0 || n >= len) |
|
285 { |
240
|
286 ::error ("idx-vector: index out of range"); |
227
|
287 return 0; |
|
288 } |
|
289 |
|
290 return elem (n); |
|
291 } |
|
292 |
209
|
293 void |
|
294 idx_vector::sort (void) |
|
295 { |
|
296 qsort ((void *) data, len, sizeof (int), |
526
|
297 (int (*)(const void*, const void*)) intcmp); |
209
|
298 } |
|
299 |
417
|
300 void |
|
301 idx_vector::sort_uniq (void) |
|
302 { |
|
303 if (len > 0) |
|
304 { |
|
305 sort (); |
|
306 |
|
307 int *new_data = new int [len]; |
|
308 new_data[0] = data[0]; |
|
309 int k = 0; |
|
310 for (int i = 1; i < len; i++) |
|
311 { |
|
312 if (data[i] != new_data[k]) |
|
313 { |
|
314 k++; |
|
315 new_data[k] = data[i]; |
|
316 } |
|
317 } |
|
318 delete [] data; |
|
319 len = k+1; |
|
320 data = new_data; |
|
321 } |
|
322 } |
|
323 |
434
|
324 void |
|
325 idx_vector::shorten (int n) |
|
326 { |
|
327 if (n > 0 && n <= len) |
|
328 len = n; |
|
329 else |
|
330 panic_impossible (); |
|
331 } |
|
332 |
1
|
333 ostream& |
|
334 operator << (ostream& os, const idx_vector& a) |
|
335 { |
|
336 for (int i = 0; i < a.len; i++) |
|
337 os << a.data[i] << "\n"; |
|
338 return os; |
|
339 } |
|
340 |
|
341 /* |
|
342 ;;; Local Variables: *** |
|
343 ;;; mode: C++ *** |
|
344 ;;; page-delimiter: "^/\\*" *** |
|
345 ;;; End: *** |
|
346 */ |