Mercurial > hg > octave-nkf
annotate src/oct-obj.cc @ 11949:841c5d2437af release-3-0-x
Added tag release-3-0-4 for changeset e09cd527b8a2
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Tue, 14 Apr 2009 07:18:07 +0200 |
parents | 63380109b520 |
children | 283989f2da9b |
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" |
517 | 30 |
2970 | 31 octave_allocator |
32 octave_value_list::allocator (sizeof (octave_value_list)); | |
33 | |
3933 | 34 bool |
35 octave_value_list::valid_scalar_indices (void) const | |
36 { | |
5275 | 37 octave_idx_type n = length (); |
3933 | 38 |
5275 | 39 for (octave_idx_type i = 0; i < n; i++) |
4591 | 40 if (! data[i].valid_as_scalar_index ()) |
3933 | 41 return false; |
42 | |
43 return true; | |
44 } | |
45 | |
4591 | 46 void |
5275 | 47 octave_value_list::resize (octave_idx_type n, const octave_value& val) |
4591 | 48 { |
5275 | 49 octave_idx_type len = length (); |
4591 | 50 |
51 if (n > len) | |
52 { | |
53 data.resize (n); | |
54 | |
5275 | 55 for (octave_idx_type i = len; i < n; i++) |
4591 | 56 data[i] = val; |
57 } | |
58 else if (n < len) | |
59 data.resize (n); | |
60 } | |
61 | |
2872 | 62 octave_value_list& |
63 octave_value_list::prepend (const octave_value& val) | |
64 { | |
5275 | 65 octave_idx_type n = length (); |
2872 | 66 |
67 resize (n + 1); | |
68 | |
69 while (n > 0) | |
70 { | |
71 elem (n) = elem (n - 1); | |
72 n--; | |
73 } | |
74 | |
75 elem (0) = val; | |
76 | |
77 return *this; | |
78 } | |
79 | |
80 octave_value_list& | |
81 octave_value_list::append (const octave_value& val) | |
82 { | |
5275 | 83 octave_idx_type n = length (); |
2872 | 84 |
85 resize (n + 1); | |
86 | |
87 elem (n) = val; | |
88 | |
89 return *this; | |
90 } | |
91 | |
92 octave_value_list& | |
93 octave_value_list::append (const octave_value_list& lst) | |
94 { | |
5275 | 95 octave_idx_type len = length (); |
96 octave_idx_type lst_len = lst.length (); | |
2872 | 97 |
98 resize (len + lst_len); | |
99 | |
5275 | 100 for (octave_idx_type i = 0; i < lst_len; i++) |
2872 | 101 elem (len + i) = lst (i); |
102 | |
103 return *this; | |
104 } | |
105 | |
106 octave_value_list& | |
107 octave_value_list::reverse (void) | |
108 { | |
5275 | 109 octave_idx_type n = length (); |
2872 | 110 |
5275 | 111 for (octave_idx_type i = 0; i < n / 2; i++) |
2872 | 112 { |
113 octave_value tmp = elem (i); | |
114 elem (i) = elem (n - i - 1); | |
115 elem (n - i - 1) = tmp; | |
116 } | |
117 | |
118 return *this; | |
119 } | |
120 | |
3195 | 121 octave_value_list |
5275 | 122 octave_value_list::splice (octave_idx_type offset, octave_idx_type rep_length, |
3195 | 123 const octave_value_list& lst) const |
124 { | |
125 octave_value_list retval; | |
126 | |
5275 | 127 octave_idx_type len = length (); |
3195 | 128 |
129 if (offset < 0 || offset >= len) | |
130 { | |
3219 | 131 if (! (rep_length == 0 && offset == len)) |
132 { | |
133 error ("octave_value_list::splice: invalid OFFSET"); | |
134 return retval; | |
135 } | |
3195 | 136 } |
137 | |
138 if (rep_length < 0 || rep_length + offset > len) | |
139 { | |
140 error ("octave_value_list::splice: invalid LENGTH"); | |
141 return retval; | |
142 } | |
143 | |
5275 | 144 octave_idx_type lst_len = lst.length (); |
3195 | 145 |
5275 | 146 octave_idx_type new_len = len - rep_length + lst_len; |
3195 | 147 |
148 retval.resize (new_len); | |
149 | |
5275 | 150 octave_idx_type k = 0; |
3195 | 151 |
5275 | 152 for (octave_idx_type i = 0; i < offset; i++) |
3195 | 153 retval(k++) = elem (i); |
154 | |
5275 | 155 for (octave_idx_type i = 0; i < lst_len; i++) |
3195 | 156 retval(k++) = lst(i); |
157 | |
5275 | 158 for (octave_idx_type i = offset + rep_length; i < len; i++) |
3195 | 159 retval(k++) = elem (i); |
160 | |
161 return retval; | |
162 } | |
163 | |
2872 | 164 bool |
165 octave_value_list::all_strings_p (void) const | |
1968 | 166 { |
5275 | 167 octave_idx_type n = length (); |
517 | 168 |
5275 | 169 for (octave_idx_type i = 0; i < n; i++) |
1968 | 170 if (! elem(i).is_string ()) |
5846 | 171 return false; |
172 | |
173 return true; | |
174 } | |
1746 | 175 |
5846 | 176 bool |
177 octave_value_list::has_magic_colon (void) const | |
178 { | |
179 octave_idx_type n = length (); | |
180 | |
181 for (octave_idx_type i = 0; i < n; i++) | |
182 if (elem(i).is_magic_colon ()) | |
183 return true; | |
184 | |
185 return false; | |
517 | 186 } |
187 | |
1968 | 188 string_vector |
3523 | 189 octave_value_list::make_argv (const std::string& fcn_name) const |
517 | 190 { |
1968 | 191 string_vector argv; |
192 | |
2872 | 193 if (all_strings_p ()) |
1968 | 194 { |
5275 | 195 octave_idx_type len = length (); |
3180 | 196 |
5275 | 197 octave_idx_type total_nr = 0; |
3180 | 198 |
5275 | 199 for (octave_idx_type i = 0; i < len; i++) |
3264 | 200 { |
3523 | 201 // An empty std::string ("") has zero columns and zero rows (a |
3264 | 202 // change that was made for Matlab contemptibility. |
203 | |
5275 | 204 octave_idx_type n = elem(i).rows (); |
3264 | 205 |
206 total_nr += n ? n : 1; | |
207 } | |
3180 | 208 |
11809
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
209 octave_idx_type k = 0; |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
210 if (! fcn_name.empty ()) |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
211 { |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
212 argv.resize (total_nr+1); |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
213 argv[0] = fcn_name; |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
214 k = 1; |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
215 } |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
216 else |
63380109b520
fix default_save_options parsing and allow mixing options with other arguments.
Jaroslav Hajek <highegg@gmail.com>
parents:
7017
diff
changeset
|
217 argv.resize (total_nr); |
3180 | 218 |
5275 | 219 for (octave_idx_type i = 0; i < len; i++) |
3180 | 220 { |
5275 | 221 octave_idx_type nr = elem(i).rows (); |
3180 | 222 |
3264 | 223 if (nr < 2) |
3180 | 224 argv[k++] = elem(i).string_value (); |
225 else | |
226 { | |
227 string_vector tmp = elem(i).all_strings (); | |
228 | |
5275 | 229 for (octave_idx_type j = 0; j < nr; j++) |
3180 | 230 argv[k++] = tmp[j]; |
231 } | |
232 } | |
1968 | 233 } |
234 else | |
235 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); | |
517 | 236 |
1968 | 237 return argv; |
517 | 238 } |
239 | |
240 /* | |
241 ;;; Local Variables: *** | |
242 ;;; mode: C++ *** | |
243 ;;; End: *** | |
244 */ |