Mercurial > hg > octave-nkf
annotate liboctave/boolSparse.cc @ 8999:dc07bc4157b8
allow empty matrices in stream input operators
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 20 Mar 2009 11:39:25 +0100 |
parents | eb63fbe60fab |
children | c6edba80dfae |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
8920 | 3 Copyright (C) 2004, 2005, 2006, 2007, 2008 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 | |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
143 SparseBoolMatrix |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
144 SparseBoolMatrix::diag (octave_idx_type k) const |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
145 { |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7515
diff
changeset
|
146 return Sparse<bool>::diag (k); |
7515
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
147 } |
f3c00dc0912b
Eliminate the rest of the dispatched sparse functions
David Bateman <dbateman@free.fr>
parents:
7017
diff
changeset
|
148 |
5164 | 149 boolMatrix |
150 SparseBoolMatrix::matrix_value (void) const | |
151 { | |
5275 | 152 octave_idx_type nr = rows (); |
153 octave_idx_type nc = cols (); | |
5164 | 154 |
155 boolMatrix retval (nr, nc, false); | |
5275 | 156 for (octave_idx_type j = 0; j < nc; j++) |
157 for (octave_idx_type i = cidx(j); i < cidx(j+1); i++) | |
5164 | 158 retval.elem (ridx(i), j) = data (i); |
159 | |
160 return retval; | |
161 } | |
162 | |
163 std::ostream& | |
164 operator << (std::ostream& os, const SparseBoolMatrix& a) | |
165 { | |
5275 | 166 octave_idx_type nc = a.cols (); |
5164 | 167 |
168 // add one to the printed indices to go from | |
169 // zero-based to one-based arrays | |
5275 | 170 for (octave_idx_type j = 0; j < nc; j++) |
5164 | 171 { |
172 OCTAVE_QUIT; | |
5275 | 173 for (octave_idx_type i = a.cidx(j); i < a.cidx(j+1); i++) |
5164 | 174 os << a.ridx(i) + 1 << " " << j + 1 << " " << a.data(i) << "\n"; |
175 } | |
176 | |
177 return os; | |
178 } | |
179 | |
180 std::istream& | |
181 operator >> (std::istream& is, SparseBoolMatrix& a) | |
182 { | |
5275 | 183 octave_idx_type nr = a.rows (); |
184 octave_idx_type nc = a.cols (); | |
5604 | 185 octave_idx_type nz = a.nzmax (); |
5164 | 186 |
8999
dc07bc4157b8
allow empty matrices in stream input operators
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
187 if (nr > 0 && nc > 0) |
5164 | 188 { |
5275 | 189 octave_idx_type itmp, jtmp, jold = 0; |
5164 | 190 bool tmp; |
5275 | 191 octave_idx_type ii = 0; |
5164 | 192 |
193 a.cidx (0) = 0; | |
5275 | 194 for (octave_idx_type i = 0; i < nz; i++) |
5164 | 195 { |
196 is >> itmp; | |
197 itmp--; | |
198 is >> jtmp; | |
199 jtmp--; | |
200 is >> tmp; | |
201 if (is) | |
202 { | |
203 if (jold != jtmp) | |
204 { | |
5275 | 205 for (octave_idx_type j = jold; j < jtmp; j++) |
5164 | 206 a.cidx(j+1) = ii; |
207 | |
208 jold = jtmp; | |
209 } | |
210 a.data (ii) = tmp; | |
211 a.ridx (ii++) = itmp; | |
212 } | |
213 else | |
214 goto done; | |
215 } | |
216 | |
5275 | 217 for (octave_idx_type j = jold; j < nc; j++) |
5164 | 218 a.cidx(j+1) = ii; |
219 } | |
220 | |
221 done: | |
222 | |
223 return is; | |
224 } | |
225 | |
226 SparseBoolMatrix | |
227 SparseBoolMatrix::squeeze (void) const | |
228 { | |
229 return Sparse<bool>::squeeze (); | |
230 } | |
231 | |
232 SparseBoolMatrix | |
233 SparseBoolMatrix::index (idx_vector& i, int resize_ok) const | |
234 { | |
235 return Sparse<bool>::index (i, resize_ok); | |
236 } | |
237 | |
238 SparseBoolMatrix | |
239 SparseBoolMatrix::index (idx_vector& i, idx_vector& j, int resize_ok) const | |
240 { | |
241 return Sparse<bool>::index (i, j, resize_ok); | |
242 } | |
243 | |
244 SparseBoolMatrix | |
245 SparseBoolMatrix::index (Array<idx_vector>& ra_idx, int resize_ok) const | |
246 { | |
247 return Sparse<bool>::index (ra_idx, resize_ok); | |
248 } | |
249 | |
250 SparseBoolMatrix | |
251 SparseBoolMatrix::reshape (const dim_vector& new_dims) const | |
252 { | |
253 return Sparse<bool>::reshape (new_dims); | |
254 } | |
255 | |
256 SparseBoolMatrix | |
5275 | 257 SparseBoolMatrix::permute (const Array<octave_idx_type>& vec, bool inv) const |
5164 | 258 { |
259 return Sparse<bool>::permute (vec, inv); | |
260 } | |
261 | |
262 SparseBoolMatrix | |
5275 | 263 SparseBoolMatrix::ipermute (const Array<octave_idx_type>& vec) const |
5164 | 264 { |
265 return Sparse<bool>::ipermute (vec); | |
266 } | |
267 | |
268 SPARSE_SMS_EQNE_OPS (SparseBoolMatrix, false, , bool, false, ) | |
269 SPARSE_SMS_BOOL_OPS (SparseBoolMatrix, bool, false) | |
270 | |
271 SPARSE_SSM_EQNE_OPS (bool, false, , SparseBoolMatrix, false, ) | |
272 SPARSE_SSM_BOOL_OPS (bool, SparseBoolMatrix, false) | |
273 | |
274 SPARSE_SMSM_EQNE_OPS (SparseBoolMatrix, false, , SparseBoolMatrix, false, ) | |
275 SPARSE_SMSM_BOOL_OPS (SparseBoolMatrix, SparseBoolMatrix, false) | |
276 | |
277 | |
278 /* | |
279 ;;; Local Variables: *** | |
280 ;;; mode: C++ *** | |
281 ;;; End: *** | |
282 */ |