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