Mercurial > octave-nkf
annotate src/oct-obj.cc @ 8579:7e0f36dfefbe
implement octave_value_list using Array
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Fri, 23 Jan 2009 09:57:19 +0100 |
parents | 3d8a914c580e |
children | 188d38a553c7 |
rev | line source |
---|---|
517 | 1 /* |
2 | |
7017 | 3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2002, 2003, |
4 2004, 2005, 2006, 2007 John W. Eaton | |
517 | 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. | |
517 | 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/>. | |
517 | 21 |
22 */ | |
23 | |
24 #ifdef HAVE_CONFIG_H | |
1192 | 25 #include <config.h> |
517 | 26 #endif |
27 | |
1968 | 28 #include "error.h" |
1742 | 29 #include "oct-obj.h" |
8546
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
30 #include "Cell.h" |
3d8a914c580e
improve parser indexed assigment code
Jaroslav Hajek <highegg@gmail.com>
parents:
8523
diff
changeset
|
31 |
2970 | 32 octave_allocator |
33 octave_value_list::allocator (sizeof (octave_value_list)); | |
34 | |
2872 | 35 octave_value_list& |
36 octave_value_list::prepend (const octave_value& val) | |
37 { | |
5275 | 38 octave_idx_type n = length (); |
2872 | 39 |
40 resize (n + 1); | |
41 | |
42 while (n > 0) | |
43 { | |
44 elem (n) = elem (n - 1); | |
45 n--; | |
46 } | |
47 | |
48 elem (0) = val; | |
49 | |
50 return *this; | |
51 } | |
52 | |
53 octave_value_list& | |
54 octave_value_list::append (const octave_value& val) | |
55 { | |
5275 | 56 octave_idx_type n = length (); |
2872 | 57 |
58 resize (n + 1); | |
59 | |
60 elem (n) = val; | |
61 | |
62 return *this; | |
63 } | |
64 | |
65 octave_value_list& | |
66 octave_value_list::append (const octave_value_list& lst) | |
67 { | |
5275 | 68 octave_idx_type len = length (); |
69 octave_idx_type lst_len = lst.length (); | |
2872 | 70 |
71 resize (len + lst_len); | |
72 | |
5275 | 73 for (octave_idx_type i = 0; i < lst_len; i++) |
2872 | 74 elem (len + i) = lst (i); |
75 | |
76 return *this; | |
77 } | |
78 | |
79 octave_value_list& | |
80 octave_value_list::reverse (void) | |
81 { | |
5275 | 82 octave_idx_type n = length (); |
2872 | 83 |
5275 | 84 for (octave_idx_type i = 0; i < n / 2; i++) |
2872 | 85 { |
86 octave_value tmp = elem (i); | |
87 elem (i) = elem (n - i - 1); | |
88 elem (n - i - 1) = tmp; | |
89 } | |
90 | |
91 return *this; | |
92 } | |
93 | |
3195 | 94 octave_value_list |
5275 | 95 octave_value_list::splice (octave_idx_type offset, octave_idx_type rep_length, |
3195 | 96 const octave_value_list& lst) const |
97 { | |
98 octave_value_list retval; | |
99 | |
5275 | 100 octave_idx_type len = length (); |
3195 | 101 |
102 if (offset < 0 || offset >= len) | |
103 { | |
3219 | 104 if (! (rep_length == 0 && offset == len)) |
105 { | |
106 error ("octave_value_list::splice: invalid OFFSET"); | |
107 return retval; | |
108 } | |
3195 | 109 } |
110 | |
111 if (rep_length < 0 || rep_length + offset > len) | |
112 { | |
113 error ("octave_value_list::splice: invalid LENGTH"); | |
114 return retval; | |
115 } | |
116 | |
5275 | 117 octave_idx_type lst_len = lst.length (); |
3195 | 118 |
5275 | 119 octave_idx_type new_len = len - rep_length + lst_len; |
3195 | 120 |
121 retval.resize (new_len); | |
122 | |
5275 | 123 octave_idx_type k = 0; |
3195 | 124 |
5275 | 125 for (octave_idx_type i = 0; i < offset; i++) |
3195 | 126 retval(k++) = elem (i); |
127 | |
5275 | 128 for (octave_idx_type i = 0; i < lst_len; i++) |
3195 | 129 retval(k++) = lst(i); |
130 | |
5275 | 131 for (octave_idx_type i = offset + rep_length; i < len; i++) |
3195 | 132 retval(k++) = elem (i); |
133 | |
134 return retval; | |
135 } | |
136 | |
2872 | 137 bool |
138 octave_value_list::all_strings_p (void) const | |
1968 | 139 { |
5275 | 140 octave_idx_type n = length (); |
517 | 141 |
5275 | 142 for (octave_idx_type i = 0; i < n; i++) |
1968 | 143 if (! elem(i).is_string ()) |
5846 | 144 return false; |
145 | |
146 return true; | |
147 } | |
1746 | 148 |
5846 | 149 bool |
8455
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
150 octave_value_list::all_scalars (void) const |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
151 { |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
152 octave_idx_type n = length (), i; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
153 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
154 for (i = 0; i < n && elem (i).is_string (); i++) ; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
155 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
156 return i == n; |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
157 } |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
158 |
fd11a08a9b31
disallow invalid {}-indexed assigments
Jaroslav Hajek <highegg@gmail.com>
parents:
8437
diff
changeset
|
159 bool |
5846 | 160 octave_value_list::has_magic_colon (void) const |
161 { | |
162 octave_idx_type n = length (); | |
163 | |
164 for (octave_idx_type i = 0; i < n; i++) | |
165 if (elem(i).is_magic_colon ()) | |
166 return true; | |
167 | |
168 return false; | |
517 | 169 } |
170 | |
1968 | 171 string_vector |
3523 | 172 octave_value_list::make_argv (const std::string& fcn_name) const |
517 | 173 { |
1968 | 174 string_vector argv; |
175 | |
2872 | 176 if (all_strings_p ()) |
1968 | 177 { |
5275 | 178 octave_idx_type len = length (); |
3180 | 179 |
5275 | 180 octave_idx_type total_nr = 0; |
3180 | 181 |
5275 | 182 for (octave_idx_type i = 0; i < len; i++) |
3264 | 183 { |
3523 | 184 // An empty std::string ("") has zero columns and zero rows (a |
3264 | 185 // change that was made for Matlab contemptibility. |
186 | |
5275 | 187 octave_idx_type n = elem(i).rows (); |
3264 | 188 |
189 total_nr += n ? n : 1; | |
190 } | |
3180 | 191 |
8034
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
192 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
|
193 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
|
194 { |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
195 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
|
196 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
|
197 k = 1; |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
198 } |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
199 else |
f61bd8e0d682
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
200 argv.resize (total_nr); |
3180 | 201 |
5275 | 202 for (octave_idx_type i = 0; i < len; i++) |
3180 | 203 { |
5275 | 204 octave_idx_type nr = elem(i).rows (); |
3180 | 205 |
3264 | 206 if (nr < 2) |
3180 | 207 argv[k++] = elem(i).string_value (); |
208 else | |
209 { | |
210 string_vector tmp = elem(i).all_strings (); | |
211 | |
5275 | 212 for (octave_idx_type j = 0; j < nr; j++) |
3180 | 213 argv[k++] = tmp[j]; |
214 } | |
215 } | |
1968 | 216 } |
217 else | |
218 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); | |
517 | 219 |
1968 | 220 return argv; |
517 | 221 } |
222 | |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
223 void |
8523
ad3afaaa19c1
implement non-copying contiguous range indexing
Jaroslav Hajek <highegg@gmail.com>
parents:
8455
diff
changeset
|
224 octave_value_list::make_storable_values (void) |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
225 { |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
226 octave_idx_type len = length (); |
8579
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
227 const Array<octave_value>& cdata = data; |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
228 |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
229 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
|
230 { |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
231 // 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
|
232 octave_value tmp = cdata(i).storable_value (); |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
233 if (! tmp.is_copy_of (cdata (i))) |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
234 data(i) = tmp; |
7e0f36dfefbe
implement octave_value_list using Array
Jaroslav Hajek <highegg@gmail.com>
parents:
8546
diff
changeset
|
235 } |
8150
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
236 } |
283989f2da9b
make null assignment matlab compatible
Jaroslav Hajek <highegg@gmail.com>
parents:
8034
diff
changeset
|
237 |
517 | 238 /* |
239 ;;; Local Variables: *** | |
240 ;;; mode: C++ *** | |
241 ;;; End: *** | |
242 */ |