Mercurial > hg > octave-nkf
annotate liboctave/Sparse.h @ 12278:52f6f2989fcd release-3-4-x
don't let uninitialized values escape from DNEUPD
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Fri, 28 Jan 2011 14:05:59 -0500 |
parents | a21a3875ca83 |
children | b646413c3d0e |
rev | line source |
---|---|
5164 | 1 // Template sparse classes |
2 /* | |
3 | |
11523 | 4 Copyright (C) 2004-2011 David Bateman |
5 Copyright (C) 1998-2004 Andy Adler | |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10515
diff
changeset
|
6 Copyright (C) 2010 VZLU Prague |
7016 | 7 |
8 This file is part of Octave. | |
5164 | 9 |
10 Octave is free software; you can redistribute it and/or modify it | |
11 under the terms of the GNU General Public License as published by the | |
7016 | 12 Free Software Foundation; either version 3 of the License, or (at your |
13 option) any later version. | |
5164 | 14 |
15 Octave is distributed in the hope that it will be useful, but WITHOUT | |
16 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
17 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
18 for more details. | |
19 | |
20 You should have received a copy of the GNU General Public License | |
7016 | 21 along with Octave; see the file COPYING. If not, see |
22 <http://www.gnu.org/licenses/>. | |
5164 | 23 |
24 */ | |
25 | |
26 #if !defined (octave_Sparse_h) | |
27 #define octave_Sparse_h 1 | |
28 | |
29 #include <cassert> | |
30 #include <cstddef> | |
31 | |
8950
d865363208d6
include <iosfwd> instead of <iostream> in header files
John W. Eaton <jwe@octave.org>
parents:
8920
diff
changeset
|
32 #include <iosfwd> |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
33 #include <algorithm> |
5164 | 34 |
35 #include "Array.h" | |
36 #include "dim-vector.h" | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
37 #include "lo-error.h" |
5164 | 38 #include "lo-utils.h" |
39 | |
7433 | 40 #include "oct-sort.h" |
10382
1766c133674c
Special case sparse index method for ranges and maybe_delete_elements method for sparse vectors
David Bateman <dbateman@free.fr>
parents:
10358
diff
changeset
|
41 #include "oct-mem.h" |
7433 | 42 |
5164 | 43 class idx_vector; |
44 | |
45 // Two dimensional sparse class. Handles the reference counting for | |
46 // all the derived classes. | |
47 | |
48 template <class T> | |
49 class | |
50 Sparse | |
51 { | |
8181
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
52 public: |
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
53 |
8918
f5408862892f
Consistently use element_type in the array classes.
Jason Riedy <jason@acm.org>
parents:
8181
diff
changeset
|
54 typedef T element_type; |
8181
1ebcb9872ced
fix sparse-matrix bool/cmp op instantiation problem
John W. Eaton <jwe@octave.org>
parents:
7717
diff
changeset
|
55 |
5164 | 56 protected: |
57 //-------------------------------------------------------------------- | |
58 // The real representation of all Sparse arrays. | |
59 //-------------------------------------------------------------------- | |
60 | |
6108 | 61 class OCTAVE_API SparseRep |
5164 | 62 { |
63 public: | |
64 | |
65 T *d; | |
5275 | 66 octave_idx_type *r; |
67 octave_idx_type *c; | |
5604 | 68 octave_idx_type nzmx; |
5275 | 69 octave_idx_type nrows; |
70 octave_idx_type ncols; | |
12125
a21a3875ca83
implement a common class for reference counts
Jaroslav Hajek <highegg@gmail.com>
parents:
11586
diff
changeset
|
71 octave_refcount<int> count; |
5164 | 72 |
5604 | 73 SparseRep (void) : d (0), r (0), c (new octave_idx_type [1]), nzmx (0), nrows (0), |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
74 ncols (0), count (1) { c[0] = 0; } |
5164 | 75 |
5604 | 76 SparseRep (octave_idx_type n) : d (0), r (0), c (new octave_idx_type [n+1]), nzmx (0), nrows (n), |
5164 | 77 ncols (n), count (1) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
78 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
79 for (octave_idx_type i = 0; i < n + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
80 c[i] = 0; |
5164 | 81 } |
82 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
83 SparseRep (octave_idx_type nr, octave_idx_type nc) : d (0), r (0), c (new octave_idx_type [nc+1]), nzmx (0), |
5164 | 84 nrows (nr), ncols (nc), count (1) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
85 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
86 for (octave_idx_type i = 0; i < nc + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
87 c[i] = 0; |
5164 | 88 } |
89 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
90 SparseRep (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) : d (new T [nz]), |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
91 r (new octave_idx_type [nz]), c (new octave_idx_type [nc+1]), nzmx (nz), nrows (nr), |
5164 | 92 ncols (nc), count (1) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
93 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
94 for (octave_idx_type i = 0; i < nc + 1; i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
95 c[i] = 0; |
5164 | 96 } |
97 | |
98 SparseRep (const SparseRep& a) | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
99 : d (new T [a.nzmx]), r (new octave_idx_type [a.nzmx]), c (new octave_idx_type [a.ncols + 1]), |
5604 | 100 nzmx (a.nzmx), nrows (a.nrows), ncols (a.ncols), count (1) |
5164 | 101 { |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
102 octave_idx_type nz = a.nnz (); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
103 copy_or_memcpy (nz, a.d, d); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
104 copy_or_memcpy (nz, a.r, r); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
105 copy_or_memcpy (ncols + 1, a.c, c); |
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
106 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
107 |
5164 | 108 ~SparseRep (void) { delete [] d; delete [] r; delete [] c; } |
109 | |
5604 | 110 octave_idx_type length (void) const { return nzmx; } |
5164 | 111 |
5604 | 112 octave_idx_type nnz (void) const { return c [ncols]; } |
5164 | 113 |
5275 | 114 T& elem (octave_idx_type _r, octave_idx_type _c); |
5164 | 115 |
5275 | 116 T celem (octave_idx_type _r, octave_idx_type _c) const; |
5164 | 117 |
5275 | 118 T& data (octave_idx_type i) { return d[i]; } |
5164 | 119 |
5275 | 120 T cdata (octave_idx_type i) const { return d[i]; } |
5164 | 121 |
5275 | 122 octave_idx_type& ridx (octave_idx_type i) { return r[i]; } |
5164 | 123 |
5275 | 124 octave_idx_type cridx (octave_idx_type i) const { return r[i]; } |
5164 | 125 |
5275 | 126 octave_idx_type& cidx (octave_idx_type i) { return c[i]; } |
5164 | 127 |
5275 | 128 octave_idx_type ccidx (octave_idx_type i) const { return c[i]; } |
5164 | 129 |
130 void maybe_compress (bool remove_zeros); | |
131 | |
5275 | 132 void change_length (octave_idx_type nz); |
5164 | 133 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
134 bool indices_ok (void) const; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
135 |
5164 | 136 private: |
137 | |
138 // No assignment! | |
139 | |
140 SparseRep& operator = (const SparseRep& a); | |
141 }; | |
142 | |
143 //-------------------------------------------------------------------- | |
144 | |
145 void make_unique (void) | |
146 { | |
147 if (rep->count > 1) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
148 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
149 --rep->count; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
150 rep = new SparseRep (*rep); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
151 } |
5164 | 152 } |
153 | |
154 public: | |
155 | |
156 // !!! WARNING !!! -- these should be protected, not public. You | |
157 // should not access these data members directly! | |
158 | |
159 typename Sparse<T>::SparseRep *rep; | |
160 | |
161 dim_vector dimensions; | |
162 | |
163 private: | |
164 | |
165 typename Sparse<T>::SparseRep *nil_rep (void) const | |
166 { | |
167 static typename Sparse<T>::SparseRep *nr | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
168 = new typename Sparse<T>::SparseRep (); |
5164 | 169 |
170 nr->count++; | |
171 | |
172 return nr; | |
173 } | |
174 | |
175 public: | |
176 | |
177 Sparse (void) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
178 : rep (nil_rep ()), dimensions (dim_vector(0,0)) { } |
5164 | 179 |
5275 | 180 explicit Sparse (octave_idx_type n) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
181 : rep (new typename Sparse<T>::SparseRep (n)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
182 dimensions (dim_vector (n, n)) { } |
5164 | 183 |
5275 | 184 explicit Sparse (octave_idx_type nr, octave_idx_type nc) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
185 : rep (new typename Sparse<T>::SparseRep (nr, nc)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
186 dimensions (dim_vector (nr, nc)) { } |
5164 | 187 |
5275 | 188 explicit Sparse (octave_idx_type nr, octave_idx_type nc, T val); |
5164 | 189 |
5275 | 190 Sparse (const dim_vector& dv, octave_idx_type nz) |
5164 | 191 : rep (new typename Sparse<T>::SparseRep (dv(0), dv(1), nz)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
192 dimensions (dv) { } |
5164 | 193 |
5275 | 194 Sparse (octave_idx_type nr, octave_idx_type nc, octave_idx_type nz) |
5164 | 195 : rep (new typename Sparse<T>::SparseRep (nr, nc, nz)), |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
196 dimensions (dim_vector (nr, nc)) { } |
5164 | 197 |
10514
40c58502a78b
improve conversion & copy ctors of sparse matrix
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
198 // Type conversion case. Preserves capacity (). |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
199 template <class U> |
10515
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
200 Sparse (const Sparse<U>& a) |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
201 : rep (new typename Sparse<T>::SparseRep (a.rep->nrows, a.rep->ncols, a.rep->nzmx)), |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
202 dimensions (a.dimensions) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
203 { |
10515
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
204 octave_idx_type nz = a.nnz (); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
205 std::copy (a.rep->d, a.rep->d + nz, rep->d); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
206 copy_or_memcpy (nz, a.rep->r, rep->r); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
207 copy_or_memcpy (rep->ncols + 1, a.rep->c, rep->c); |
189274f6c7c4
omissions from last patch
Jaroslav Hajek <highegg@gmail.com>
parents:
10514
diff
changeset
|
208 } |
5164 | 209 |
210 // No type conversion case. | |
211 Sparse (const Sparse<T>& a) | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
212 : rep (a.rep), dimensions (a.dimensions) |
5164 | 213 { |
214 rep->count++; | |
215 } | |
216 | |
217 public: | |
218 | |
219 Sparse (const dim_vector& dv); | |
220 | |
221 Sparse (const Sparse<T>& a, const dim_vector& dv); | |
222 | |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
223 Sparse (const Array<T>& a, const idx_vector& r, const idx_vector& c, |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
224 octave_idx_type nr = -1, octave_idx_type nc = -1, |
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
225 bool sum_terms = true, octave_idx_type nzm = -1); |
10479
ded9beac7582
optimize sparse matrix assembly
Jaroslav Hajek <highegg@gmail.com>
parents:
10425
diff
changeset
|
226 |
5164 | 227 // Sparsify a normal matrix |
228 Sparse (const Array<T>& a); | |
229 | |
230 virtual ~Sparse (void); | |
231 | |
7717
ff918ee1a983
Delete idx in Sparse<T> and Array<T> operator =
David Bateman <dbateman@free.fr>
parents:
7620
diff
changeset
|
232 Sparse<T>& operator = (const Sparse<T>& a); |
5164 | 233 |
5604 | 234 // Note that nzmax and capacity are the amount of storage for |
235 // non-zero elements, while nnz is the actual number of non-zero | |
236 // terms. | |
237 octave_idx_type nzmax (void) const { return rep->length (); } | |
238 octave_idx_type capacity (void) const { return nzmax (); } | |
239 octave_idx_type nnz (void) const { return rep->nnz (); } | |
5164 | 240 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
241 // Querying the number of elements (incl. zeros) may overflow the index type, |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
242 // so don't do it unless you really need it. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
243 octave_idx_type numel (void) const |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
244 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
245 return dimensions.safe_numel (); |
5164 | 246 } |
247 | |
5275 | 248 octave_idx_type nelem (void) const { return capacity (); } |
249 octave_idx_type length (void) const { return numel (); } | |
5164 | 250 |
5275 | 251 octave_idx_type dim1 (void) const { return dimensions(0); } |
252 octave_idx_type dim2 (void) const { return dimensions(1); } | |
5164 | 253 |
5275 | 254 octave_idx_type rows (void) const { return dim1 (); } |
255 octave_idx_type cols (void) const { return dim2 (); } | |
256 octave_idx_type columns (void) const { return dim2 (); } | |
5164 | 257 |
5275 | 258 octave_idx_type get_row_index (octave_idx_type k) { return ridx (k); } |
259 octave_idx_type get_col_index (octave_idx_type k) | |
5164 | 260 { |
5275 | 261 octave_idx_type ret = 0; |
5164 | 262 while (cidx(ret+1) < k) |
263 ret++; | |
264 return ret; | |
265 } | |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
266 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
267 size_t byte_size (void) const |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
268 { |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
269 return (static_cast<size_t>(cols () + 1) * sizeof (octave_idx_type) |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
270 + static_cast<size_t> (capacity ()) * (sizeof (T) + sizeof (octave_idx_type))); |
10358
72fab01e5d68
improve some size_t queries
Jaroslav Hajek <highegg@gmail.com>
parents:
10352
diff
changeset
|
271 } |
5164 | 272 |
273 dim_vector dims (void) const { return dimensions; } | |
274 | |
275 Sparse<T> squeeze (void) const { return *this; } | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
276 |
5275 | 277 octave_idx_type compute_index (const Array<octave_idx_type>& ra_idx) const; |
5164 | 278 |
5275 | 279 T range_error (const char *fcn, octave_idx_type n) const; |
280 T& range_error (const char *fcn, octave_idx_type n); | |
5164 | 281 |
5275 | 282 T range_error (const char *fcn, octave_idx_type i, octave_idx_type j) const; |
283 T& range_error (const char *fcn, octave_idx_type i, octave_idx_type j); | |
5164 | 284 |
5275 | 285 T range_error (const char *fcn, const Array<octave_idx_type>& ra_idx) const; |
286 T& range_error (const char *fcn, const Array<octave_idx_type>& ra_idx); | |
5164 | 287 |
288 // No checking, even for multiple references, ever. | |
289 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
290 T& xelem (octave_idx_type n) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
291 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
292 octave_idx_type i = n % rows (), j = n / rows(); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
293 return xelem (i, j); |
5164 | 294 } |
295 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
296 T xelem (octave_idx_type n) const |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
297 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
298 octave_idx_type i = n % rows (), j = n / rows(); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
299 return xelem (i, j); |
5164 | 300 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
301 |
5275 | 302 T& xelem (octave_idx_type i, octave_idx_type j) { return rep->elem (i, j); } |
303 T xelem (octave_idx_type i, octave_idx_type j) const { return rep->celem (i, j); } | |
5164 | 304 |
5275 | 305 T& xelem (const Array<octave_idx_type>& ra_idx) |
5164 | 306 { return xelem (compute_index (ra_idx)); } |
307 | |
5275 | 308 T xelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 309 { return xelem (compute_index (ra_idx)); } |
310 | |
5775 | 311 // FIXME -- would be nice to fix this so that we don't |
5164 | 312 // unnecessarily force a copy, but that is not so easy, and I see no |
313 // clean way to do it. | |
314 | |
5275 | 315 T& checkelem (octave_idx_type n) |
5164 | 316 { |
317 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
318 return range_error ("T& Sparse<T>::checkelem", n); |
5164 | 319 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
320 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
321 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
322 return xelem (n); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
323 } |
5164 | 324 } |
325 | |
5275 | 326 T& checkelem (octave_idx_type i, octave_idx_type j) |
5164 | 327 { |
328 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
329 return range_error ("T& Sparse<T>::checkelem", i, j); |
5164 | 330 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
331 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
332 make_unique (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
333 return xelem (i, j); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
334 } |
5164 | 335 } |
336 | |
5275 | 337 T& checkelem (const Array<octave_idx_type>& ra_idx) |
5164 | 338 { |
5275 | 339 octave_idx_type i = compute_index (ra_idx); |
5164 | 340 |
341 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
342 return range_error ("T& Sparse<T>::checkelem", ra_idx); |
5164 | 343 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
344 return elem (i); |
5164 | 345 } |
346 | |
5275 | 347 T& elem (octave_idx_type n) |
5164 | 348 { |
349 make_unique (); | |
350 return xelem (n); | |
351 } | |
352 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
353 T& elem (octave_idx_type i, octave_idx_type j) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
354 { |
5164 | 355 make_unique (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
356 return xelem (i, j); |
5164 | 357 } |
358 | |
5275 | 359 T& elem (const Array<octave_idx_type>& ra_idx) |
5164 | 360 { return Sparse<T>::elem (compute_index (ra_idx)); } |
361 | |
362 #if defined (BOUNDS_CHECKING) | |
5275 | 363 T& operator () (octave_idx_type n) { return checkelem (n); } |
364 T& operator () (octave_idx_type i, octave_idx_type j) { return checkelem (i, j); } | |
365 T& operator () (const Array<octave_idx_type>& ra_idx) { return checkelem (ra_idx); } | |
5164 | 366 #else |
5275 | 367 T& operator () (octave_idx_type n) { return elem (n); } |
368 T& operator () (octave_idx_type i, octave_idx_type j) { return elem (i, j); } | |
369 T& operator () (const Array<octave_idx_type>& ra_idx) { return elem (ra_idx); } | |
5164 | 370 #endif |
371 | |
5275 | 372 T checkelem (octave_idx_type n) const |
5164 | 373 { |
374 if (n < 0 || n >= numel ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
375 return range_error ("T Sparse<T>::checkelem", n); |
5164 | 376 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
377 return xelem (n); |
5164 | 378 } |
379 | |
5275 | 380 T checkelem (octave_idx_type i, octave_idx_type j) const |
5164 | 381 { |
382 if (i < 0 || j < 0 || i >= dim1 () || j >= dim2 ()) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
383 return range_error ("T Sparse<T>::checkelem", i, j); |
5164 | 384 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
385 return xelem (i, j); |
5164 | 386 } |
387 | |
5275 | 388 T checkelem (const Array<octave_idx_type>& ra_idx) const |
5164 | 389 { |
5275 | 390 octave_idx_type i = compute_index (ra_idx); |
5164 | 391 |
392 if (i < 0) | |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
393 return range_error ("T Sparse<T>::checkelem", ra_idx); |
5164 | 394 else |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
395 return Sparse<T>::elem (i); |
5164 | 396 } |
397 | |
5275 | 398 T elem (octave_idx_type n) const { return xelem (n); } |
5164 | 399 |
5275 | 400 T elem (octave_idx_type i, octave_idx_type j) const { return xelem (i, j); } |
5164 | 401 |
5275 | 402 T elem (const Array<octave_idx_type>& ra_idx) const |
5164 | 403 { return Sparse<T>::elem (compute_index (ra_idx)); } |
404 | |
405 #if defined (BOUNDS_CHECKING) | |
5275 | 406 T operator () (octave_idx_type n) const { return checkelem (n); } |
407 T operator () (octave_idx_type i, octave_idx_type j) const { return checkelem (i, j); } | |
408 T operator () (const Array<octave_idx_type>& ra_idx) const { return checkelem (ra_idx); } | |
5164 | 409 #else |
5275 | 410 T operator () (octave_idx_type n) const { return elem (n); } |
411 T operator () (octave_idx_type i, octave_idx_type j) const { return elem (i, j); } | |
412 T operator () (const Array<octave_idx_type>& ra_idx) const { return elem (ra_idx); } | |
5164 | 413 #endif |
414 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
415 Sparse<T> maybe_compress (bool remove_zeros = false) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
416 { |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
417 if (remove_zeros) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
418 make_unique (); // Needs to unshare because elements are removed. |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
419 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
420 rep->maybe_compress (remove_zeros); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
421 return (*this); |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
422 } |
5164 | 423 |
424 Sparse<T> reshape (const dim_vector& new_dims) const; | |
425 | |
5275 | 426 Sparse<T> permute (const Array<octave_idx_type>& vec, bool inv = false) const; |
5164 | 427 |
5275 | 428 Sparse<T> ipermute (const Array<octave_idx_type>& vec) const |
5164 | 429 { return permute (vec, true); } |
430 | |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
431 void resize1 (octave_idx_type n); |
5164 | 432 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
433 void resize (octave_idx_type r, octave_idx_type c); |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
434 |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
435 void resize (const dim_vector& dv); |
5164 | 436 |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
437 void change_capacity (octave_idx_type nz) |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
438 { |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
439 if (nz < nnz ()) |
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
440 make_unique (); // Unshare now because elements will be truncated. |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
441 rep->change_length (nz); |
10497
cb7ffe7288f0
improve & fix SparseRep reallocation and compression
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
442 } |
5164 | 443 |
5275 | 444 Sparse<T>& insert (const Sparse<T>& a, octave_idx_type r, octave_idx_type c); |
445 Sparse<T>& insert (const Sparse<T>& a, const Array<octave_idx_type>& idx); | |
5164 | 446 |
447 bool is_square (void) const { return (dim1 () == dim2 ()); } | |
448 | |
449 bool is_empty (void) const { return (rows () < 1 && cols () < 1); } | |
450 | |
451 Sparse<T> transpose (void) const; | |
452 | |
453 T* data (void) { make_unique (); return rep->d; } | |
5275 | 454 T& data (octave_idx_type i) { make_unique (); return rep->data (i); } |
5164 | 455 T* xdata (void) { return rep->d; } |
5275 | 456 T& xdata (octave_idx_type i) { return rep->data (i); } |
5164 | 457 |
5275 | 458 T data (octave_idx_type i) const { return rep->data (i); } |
5900 | 459 // FIXME -- shouldn't this be returning const T*? |
5164 | 460 T* data (void) const { return rep->d; } |
461 | |
5275 | 462 octave_idx_type* ridx (void) { make_unique (); return rep->r; } |
463 octave_idx_type& ridx (octave_idx_type i) { make_unique (); return rep->ridx (i); } | |
464 octave_idx_type* xridx (void) { return rep->r; } | |
465 octave_idx_type& xridx (octave_idx_type i) { return rep->ridx (i); } | |
5164 | 466 |
5275 | 467 octave_idx_type ridx (octave_idx_type i) const { return rep->cridx (i); } |
5900 | 468 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 469 octave_idx_type* ridx (void) const { return rep->r; } |
5164 | 470 |
5275 | 471 octave_idx_type* cidx (void) { make_unique (); return rep->c; } |
472 octave_idx_type& cidx (octave_idx_type i) { make_unique (); return rep->cidx (i); } | |
473 octave_idx_type* xcidx (void) { return rep->c; } | |
474 octave_idx_type& xcidx (octave_idx_type i) { return rep->cidx (i); } | |
5164 | 475 |
5275 | 476 octave_idx_type cidx (octave_idx_type i) const { return rep->ccidx (i); } |
5900 | 477 // FIXME -- shouldn't this be returning const octave_idx_type*? |
5275 | 478 octave_idx_type* cidx (void) const { return rep->c; } |
5164 | 479 |
5275 | 480 octave_idx_type ndims (void) const { return dimensions.length (); } |
5164 | 481 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
482 void delete_elements (const idx_vector& i); |
5164 | 483 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
484 void delete_elements (int dim, const idx_vector& i); |
5164 | 485 |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10480
diff
changeset
|
486 void delete_elements (const idx_vector& i, const idx_vector& j); |
5164 | 487 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
488 Sparse<T> index (const idx_vector& i, bool resize_ok = false) const; |
5164 | 489 |
10421
99e9bae2d81e
improve sparse indexing interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10382
diff
changeset
|
490 Sparse<T> index (const idx_vector& i, const idx_vector& j, bool resize_ok = false) const; |
5164 | 491 |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
492 void assign (const idx_vector& i, const Sparse<T>& rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
493 |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
494 void assign (const idx_vector& i, const idx_vector& j, const Sparse<T>& rhs); |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10497
diff
changeset
|
495 |
5164 | 496 void print_info (std::ostream& os, const std::string& prefix) const; |
497 | |
5900 | 498 // Unsafe. These functions exist to support the MEX interface. |
499 // You should not use them anywhere else. | |
500 void *mex_get_data (void) const { return const_cast<T *> (data ()); } | |
501 | |
502 octave_idx_type *mex_get_ir (void) const { return const_cast<octave_idx_type *> (ridx ()); } | |
503 | |
504 octave_idx_type *mex_get_jc (void) const { return const_cast<octave_idx_type *> (cidx ()); } | |
7433 | 505 |
7463
2467639bd8c0
eliminate UNDEFINED sort mode
John W. Eaton <jwe@octave.org>
parents:
7433
diff
changeset
|
506 Sparse<T> sort (octave_idx_type dim = 0, sortmode mode = ASCENDING) const; |
7433 | 507 Sparse<T> sort (Array<octave_idx_type> &sidx, octave_idx_type dim = 0, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
508 sortmode mode = ASCENDING) const; |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
509 |
7620
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7602
diff
changeset
|
510 Sparse<T> diag (octave_idx_type k = 0) const; |
36594d5bbe13
Move diag function into the octave_value class
David Bateman <dbateman@free.fr>
parents:
7602
diff
changeset
|
511 |
10716
f7f26094021b
improve cat code design in data.cc, make horzcat/vertcat more Matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
10537
diff
changeset
|
512 // dim = -1 and dim = -2 are special; see Array<T>::cat description. |
10531
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10527
diff
changeset
|
513 static Sparse<T> |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10527
diff
changeset
|
514 cat (int dim, octave_idx_type n, const Sparse<T> *sparse_list); |
2dd8ea8bfd71
basic cat functionality in liboctave
Jaroslav Hajek <highegg@gmail.com>
parents:
10527
diff
changeset
|
515 |
10425
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
516 Array<T> array_value (void) const; |
0677c5d80b77
rewrite 1D sparse indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
517 |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
518 template <class U, class F> |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
519 Sparse<U> |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
520 map (F fcn) const |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
521 { |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
522 Sparse<U> result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
523 U f_zero = fcn (0.); |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
524 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
525 if (f_zero != 0.) |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
526 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
527 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
528 octave_idx_type nc = cols (); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
529 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
530 result = Sparse<U> (nr, nc, f_zero); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
531 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
532 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
533 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
534 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
535 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
536 /* Use data instead of elem for better performance. */ |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
537 result.data (ridx (i) + j * nr) = fcn (data(i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
538 } |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
539 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
540 result.maybe_compress (true); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
541 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
542 else |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
543 { |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
544 octave_idx_type nz = nnz (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
545 octave_idx_type nr = rows (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
546 octave_idx_type nc = cols (); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
547 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
548 result = Sparse<U> (nr, nc, nz); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
549 octave_idx_type ii = 0; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
550 result.cidx (ii) = 0; |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
551 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
552 for (octave_idx_type j = 0; j < nc; j++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
553 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
554 for (octave_idx_type i = cidx(j); i < cidx (j+1); i++) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
555 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
556 U val = fcn (data (i)); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
557 if (val != 0.0) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
558 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
559 result.data (ii) = val; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
560 result.ridx (ii++) = ridx (i); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
561 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
562 octave_quit (); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
563 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
564 result.cidx (j+1) = ii; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
565 } |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
566 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
567 result.maybe_compress (false); |
7602
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
568 } |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
569 |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
570 return result; |
7bfaa9611558
Rewrite sparse mappers in terms of a functor template function
David Bateman <dbateman@free.fr>
parents:
7470
diff
changeset
|
571 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
572 |
9812
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
573 // Overloads for function references. |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
574 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
575 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
576 map (U (&fcn) (T)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
577 { return map<U, U (&) (T)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
578 |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
579 template <class U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
580 Sparse<U> |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
581 map (U (&fcn) (const T&)) const |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
582 { return map<U, U (&) (const T&)> (fcn); } |
f80c566bc751
improve unary mapper system
Jaroslav Hajek <highegg@gmail.com>
parents:
9469
diff
changeset
|
583 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
584 bool indices_ok (void) const { return rep->indices_ok (); } |
5164 | 585 }; |
586 | |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
587 template<typename T> |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
588 std::istream& |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
589 read_sparse_matrix (std::istream& is, Sparse<T>& a, |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
590 T (*read_fcn) (std::istream&)) |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
591 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
592 octave_idx_type nr = a.rows (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
593 octave_idx_type nc = a.cols (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
594 octave_idx_type nz = a.nzmax (); |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
595 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
596 if (nr > 0 && nc > 0) |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
597 { |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
598 octave_idx_type itmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
599 octave_idx_type jtmp; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
600 octave_idx_type iold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
601 octave_idx_type jold = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
602 octave_idx_type ii = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
603 T tmp; |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
604 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
605 a.cidx (0) = 0; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
606 for (octave_idx_type i = 0; i < nz; i++) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
607 { |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
608 itmp = 0; jtmp = 0; |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
609 is >> itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
610 itmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
611 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
612 is >> jtmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
613 jtmp--; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
614 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
615 if (itmp < 0 || itmp >= nr) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
616 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
617 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
618 ("invalid sparse matrix: row index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
619 itmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
620 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
621 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
622 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
623 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
624 if (jtmp < 0 || jtmp >= nc) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
625 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
626 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
627 ("invalid sparse matrix: column index = %d out of range", |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
628 jtmp + 1); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
629 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
630 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
631 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
632 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
633 if (jtmp < jold) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
634 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
635 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
636 ("invalid sparse matrix: column indices must appear in ascending order"); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
637 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
638 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
639 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
640 else if (jtmp > jold) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
641 { |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
642 for (octave_idx_type j = jold; j < jtmp; j++) |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
643 a.cidx(j+1) = ii; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
644 } |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
645 else if (itmp < iold) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
646 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
647 (*current_liboctave_error_handler) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
648 ("invalid sparse matrix: row indices must appear in ascending order in each column"); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
649 is.setstate (std::ios::failbit); |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
650 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
651 } |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
652 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
653 iold = itmp; |
9829
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
654 jold = jtmp; |
8fd88cc36fa4
fix loading sparse matrices
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
655 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
656 tmp = read_fcn (is); |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
657 |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
658 if (is) |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
659 { |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
660 a.data (ii) = tmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
661 a.ridx (ii++) = itmp; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
662 } |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
663 else |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
664 goto done; |
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
665 } |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
666 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
667 for (octave_idx_type j = jold; j < nc; j++) |
10312
cbc402e64d83
untabify liboctave header files
John W. Eaton <jwe@octave.org>
parents:
10158
diff
changeset
|
668 a.cidx(j+1) = ii; |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
669 } |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
670 |
9469
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
671 done: |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
672 |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
673 return is; |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
674 } |
c6edba80dfae
sanity checks for loading sparse matrices
John W. Eaton <jwe@octave.org>
parents:
8950
diff
changeset
|
675 |
5164 | 676 #endif |