Mercurial > hg > octave-nkf
annotate libinterp/octave-value/ov-base-sparse.cc @ 20787:40ed9b46a800
new octave_value::string_value method with optional error message
* ov.h (octave_value::string_vector): New method.
ov-base.cc, ov-base.h (octave_base_value::string_vector):
New default method.
ov-str-mat.cc, ov-str-mat.h (octave_char_matrix_str::string_value):
New method.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 08 Oct 2015 16:43:22 -0400 |
parents | f90c8372b7ba |
children |
rev | line source |
---|---|
5164 | 1 /* |
2 | |
19898
4197fc428c7d
maint: Update copyright notices for 2015.
John W. Eaton <jwe@octave.org>
parents:
18805
diff
changeset
|
3 Copyright (C) 2004-2015 David Bateman |
11523 | 4 Copyright (C) 1998-2004 Andy Adler |
10521
4d1fc073fbb7
add some missing copyright stmts
Jaroslav Hajek <highegg@gmail.com>
parents:
10513
diff
changeset
|
5 Copyright (C) 2010 VZLU Prague |
7016 | 6 |
7 This file is part of Octave. | |
5164 | 8 |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
5164 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
5164 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
26 #include <config.h> | |
27 #endif | |
28 | |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
29 #include <iomanip> |
5164 | 30 #include <iostream> |
31 | |
32 #include "oct-obj.h" | |
33 #include "ov-base.h" | |
34 #include "quit.h" | |
35 #include "pr-output.h" | |
36 | |
37 #include "byte-swap.h" | |
20657
c6224b4e7774
maint: Rename instances of LS_ASCII to LS_TEXT for clarity.
Rik <rik@octave.org>
parents:
20428
diff
changeset
|
38 #include "ls-oct-text.h" |
5164 | 39 #include "ls-utils.h" |
40 #include "ls-hdf5.h" | |
41 | |
42 #include "boolSparse.h" | |
43 #include "ov-base-sparse.h" | |
5731 | 44 #include "pager.h" |
17818
f1b59ef34eda
attempt to avoid setting persistent state on i/o streams (bug #40396)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
45 #include "utils.h" |
5164 | 46 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
47 #include "lo-array-gripes.h" |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
48 |
5164 | 49 template <class T> |
50 octave_value | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
51 octave_base_sparse<T>::do_index_op (const octave_value_list& idx, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
52 bool resize_ok) |
5164 | 53 { |
54 octave_value retval; | |
55 | |
5275 | 56 octave_idx_type n_idx = idx.length (); |
5164 | 57 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
58 // If we catch an indexing error in index_vector, we flag an error in |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
59 // index k. Ensure it is the right value befor each idx_vector call. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
60 // Same variable as used in the for loop in the default case. |
5164 | 61 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
62 octave_idx_type k = 0; |
5164 | 63 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
64 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
65 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
66 switch (n_idx) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
67 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
68 case 0: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
69 retval = matrix; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
70 break; |
5164 | 71 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
72 case 1: |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
73 { |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
74 idx_vector i = idx (0).index_vector (); |
5164 | 75 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
76 retval = octave_value (matrix.index (i, resize_ok)); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
77 } |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
78 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
79 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
80 case 2: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
81 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
82 idx_vector i = idx (0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
83 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
84 k = 1; |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
85 idx_vector j = idx (1).index_vector (); |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
86 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
87 retval = octave_value (matrix.index (i, j, resize_ok)); |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
88 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
89 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
90 default: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
91 error ("sparse indexing needs 1 or 2 indices"); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
92 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
93 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
94 catch (index_exception& e) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
95 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
96 // Rethrow to allow more info to be reported later. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
97 e.set_pos_if_unset (n_idx, k+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
98 throw; |
5164 | 99 } |
100 | |
101 return retval; | |
102 } | |
103 | |
104 template <class T> | |
105 octave_value | |
106 octave_base_sparse<T>::subsref (const std::string& type, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
107 const std::list<octave_value_list>& idx) |
5164 | 108 { |
109 octave_value retval; | |
110 | |
111 switch (type[0]) | |
112 { | |
113 case '(': | |
114 retval = do_index_op (idx.front ()); | |
115 break; | |
116 | |
117 case '{': | |
118 case '.': | |
119 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
120 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
121 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
5164 | 122 } |
123 break; | |
124 | |
125 default: | |
126 panic_impossible (); | |
127 } | |
128 | |
129 return retval.next_subsref (type, idx); | |
130 } | |
131 | |
132 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
133 octave_value |
5164 | 134 octave_base_sparse<T>::subsasgn (const std::string& type, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
135 const std::list<octave_value_list>& idx, |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 const octave_value& rhs) |
5164 | 137 { |
138 octave_value retval; | |
139 | |
140 switch (type[0]) | |
141 { | |
142 case '(': | |
143 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
144 if (type.length () == 1) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
145 retval = numeric_assign (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
146 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
147 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
148 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
149 error ("in indexed assignment of %s, last lhs index must be ()", |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
150 nm.c_str ()); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
151 } |
5164 | 152 } |
153 break; | |
154 | |
155 case '{': | |
156 case '.': | |
157 { | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
158 if (is_empty ()) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
159 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
160 octave_value tmp = octave_value::empty_conv (type, rhs); |
5164 | 161 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
162 retval = tmp.subsasgn (type, idx, rhs); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
163 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
164 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
165 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
166 std::string nm = type_name (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
167 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
168 } |
5164 | 169 } |
170 break; | |
171 | |
172 default: | |
173 panic_impossible (); | |
174 } | |
175 | |
176 return retval; | |
177 } | |
178 | |
179 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
180 void |
5164 | 181 octave_base_sparse<T>::assign (const octave_value_list& idx, const T& rhs) |
182 { | |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
183 |
5275 | 184 octave_idx_type len = idx.length (); |
5164 | 185 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
186 // If we catch an indexing error in index_vector, we flag an error in |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
187 // index k. Ensure it is the right value befor each idx_vector call. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
188 // Same variable as used in the for loop in the default case. |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
189 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
190 octave_idx_type k = 0; |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
191 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
192 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
193 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
194 switch (len) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
195 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
196 case 1: |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
197 { |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
198 idx_vector i = idx (0).index_vector (); |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
199 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
200 matrix.assign (i, rhs); |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
201 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
202 break; |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
203 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
204 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
205 case 2: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
206 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
207 idx_vector i = idx (0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
208 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
209 k = 1; |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
210 idx_vector j = idx (1).index_vector (); |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
211 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
212 matrix.assign (i, j, rhs); |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
213 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
214 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
215 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
216 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
217 default: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
218 error ("sparse indexing needs 1 or 2 indices"); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
219 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
220 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
221 catch (index_exception& e) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
222 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
223 // Rethrow to allow more info to be reported later. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
224 e.set_pos_if_unset (len, k+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
225 throw; |
10512
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
226 } |
aac9f4265048
rewrite sparse indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
10490
diff
changeset
|
227 |
5322 | 228 |
229 // Invalidate matrix type. | |
230 typ.invalidate_type (); | |
5164 | 231 } |
232 | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
233 template <class MT> |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
234 void |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
235 octave_base_sparse<MT>::delete_elements (const octave_value_list& idx) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
236 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
237 octave_idx_type len = idx.length (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
238 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
239 // If we catch an indexing error in index_vector, we flag an error in |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
240 // index k. Ensure it is the right value befor each idx_vector call. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
241 // Same variable as used in the for loop in the default case. |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
242 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
243 octave_idx_type k = 0; |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
244 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
245 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
246 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
247 switch (len) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
248 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
249 case 1: |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
250 { |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
251 idx_vector i = idx (0).index_vector (); |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
252 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
253 matrix.delete_elements (i); |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
254 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
255 break; |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
256 } |
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
257 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
258 case 2: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
259 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
260 idx_vector i = idx (0).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
261 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
262 k = 1; |
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
263 idx_vector j = idx (1).index_vector (); |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
264 |
20762
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20749
diff
changeset
|
265 matrix.delete_elements (i, j); |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
266 |
20749
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
267 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
268 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
269 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
270 default: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
271 error ("sparse indexing needs 1 or 2 indices"); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
272 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
273 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
274 catch (index_exception& e) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
275 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
276 // Rethrow to allow more info to be reported later. |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
277 e.set_pos_if_unset (len, k+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20657
diff
changeset
|
278 throw; |
10490
fdccd69d26bd
rewrite sparse null assignment (part 2)
Jaroslav Hajek <highegg@gmail.com>
parents:
10421
diff
changeset
|
279 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
280 |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
281 // Invalidate the matrix type |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
282 typ.invalidate_type (); |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
283 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7644
diff
changeset
|
284 |
5731 | 285 template <class T> |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
286 octave_value |
5731 | 287 octave_base_sparse<T>::resize (const dim_vector& dv, bool) const |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
288 { |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
289 T retval (matrix); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
290 retval.resize (dv); |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
291 return retval; |
5731 | 292 } |
5164 | 293 |
294 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
295 bool |
5164 | 296 octave_base_sparse<T>::is_true (void) const |
297 { | |
298 bool retval = false; | |
299 dim_vector dv = matrix.dims (); | |
5275 | 300 octave_idx_type nel = dv.numel (); |
10513
c5005bc2b7a9
implement working spalloc
Jaroslav Hajek <highegg@gmail.com>
parents:
10512
diff
changeset
|
301 octave_idx_type nz = nnz (); |
5164 | 302 |
303 if (nz == nel && nel > 0) | |
304 { | |
305 T t1 (matrix.reshape (dim_vector (nel, 1))); | |
306 | |
307 SparseBoolMatrix t2 = t1.all (); | |
308 | |
309 retval = t2(0); | |
310 } | |
311 | |
312 return retval; | |
313 } | |
314 | |
315 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
316 bool |
5164 | 317 octave_base_sparse<T>::print_as_scalar (void) const |
318 { | |
319 dim_vector dv = dims (); | |
320 | |
321 return (dv.all_ones () || dv.any_zero ()); | |
322 } | |
323 | |
324 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
325 void |
18484
bcd71a2531d3
Support disp/display overloading in classdef
Michael Goffioul <michael.goffioul@gmail.com>
parents:
18199
diff
changeset
|
326 octave_base_sparse<T>::print (std::ostream& os, bool pr_as_read_syntax) |
5164 | 327 { |
328 print_raw (os, pr_as_read_syntax); | |
329 newline (os); | |
330 } | |
331 | |
332 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
333 void |
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
334 octave_base_sparse<T>::print_info (std::ostream& os, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
335 const std::string& prefix) const |
5164 | 336 { |
337 matrix.print_info (os, prefix); | |
338 } | |
339 | |
340 template <class T> | |
341 void | |
342 octave_base_sparse<T>::print_raw (std::ostream& os, | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
343 bool pr_as_read_syntax) const |
5164 | 344 { |
17818
f1b59ef34eda
attempt to avoid setting persistent state on i/o streams (bug #40396)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
345 octave_preserve_stream_state stream_state (os); |
f1b59ef34eda
attempt to avoid setting persistent state on i/o streams (bug #40396)
John W. Eaton <jwe@octave.org>
parents:
17787
diff
changeset
|
346 |
5275 | 347 octave_idx_type nr = matrix.rows (); |
348 octave_idx_type nc = matrix.cols (); | |
5604 | 349 octave_idx_type nz = nnz (); |
5164 | 350 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
351 // FIXME: this should probably all be handled by a |
5355 | 352 // separate octave_print_internal function that can handle format |
353 // compact, loose, etc. | |
354 | |
355 os << "Compressed Column Sparse (rows = " << nr | |
356 << ", cols = " << nc | |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
357 << ", nnz = " << nz; |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
358 |
14566
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
359 // Avoid calling numel here since it can easily overflow |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
360 // octave_idx_type even when there is no real problem storing the |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
361 // sparse array. |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
362 |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
363 double dnr = nr; |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
364 double dnc = nc; |
aa491bd9e19b
avoid unnecessary index overflow error when printing sparse array (bug #35148)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
365 double dnel = dnr * dnc; |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
366 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
367 if (dnel > 0) |
11439
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
368 { |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
369 double pct = (nz / dnel * 100); |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
370 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
371 int prec = 2; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
372 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
373 // Display at least 2 significant figures and up to 4 as we |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
374 // approach 100%. Avoid having limited precision of the display |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
375 // result in reporting 100% for matrices that are not actually |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
376 // 100% full. |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
377 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
378 if (pct == 100) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
379 prec = 3; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
380 else |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
381 { |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
382 if (pct > 99.9) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
383 prec = 4; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
384 else if (pct > 99) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
385 prec = 3; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
386 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
387 if (pct > 99.99) |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
388 pct = 99.99; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
389 } |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
390 |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
391 os << " [" << std::setprecision (prec) << pct << "%]"; |
c2f44cba24c9
improve display of percentage full when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
10527
diff
changeset
|
392 } |
7644
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
393 |
91d7440211e7
display percentage of elements that are nonzero when printing sparse matrices
John W. Eaton <jwe@octave.org>
parents:
7017
diff
changeset
|
394 os << ")\n"; |
5164 | 395 |
396 // add one to the printed indices to go from | |
397 // zero-based to one-based arrays | |
398 | |
399 if (nz != 0) | |
400 { | |
5275 | 401 for (octave_idx_type j = 0; j < nc; j++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
402 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
403 octave_quit (); |
5355 | 404 |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
405 // FIXME: is there an easy way to get the max row |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
406 // and column indices so we can set the width appropriately |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
407 // and line up the columns here? Similarly, we should look |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
408 // at all the nonzero values and display them with the same |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
409 // formatting rules that apply to columns of a matrix. |
5355 | 410 |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14566
diff
changeset
|
411 for (octave_idx_type i = matrix.cidx (j); i < matrix.cidx (j+1); i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
412 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
413 os << "\n"; |
17787
175b392e91fe
Use GNU style coding conventions for code in libinterp/
Rik <rik@octave.org>
parents:
17744
diff
changeset
|
414 os << " (" << matrix.ridx (i)+1 << ", " << j+1 << ") -> "; |
5355 | 415 |
14861
f7afecdd87ef
maint: Use Octave coding conventions for cuddling parentheses in src/ directory
Rik <octave@nomad.inbox5.com>
parents:
14566
diff
changeset
|
416 octave_print_internal (os, matrix.data (i), pr_as_read_syntax); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
417 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
418 } |
5164 | 419 } |
420 } | |
421 | |
422 template <class T> | |
423 bool | |
6974 | 424 octave_base_sparse<T>::save_ascii (std::ostream& os) |
5164 | 425 { |
426 dim_vector dv = this->dims (); | |
427 | |
428 // Ensure that additional memory is deallocated | |
429 matrix.maybe_compress (); | |
430 | |
10527
b4d2080b6df7
Replace nzmax by nnz as needed
David Bateman <dbateman@free.fr>
parents:
10521
diff
changeset
|
431 os << "# nnz: " << nnz () << "\n"; |
20428
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
20071
diff
changeset
|
432 os << "# rows: " << dv(0) << "\n"; |
b2100e1659ac
maint: Use cuddled parentheses when indexing dimension_vectors.
Rik <rik@octave.org>
parents:
20071
diff
changeset
|
433 os << "# columns: " << dv(1) << "\n"; |
5164 | 434 |
435 os << this->matrix; | |
436 | |
437 return true; | |
438 } | |
439 | |
440 template <class T> | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
441 bool |
5164 | 442 octave_base_sparse<T>::load_ascii (std::istream& is) |
443 { | |
5275 | 444 octave_idx_type nz = 0; |
445 octave_idx_type nr = 0; | |
446 octave_idx_type nc = 0; | |
5164 | 447 bool success = true; |
448 | |
20071
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19898
diff
changeset
|
449 if (extract_keyword (is, "nnz", nz, true) |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19898
diff
changeset
|
450 && extract_keyword (is, "rows", nr, true) |
17d647821d61
maint: More cleanup of C++ code to follow Octave coding conventions.
John W. Eaton <jwe@octave.org>
parents:
19898
diff
changeset
|
451 && extract_keyword (is, "columns", nc, true)) |
5164 | 452 { |
453 T tmp (nr, nc, nz); | |
454 | |
455 is >> tmp; | |
456 | |
11586
12df7854fa7c
strip trailing whitespace from source files
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
457 if (!is) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
458 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
459 error ("load: failed to load matrix constant"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
460 success = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
461 } |
5164 | 462 |
463 matrix = tmp; | |
464 } | |
465 else | |
466 { | |
467 error ("load: failed to extract number of rows and columns"); | |
468 success = false; | |
469 } | |
470 | |
471 return success; | |
472 } | |
473 | |
18805
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
474 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
475 template <class T> |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
476 octave_value |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
477 octave_base_sparse<T>::fast_elem_extract (octave_idx_type n) const |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
478 { |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
479 octave_idx_type nr = matrix.rows (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
480 octave_idx_type nc = matrix.cols (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
481 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
482 octave_idx_type i = n % nr; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
483 octave_idx_type j = n / nr; |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
484 |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
485 return (i < nr && j < nc) ? octave_value (matrix(i,j)) : octave_value (); |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
486 } |
491b0adfec95
compatibility fixes for printf integer format specifiers
John W. Eaton <jwe@octave.org>
parents:
18484
diff
changeset
|
487 |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
488 template <class T> |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
489 octave_value |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
490 octave_base_sparse<T>::map (octave_base_value::unary_mapper_t umap) const |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
491 { |
18195
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
492 if (umap == umap_xtolower || umap == umap_xtoupper) |
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
493 return matrix; |
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
494 |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
495 // Try the map on the dense value. |
18195
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
496 // FIXME: We should probably be smarter about this, especially for the |
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
497 // cases that are expected to return sparse matrices. |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
498 octave_value retval = this->full_value ().map (umap); |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
499 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
500 // Sparsify the result if possible. |
18195
1b6db9303933
allow toupper and tolower to handle numeric values (bug #33537)
John W. Eaton <jwe@octave.org>
parents:
17818
diff
changeset
|
501 |
18196
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
502 switch (umap) |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
503 { |
18196
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
504 case umap_xisalnum: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
505 case umap_xisalpha: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
506 case umap_xisascii: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
507 case umap_xiscntrl: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
508 case umap_xisdigit: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
509 case umap_xisgraph: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
510 case umap_xislower: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
511 case umap_xisprint: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
512 case umap_xispunct: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
513 case umap_xisspace: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
514 case umap_xisupper: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
515 case umap_xisxdigit: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
516 case umap_xtoascii: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
517 // FIXME: intentionally skip this step for string mappers. |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
518 // Is this wanted? |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
519 break; |
18196
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
520 |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
521 default: |
18196
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
522 { |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
523 switch (retval.builtin_type ()) |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
524 { |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
525 case btyp_double: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
526 retval = retval.sparse_matrix_value (); |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
527 break; |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
528 |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
529 case btyp_complex: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
530 retval = retval.sparse_complex_matrix_value (); |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
531 break; |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
532 |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
533 case btyp_bool: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
534 retval = retval.sparse_bool_matrix_value (); |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
535 break; |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
536 |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
537 default: |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
538 break; |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
539 } |
0d5721873d6b
avoid some tests that rely on ordering of enum values
John W. Eaton <jwe@octave.org>
parents:
18195
diff
changeset
|
540 } |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
541 } |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
542 |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
543 return retval; |
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
544 } |