Mercurial > hg > octave-jordi
annotate src/oct-obj.cc @ 11129:0de5cc44e690
use gripe functions for NaN to logical and NaN to character conversions more consistently
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 21 Oct 2010 16:12:00 -0400 |
parents | 394a83606f03 |
children | fd0a3ac60b0e |
rev | line source |
---|---|
517 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
8920 | 4 2004, 2005, 2006, 2007, 2008 John W. Eaton |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
5 Copyright (C) 2009 VZLU Prague |
517 | 6 |
7 This file is part of Octave. | |
8 | |
9 Octave is free software; you can redistribute it and/or modify it | |
10 under the terms of the GNU General Public License as published by the | |
7016 | 11 Free Software Foundation; either version 3 of the License, or (at your |
12 option) any later version. | |
517 | 13 |
14 Octave is distributed in the hope that it will be useful, but WITHOUT | |
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
16 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
17 for more details. | |
18 | |
19 You should have received a copy of the GNU General Public License | |
7016 | 20 along with Octave; see the file COPYING. If not, see |
21 <http://www.gnu.org/licenses/>. | |
517 | 22 |
23 */ | |
24 | |
25 #ifdef HAVE_CONFIG_H | |
1192 | 26 #include <config.h> |
517 | 27 #endif |
28 | |
1968 | 29 #include "error.h" |
1742 | 30 #include "oct-obj.h" |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
31 #include "Cell.h" |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
32 |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
33 octave_value_list::octave_value_list (const std::list<octave_value_list>& lst) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
34 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
35 octave_idx_type n = 0, nel = 0; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
36 |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
37 // Determine number. |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
38 for (std::list<octave_value_list>::const_iterator p = lst.begin (); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
39 p != lst.end (); p++) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
40 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
41 n++; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
42 nel += p->length (); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
43 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
44 |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
45 // Optimize single-element case |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
46 if (n == 1) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
47 data = lst.front ().data; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
48 else if (nel > 0) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
49 { |
10552
394a83606f03
avoid deprecated function warnings from GCC
John W. Eaton <jwe@octave.org>
parents:
10315
diff
changeset
|
50 data.resize (1, nel); |
8580
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
51 octave_idx_type k = 0; |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
52 for (std::list<octave_value_list>::const_iterator p = lst.begin (); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
53 p != lst.end (); p++) |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
54 { |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
55 data.assign (idx_vector (k, k + p->length ()), p->data); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
56 k += p->length (); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
57 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
58 assert (k == nel); |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
59 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
60 |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
61 } |
188d38a553c7
further indexing optimization touches
Jaroslav Hajek <highegg@gmail.com>
parents:
8579
diff
changeset
|
62 |
2970 | 63 octave_allocator |
64 octave_value_list::allocator (sizeof (octave_value_list)); | |
65 | |
2872 | 66 octave_value_list& |
67 octave_value_list::prepend (const octave_value& val) | |
68 { | |
5275 | 69 octave_idx_type n = length (); |
2872 | 70 |
71 resize (n + 1); | |
72 | |
73 while (n > 0) | |
74 { | |
75 elem (n) = elem (n - 1); | |
76 n--; | |
77 } | |
78 | |
79 elem (0) = val; | |
80 | |
81 return *this; | |
82 } | |
83 | |
84 octave_value_list& | |
85 octave_value_list::append (const octave_value& val) | |
86 { | |
5275 | 87 octave_idx_type n = length (); |
2872 | 88 |
89 resize (n + 1); | |
90 | |
91 elem (n) = val; | |
92 | |
93 return *this; | |
94 } | |
95 | |
96 octave_value_list& | |
97 octave_value_list::append (const octave_value_list& lst) | |
98 { | |
5275 | 99 octave_idx_type len = length (); |
100 octave_idx_type lst_len = lst.length (); | |
2872 | 101 |
102 resize (len + lst_len); | |
103 | |
5275 | 104 for (octave_idx_type i = 0; i < lst_len; i++) |
2872 | 105 elem (len + i) = lst (i); |
106 | |
107 return *this; | |
108 } | |
109 | |
110 octave_value_list& | |
111 octave_value_list::reverse (void) | |
112 { | |
5275 | 113 octave_idx_type n = length (); |
2872 | 114 |
5275 | 115 for (octave_idx_type i = 0; i < n / 2; i++) |
2872 | 116 { |
117 octave_value tmp = elem (i); | |
118 elem (i) = elem (n - i - 1); | |
119 elem (n - i - 1) = tmp; | |
120 } | |
121 | |
122 return *this; | |
123 } | |
124 | |
3195 | 125 octave_value_list |
5275 | 126 octave_value_list::splice (octave_idx_type offset, octave_idx_type rep_length, |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
127 const octave_value_list& lst) const |
3195 | 128 { |
129 octave_value_list retval; | |
130 | |
5275 | 131 octave_idx_type len = length (); |
3195 | 132 |
133 if (offset < 0 || offset >= len) | |
134 { | |
3219 | 135 if (! (rep_length == 0 && offset == len)) |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
136 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
137 error ("octave_value_list::splice: invalid OFFSET"); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
138 return retval; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
139 } |
3195 | 140 } |
141 | |
142 if (rep_length < 0 || rep_length + offset > len) | |
143 { | |
144 error ("octave_value_list::splice: invalid LENGTH"); | |
145 return retval; | |
146 } | |
147 | |
5275 | 148 octave_idx_type lst_len = lst.length (); |
3195 | 149 |
5275 | 150 octave_idx_type new_len = len - rep_length + lst_len; |
3195 | 151 |
152 retval.resize (new_len); | |
153 | |
5275 | 154 octave_idx_type k = 0; |
3195 | 155 |
5275 | 156 for (octave_idx_type i = 0; i < offset; i++) |
3195 | 157 retval(k++) = elem (i); |
158 | |
5275 | 159 for (octave_idx_type i = 0; i < lst_len; i++) |
3195 | 160 retval(k++) = lst(i); |
161 | |
5275 | 162 for (octave_idx_type i = offset + rep_length; i < len; i++) |
3195 | 163 retval(k++) = elem (i); |
164 | |
165 return retval; | |
166 } | |
167 | |
2872 | 168 bool |
169 octave_value_list::all_strings_p (void) const | |
1968 | 170 { |
5275 | 171 octave_idx_type n = length (); |
517 | 172 |
5275 | 173 for (octave_idx_type i = 0; i < n; i++) |
1968 | 174 if (! elem(i).is_string ()) |
5846 | 175 return false; |
176 | |
177 return true; | |
178 } | |
1746 | 179 |
5846 | 180 bool |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
181 octave_value_list::all_scalars (void) const |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
182 { |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
183 octave_idx_type n = length (), i; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
184 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
185 for (i = 0; i < n && elem (i).is_string (); i++) ; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
186 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
187 return i == n; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
188 } |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
189 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
190 bool |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
191 octave_value_list::any_cell (void) const |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
192 { |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
193 octave_idx_type n = length (); |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
194 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
195 for (octave_idx_type i = 0; i < n; i++) |
10099
29959c705df2
avoid assignment in condition of if statement
John W. Eaton <jwe@octave.org>
parents:
10086
diff
changeset
|
196 if (elem (i).is_cell ()) |
29959c705df2
avoid assignment in condition of if statement
John W. Eaton <jwe@octave.org>
parents:
10086
diff
changeset
|
197 return true; |
29959c705df2
avoid assignment in condition of if statement
John W. Eaton <jwe@octave.org>
parents:
10086
diff
changeset
|
198 |
29959c705df2
avoid assignment in condition of if statement
John W. Eaton <jwe@octave.org>
parents:
10086
diff
changeset
|
199 return false; |
10086
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
200 } |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
201 |
76df75b10c80
allow more cells in strfind/strrep + supply a general mechanism
Jaroslav Hajek <highegg@gmail.com>
parents:
8920
diff
changeset
|
202 bool |
5846 | 203 octave_value_list::has_magic_colon (void) const |
204 { | |
205 octave_idx_type n = length (); | |
206 | |
207 for (octave_idx_type i = 0; i < n; i++) | |
208 if (elem(i).is_magic_colon ()) | |
209 return true; | |
210 | |
211 return false; | |
517 | 212 } |
213 | |
1968 | 214 string_vector |
3523 | 215 octave_value_list::make_argv (const std::string& fcn_name) const |
517 | 216 { |
1968 | 217 string_vector argv; |
218 | |
2872 | 219 if (all_strings_p ()) |
1968 | 220 { |
5275 | 221 octave_idx_type len = length (); |
3180 | 222 |
5275 | 223 octave_idx_type total_nr = 0; |
3180 | 224 |
5275 | 225 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
|
226 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
227 // An empty std::string ("") has zero columns and zero rows (a |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
228 // change that was made for Matlab contemptibility. |
3264 | 229 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
230 octave_idx_type n = elem(i).rows (); |
3264 | 231 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
232 total_nr += n ? n : 1; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
233 } |
3180 | 234 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
235 octave_idx_type k = 0; |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
236 if (! fcn_name.empty ()) |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
237 { |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
238 argv.resize (total_nr+1); |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
239 argv[0] = fcn_name; |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
240 k = 1; |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
241 } |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
242 else |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
243 argv.resize (total_nr); |
3180 | 244 |
5275 | 245 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
|
246 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
247 octave_idx_type nr = elem(i).rows (); |
3180 | 248 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
249 if (nr < 2) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
250 argv[k++] = elem(i).string_value (); |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
251 else |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
252 { |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
253 string_vector tmp = elem(i).all_strings (); |
3180 | 254 |
10315
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
255 for (octave_idx_type j = 0; j < nr; j++) |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
256 argv[k++] = tmp[j]; |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
257 } |
57a59eae83cc
untabify src C++ source files
John W. Eaton <jwe@octave.org>
parents:
10160
diff
changeset
|
258 } |
1968 | 259 } |
260 else | |
261 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); | |
517 | 262 |
1968 | 263 return argv; |
517 | 264 } |
265 | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
266 void |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
267 octave_value_list::make_storable_values (void) |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
268 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
269 octave_idx_type len = length (); |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
270 const Array<octave_value>& cdata = data; |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
271 |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
272 for (octave_idx_type i = 0; i < len; i++) |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
273 { |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
274 // This is optimized so that we don't force a copy unless necessary. |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
275 octave_value tmp = cdata(i).storable_value (); |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
276 if (! tmp.is_copy_of (cdata (i))) |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
277 data(i) = tmp; |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
278 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
279 } |