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