517
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
517
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
517
|
20 |
|
21 */ |
|
22 |
4192
|
23 #if defined (__GNUG__) && defined (USE_PRAGMA_INTERFACE_IMPLEMENTATION) |
1297
|
24 #pragma implementation |
|
25 #endif |
|
26 |
517
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
517
|
29 #endif |
|
30 |
1968
|
31 #include "error.h" |
1742
|
32 #include "oct-obj.h" |
517
|
33 |
2970
|
34 octave_allocator |
|
35 octave_value_list::allocator (sizeof (octave_value_list)); |
|
36 |
3933
|
37 bool |
|
38 octave_value_list::valid_scalar_indices (void) const |
|
39 { |
4591
|
40 int n = length (); |
3933
|
41 |
|
42 for (int i = 0; i < n; i++) |
4591
|
43 if (! data[i].valid_as_scalar_index ()) |
3933
|
44 return false; |
|
45 |
|
46 return true; |
|
47 } |
|
48 |
4591
|
49 void |
|
50 octave_value_list::resize (int n, const octave_value& val) |
|
51 { |
|
52 int len = length (); |
|
53 |
|
54 if (n > len) |
|
55 { |
|
56 data.resize (n); |
|
57 |
|
58 for (int i = len; i < n; i++) |
|
59 data[i] = val; |
|
60 } |
|
61 else if (n < len) |
|
62 data.resize (n); |
|
63 } |
|
64 |
2872
|
65 octave_value_list& |
|
66 octave_value_list::prepend (const octave_value& val) |
|
67 { |
|
68 int n = length (); |
|
69 |
|
70 resize (n + 1); |
|
71 |
|
72 while (n > 0) |
|
73 { |
|
74 elem (n) = elem (n - 1); |
|
75 n--; |
|
76 } |
|
77 |
|
78 elem (0) = val; |
|
79 |
|
80 return *this; |
|
81 } |
|
82 |
|
83 octave_value_list& |
|
84 octave_value_list::append (const octave_value& val) |
|
85 { |
|
86 int n = length (); |
|
87 |
|
88 resize (n + 1); |
|
89 |
|
90 elem (n) = val; |
|
91 |
|
92 return *this; |
|
93 } |
|
94 |
|
95 octave_value_list& |
|
96 octave_value_list::append (const octave_value_list& lst) |
|
97 { |
|
98 int len = length (); |
|
99 int lst_len = lst.length (); |
|
100 |
|
101 resize (len + lst_len); |
|
102 |
|
103 for (int i = 0; i < lst_len; i++) |
|
104 elem (len + i) = lst (i); |
|
105 |
|
106 return *this; |
|
107 } |
|
108 |
|
109 octave_value_list& |
|
110 octave_value_list::reverse (void) |
|
111 { |
|
112 int n = length (); |
|
113 |
|
114 for (int i = 0; i < n / 2; i++) |
|
115 { |
|
116 octave_value tmp = elem (i); |
|
117 elem (i) = elem (n - i - 1); |
|
118 elem (n - i - 1) = tmp; |
|
119 } |
|
120 |
|
121 return *this; |
|
122 } |
|
123 |
3195
|
124 octave_value_list |
|
125 octave_value_list::splice (int offset, int rep_length, |
|
126 const octave_value_list& lst) const |
|
127 { |
|
128 octave_value_list retval; |
|
129 |
|
130 int len = length (); |
|
131 |
|
132 if (offset < 0 || offset >= len) |
|
133 { |
3219
|
134 if (! (rep_length == 0 && offset == len)) |
|
135 { |
|
136 error ("octave_value_list::splice: invalid OFFSET"); |
|
137 return retval; |
|
138 } |
3195
|
139 } |
|
140 |
|
141 if (rep_length < 0 || rep_length + offset > len) |
|
142 { |
|
143 error ("octave_value_list::splice: invalid LENGTH"); |
|
144 return retval; |
|
145 } |
|
146 |
|
147 int lst_len = lst.length (); |
|
148 |
|
149 int new_len = len - rep_length + lst_len; |
|
150 |
|
151 retval.resize (new_len); |
|
152 |
|
153 int k = 0; |
|
154 |
|
155 for (int i = 0; i < offset; i++) |
|
156 retval(k++) = elem (i); |
|
157 |
|
158 for (int i = 0; i < lst_len; i++) |
|
159 retval(k++) = lst(i); |
|
160 |
|
161 for (int i = offset + rep_length; i < len; i++) |
|
162 retval(k++) = elem (i); |
|
163 |
|
164 return retval; |
|
165 } |
|
166 |
2872
|
167 bool |
|
168 octave_value_list::all_strings_p (void) const |
1968
|
169 { |
|
170 int n = length (); |
517
|
171 |
1968
|
172 for (int i = 0; i < n; i++) |
|
173 if (! elem(i).is_string ()) |
|
174 return 0; |
1746
|
175 |
1968
|
176 return 1; |
517
|
177 } |
|
178 |
1968
|
179 string_vector |
3523
|
180 octave_value_list::make_argv (const std::string& fcn_name) const |
517
|
181 { |
1968
|
182 string_vector argv; |
|
183 |
2872
|
184 if (all_strings_p ()) |
1968
|
185 { |
3180
|
186 int len = length (); |
|
187 |
|
188 int total_nr = 0; |
|
189 |
|
190 for (int i = 0; i < len; i++) |
3264
|
191 { |
3523
|
192 // An empty std::string ("") has zero columns and zero rows (a |
3264
|
193 // change that was made for Matlab contemptibility. |
|
194 |
|
195 int n = elem(i).rows (); |
|
196 |
|
197 total_nr += n ? n : 1; |
|
198 } |
3180
|
199 |
|
200 argv.resize (total_nr+1); |
|
201 |
1968
|
202 argv[0] = fcn_name; |
517
|
203 |
3180
|
204 int k = 1; |
|
205 for (int i = 0; i < len; i++) |
|
206 { |
|
207 int nr = elem(i).rows (); |
|
208 |
3264
|
209 if (nr < 2) |
3180
|
210 argv[k++] = elem(i).string_value (); |
|
211 else |
|
212 { |
|
213 string_vector tmp = elem(i).all_strings (); |
|
214 |
|
215 for (int j = 0; j < nr; j++) |
|
216 argv[k++] = tmp[j]; |
|
217 } |
|
218 } |
1968
|
219 } |
|
220 else |
|
221 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
517
|
222 |
1968
|
223 return argv; |
517
|
224 } |
|
225 |
|
226 /* |
|
227 ;;; Local Variables: *** |
|
228 ;;; mode: C++ *** |
|
229 ;;; End: *** |
|
230 */ |