1
|
1 // Very simple integer vectors for indexing -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993 John W. Eaton |
|
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 |
|
24 #ifdef __GNUG__ |
|
25 #pragma implementation |
|
26 #endif |
|
27 |
164
|
28 #include <iostream.h> |
209
|
29 #include <stdlib.h> |
164
|
30 |
|
31 #include "Matrix.h" |
|
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, |
|
70 const char *rc, int z_len = 0) |
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 { |
|
89 double *cop_out = m.fortran_vec (); |
|
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 { |
191
|
111 error ("invalid matrix used as index"); |
|
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 { |
|
126 error ("invalid range used as index"); |
|
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 |
164
|
177 idx_vector::init_state (const char *rc, int z_len = 0) |
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 { |
|
220 error ("%s index %d out of range", rc, min_val+1); |
191
|
221 initialized = 0; |
|
222 return; |
1
|
223 } |
191
|
224 |
|
225 initialized = 1; |
1
|
226 } |
|
227 |
|
228 void |
|
229 idx_vector::convert_one_zero_to_idx (void) |
|
230 { |
|
231 if (num_ones == 0) |
|
232 { |
|
233 len = 0; |
|
234 max_val = 0; |
|
235 min_val = 0; |
|
236 delete [] data; |
|
237 } |
|
238 else |
|
239 { |
|
240 assert (num_ones + num_zeros == len); |
|
241 |
|
242 int *new_data = new int [num_ones]; |
|
243 int count = 0; |
|
244 for (int i = 0; i < len; i++) |
|
245 if (data[i] == 0) |
|
246 new_data[count++] = i; |
|
247 |
|
248 delete [] data; |
|
249 len = num_ones; |
|
250 data = new_data; |
|
251 |
|
252 min_val = max_val = data[0]; |
|
253 |
|
254 i = 0; |
|
255 do |
|
256 { |
|
257 if (data[i] > max_val) |
|
258 max_val = data[i]; |
|
259 |
|
260 if (data[i] < min_val) |
|
261 min_val = data[i]; |
|
262 } |
|
263 while (++i < len); |
|
264 } |
|
265 } |
|
266 |
209
|
267 static inline int |
|
268 intcmp (int *i, int *j) |
|
269 { |
|
270 return (*i - *j); |
|
271 } |
|
272 |
227
|
273 int |
|
274 idx_vector::checkelem (int n) const |
|
275 { |
|
276 if (n < 0 || n >= len) |
|
277 { |
|
278 error ("idx-vector: index out of range"); |
|
279 return 0; |
|
280 } |
|
281 |
|
282 return elem (n); |
|
283 } |
|
284 |
209
|
285 void |
|
286 idx_vector::sort (void) |
|
287 { |
|
288 qsort ((void *) data, len, sizeof (int), |
|
289 (int (*)(void*, void*)) intcmp); |
|
290 } |
|
291 |
1
|
292 ostream& |
|
293 operator << (ostream& os, const idx_vector& a) |
|
294 { |
|
295 for (int i = 0; i < a.len; i++) |
|
296 os << a.data[i] << "\n"; |
|
297 return os; |
|
298 } |
|
299 |
|
300 /* |
|
301 ;;; Local Variables: *** |
|
302 ;;; mode: C++ *** |
|
303 ;;; page-delimiter: "^/\\*" *** |
|
304 ;;; End: *** |
|
305 */ |