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