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 (); |
|
44 octave_idx_type nz = nnz (); |
|
45 octave_idx_type nr_a = a.rows (); |
|
46 octave_idx_type nc_a = a.cols (); |
|
47 octave_idx_type nz_a = a.nnz (); |
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 |
|
76 SparseBoolMatrix |
5275
|
77 SparseBoolMatrix::concat (const SparseBoolMatrix& rb, const Array<octave_idx_type>& ra_idx) |
5164
|
78 { |
|
79 // Don't use numel to avoid all possiblity of an overflow |
|
80 if (rb.rows () > 0 && rb.cols () > 0) |
|
81 insert (rb, ra_idx(0), ra_idx(1)); |
|
82 return *this; |
|
83 } |
|
84 |
|
85 // unary operations |
|
86 |
|
87 SparseBoolMatrix |
|
88 SparseBoolMatrix::operator ! (void) const |
|
89 { |
5275
|
90 octave_idx_type nr = rows (); |
|
91 octave_idx_type nc = cols (); |
|
92 octave_idx_type nz1 = nnz (); |
|
93 octave_idx_type nz2 = nr*nc - nz1; |
5164
|
94 |
|
95 SparseBoolMatrix r (nr, nc, nz2); |
|
96 |
5275
|
97 octave_idx_type ii = 0; |
|
98 octave_idx_type jj = 0; |
|
99 for (octave_idx_type i = 0; i < nc; i++) |
5164
|
100 { |
5275
|
101 for (octave_idx_type j = 0; j < nr; j++) |
5164
|
102 { |
|
103 if (jj < cidx(i+1) && ridx(jj) == j) |
|
104 jj++; |
|
105 else |
|
106 { |
|
107 r.data(ii) = true; |
|
108 r.ridx(ii++) = j; |
|
109 } |
|
110 } |
|
111 r.cidx (i) = ii; |
|
112 } |
|
113 |
|
114 return r; |
|
115 } |
|
116 |
|
117 // other operations |
|
118 |
|
119 // XXX FIXME XXX Do these really belong here? Maybe they should be |
|
120 // in a base class? |
|
121 |
|
122 SparseBoolMatrix |
|
123 SparseBoolMatrix::all (int dim) const |
|
124 { |
|
125 SPARSE_ALL_OP (dim); |
|
126 } |
|
127 |
|
128 SparseBoolMatrix |
|
129 SparseBoolMatrix::any (int dim) const |
|
130 { |
|
131 SPARSE_ANY_OP (dim); |
|
132 } |
|
133 |
|
134 boolMatrix |
|
135 SparseBoolMatrix::matrix_value (void) const |
|
136 { |
5275
|
137 octave_idx_type nr = rows (); |
|
138 octave_idx_type nc = cols (); |
5164
|
139 |
|
140 boolMatrix retval (nr, nc, false); |
5275
|
141 for (octave_idx_type j = 0; j < nc; j++) |
|
142 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) |
5164
|
143 retval.elem (ridx(i), j) = data (i); |
|
144 |
|
145 return retval; |
|
146 } |
|
147 |
|
148 std::ostream& |
|
149 operator << (std::ostream& os, const SparseBoolMatrix& a) |
|
150 { |
5275
|
151 octave_idx_type nc = a.cols (); |
5164
|
152 |
|
153 // add one to the printed indices to go from |
|
154 // zero-based to one-based arrays |
5275
|
155 for (octave_idx_type j = 0; j < nc; j++) |
5164
|
156 { |
|
157 OCTAVE_QUIT; |
5275
|
158 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164
|
159 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
|
160 } |
|
161 |
|
162 return os; |
|
163 } |
|
164 |
|
165 std::istream& |
|
166 operator >> (std::istream& is, SparseBoolMatrix& a) |
|
167 { |
5275
|
168 octave_idx_type nr = a.rows (); |
|
169 octave_idx_type nc = a.cols (); |
|
170 octave_idx_type nz = a.nnz (); |
5164
|
171 |
|
172 if (nr < 1 || nc < 1) |
|
173 is.clear (std::ios::badbit); |
|
174 else |
|
175 { |
5275
|
176 octave_idx_type itmp, jtmp, jold = 0; |
5164
|
177 bool tmp; |
5275
|
178 octave_idx_type ii = 0; |
5164
|
179 |
|
180 a.cidx (0) = 0; |
5275
|
181 for (octave_idx_type i = 0; i < nz; i++) |
5164
|
182 { |
|
183 is >> itmp; |
|
184 itmp--; |
|
185 is >> jtmp; |
|
186 jtmp--; |
|
187 is >> tmp; |
|
188 if (is) |
|
189 { |
|
190 if (jold != jtmp) |
|
191 { |
5275
|
192 for (octave_idx_type j = jold; j < jtmp; j++) |
5164
|
193 a.cidx(j+1) = ii; |
|
194 |
|
195 jold = jtmp; |
|
196 } |
|
197 a.data (ii) = tmp; |
|
198 a.ridx (ii++) = itmp; |
|
199 } |
|
200 else |
|
201 goto done; |
|
202 } |
|
203 |
5275
|
204 for (octave_idx_type j = jold; j < nc; j++) |
5164
|
205 a.cidx(j+1) = ii; |
|
206 } |
|
207 |
|
208 done: |
|
209 |
|
210 return is; |
|
211 } |
|
212 |
|
213 SparseBoolMatrix |
|
214 SparseBoolMatrix::squeeze (void) const |
|
215 { |
|
216 return Sparse<bool>::squeeze (); |
|
217 } |
|
218 |
|
219 SparseBoolMatrix |
|
220 SparseBoolMatrix::index (idx_vector& i, int resize_ok) const |
|
221 { |
|
222 return Sparse<bool>::index (i, resize_ok); |
|
223 } |
|
224 |
|
225 SparseBoolMatrix |
|
226 SparseBoolMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const |
|
227 { |
|
228 return Sparse<bool>::index (i, j, resize_ok); |
|
229 } |
|
230 |
|
231 SparseBoolMatrix |
|
232 SparseBoolMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const |
|
233 { |
|
234 return Sparse<bool>::index (ra_idx, resize_ok); |
|
235 } |
|
236 |
|
237 SparseBoolMatrix |
|
238 SparseBoolMatrix::reshape (const dim_vector& new_dims) const |
|
239 { |
|
240 return Sparse<bool>::reshape (new_dims); |
|
241 } |
|
242 |
|
243 SparseBoolMatrix |
5275
|
244 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164
|
245 { |
|
246 return Sparse<bool>::permute (vec, inv); |
|
247 } |
|
248 |
|
249 SparseBoolMatrix |
5275
|
250 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164
|
251 { |
|
252 return Sparse<bool>::ipermute (vec); |
|
253 } |
|
254 |
|
255 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) |
|
256 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) |
|
257 |
|
258 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) |
|
259 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) |
|
260 |
|
261 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) |
|
262 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) |
|
263 |
|
264 |
|
265 /* |
|
266 ;;; Local Variables: *** |
|
267 ;;; mode: C++ *** |
|
268 ;;; End: *** |
|
269 */ |