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> |
|
29 |
|
30 #include "Matrix.h" |
|
31 #include "Range.h" |
1
|
32 #include "idx-vector.h" |
164
|
33 #include "user-prefs.h" |
1
|
34 #include "error.h" |
|
35 #include "utils.h" |
|
36 |
|
37 idx_vector::idx_vector (const idx_vector& a) |
|
38 { |
191
|
39 initialized = a.initialized; |
|
40 |
1
|
41 len = a.len; |
|
42 if (len > 0) |
|
43 { |
|
44 data = new int [len]; |
|
45 for (int i = 0; i < len; i++) |
|
46 data[i] = a.data[i]; |
|
47 |
|
48 num_zeros = a.num_zeros; |
|
49 num_ones = a.num_ones; |
|
50 one_zero = a.one_zero; |
|
51 |
|
52 max_val = a.max_val; |
|
53 min_val = a.min_val; |
|
54 } |
|
55 else |
191
|
56 data = (int *) 0; |
1
|
57 } |
|
58 |
|
59 static inline int |
|
60 tree_to_mat_idx (double x) |
|
61 { |
|
62 if (x > 0) |
|
63 return ((int) (x + 0.5) - 1); |
|
64 else |
|
65 return ((int) (x - 0.5) - 1); |
|
66 } |
|
67 |
164
|
68 idx_vector::idx_vector (const Matrix& m, int do_ftn_idx, |
|
69 const char *rc, int z_len = 0) |
1
|
70 { |
191
|
71 initialized = 0; |
|
72 |
1
|
73 int nr = m.rows (); |
|
74 int nc = m.columns (); |
|
75 |
|
76 if (nr == 0 || nc == 0) |
|
77 { |
|
78 len = 0; |
191
|
79 data = (int *) 0; |
1
|
80 num_zeros = 0; |
|
81 num_ones = 0; |
|
82 one_zero = 0; |
191
|
83 initialized = 1; |
1
|
84 return; |
|
85 } |
|
86 else if (nr > 1 && nc > 1 && do_ftn_idx) |
|
87 { |
|
88 double *cop_out = m.fortran_vec (); |
|
89 len = nr * nc; |
|
90 data = new int [len]; |
|
91 for (int i = 0; i < len; i++) |
|
92 data[i] = tree_to_mat_idx (*cop_out++); |
|
93 } |
|
94 else if (nr == 1 && nc > 0) |
|
95 { |
|
96 len = nc; |
|
97 data = new int [len]; |
|
98 for (int i = 0; i < len; i++) |
|
99 data[i] = tree_to_mat_idx (m.elem (0, i)); |
|
100 } |
|
101 else if (nc == 1 && nr > 0) |
|
102 { |
|
103 len = nr; |
|
104 data = new int [len]; |
|
105 for (int i = 0; i < len; i++) |
|
106 data[i] = tree_to_mat_idx (m.elem (i, 0)); |
|
107 } |
|
108 else |
|
109 { |
191
|
110 error ("invalid matrix used as index"); |
|
111 return; |
1
|
112 } |
|
113 |
|
114 init_state (rc, z_len); |
|
115 } |
|
116 |
|
117 idx_vector::idx_vector (const Range& r) |
|
118 { |
191
|
119 initialized = 0; |
|
120 |
1
|
121 len = r.nelem (); |
|
122 |
191
|
123 if (len < 0) |
|
124 { |
|
125 error ("invalid range used as index"); |
|
126 return; |
|
127 } |
|
128 else if (len == 0) |
|
129 { |
|
130 data = (int *) 0; |
|
131 num_zeros = 0; |
|
132 num_ones = 0; |
|
133 one_zero = 0; |
|
134 initialized = 1; |
|
135 return; |
|
136 } |
1
|
137 |
|
138 double b = r.base (); |
|
139 double step = r.inc (); |
|
140 |
|
141 data = new int [len]; |
|
142 |
|
143 for (int i = 0; i < len; i++) |
|
144 { |
|
145 double val = b + i * step; |
|
146 data[i] = tree_to_mat_idx (val); |
|
147 } |
|
148 |
|
149 init_state (); |
|
150 } |
|
151 |
|
152 idx_vector& |
|
153 idx_vector::operator = (const idx_vector& a) |
|
154 { |
|
155 if (this != &a) |
|
156 { |
191
|
157 initialized = a.initialized; |
|
158 |
1
|
159 delete [] data; |
|
160 len = a.len; |
|
161 data = new int [len]; |
|
162 for (int i = 0; i < len; i++) |
|
163 data[i] = a.data[i]; |
|
164 |
|
165 num_zeros = a.num_zeros; |
|
166 num_ones = a.num_ones; |
|
167 one_zero = a.one_zero; |
|
168 |
|
169 max_val = a.max_val; |
|
170 min_val = a.min_val; |
|
171 } |
|
172 return *this; |
|
173 } |
|
174 |
|
175 void |
164
|
176 idx_vector::init_state (const char *rc, int z_len = 0) |
1
|
177 { |
|
178 one_zero = 1; |
|
179 num_zeros = 0; |
|
180 num_ones = 0; |
|
181 |
|
182 min_val = max_val = data[0]; |
|
183 |
|
184 int i = 0; |
|
185 do |
|
186 { |
|
187 if (data[i] == -1) |
|
188 num_zeros++; |
|
189 else if (data[i] == 0) |
|
190 num_ones++; |
|
191 |
|
192 if (one_zero && data[i] != -1 && data[i] != 0) |
|
193 one_zero = 0; |
|
194 |
|
195 if (data[i] > max_val) |
|
196 max_val = data[i]; |
|
197 |
|
198 if (data[i] < min_val) |
|
199 min_val = data[i]; |
|
200 } |
|
201 while (++i < len); |
|
202 |
|
203 if (one_zero && z_len == len) |
|
204 { |
|
205 if (num_zeros == len) |
|
206 { |
|
207 delete [] data; |
|
208 len = 0; |
191
|
209 data = (int *) 0; |
1
|
210 num_zeros = 0; |
|
211 num_ones = 0; |
|
212 one_zero = 0; |
|
213 } |
|
214 else if (num_ones != len || user_pref.prefer_zero_one_indexing) |
|
215 convert_one_zero_to_idx (); |
|
216 } |
|
217 else if (min_val < 0) |
|
218 { |
|
219 error ("%s index %d out of range", rc, min_val+1); |
191
|
220 initialized = 0; |
|
221 return; |
1
|
222 } |
191
|
223 |
|
224 initialized = 1; |
1
|
225 } |
|
226 |
|
227 void |
|
228 idx_vector::convert_one_zero_to_idx (void) |
|
229 { |
|
230 if (num_ones == 0) |
|
231 { |
|
232 len = 0; |
|
233 max_val = 0; |
|
234 min_val = 0; |
|
235 delete [] data; |
|
236 } |
|
237 else |
|
238 { |
|
239 assert (num_ones + num_zeros == len); |
|
240 |
|
241 int *new_data = new int [num_ones]; |
|
242 int count = 0; |
|
243 for (int i = 0; i < len; i++) |
|
244 if (data[i] == 0) |
|
245 new_data[count++] = i; |
|
246 |
|
247 delete [] data; |
|
248 len = num_ones; |
|
249 data = new_data; |
|
250 |
|
251 min_val = max_val = data[0]; |
|
252 |
|
253 i = 0; |
|
254 do |
|
255 { |
|
256 if (data[i] > max_val) |
|
257 max_val = data[i]; |
|
258 |
|
259 if (data[i] < min_val) |
|
260 min_val = data[i]; |
|
261 } |
|
262 while (++i < len); |
|
263 } |
|
264 } |
|
265 |
|
266 ostream& |
|
267 operator << (ostream& os, const idx_vector& a) |
|
268 { |
|
269 for (int i = 0; i < a.len; i++) |
|
270 os << a.data[i] << "\n"; |
|
271 return os; |
|
272 } |
|
273 |
|
274 /* |
|
275 ;;; Local Variables: *** |
|
276 ;;; mode: C++ *** |
|
277 ;;; page-delimiter: "^/\\*" *** |
|
278 ;;; End: *** |
|
279 */ |