5164
|
1 /* |
|
2 |
|
3 Copyright (C) 2004 David Bateman |
|
4 Copyright (C) 1998-2004 Andy Adler |
|
5 |
|
6 Octave is free software; you can redistribute it and/or modify it |
|
7 under the terms of the GNU General Public License as published by the |
|
8 Free Software Foundation; either version 2, or (at your option) any |
|
9 later version. |
|
10 |
|
11 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 for more details. |
|
15 |
|
16 You should have received a copy of the GNU General Public License |
5307
|
17 along with this program; see the file COPYING. If not, write to the |
|
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
19 Boston, MA 02110-1301, USA. |
5164
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
|
27 #include <iostream> |
|
28 #include <vector> |
|
29 |
|
30 #include "config.h" |
|
31 #include "quit.h" |
|
32 #include "lo-ieee.h" |
|
33 #include "lo-mappers.h" |
|
34 |
|
35 #include "boolSparse.h" |
|
36 |
|
37 // SparseBoolMatrix class. |
|
38 |
|
39 bool |
|
40 SparseBoolMatrix::operator == (const SparseBoolMatrix& a) const |
|
41 { |
5275
|
42 octave_idx_type nr = rows (); |
|
43 octave_idx_type nc = cols (); |
5604
|
44 octave_idx_type nz = nzmax (); |
5275
|
45 octave_idx_type nr_a = a.rows (); |
|
46 octave_idx_type nc_a = a.cols (); |
5604
|
47 octave_idx_type nz_a = a.nzmax (); |
5164
|
48 |
|
49 if (nr != nr_a || nc != nc_a || nz != nz_a) |
|
50 return false; |
|
51 |
5275
|
52 for (octave_idx_type i = 0; i < nc + 1; i++) |
5164
|
53 if (cidx(i) != a.cidx(i)) |
|
54 return false; |
|
55 |
5275
|
56 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
57 if (data(i) != a.data(i) || ridx(i) != a.ridx(i)) |
|
58 return false; |
|
59 |
|
60 return true; |
|
61 } |
|
62 |
|
63 bool |
|
64 SparseBoolMatrix::operator != (const SparseBoolMatrix& a) const |
|
65 { |
|
66 return !(*this == a); |
|
67 } |
|
68 |
|
69 SparseBoolMatrix& |
5275
|
70 SparseBoolMatrix::insert (const SparseBoolMatrix& a, octave_idx_type r, octave_idx_type c) |
5164
|
71 { |
|
72 Sparse<bool>::insert (a, r, c); |
|
73 return *this; |
|
74 } |
|
75 |
6823
|
76 SparseBoolMatrix& |
|
77 SparseBoolMatrix::insert (const SparseBoolMatrix& a, const Array<octave_idx_type>& indx) |
|
78 { |
|
79 Sparse<bool>::insert (a, indx); |
|
80 return *this; |
|
81 } |
|
82 |
5164
|
83 SparseBoolMatrix |
5275
|
84 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164
|
85 { |
|
86 // Don't use numel to avoid all possiblity of an overflow |
|
87 if (rb.rows () > 0 && rb.cols () > 0) |
|
88 insert (rb, ra_idx(0), ra_idx(1)); |
|
89 return *this; |
|
90 } |
|
91 |
|
92 // unary operations |
|
93 |
|
94 SparseBoolMatrix |
|
95 SparseBoolMatrix::operator ! (void) const |
|
96 { |
5275
|
97 octave_idx_type nr = rows (); |
|
98 octave_idx_type nc = cols (); |
5604
|
99 octave_idx_type nz1 = nzmax (); |
5275
|
100 octave_idx_type nz2 = nr*nc - nz1; |
5164
|
101 |
|
102 SparseBoolMatrix r (nr, nc, nz2); |
|
103 |
5275
|
104 octave_idx_type ii = 0; |
|
105 octave_idx_type jj = 0; |
6217
|
106 r.cidx (0) = 0; |
5275
|
107 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
108 { |
5275
|
109 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
110 { |
|
111 if (jj < cidx(i+1) && ridx(jj) == j) |
|
112 jj++; |
|
113 else |
|
114 { |
|
115 r.data(ii) = true; |
|
116 r.ridx(ii++) = j; |
|
117 } |
|
118 } |
6217
|
119 r.cidx (i+1) = ii; |
5164
|
120 } |
|
121 |
|
122 return r; |
|
123 } |
|
124 |
|
125 // other operations |
|
126 |
5775
|
127 // FIXME Do these really belong here? Maybe they should be |
5164
|
128 // in a base class? |
|
129 |
|
130 SparseBoolMatrix |
|
131 SparseBoolMatrix::all (int dim) const |
|
132 { |
|
133 SPARSE_ALL_OP (dim); |
|
134 } |
|
135 |
|
136 SparseBoolMatrix |
|
137 SparseBoolMatrix::any (int dim) const |
|
138 { |
|
139 SPARSE_ANY_OP (dim); |
|
140 } |
|
141 |
|
142 boolMatrix |
|
143 SparseBoolMatrix::matrix_value (void) const |
|
144 { |
5275
|
145 octave_idx_type nr = rows (); |
|
146 octave_idx_type nc = cols (); |
5164
|
147 |
|
148 boolMatrix retval (nr, nc, false); |
5275
|
149 for (octave_idx_type j = 0; j < nc; j++) |
|
150 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
151 retval.elem (ridx(i), j) = data (i); |
|
152 |
|
153 return retval; |
|
154 } |
|
155 |
|
156 std::ostream& |
|
157 operator << (std::ostream& os, const SparseBoolMatrix& a) |
|
158 { |
5275
|
159 octave_idx_type nc = a.cols (); |
5164
|
160 |
|
161 // add one to the printed indices to go from |
|
162 // zero-based to one-based arrays |
5275
|
163 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
164 { |
|
165 OCTAVE_QUIT; |
5275
|
166 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
167 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
|
168 } |
|
169 |
|
170 return os; |
|
171 } |
|
172 |
|
173 std::istream& |
|
174 operator >> (std::istream& is, SparseBoolMatrix& a) |
|
175 { |
5275
|
176 octave_idx_type nr = a.rows (); |
|
177 octave_idx_type nc = a.cols (); |
5604
|
178 octave_idx_type nz = a.nzmax (); |
5164
|
179 |
|
180 if (nr < 1 || nc < 1) |
|
181 is.clear (std::ios::badbit); |
|
182 else |
|
183 { |
5275
|
184 octave_idx_type itmp, jtmp, jold = 0; |
5164
|
185 bool tmp; |
5275
|
186 octave_idx_type ii = 0; |
5164
|
187 |
|
188 a.cidx (0) = 0; |
5275
|
189 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
190 { |
|
191 is >> itmp; |
|
192 itmp--; |
|
193 is >> jtmp; |
|
194 jtmp--; |
|
195 is >> tmp; |
|
196 if (is) |
|
197 { |
|
198 if (jold != jtmp) |
|
199 { |
5275
|
200 for (octave_idx_type j = jold; j < jtmp; j++) |
5164
|
201 a.cidx(j+1) = ii; |
|
202 |
|
203 jold = jtmp; |
|
204 } |
|
205 a.data (ii) = tmp; |
|
206 a.ridx (ii++) = itmp; |
|
207 } |
|
208 else |
|
209 goto done; |
|
210 } |
|
211 |
5275
|
212 for (octave_idx_type j = jold; j < nc; j++) |
5164
|
213 a.cidx(j+1) = ii; |
|
214 } |
|
215 |
|
216 done: |
|
217 |
|
218 return is; |
|
219 } |
|
220 |
|
221 SparseBoolMatrix |
|
222 SparseBoolMatrix::squeeze (void) const |
|
223 { |
|
224 return Sparse<bool>::squeeze (); |
|
225 } |
|
226 |
|
227 SparseBoolMatrix |
|
228 SparseBoolMatrix::index (idx_vector& i, int resize_ok) const |
|
229 { |
|
230 return Sparse<bool>::index (i, resize_ok); |
|
231 } |
|
232 |
|
233 SparseBoolMatrix |
|
234 SparseBoolMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const |
|
235 { |
|
236 return Sparse<bool>::index (i, j, resize_ok); |
|
237 } |
|
238 |
|
239 SparseBoolMatrix |
|
240 SparseBoolMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
241 { |
|
242 return Sparse<bool>::index (ra_idx, resize_ok); |
|
243 } |
|
244 |
|
245 SparseBoolMatrix |
|
246 SparseBoolMatrix::reshape (const dim_vector& new_dims) const |
|
247 { |
|
248 return Sparse<bool>::reshape (new_dims); |
|
249 } |
|
250 |
|
251 SparseBoolMatrix |
5275
|
252 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164
|
253 { |
|
254 return Sparse<bool>::permute (vec, inv); |
|
255 } |
|
256 |
|
257 SparseBoolMatrix |
5275
|
258 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164
|
259 { |
|
260 return Sparse<bool>::ipermute (vec); |
|
261 } |
|
262 |
|
263 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) |
|
264 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) |
|
265 |
|
266 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) |
|
267 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) |
|
268 |
|
269 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) |
|
270 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |
|
271 |
|
272 |
|
273 /* |
|
274 ;;; Local Variables: *** |
|
275 ;;; mode: C++ *** |
|
276 ;;; End: *** |
|
277 */ |