Mercurial > hg > octave-lyh
annotate src/ov-cell.cc @ 8947:1e4b3149365a
allow size_equal called with 1 arg
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 10 Mar 2009 12:27:51 +0100 |
parents | eb63fbe60fab |
children | 1eb5b24186b6 |
rev | line source |
---|---|
3353 | 1 /* |
2 | |
8920 | 3 Copyright (C) 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 |
7017 | 4 John W. Eaton |
3353 | 5 |
6 This file is part of Octave. | |
7 | |
8 Octave is free software; you can redistribute it and/or modify it | |
9 under the terms of the GNU General Public License as published by the | |
7016 | 10 Free Software Foundation; either version 3 of the License, or (at your |
11 option) any later version. | |
3353 | 12 |
13 Octave is distributed in the hope that it will be useful, but WITHOUT | |
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 for more details. | |
17 | |
18 You should have received a copy of the GNU General Public License | |
7016 | 19 along with Octave; see the file COPYING. If not, see |
20 <http://www.gnu.org/licenses/>. | |
3353 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
25 #include <config.h> | |
26 #endif | |
27 | |
5850 | 28 #include <iomanip> |
3503 | 29 #include <iostream> |
5765 | 30 #include <sstream> |
5164 | 31 #include <vector> |
3353 | 32 |
5360 | 33 #include "Array-util.h" |
34 #include "byte-swap.h" | |
3353 | 35 #include "lo-utils.h" |
4153 | 36 #include "quit.h" |
8377
25bc2d31e1bf
improve OCTAVE_LOCAL_BUFFER
Jaroslav Hajek <highegg@gmail.com>
parents:
8290
diff
changeset
|
37 #include "oct-locbuf.h" |
3353 | 38 |
39 #include "defun.h" | |
40 #include "error.h" | |
41 #include "ov-cell.h" | |
3354 | 42 #include "oct-obj.h" |
3353 | 43 #include "unwind-prot.h" |
3354 | 44 #include "utils.h" |
3928 | 45 #include "ov-base-mat.h" |
46 #include "ov-base-mat.cc" | |
47 #include "ov-re-mat.h" | |
48 #include "ov-scalar.h" | |
5360 | 49 #include "pr-output.h" |
50 #include "ov-scalar.h" | |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8551
diff
changeset
|
51 #include "gripes.h" |
3928 | 52 |
4687 | 53 #include "ls-oct-ascii.h" |
54 #include "ls-oct-binary.h" | |
55 #include "ls-hdf5.h" | |
56 #include "ls-utils.h" | |
57 | |
8679
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
58 // Cell is able to handle octave_value indexing by itself, so just forward |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
59 // everything. |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
60 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
61 template <> |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
62 octave_value |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
63 octave_base_matrix<Cell>::do_index_op (const octave_value_list& idx, |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
64 bool resize_ok) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
65 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
66 return matrix.index (idx, resize_ok); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
67 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
68 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
69 template <> |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
70 void |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
71 octave_base_matrix<Cell>::assign (const octave_value_list& idx, const Cell& rhs) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
72 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
73 matrix.assign (idx, rhs); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
74 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
75 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
76 template <> |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
77 void |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
78 octave_base_matrix<Cell>::assign (const octave_value_list& idx, octave_value rhs) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
79 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
80 // FIXME: Really? |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
81 if (rhs.is_cell ()) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
82 matrix.assign (idx, rhs.cell_value ()); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
83 else |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
84 matrix.assign (idx, Cell (rhs)); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
85 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
86 |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
87 template <> |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
88 void |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
89 octave_base_matrix<Cell>::delete_elements (const octave_value_list& idx) |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
90 { |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
91 matrix.delete_elements (idx); |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
92 } |
280fae940bb0
optimize scalar indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8626
diff
changeset
|
93 |
3928 | 94 template class octave_base_matrix<Cell>; |
3353 | 95 |
96 DEFINE_OCTAVE_ALLOCATOR (octave_cell); | |
97 | |
4612 | 98 DEFINE_OV_TYPEID_FUNCTIONS_AND_DATA (octave_cell, "cell", "cell"); |
3353 | 99 |
6833 | 100 static void |
101 gripe_failed_assignment (void) | |
102 { | |
103 error ("assignment to cell array failed"); | |
104 } | |
105 | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
106 octave_value_list |
4247 | 107 octave_cell::subsref (const std::string& type, |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
108 const std::list<octave_value_list>& idx, |
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
109 int nargout) |
3933 | 110 { |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
111 octave_value_list retval; |
3933 | 112 |
113 switch (type[0]) | |
114 { | |
115 case '(': | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
116 retval(0) = do_index_op (idx.front ()); |
3933 | 117 break; |
118 | |
119 case '{': | |
120 { | |
121 octave_value tmp = do_index_op (idx.front ()); | |
122 | |
4582 | 123 if (! error_state) |
3933 | 124 { |
4582 | 125 Cell tcell = tmp.cell_value (); |
126 | |
127 if (tcell.length () == 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
128 retval(0) = tcell(0,0); |
4582 | 129 else |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
130 retval = octave_value (octave_value_list (tcell), true); |
3933 | 131 } |
132 } | |
133 break; | |
134 | |
135 case '.': | |
136 { | |
137 std::string nm = type_name (); | |
138 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
139 } | |
140 break; | |
141 | |
142 default: | |
143 panic_impossible (); | |
144 } | |
145 | |
5775 | 146 // FIXME -- perhaps there should be an |
4994 | 147 // octave_value_list::next_subsref member function? See also |
148 // octave_user_function::subsref. | |
149 | |
150 if (idx.size () > 1) | |
7651
443a8f5a50fd
require both subsref variants to be defined in octave_value subclasses
John W. Eaton <jwe@octave.org>
parents:
7622
diff
changeset
|
151 retval = retval(0).next_subsref (nargout, type, idx); |
4994 | 152 |
153 return retval; | |
3933 | 154 } |
155 | |
156 octave_value | |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
157 octave_cell::subsref (const std::string& type, |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
158 const std::list<octave_value_list>& idx, |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
159 bool auto_add) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
160 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
161 octave_value retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
162 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
163 switch (type[0]) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
164 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
165 case '(': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
166 retval = do_index_op (idx.front (), auto_add); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
167 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
168 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
169 case '{': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
170 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
171 octave_value tmp = do_index_op (idx.front (), auto_add); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
172 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
173 if (! error_state) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
174 { |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
175 const Cell tcell = tmp.cell_value (); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
176 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
177 if (tcell.length () == 1) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
178 retval = tcell(0,0); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
179 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8551
diff
changeset
|
180 retval = octave_value (octave_value_list (tcell), true); |
8551
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
181 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
182 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
183 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
184 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
185 case '.': |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
186 { |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
187 std::string nm = type_name (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
188 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
189 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
190 break; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
191 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
192 default: |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
193 panic_impossible (); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
194 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
195 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
196 // FIXME -- perhaps there should be an |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
197 // octave_value_list::next_subsref member function? See also |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
198 // octave_user_function::subsref. |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
199 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
200 if (idx.size () > 1) |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
201 retval = retval.next_subsref (auto_add, type, idx); |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
202 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
203 return retval; |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
204 } |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
205 |
906f976d35a8
further improve struct&cell indexing & indexed assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
206 octave_value |
4247 | 207 octave_cell::subsasgn (const std::string& type, |
4219 | 208 const std::list<octave_value_list>& idx, |
3933 | 209 const octave_value& rhs) |
210 { | |
211 octave_value retval; | |
212 | |
213 int n = type.length (); | |
214 | |
215 octave_value t_rhs = rhs; | |
216 | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
217 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
218 |
3933 | 219 if (n > 1) |
220 { | |
221 switch (type[0]) | |
222 { | |
223 case '(': | |
224 { | |
6767 | 225 if (is_empty () && type[1] == '.') |
226 { | |
227 // Allow conversion of empty cell array to some other | |
228 // type in cases like | |
229 // | |
230 // x = []; x(i).f = rhs | |
3933 | 231 |
6767 | 232 octave_value tmp = octave_value::empty_conv (type, rhs); |
3933 | 233 |
6767 | 234 return tmp.subsasgn (type, idx, rhs); |
235 } | |
236 else | |
3933 | 237 { |
6767 | 238 octave_value tmp = do_index_op (idx.front (), true); |
3933 | 239 |
6767 | 240 if (! tmp.is_defined ()) |
241 tmp = octave_value::empty_conv (type.substr (1), rhs); | |
3933 | 242 |
6767 | 243 if (! error_state) |
244 { | |
245 std::list<octave_value_list> next_idx (idx); | |
4362 | 246 |
6767 | 247 next_idx.erase (next_idx.begin ()); |
248 | |
249 tmp.make_unique (); | |
250 | |
251 t_rhs = tmp.subsasgn (type.substr (1), next_idx, rhs); | |
252 } | |
3933 | 253 } |
254 } | |
255 break; | |
256 | |
257 case '{': | |
258 { | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
259 Cell tmpc = matrix.index (idx.front (), true); |
3933 | 260 |
4362 | 261 if (! error_state) |
262 { | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
263 std::list<octave_value_list> next_idx (idx); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
264 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
265 next_idx.erase (next_idx.begin ()); |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
266 |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
267 std::string next_type = type.substr (1); |
3933 | 268 |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8551
diff
changeset
|
269 if (tmpc.numel () == 1) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
270 { |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
271 octave_value tmp = tmpc(0); |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
272 tmpc = Cell (); |
3933 | 273 |
5927 | 274 if (! tmp.is_defined () || tmp.is_zero_by_zero ()) |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
275 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
276 tmp = octave_value::empty_conv (type.substr (1), rhs); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
277 tmp.make_unique (); // probably a no-op. |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
278 } |
8452
d6a349c7bd39
fix {} assigment if error occurs on subsequent assignment component
Jaroslav Hajek <highegg@gmail.com>
parents:
8446
diff
changeset
|
279 else |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
280 // optimization: ignore the copy still stored inside our array. |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
281 tmp.make_unique (1); |
4519 | 282 |
283 if (! error_state) | |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
284 t_rhs = tmp.subsasgn (next_type, next_idx, rhs); |
4362 | 285 } |
8446
7b25349b32e6
avoid redundant copying in {} assignment
Jaroslav Hajek <highegg@gmail.com>
parents:
8377
diff
changeset
|
286 else |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8551
diff
changeset
|
287 gripe_indexed_cs_list (); |
3933 | 288 } |
289 } | |
290 break; | |
291 | |
292 case '.': | |
293 { | |
294 std::string nm = type_name (); | |
295 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
296 } | |
297 break; | |
298 | |
299 default: | |
300 panic_impossible (); | |
301 } | |
302 } | |
303 | |
3940 | 304 if (! error_state) |
3933 | 305 { |
3940 | 306 switch (type[0]) |
307 { | |
308 case '(': | |
309 { | |
310 octave_value_list i = idx.front (); | |
3933 | 311 |
3940 | 312 if (t_rhs.is_cell ()) |
313 octave_base_matrix<Cell>::assign (i, t_rhs.cell_value ()); | |
314 else | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
315 if (t_rhs.is_null_value ()) |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
7813
diff
changeset
|
316 octave_base_matrix<Cell>::delete_elements (i); |
4941 | 317 else |
318 octave_base_matrix<Cell>::assign (i, Cell (t_rhs)); | |
3933 | 319 |
6833 | 320 if (! error_state) |
321 { | |
322 count++; | |
323 retval = octave_value (this); | |
324 } | |
325 else | |
326 gripe_failed_assignment (); | |
3940 | 327 } |
328 break; | |
3933 | 329 |
3940 | 330 case '{': |
331 { | |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
332 octave_value_list idxf = idx.front (); |
3933 | 333 |
5846 | 334 if (t_rhs.is_cs_list ()) |
335 { | |
336 Cell tmp_cell = Cell (t_rhs.list_value ()); | |
337 | |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
338 // Inquire the proper shape of the RHS. |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
339 |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
340 dim_vector didx = dims ().redim (idxf.length ()); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
341 for (octave_idx_type k = 0; k < idxf.length (); k++) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
342 if (! idxf(k).is_magic_colon ()) didx(k) = idxf(k).numel (); |
7040 | 343 |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
344 if (didx.numel () == tmp_cell.numel ()) |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
345 tmp_cell = tmp_cell.reshape (didx); |
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
346 |
5846 | 347 |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
348 octave_base_matrix<Cell>::assign (idxf, tmp_cell); |
5846 | 349 } |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
350 else if (idxf.all_scalars () || do_index_op (idxf, true).numel () == 1) |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
351 // Regularize a null matrix if stored into a cell. |
8587
35656d6ad061
properly reshape cs-lists assigned to struct & cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8580
diff
changeset
|
352 octave_base_matrix<Cell>::assign (idxf, Cell (t_rhs.storable_value ())); |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8452
diff
changeset
|
353 else if (! error_state) |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
354 error ("invalid assignment to cs-list outside multiple assignment."); |
3933 | 355 |
6833 | 356 if (! error_state) |
357 { | |
358 count++; | |
359 retval = octave_value (this); | |
360 } | |
361 else | |
362 gripe_failed_assignment (); | |
3940 | 363 } |
364 break; | |
3933 | 365 |
3940 | 366 case '.': |
367 { | |
368 std::string nm = type_name (); | |
369 error ("%s cannot be indexed with %c", nm.c_str (), type[0]); | |
370 } | |
371 break; | |
3933 | 372 |
3940 | 373 default: |
374 panic_impossible (); | |
375 } | |
3933 | 376 } |
377 | |
378 return retval; | |
379 } | |
380 | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
381 void |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
382 octave_cell::clear_cellstr_cache (void) const |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
383 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
384 cellstr_cache = Array<std::string> (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
385 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
386 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
387 void |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
388 octave_cell::make_cellstr_cache (void) const |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
389 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
390 cellstr_cache = Array<std::string> (matrix.dims ()); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
391 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
392 octave_idx_type n = numel (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
393 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
394 std::string *dst = cellstr_cache.fortran_vec (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
395 const octave_value *src = matrix.data (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
396 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
397 for (octave_idx_type i = 0; i < n; i++) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
398 dst[i] = src[i].string_value (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
399 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
400 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
401 bool |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
402 octave_cell::is_cellstr (void) const |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
403 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
404 bool retval; |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
405 if (! cellstr_cache.is_empty ()) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
406 retval = true; |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
407 else |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
408 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
409 retval = matrix.is_cellstr (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
410 // force cache to be created here |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
411 if (retval) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
412 make_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
413 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
414 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
415 return retval; |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
416 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
417 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
418 void |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
419 octave_cell::assign (const octave_value_list& idx, const Cell& rhs) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
420 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
421 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
422 octave_base_matrix<Cell>::assign (idx, rhs); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
423 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
424 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
425 void |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
426 octave_cell::assign (const octave_value_list& idx, const octave_value& rhs) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
427 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
428 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
429 octave_base_matrix<Cell>::assign (idx, rhs); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
430 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
431 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
432 |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
433 void |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
434 octave_cell::delete_elements (const octave_value_list& idx) |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
435 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
436 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
437 octave_base_matrix<Cell>::delete_elements (idx); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
438 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
439 |
4791 | 440 size_t |
441 octave_cell::byte_size (void) const | |
442 { | |
443 size_t retval = 0; | |
444 | |
5275 | 445 for (octave_idx_type i = 0; i < numel (); i++) |
4791 | 446 retval += matrix(i).byte_size (); |
447 | |
448 return retval; | |
449 } | |
450 | |
8732 | 451 octave_value |
452 octave_cell::sort (octave_idx_type dim, sortmode mode) const | |
453 { | |
454 octave_value retval; | |
455 | |
456 if (is_cellstr ()) | |
457 { | |
458 Array<std::string> tmp = cellstr_value (); | |
459 | |
8824
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
460 tmp = tmp.sort (dim, mode); |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
461 |
8825
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8824
diff
changeset
|
462 // We already have the cache. |
c3445f1c8cb4
reuse cellstr cache in strcmp
Jaroslav Hajek <highegg@gmail.com>
parents:
8824
diff
changeset
|
463 retval = new octave_cell (tmp); |
8732 | 464 } |
465 else | |
466 error ("sort: only cell arrays of character strings may be sorted"); | |
467 | |
468 return retval; | |
469 } | |
470 | |
471 octave_value | |
472 octave_cell::sort (Array<octave_idx_type> &sidx, octave_idx_type dim, | |
473 sortmode mode) const | |
474 { | |
475 octave_value retval; | |
476 | |
477 if (is_cellstr ()) | |
478 { | |
479 Array<std::string> tmp = cellstr_value (); | |
480 | |
8824
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
481 tmp = tmp.sort (sidx, dim, mode); |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
482 |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
483 // We do it the hard way to auto-create the result's cache |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
484 octave_cell *result = new octave_cell (Cell (tmp)); |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
485 result->cellstr_cache = tmp; |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
486 |
76ddf0ab985d
auto-set cellstr cache when sorting cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8823
diff
changeset
|
487 retval = result; |
8732 | 488 } |
489 else | |
490 error ("sort: only cell arrays of character strings may be sorted"); | |
491 | |
492 return retval; | |
493 } | |
494 | |
8823
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
495 sortmode |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
496 octave_cell::is_sorted (sortmode mode) const |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
497 { |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
498 sortmode retval = UNSORTED; |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
499 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
500 if (is_cellstr ()) |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
501 { |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
502 Array<std::string> tmp = cellstr_value (); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
503 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
504 retval = tmp.is_sorted (mode); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
505 } |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
506 else |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
507 error ("issorted: not a cell array of strings"); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
508 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
509 return retval; |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
510 } |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
511 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
512 |
8732 | 513 Array<octave_idx_type> |
8733
3ef774603887
rename all uses of sortrows_idx to sort_rows_idx
John W. Eaton <jwe@octave.org>
parents:
8732
diff
changeset
|
514 octave_cell::sort_rows_idx (sortmode mode) const |
8732 | 515 { |
516 Array<octave_idx_type> retval; | |
517 | |
518 if (is_cellstr ()) | |
519 { | |
520 Array<std::string> tmp = cellstr_value (); | |
521 | |
522 retval = tmp.sort_rows_idx (mode); | |
523 } | |
524 else | |
525 error ("sortrows: only cell arrays of character strings may be sorted"); | |
526 | |
527 return retval; | |
528 } | |
529 | |
8823
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
530 sortmode |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
531 octave_cell::is_sorted_rows (sortmode mode) const |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
532 { |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
533 sortmode retval = UNSORTED; |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
534 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
535 if (is_cellstr ()) |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
536 { |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
537 Array<std::string> tmp = cellstr_value (); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
538 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
539 retval = tmp.is_sorted_rows (mode); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
540 } |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
541 else |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
542 error ("issorted: not a cell array of strings"); |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
543 |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
544 return retval; |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
545 } |
3efa512a0957
make issorted work for cells
Jaroslav Hajek <highegg@gmail.com>
parents:
8815
diff
changeset
|
546 |
8626
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
547 bool |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
548 octave_cell::is_true (void) const |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
549 { |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
550 error ("invalid conversion from cell array to logical value"); |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
551 return false; |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
552 } |
1dce30ab0e72
don't convert NaN to logical in bool expressions
John W. Eaton <jwe@octave.org>
parents:
8587
diff
changeset
|
553 |
3933 | 554 octave_value_list |
555 octave_cell::list_value (void) const | |
556 { | |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
557 return octave_value_list (matrix); |
3933 | 558 } |
559 | |
4243 | 560 string_vector |
5715 | 561 octave_cell::all_strings (bool pad) const |
4243 | 562 { |
4358 | 563 string_vector retval; |
564 | |
7285 | 565 octave_idx_type nel = numel (); |
4243 | 566 |
4358 | 567 int n_elts = 0; |
568 | |
5275 | 569 octave_idx_type max_len = 0; |
4358 | 570 |
7285 | 571 for (octave_idx_type i = 0; i < nel; i++) |
4358 | 572 { |
7285 | 573 string_vector s = matrix(i).all_strings (); |
574 | |
575 if (error_state) | |
576 return retval; | |
4358 | 577 |
7285 | 578 octave_idx_type s_len = s.length (); |
4358 | 579 |
7285 | 580 n_elts += s_len ? s_len : 1; |
4358 | 581 |
7285 | 582 octave_idx_type s_max_len = s.max_length (); |
4358 | 583 |
7285 | 584 if (s_max_len > max_len) |
585 max_len = s_max_len; | |
4358 | 586 } |
587 | |
588 retval.resize (n_elts); | |
4243 | 589 |
5275 | 590 octave_idx_type k = 0; |
4243 | 591 |
7285 | 592 for (octave_idx_type i = 0; i < nel; i++) |
4243 | 593 { |
7285 | 594 string_vector s = matrix(i).all_strings (); |
595 | |
596 octave_idx_type s_len = s.length (); | |
4358 | 597 |
7285 | 598 if (s_len) |
599 { | |
600 for (octave_idx_type j = 0; j < s_len; j++) | |
5715 | 601 { |
7285 | 602 std::string t = s[j]; |
5715 | 603 int t_len = t.length (); |
604 | |
605 if (pad && max_len > t_len) | |
606 t += std::string (max_len - t_len, ' '); | |
607 | |
608 retval[k++] = t; | |
609 } | |
4243 | 610 } |
7285 | 611 else if (pad) |
612 retval[k++] = std::string (max_len, ' '); | |
613 else | |
614 retval[k++] = std::string (); | |
4243 | 615 } |
616 | |
617 return retval; | |
618 } | |
619 | |
8732 | 620 Array<std::string> |
621 octave_cell::cellstr_value (void) const | |
622 { | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
623 Array<std::string> retval; |
8732 | 624 |
625 if (is_cellstr ()) | |
626 { | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
627 retval = cellstr_cache; |
8732 | 628 } |
629 else | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
630 error ("invalid conversion from cell array to array of strings"); |
8732 | 631 |
632 return retval; | |
633 } | |
634 | |
4604 | 635 bool |
636 octave_cell::print_as_scalar (void) const | |
637 { | |
638 return (ndims () > 2 || numel () == 0); | |
639 } | |
640 | |
3933 | 641 void |
642 octave_cell::print (std::ostream& os, bool) const | |
643 { | |
644 print_raw (os); | |
645 } | |
646 | |
647 void | |
648 octave_cell::print_raw (std::ostream& os, bool) const | |
649 { | |
4587 | 650 int nd = matrix.ndims (); |
4513 | 651 |
4587 | 652 if (nd == 2) |
4513 | 653 { |
5275 | 654 octave_idx_type nr = rows (); |
655 octave_idx_type nc = columns (); | |
4513 | 656 |
657 if (nr > 0 && nc > 0) | |
658 { | |
659 indent (os); | |
660 os << "{"; | |
661 newline (os); | |
662 | |
663 increment_indent_level (); | |
664 | |
5275 | 665 for (octave_idx_type j = 0; j < nc; j++) |
4513 | 666 { |
5275 | 667 for (octave_idx_type i = 0; i < nr; i++) |
4513 | 668 { |
669 OCTAVE_QUIT; | |
670 | |
5765 | 671 std::ostringstream buf; |
672 buf << "[" << i+1 << "," << j+1 << "]"; | |
3933 | 673 |
4513 | 674 octave_value val = matrix(i,j); |
675 | |
5765 | 676 val.print_with_name (os, buf.str ()); |
4513 | 677 } |
678 } | |
679 | |
680 decrement_indent_level (); | |
681 | |
682 indent (os); | |
683 os << "}"; | |
684 newline (os); | |
685 } | |
686 else | |
687 { | |
688 os << "{}"; | |
5360 | 689 if (Vprint_empty_dimensions) |
4513 | 690 os << "(" << nr << "x" << nc << ")"; |
691 os << "\n"; | |
692 } | |
693 } | |
694 else | |
3933 | 695 { |
696 indent (os); | |
4513 | 697 dim_vector dv = matrix.dims (); |
4587 | 698 os << "{" << dv.str () << " Cell Array}"; |
3933 | 699 newline (os); |
700 } | |
701 } | |
702 | |
4687 | 703 #define CELL_ELT_TAG "<cell-element>" |
704 | |
705 bool | |
6974 | 706 octave_cell::save_ascii (std::ostream& os) |
4687 | 707 { |
708 dim_vector d = dims (); | |
709 if (d.length () > 2) | |
710 { | |
711 os << "# ndims: " << d.length () << "\n"; | |
712 | |
5275 | 713 for (int i = 0; i < d.length (); i++) |
4687 | 714 os << " " << d (i); |
715 os << "\n"; | |
716 | |
717 Cell tmp = cell_value (); | |
718 | |
5275 | 719 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 720 { |
721 octave_value o_val = tmp.elem (i); | |
722 | |
723 // Recurse to print sub-value. | |
6974 | 724 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 725 |
726 if (! b) | |
727 return os; | |
728 } | |
729 } | |
730 else | |
731 { | |
732 // Keep this case, rather than use generic code above for backward | |
733 // compatiability. Makes load_ascii much more complex!! | |
734 os << "# rows: " << rows () << "\n" | |
735 << "# columns: " << columns () << "\n"; | |
736 | |
737 Cell tmp = cell_value (); | |
738 | |
5275 | 739 for (octave_idx_type j = 0; j < tmp.cols (); j++) |
4687 | 740 { |
5275 | 741 for (octave_idx_type i = 0; i < tmp.rows (); i++) |
4687 | 742 { |
743 octave_value o_val = tmp.elem (i, j); | |
744 | |
745 // Recurse to print sub-value. | |
6974 | 746 bool b = save_ascii_data (os, o_val, CELL_ELT_TAG, false, 0); |
4687 | 747 |
748 if (! b) | |
749 return os; | |
750 } | |
751 | |
752 os << "\n"; | |
753 } | |
754 } | |
755 | |
756 return true; | |
757 } | |
758 | |
759 bool | |
760 octave_cell::load_ascii (std::istream& is) | |
761 { | |
762 bool success = true; | |
5099 | 763 |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
764 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
765 |
5099 | 766 string_vector keywords(2); |
4687 | 767 |
5099 | 768 keywords[0] = "ndims"; |
769 keywords[1] = "rows"; | |
770 | |
771 std::string kw; | |
5275 | 772 octave_idx_type val = 0; |
5099 | 773 |
774 if (extract_keyword (is, keywords, kw, val, true)) | |
4687 | 775 { |
5099 | 776 if (kw == "ndims") |
4687 | 777 { |
5275 | 778 int mdims = static_cast<int> (val); |
4687 | 779 |
5099 | 780 if (mdims >= 0) |
781 { | |
782 dim_vector dv; | |
783 dv.resize (mdims); | |
784 | |
785 for (int i = 0; i < mdims; i++) | |
786 is >> dv(i); | |
4687 | 787 |
5099 | 788 Cell tmp(dv); |
4687 | 789 |
5275 | 790 for (octave_idx_type i = 0; i < dv.numel (); i++) |
5099 | 791 { |
792 octave_value t2; | |
793 bool dummy; | |
794 | |
795 // recurse to read cell elements | |
796 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 797 dummy, t2, i); |
4687 | 798 |
5099 | 799 if (nm == CELL_ELT_TAG) |
800 { | |
801 if (is) | |
802 tmp.elem (i) = t2; | |
803 } | |
804 else | |
805 { | |
806 error ("load: cell array element had unexpected name"); | |
807 success = false; | |
808 break; | |
809 } | |
810 } | |
4687 | 811 |
5099 | 812 if (is) |
813 matrix = tmp; | |
4687 | 814 else |
815 { | |
5099 | 816 error ("load: failed to load matrix constant"); |
4687 | 817 success = false; |
818 } | |
819 } | |
820 else | |
821 { | |
5099 | 822 error ("load: failed to extract number of rows and columns"); |
823 success = false; | |
824 } | |
825 } | |
826 else if (kw == "rows") | |
827 { | |
5275 | 828 octave_idx_type nr = val; |
829 octave_idx_type nc = 0; | |
5099 | 830 |
831 if (nr >= 0 && extract_keyword (is, "columns", nc) && nc >= 0) | |
832 { | |
833 if (nr > 0 && nc > 0) | |
834 { | |
835 Cell tmp (nr, nc); | |
836 | |
5275 | 837 for (octave_idx_type j = 0; j < nc; j++) |
5099 | 838 { |
5275 | 839 for (octave_idx_type i = 0; i < nr; i++) |
5099 | 840 { |
841 octave_value t2; | |
842 bool dummy; | |
843 | |
844 // recurse to read cell elements | |
845 std::string nm = read_ascii_data (is, std::string (), | |
5759 | 846 dummy, t2, i); |
5099 | 847 |
848 if (nm == CELL_ELT_TAG) | |
849 { | |
850 if (is) | |
851 tmp.elem (i, j) = t2; | |
852 } | |
853 else | |
854 { | |
855 error ("load: cell array element had unexpected name"); | |
856 success = false; | |
857 goto cell_read_error; | |
858 } | |
859 } | |
860 } | |
861 | |
862 cell_read_error: | |
863 | |
864 if (is) | |
865 matrix = tmp; | |
866 else | |
867 { | |
868 error ("load: failed to load cell element"); | |
869 success = false; | |
870 } | |
871 } | |
872 else if (nr == 0 || nc == 0) | |
873 matrix = Cell (nr, nc); | |
874 else | |
875 panic_impossible (); | |
876 } | |
877 else | |
878 { | |
879 error ("load: failed to extract number of rows and columns for cell array"); | |
4687 | 880 success = false; |
881 } | |
882 } | |
883 else | |
5099 | 884 panic_impossible (); |
4687 | 885 } |
886 else | |
887 { | |
5099 | 888 error ("load: failed to extract number of rows and columns"); |
889 success = false; | |
4687 | 890 } |
891 | |
892 return success; | |
893 } | |
894 | |
895 bool | |
896 octave_cell::save_binary (std::ostream& os, bool& save_as_floats) | |
897 { | |
898 dim_vector d = dims (); | |
899 if (d.length () < 1) | |
900 return false; | |
901 | |
902 // Use negative value for ndims | |
5828 | 903 int32_t di = - d.length(); |
5760 | 904 os.write (reinterpret_cast<char *> (&di), 4); |
5275 | 905 for (int i = 0; i < d.length (); i++) |
4687 | 906 { |
907 di = d(i); | |
5760 | 908 os.write (reinterpret_cast<char *> (&di), 4); |
4687 | 909 } |
910 | |
911 Cell tmp = cell_value (); | |
912 | |
5275 | 913 for (octave_idx_type i = 0; i < d.numel (); i++) |
4687 | 914 { |
915 octave_value o_val = tmp.elem (i); | |
916 | |
917 // Recurse to print sub-value. | |
918 bool b = save_binary_data (os, o_val, CELL_ELT_TAG, "", 0, | |
919 save_as_floats); | |
920 | |
921 if (! b) | |
922 return false; | |
923 } | |
924 | |
925 return true; | |
926 } | |
927 | |
928 bool | |
929 octave_cell::load_binary (std::istream& is, bool swap, | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
930 oct_mach_info::float_format fmt) |
4687 | 931 { |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
932 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
933 |
4687 | 934 bool success = true; |
5828 | 935 int32_t mdims; |
5760 | 936 if (! is.read (reinterpret_cast<char *> (&mdims), 4)) |
4687 | 937 return false; |
938 if (swap) | |
4944 | 939 swap_bytes<4> (&mdims); |
4687 | 940 if (mdims >= 0) |
941 return false; | |
942 | |
943 mdims = -mdims; | |
5828 | 944 int32_t di; |
4687 | 945 dim_vector dv; |
946 dv.resize (mdims); | |
947 | |
948 for (int i = 0; i < mdims; i++) | |
949 { | |
5760 | 950 if (! is.read (reinterpret_cast<char *> (&di), 4)) |
4687 | 951 return false; |
952 if (swap) | |
4944 | 953 swap_bytes<4> (&di); |
4687 | 954 dv(i) = di; |
955 } | |
956 | |
5157 | 957 // Convert an array with a single dimension to be a row vector. |
958 // Octave should never write files like this, other software | |
959 // might. | |
960 | |
961 if (mdims == 1) | |
962 { | |
963 mdims = 2; | |
964 dv.resize (mdims); | |
965 dv(1) = dv(0); | |
966 dv(0) = 1; | |
967 } | |
968 | |
5275 | 969 octave_idx_type nel = dv.numel (); |
4687 | 970 Cell tmp(dv); |
971 | |
5275 | 972 for (octave_idx_type i = 0; i < nel; i++) |
4687 | 973 { |
974 octave_value t2; | |
975 bool dummy; | |
976 std::string doc; | |
977 | |
978 // recurse to read cell elements | |
979 std::string nm = read_binary_data (is, swap, fmt, std::string (), | |
980 dummy, t2, doc); | |
981 | |
982 if (nm == CELL_ELT_TAG) | |
983 { | |
984 if (is) | |
985 tmp.elem (i) = t2; | |
986 } | |
987 else | |
988 { | |
989 error ("load: cell array element had unexpected name"); | |
990 success = false; | |
991 break; | |
992 } | |
993 } | |
994 | |
995 if (is) | |
996 matrix = tmp; | |
997 else | |
998 { | |
999 error ("load: failed to load matrix constant"); | |
1000 success = false; | |
1001 } | |
1002 | |
1003 return success; | |
1004 } | |
1005 | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1006 void * |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1007 octave_cell::mex_get_data (void) const |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1008 { |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1009 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1010 return matrix.mex_get_data (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1011 } |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1012 |
4687 | 1013 #if defined (HAVE_HDF5) |
4815 | 1014 |
4687 | 1015 bool |
1016 octave_cell::save_hdf5 (hid_t loc_id, const char *name, bool save_as_floats) | |
1017 { | |
4814 | 1018 dim_vector dv = dims (); |
4837 | 1019 int empty = save_hdf5_empty (loc_id, name, dv); |
1020 if (empty) | |
1021 return (empty > 0); | |
1022 | |
4815 | 1023 hsize_t rank = dv.length (); |
4687 | 1024 hid_t space_hid = -1, data_hid = -1, size_hid = -1; |
1025 | |
1026 data_hid = H5Gcreate (loc_id, name, 0); | |
4815 | 1027 |
1028 if (data_hid < 0) | |
1029 return false; | |
4687 | 1030 |
4814 | 1031 // Have to save cell array shape, since can't have a |
1032 // dataset of groups.... | |
4815 | 1033 |
1034 space_hid = H5Screate_simple (1, &rank, 0); | |
1035 | |
4687 | 1036 if (space_hid < 0) |
1037 { | |
1038 H5Gclose (data_hid); | |
1039 return false; | |
1040 } | |
1041 | |
5351 | 1042 OCTAVE_LOCAL_BUFFER (octave_idx_type, hdims, rank); |
4814 | 1043 |
1044 // Octave uses column-major, while HDF5 uses row-major ordering | |
4933 | 1045 for (hsize_t i = 0; i < rank; i++) |
4815 | 1046 hdims[i] = dv(rank-i-1); |
4814 | 1047 |
5351 | 1048 size_hid = H5Dcreate (data_hid, "dims", H5T_NATIVE_IDX, space_hid, |
4687 | 1049 H5P_DEFAULT); |
1050 if (size_hid < 0) | |
1051 { | |
1052 H5Sclose (space_hid); | |
1053 H5Gclose (data_hid); | |
1054 return false; | |
1055 } | |
1056 | |
6276 | 1057 if (H5Dwrite (size_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
1058 H5P_DEFAULT, hdims) < 0) | |
4687 | 1059 { |
1060 H5Dclose (size_hid); | |
1061 H5Sclose (space_hid); | |
1062 H5Gclose (data_hid); | |
1063 return false; | |
1064 } | |
4815 | 1065 |
4687 | 1066 H5Dclose (size_hid); |
1067 H5Sclose (space_hid); | |
1068 | |
4815 | 1069 // Recursively add each element of the cell to this group. |
1070 | |
4687 | 1071 Cell tmp = cell_value (); |
5850 | 1072 |
1073 octave_idx_type nel = dv.numel (); | |
1074 | |
1075 for (octave_idx_type i = 0; i < nel; i++) | |
4687 | 1076 { |
5765 | 1077 std::ostringstream buf; |
7503
8c32f95c2639
convert mapper functions to new format
David Bateman <dbateman@free.fr>
parents:
7285
diff
changeset
|
1078 int digits = static_cast<int> (::floor (::log10 (static_cast<double> (nel)) + 1.0)); |
5850 | 1079 buf << "_" << std::setw (digits) << std::setfill ('0') << i; |
5765 | 1080 std::string s = buf.str (); |
4687 | 1081 |
5850 | 1082 if (! add_hdf5_data (data_hid, tmp.elem (i), s.c_str (), "", false, |
1083 save_as_floats)) | |
4814 | 1084 { |
1085 H5Gclose (data_hid); | |
1086 return false; | |
4687 | 1087 } |
1088 } | |
1089 | |
1090 H5Gclose (data_hid); | |
4815 | 1091 |
4687 | 1092 return true; |
1093 } | |
1094 | |
1095 bool | |
1096 octave_cell::load_hdf5 (hid_t loc_id, const char *name, | |
1097 bool have_h5giterate_bug) | |
1098 { | |
8815
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1099 clear_cellstr_cache (); |
af907aeedbf4
cache cellstr_value in ov-cell
Jaroslav Hajek <highegg@gmail.com>
parents:
8733
diff
changeset
|
1100 |
4687 | 1101 bool retval = false; |
4837 | 1102 |
1103 dim_vector dv; | |
1104 int empty = load_hdf5_empty (loc_id, name, dv); | |
1105 if (empty > 0) | |
1106 matrix.resize(dv); | |
1107 if (empty) | |
1108 return (empty > 0); | |
1109 | |
4687 | 1110 hid_t group_id = H5Gopen (loc_id, name); |
1111 | |
1112 if (group_id < 0) | |
1113 return false; | |
1114 | |
4814 | 1115 hid_t data_hid = H5Dopen (group_id, "dims"); |
4687 | 1116 hid_t space_hid = H5Dget_space (data_hid); |
1117 hsize_t rank = H5Sget_simple_extent_ndims (space_hid); | |
4814 | 1118 if (rank != 1) |
4687 | 1119 { |
4837 | 1120 H5Dclose (data_hid); |
1121 H5Gclose (group_id); | |
4687 | 1122 return false; |
1123 } | |
1124 | |
4814 | 1125 OCTAVE_LOCAL_BUFFER (hsize_t, hdims, rank); |
1126 OCTAVE_LOCAL_BUFFER (hsize_t, maxdims, rank); | |
4815 | 1127 |
4814 | 1128 H5Sget_simple_extent_dims (space_hid, hdims, maxdims); |
4687 | 1129 |
4815 | 1130 // Octave uses column-major, while HDF5 uses row-major ordering. |
1131 | |
4814 | 1132 dv.resize (hdims[0]); |
4815 | 1133 |
5351 | 1134 OCTAVE_LOCAL_BUFFER (octave_idx_type, tmp, hdims[0]); |
4814 | 1135 |
5351 | 1136 if (H5Dread (data_hid, H5T_NATIVE_IDX, H5S_ALL, H5S_ALL, |
4815 | 1137 H5P_DEFAULT, tmp) < 0) |
4687 | 1138 { |
4837 | 1139 H5Dclose (data_hid); |
1140 H5Gclose (group_id); | |
4687 | 1141 return false; |
1142 } | |
4815 | 1143 |
4687 | 1144 H5Dclose (data_hid); |
1145 H5Gclose (group_id); | |
1146 | |
4815 | 1147 for (hsize_t i = 0, j = hdims[0] - 1; i < hdims[0]; i++, j--) |
4814 | 1148 dv(j) = tmp[i]; |
1149 | |
4687 | 1150 hdf5_callback_data dsub; |
1151 | |
1152 herr_t retval2 = -1; | |
4815 | 1153 |
4814 | 1154 Cell m (dv); |
4815 | 1155 |
4687 | 1156 int current_item = 0; |
4815 | 1157 |
4687 | 1158 if (have_h5giterate_bug) |
4815 | 1159 current_item = 1; // Skip dims items in group. |
4687 | 1160 |
4696 | 1161 #ifdef HAVE_H5GGET_NUM_OBJS |
1162 hsize_t num_obj = 0; | |
5060 | 1163 group_id = H5Gopen (loc_id, name); |
1164 H5Gget_num_objs (group_id, &num_obj); | |
1165 H5Gclose (group_id); | |
4696 | 1166 #endif |
1167 | |
5275 | 1168 for (octave_idx_type i = 0; i < dv.numel (); i++) |
4687 | 1169 { |
4696 | 1170 |
1171 #ifdef HAVE_H5GGET_NUM_OBJS | |
4814 | 1172 if (current_item >= static_cast<int> (num_obj)) |
1173 retval2 = -1; | |
1174 else | |
4696 | 1175 #endif |
4814 | 1176 retval2 = H5Giterate (loc_id, name, ¤t_item, |
1177 hdf5_read_next_data, &dsub); | |
1178 | |
4687 | 1179 if (retval2 <= 0) |
1180 break; | |
4814 | 1181 |
1182 octave_value ov = dsub.tc; | |
1183 m.elem (i) = ov; | |
1184 | |
1185 if (have_h5giterate_bug) | |
4815 | 1186 current_item++; // H5Giterate returned the last index processed. |
4814 | 1187 |
4687 | 1188 } |
1189 | |
1190 if (retval2 >= 0) | |
1191 { | |
1192 matrix = m; | |
1193 retval = true; | |
1194 } | |
1195 | |
1196 return retval; | |
1197 } | |
4815 | 1198 |
4687 | 1199 #endif |
1200 | |
3354 | 1201 DEFUN (iscell, args, , |
3448 | 1202 "-*- texinfo -*-\n\ |
1203 @deftypefn {Built-in Function} {} iscell (@var{x})\n\ | |
1204 Return true if @var{x} is a cell array object. Otherwise, return\n\ | |
1205 false.\n\ | |
1206 @end deftypefn") | |
3354 | 1207 { |
1208 octave_value retval; | |
1209 | |
1210 if (args.length () == 1) | |
1211 retval = args(0).is_cell (); | |
1212 else | |
5823 | 1213 print_usage (); |
3354 | 1214 |
1215 return retval; | |
1216 } | |
1217 | |
1218 DEFUN (cell, args, , | |
3448 | 1219 "-*- texinfo -*-\n\ |
1220 @deftypefn {Built-in Function} {} cell (@var{x})\n\ | |
1221 @deftypefnx {Built-in Function} {} cell (@var{n}, @var{m})\n\ | |
1222 Create a new cell array object. If invoked with a single scalar\n\ | |
1223 argument, @code{cell} returns a square cell array with the dimension\n\ | |
1224 specified. If you supply two scalar arguments, @code{cell} takes\n\ | |
1225 them to be the number of rows and columns. If given a vector with two\n\ | |
1226 elements, @code{cell} uses the values of the elements as the number of\n\ | |
1227 rows and columns, respectively.\n\ | |
1228 @end deftypefn") | |
3354 | 1229 { |
1230 octave_value retval; | |
1231 | |
1232 int nargin = args.length (); | |
1233 | |
4563 | 1234 dim_vector dims; |
1235 | |
3354 | 1236 switch (nargin) |
1237 { | |
4563 | 1238 case 0: |
1239 dims = dim_vector (0, 0); | |
3354 | 1240 break; |
1241 | |
4563 | 1242 case 1: |
1243 get_dimensions (args(0), "cell", dims); | |
3354 | 1244 break; |
1245 | |
1246 default: | |
4563 | 1247 { |
1248 dims.resize (nargin); | |
1249 | |
1250 for (int i = 0; i < nargin; i++) | |
1251 { | |
1252 dims(i) = args(i).is_empty () ? 0 : args(i).nint_value (); | |
1253 | |
1254 if (error_state) | |
1255 { | |
1256 error ("cell: expecting scalar arguments"); | |
1257 break; | |
1258 } | |
1259 } | |
1260 } | |
3354 | 1261 break; |
1262 } | |
1263 | |
4563 | 1264 if (! error_state) |
1265 { | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1266 dims.chop_trailing_singletons (); |
4563 | 1267 |
1268 check_dimensions (dims, "cell"); | |
1269 | |
1270 if (! error_state) | |
8290
7cbe01c21986
improve dense array indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8150
diff
changeset
|
1271 retval = Cell (dims, Matrix ()); |
4563 | 1272 } |
1273 | |
3354 | 1274 return retval; |
1275 } | |
1276 | |
4610 | 1277 DEFUN (iscellstr, args, , |
1278 "-*- texinfo -*-\n\ | |
1279 @deftypefn {Built-in Function} {} iscellstr (@var{cell})\n\ | |
1280 Return true if every element of the cell array @var{cell} is a\n\ | |
1281 character string\n\ | |
1282 @end deftypefn") | |
1283 { | |
4699 | 1284 octave_value retval; |
4610 | 1285 |
1286 if (args.length () == 1) | |
6116 | 1287 retval = args(0).is_cellstr (); |
4610 | 1288 else |
5823 | 1289 print_usage (); |
4610 | 1290 |
1291 return retval; | |
1292 } | |
1293 | |
4817 | 1294 // Note that since Fcellstr calls Fiscellstr, we need to have |
1295 // Fiscellstr defined first (to provide a declaration) and also we | |
1296 // should keep it in the same file (so we don't have to provide a | |
1297 // declaration) and so we don't have to use feval to call it. | |
1298 | |
1299 DEFUN (cellstr, args, , | |
1300 "-*- texinfo -*-\n\ | |
1301 @deftypefn {Built-in Function} {} cellstr (@var{string})\n\ | |
1302 Create a new cell array object from the elements of the string\n\ | |
1303 array @var{string}.\n\ | |
1304 @end deftypefn") | |
1305 { | |
1306 octave_value retval; | |
1307 | |
1308 if (args.length () == 1) | |
1309 { | |
1310 octave_value_list tmp = Fiscellstr (args, 1); | |
1311 | |
1312 if (tmp(0).is_true ()) | |
1313 retval = args(0); | |
1314 else | |
1315 { | |
1316 string_vector s = args(0).all_strings (); | |
1317 | |
1318 if (! error_state) | |
7813 | 1319 retval = (s.is_empty () |
1320 ? Cell (octave_value (std::string ())) | |
1321 : Cell (s, true)); | |
4817 | 1322 else |
1323 error ("cellstr: expecting argument to be a 2-d character array"); | |
1324 } | |
1325 } | |
1326 else | |
5823 | 1327 print_usage (); |
4817 | 1328 |
1329 return retval; | |
1330 } | |
1331 | |
4762 | 1332 DEFUN (struct2cell, args, , |
1333 "-*- texinfo -*-\n\ | |
1334 @deftypefn {Built-in Function} {} struct2cell (@var{S})\n\ | |
1335 Create a new cell array from the objects stored in the struct object.\n\ | |
4764 | 1336 If @var{f} is the number of fields in the structure, the resulting\n\ |
1337 cell array will have a dimension vector corresponding to\n\ | |
1338 @code{[@var{F} size(@var{S})]}.\n\ | |
5642 | 1339 @seealso{cell2struct, fieldnames}\n\ |
1340 @end deftypefn") | |
4762 | 1341 { |
1342 octave_value retval; | |
4764 | 1343 |
4762 | 1344 int nargin = args.length (); |
4764 | 1345 |
4762 | 1346 if (nargin == 1) |
1347 { | |
4764 | 1348 Octave_map m = args(0).map_value (); |
1349 | |
4762 | 1350 if (! error_state) |
1351 { | |
1352 dim_vector m_dv = m.dims (); | |
4764 | 1353 |
4762 | 1354 string_vector keys = m.keys (); |
4764 | 1355 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1356 octave_idx_type num_fields = keys.length (); |
4764 | 1357 |
4762 | 1358 // The resulting dim_vector should have dimensions: |
1359 // [numel(fields) size(struct)] | |
4764 | 1360 |
4762 | 1361 dim_vector result_dv; |
4764 | 1362 result_dv.resize (m_dv.length () + 1); // Add 1 for the fields. |
1363 | |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1364 result_dv(0) = num_fields; |
4762 | 1365 |
1366 for (int i = 1; i < result_dv.length (); i++) | |
1367 result_dv(i) = m_dv(i-1); | |
4764 | 1368 |
4762 | 1369 Cell c (result_dv); |
4764 | 1370 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1371 octave_idx_type n_elts = m.numel (); |
4764 | 1372 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1373 for (octave_idx_type j = 0; j < num_fields; j++) |
4762 | 1374 { |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1375 octave_idx_type k = j; |
4764 | 1376 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1377 const Cell vals = m.contents (keys(j)); |
4764 | 1378 |
7760
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1379 for (octave_idx_type i = 0; i < n_elts; i++) |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1380 { |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1381 c(k) = vals(i); |
f5268d7045d7
struct2cell: handle structure arrays properly
John W. Eaton <jwe@octave.org>
parents:
7651
diff
changeset
|
1382 k += num_fields; |
4762 | 1383 } |
1384 } | |
1385 | |
1386 retval = c; | |
1387 } | |
1388 else | |
1389 error ("struct2cell: expecting argument to be a cell array"); | |
1390 } | |
1391 else | |
5823 | 1392 print_usage (); |
4764 | 1393 |
4762 | 1394 return retval; |
1395 } | |
1396 | |
5900 | 1397 mxArray * |
1398 octave_cell::as_mxArray (void) const | |
1399 { | |
1400 mxArray *retval = new mxArray (dims ()); | |
1401 | |
1402 mxArray **elts = static_cast<mxArray **> (retval->get_data ()); | |
1403 | |
6686 | 1404 mwSize nel = numel (); |
5900 | 1405 |
1406 const octave_value *p = matrix.data (); | |
1407 | |
6686 | 1408 for (mwIndex i = 0; i < nel; i++) |
5900 | 1409 elts[i] = new mxArray (p[i]); |
1410 | |
1411 return retval; | |
1412 } | |
1413 | |
3353 | 1414 /* |
1415 ;;; Local Variables: *** | |
1416 ;;; mode: C++ *** | |
1417 ;;; End: *** | |
1418 */ |