Mercurial > octave
annotate libinterp/corefcn/Cell.cc @ 27060:3140380861ce
error for x{} for cell array objects (bug #56167)
* ov-cell.cc (octave_cell::subsref): Error for empty {} index expression.
* Cell.cc: Update test. Note Matlab compatibility requirement.
* ov-base-mat.cc: New test.
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Tue, 23 Apr 2019 10:47:02 +0000 |
parents | 00f796120a6d |
children | d61825e693f2 |
rev | line source |
---|---|
3353 | 1 /* |
2 | |
26376
00f796120a6d
maint: Update copyright dates in all source files.
John W. Eaton <jwe@octave.org>
parents:
25054
diff
changeset
|
3 Copyright (C) 1999-2019 John W. Eaton |
11523 | 4 Copyright (C) 2009-2010 VZLU Prague |
3353 | 5 |
6 This file is part of Octave. | |
7 | |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23677
diff
changeset
|
8 Octave is free software: you can redistribute it and/or modify it |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
9 under the terms of the GNU General Public License as published by |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23677
diff
changeset
|
10 the Free Software Foundation, either version 3 of the License, or |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
11 (at your option) any later version. |
3353 | 12 |
22755
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
13 Octave is distributed in the hope that it will be useful, but |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
14 WITHOUT ANY WARRANTY; without even the implied warranty of |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
3a2b891d0b33
maint: Standardize Copyright formatting.
Rik <rik@octave.org>
parents:
22417
diff
changeset
|
16 GNU General Public License for more details. |
3353 | 17 |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
24534
194eb4bd202b
maint: Update punctuation for GPL v3 license text.
Rik <rik@octave.org>
parents:
23677
diff
changeset
|
20 <https://www.gnu.org/licenses/>. |
3353 | 21 |
22 */ | |
23 | |
21724
aba2e6293dd8
use "#if ..." consistently instead of "#ifdef" and "#ifndef"
John W. Eaton <jwe@octave.org>
parents:
21580
diff
changeset
|
24 #if defined (HAVE_CONFIG_H) |
21301
40de9f8f23a6
Use '#include "config.h"' rather than <config.h>.
Rik <rik@octave.org>
parents:
21200
diff
changeset
|
25 # include "config.h" |
3353 | 26 #endif |
27 | |
4513 | 28 #include "idx-vector.h" |
29 | |
3353 | 30 #include "Cell.h" |
4919 | 31 #include "error.h" |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20980
diff
changeset
|
32 #include "errwarn.h" |
20940
48b2ad5ee801
maint: Rename oct-obj.[cc|h] to ovl.[cc|h] for clarity.
Rik <rik@octave.org>
parents:
20831
diff
changeset
|
33 #include "ovl.h" |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
34 |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
35 Cell::Cell (const octave_value_list& ovl) |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9415
diff
changeset
|
36 : Array<octave_value> (ovl.cell_value ()) |
22417
48c00363dc74
maint: Use '{ }' for empty function bodies in C++.
Rik <rik@octave.org>
parents:
22407
diff
changeset
|
37 { } |
3353 | 38 |
5805 | 39 Cell::Cell (const string_vector& sv, bool trim) |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9415
diff
changeset
|
40 : Array<octave_value> () |
4216 | 41 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
42 octave_idx_type n = sv.numel (); |
4216 | 43 |
44 if (n > 0) | |
45 { | |
4625 | 46 resize (dim_vector (n, 1)); |
4216 | 47 |
5275 | 48 for (octave_idx_type i = 0; i < n; i++) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
49 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
50 std::string s = sv[i]; |
5805 | 51 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
52 if (trim) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
53 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
54 size_t pos = s.find_last_not_of (' '); |
5805 | 55 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
56 s = (pos == std::string::npos) ? "" : s.substr (0, pos+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
57 } |
5805 | 58 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
59 elem(i,0) = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
60 } |
4216 | 61 } |
62 } | |
3354 | 63 |
8732 | 64 Cell::Cell (const Array<std::string>& sa) |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9415
diff
changeset
|
65 : Array<octave_value> (sa.dims ()) |
8732 | 66 { |
67 octave_idx_type n = sa.numel (); | |
68 | |
69 octave_value *dst = fortran_vec (); | |
70 const std::string *src = sa.data (); | |
71 | |
72 for (octave_idx_type i = 0; i < n; i++) | |
73 dst[i] = src[i]; | |
74 } | |
75 | |
6116 | 76 // Set size to DV, filling with []. Then fill with as many elements of |
77 // SV as possible. | |
7209 | 78 |
6116 | 79 Cell::Cell (const dim_vector& dv, const string_vector& sv, bool trim) |
14616
13cc11418393
improve handling of default resize fill value for arrays
John W. Eaton <jwe@octave.org>
parents:
14557
diff
changeset
|
80 : Array<octave_value> (dv, Matrix ()) |
6116 | 81 { |
20232
a9574e3c6e9e
Deprecate Array::length() and Sparse::length() in favour of ::numel().
Carnë Draug <carandraug@octave.org>
parents:
19697
diff
changeset
|
82 octave_idx_type n = sv.numel (); |
6116 | 83 |
84 if (n > 0) | |
85 { | |
86 octave_idx_type m = numel (); | |
87 | |
23450
855122b993da
maint: Wrap tertiary operator in parentheses "(COND ? x : y)".
Rik <rik@octave.org>
parents:
23355
diff
changeset
|
88 octave_idx_type len = (n > m ? m : n); |
6116 | 89 |
90 for (octave_idx_type i = 0; i < len; i++) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
91 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
92 std::string s = sv[i]; |
6116 | 93 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
94 if (trim) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
95 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
96 size_t pos = s.find_last_not_of (' '); |
6116 | 97 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
98 s = (pos == std::string::npos) ? "" : s.substr (0, pos+1); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
99 } |
6116 | 100 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
101 elem(i) = s; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
102 } |
6116 | 103 } |
104 } | |
105 | |
106 bool | |
23575
e95738a119da
maint: Deprecate is_cellstr and replace with iscellstr.
Rik <rik@octave.org>
parents:
23450
diff
changeset
|
107 Cell::iscellstr (void) const |
6116 | 108 { |
109 bool retval = true; | |
110 | |
10065
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
111 octave_idx_type n = numel (); |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
112 |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
113 for (octave_idx_type i = 0; i < n; i++) |
6116 | 114 { |
115 if (! elem(i).is_string ()) | |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
116 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
117 retval = false; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
118 break; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
119 } |
6116 | 120 } |
121 | |
122 return retval; | |
123 } | |
124 | |
10065
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
125 Array<std::string> |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
126 Cell::cellstr_value (void) const |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
127 { |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
128 Array<std::string> retval (dims ()); |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
129 |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
130 octave_idx_type n = numel (); |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
131 |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
132 for (octave_idx_type i = 0; i < n; i++) |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
133 retval.xelem (i) = elem (i).string_value (); |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
134 |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
135 return retval; |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
136 } |
64a06079cae4
improve cellstr cache implementation
Jaroslav Hajek <highegg@gmail.com>
parents:
9813
diff
changeset
|
137 |
23355
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
138 string_vector |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
139 Cell::string_vector_value (void) const |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
140 { |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
141 octave_idx_type n = numel (); |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
142 |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
143 string_vector retval (n); |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
144 |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
145 for (octave_idx_type i = 0; i < n; i++) |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
146 retval.xelem (i) = elem (i).string_value (); |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
147 |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
148 return retval; |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
149 } |
08db4e9dd149
* Cell.h, Cell.cc (Cell::string_vector_value): New function.
John W. Eaton <jwe@octave.org>
parents:
23220
diff
changeset
|
150 |
4513 | 151 Cell |
4587 | 152 Cell::index (const octave_value_list& idx_arg, bool resize_ok) const |
4513 | 153 { |
154 Cell retval; | |
155 | |
5275 | 156 octave_idx_type n = idx_arg.length (); |
4513 | 157 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
158 // If we catch an indexing error in index_vector, we flag an error |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
159 // in index k. Ensure it is the right value befor each idx_vector |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
160 // call. Same variable as used in for loop in default case. |
5539 | 161 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
162 octave_idx_type k = 0; |
4513 | 163 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
164 try |
4513 | 165 { |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
166 switch (n) |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
167 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
168 case 0: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
169 retval = *this; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
170 break; |
4513 | 171 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
172 case 1: |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
173 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
174 idx_vector i = idx_arg(0).index_vector (); |
4513 | 175 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
176 retval = Array<octave_value>::index (i, resize_ok, Matrix ()); |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
177 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
178 break; |
4513 | 179 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
180 case 2: |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
181 { |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
182 idx_vector i = idx_arg(0).index_vector (); |
4919 | 183 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
184 k = 1; |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
185 idx_vector j = idx_arg(1).index_vector (); |
4919 | 186 |
20667
8742e0b1cc49
eliminate more uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20555
diff
changeset
|
187 retval = Array<octave_value>::index (i, j, resize_ok, Matrix ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
188 } |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
189 break; |
4513 | 190 |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
191 default: |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
192 { |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
193 Array<idx_vector> iv (dim_vector (n, 1)); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
194 |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
195 for (k = 0; k < n; k++) |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
196 iv(k) = idx_arg(k).index_vector (); |
4513 | 197 |
20555
f90c8372b7ba
eliminate many more simple uses of error_state
John W. Eaton <jwe@octave.org>
parents:
20542
diff
changeset
|
198 retval = Array<octave_value>::index (iv, resize_ok, Matrix ()); |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
199 } |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
200 break; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
201 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
202 } |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
203 catch (octave::index_exception& e) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
204 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
205 // 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:
20232
diff
changeset
|
206 e.set_pos_if_unset (n, k+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
207 throw; |
4513 | 208 } |
209 | |
210 return retval; | |
211 } | |
212 | |
19140
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
213 /* |
27060
3140380861ce
error for x{} for cell array objects (bug #56167)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
214 %% This behavior is required for Matlab compatibility. |
3140380861ce
error for x{} for cell array objects (bug #56167)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
215 %!shared a |
19140
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
216 %! a = {"foo", "bar"}; |
27060
3140380861ce
error for x{} for cell array objects (bug #56167)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
217 %!assert (a(), a); |
3140380861ce
error for x{} for cell array objects (bug #56167)
John W. Eaton <jwe@octave.org>
parents:
26376
diff
changeset
|
218 %!error <invalid empty index expression> a{} |
19140
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
219 */ |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
220 |
10649
64472dd48517
cosmetic changes in Cell interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
221 void |
4587 | 222 Cell::assign (const octave_value_list& idx_arg, const Cell& rhs, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
223 const octave_value& fill_val) |
4513 | 224 |
225 { | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
226 octave_idx_type len = idx_arg.length (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
227 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
228 Array<idx_vector> ra_idx (dim_vector (len, 1)); |
4513 | 229 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
230 for (octave_idx_type i = 0; i < len; i++) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
231 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
232 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
233 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
234 ra_idx(i) = idx_arg(i).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
235 } |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
236 catch (octave::index_exception& e) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
237 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
238 // 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:
20232
diff
changeset
|
239 e.set_pos (len, i+1); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
240 throw; |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
241 } |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
242 } |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
243 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
244 Array<octave_value>::assign (ra_idx, rhs, fill_val); |
4513 | 245 } |
246 | |
10649
64472dd48517
cosmetic changes in Cell interface
Jaroslav Hajek <highegg@gmail.com>
parents:
10521
diff
changeset
|
247 void |
8175
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
248 Cell::delete_elements (const octave_value_list& idx_arg) |
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
249 |
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
250 { |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
251 octave_idx_type len = idx_arg.length (); |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
252 |
11570
57632dea2446
attempt better backward compatibility for Array constructors
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
253 Array<idx_vector> ra_idx (dim_vector (len, 1)); |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
254 |
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
255 for (octave_idx_type i = 0; i < len; i++) |
20542
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
256 try |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
257 { |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
258 ra_idx.xelem (i) = idx_arg(i).index_vector (); |
dd6345fd8a97
use exceptions for better invalid index error reporting (bug #45957)
Lachlan Andrew <lachlanbis@gmail.com>
parents:
20232
diff
changeset
|
259 } |
22327
d0562b3159c7
move more classes inside octave namespace
John W. Eaton <jwe@octave.org>
parents:
22323
diff
changeset
|
260 catch (octave::index_exception& e) |
22407
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
261 { |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
262 // Rethrow to allow more info to be reported later. |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
263 e.set_pos (len, i+1); |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
264 throw; |
34ce5be04942
maint: Style check C++ code in libinterp/.
Rik <rik@octave.org>
parents:
22327
diff
changeset
|
265 } |
8175
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
266 |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8179
diff
changeset
|
267 Array<octave_value>::delete_elements (ra_idx); |
8175
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
268 } |
977d5204cf67
fix null assignment for structs
Jaroslav Hajek <highegg@gmail.com>
parents:
8021
diff
changeset
|
269 |
5602 | 270 octave_idx_type |
271 Cell::nnz (void) const | |
272 { | |
21100
e39e05d90788
Switch gripe_XXX to either err_XXX or warn_XXX naming scheme.
Rik <rik@octave.org>
parents:
20980
diff
changeset
|
273 err_wrong_type_arg ("nnz", "cell array"); |
5602 | 274 } |
275 | |
19140
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
276 /* |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
277 %!error <wrong type argument 'cell array'> nnz ({0, 1, 2}) |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
278 %!error <wrong type argument 'cell array'> nnz (cell ()) |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
279 %!error <wrong type argument 'cell array'> nnz ({"foo", "bar"}) |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
280 */ |
b5a0f11db158
Cell.cc: add tests for Cell::nnz
Carnë Draug <carandraug+dev@gmail.com>
parents:
17744
diff
changeset
|
281 |
4915 | 282 Cell |
5570 | 283 Cell::column (octave_idx_type i) const |
284 { | |
285 Cell retval; | |
286 | |
20980
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
287 if (ndims () > 2) |
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
288 error ("Cell::column: requires 2-D cell array"); |
20831
35241c4b696c
eliminate return statements after calls to error
John W. Eaton <jwe@octave.org>
parents:
20726
diff
changeset
|
289 |
20980
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
290 if (i < 0 || i >= cols ()) |
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
291 error ("invalid column selection"); |
5570 | 292 |
20980
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
293 octave_idx_type nr = rows (); |
5570 | 294 |
20980
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
295 retval.resize (dim_vector (nr, 1)); |
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
296 |
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
297 for (octave_idx_type j = 0; j < nr; j++) |
81c2b14c209f
maint: invert if/else/error instances.
John W. Eaton <jwe@octave.org>
parents:
20940
diff
changeset
|
298 retval.xelem (j) = elem (j, i); |
5570 | 299 |
300 return retval; | |
301 } | |
302 | |
303 Cell | |
5275 | 304 Cell::concat (const Cell& rb, const Array<octave_idx_type>& ra_idx) |
4806 | 305 { |
5073 | 306 return insert (rb, ra_idx); |
4915 | 307 } |
308 | |
309 Cell& | |
5275 | 310 Cell::insert (const Cell& a, octave_idx_type r, octave_idx_type c) |
4915 | 311 { |
312 Array<octave_value>::insert (a, r, c); | |
313 return *this; | |
314 } | |
315 | |
316 Cell& | |
5275 | 317 Cell::insert (const Cell& a, const Array<octave_idx_type>& ra_idx) |
4915 | 318 { |
319 Array<octave_value>::insert (a, ra_idx); | |
320 return *this; | |
4806 | 321 } |
322 | |
7530
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
323 Cell |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
324 Cell::map (ctype_mapper fcn) const |
7530
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
325 { |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
326 Cell retval (dims ()); |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
327 octave_value *r = retval.fortran_vec (); |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
328 |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
329 const octave_value *p = data (); |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
330 |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
331 for (octave_idx_type i = 0; i < numel (); i++) |
9813
8fa32b527d9a
improve & partially revert previous change
Jaroslav Hajek <highegg@gmail.com>
parents:
9812
diff
changeset
|
332 r[i] = ((p++)->*fcn) (); |
7530
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
333 |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
334 return retval; |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
335 } |
bb0f2353cff5
new cell array ctype mappers
John W. Eaton <jwe@octave.org>
parents:
7209
diff
changeset
|
336 |
21573
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
337 octave_value |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
338 Cell::resize_fill_value (void) const |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
339 { |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
340 static octave_value rfv = octave_value (Matrix ()); |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
341 return rfv; |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
342 } |
f3f8e1d3e399
avoid mulitple definitions of static function-scope vars (bug #47372)
John W. Eaton <jwe@octave.org>
parents:
19697
diff
changeset
|
343 |
7618
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7530
diff
changeset
|
344 Cell |
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7530
diff
changeset
|
345 Cell::diag (octave_idx_type k) const |
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7530
diff
changeset
|
346 { |
9732
b4fdfee405b5
remove ArrayN<T> + fix nonhom. diag-scalar ops
Jaroslav Hajek <highegg@gmail.com>
parents:
9415
diff
changeset
|
347 return Array<octave_value>::diag (k); |
7618
3209a584e1ac
Further type preservation tests and fix of diag for cell arrays
David Bateman <dbateman@free.fr>
parents:
7530
diff
changeset
|
348 } |
14557
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
349 |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
350 Cell |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
351 Cell::diag (octave_idx_type m, octave_idx_type n) const |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
352 { |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
353 return Array<octave_value>::diag (m, n); |
e8e86ae3abbc
make diag (x, m, n) return a proper diagonal matrix object (bug #36099)
John W. Eaton <jwe@octave.org>
parents:
14138
diff
changeset
|
354 } |