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