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 { |
|
40 int n = data.length (); |
|
41 |
|
42 for (int i = 0; i < n; i++) |
|
43 if (! data(i).valid_as_scalar_index ()) |
|
44 return false; |
|
45 |
|
46 return true; |
|
47 } |
|
48 |
2872
|
49 octave_value_list& |
|
50 octave_value_list::prepend (const octave_value& val) |
|
51 { |
|
52 int n = length (); |
|
53 |
|
54 resize (n + 1); |
|
55 |
|
56 while (n > 0) |
|
57 { |
|
58 elem (n) = elem (n - 1); |
|
59 n--; |
|
60 } |
|
61 |
|
62 elem (0) = val; |
|
63 |
|
64 return *this; |
|
65 } |
|
66 |
|
67 octave_value_list& |
|
68 octave_value_list::append (const octave_value& val) |
|
69 { |
|
70 int n = length (); |
|
71 |
|
72 resize (n + 1); |
|
73 |
|
74 elem (n) = val; |
|
75 |
|
76 return *this; |
|
77 } |
|
78 |
|
79 octave_value_list& |
|
80 octave_value_list::append (const octave_value_list& lst) |
|
81 { |
|
82 int len = length (); |
|
83 int lst_len = lst.length (); |
|
84 |
|
85 resize (len + lst_len); |
|
86 |
|
87 for (int i = 0; i < lst_len; i++) |
|
88 elem (len + i) = lst (i); |
|
89 |
|
90 return *this; |
|
91 } |
|
92 |
|
93 octave_value_list& |
|
94 octave_value_list::reverse (void) |
|
95 { |
|
96 int n = length (); |
|
97 |
|
98 for (int i = 0; i < n / 2; i++) |
|
99 { |
|
100 octave_value tmp = elem (i); |
|
101 elem (i) = elem (n - i - 1); |
|
102 elem (n - i - 1) = tmp; |
|
103 } |
|
104 |
|
105 return *this; |
|
106 } |
|
107 |
3195
|
108 octave_value_list |
|
109 octave_value_list::splice (int offset, int rep_length, |
|
110 const octave_value_list& lst) const |
|
111 { |
|
112 octave_value_list retval; |
|
113 |
|
114 int len = length (); |
|
115 |
|
116 if (offset < 0 || offset >= len) |
|
117 { |
3219
|
118 if (! (rep_length == 0 && offset == len)) |
|
119 { |
|
120 error ("octave_value_list::splice: invalid OFFSET"); |
|
121 return retval; |
|
122 } |
3195
|
123 } |
|
124 |
|
125 if (rep_length < 0 || rep_length + offset > len) |
|
126 { |
|
127 error ("octave_value_list::splice: invalid LENGTH"); |
|
128 return retval; |
|
129 } |
|
130 |
|
131 int lst_len = lst.length (); |
|
132 |
|
133 int new_len = len - rep_length + lst_len; |
|
134 |
|
135 retval.resize (new_len); |
|
136 |
|
137 int k = 0; |
|
138 |
|
139 for (int i = 0; i < offset; i++) |
|
140 retval(k++) = elem (i); |
|
141 |
|
142 for (int i = 0; i < lst_len; i++) |
|
143 retval(k++) = lst(i); |
|
144 |
|
145 for (int i = offset + rep_length; i < len; i++) |
|
146 retval(k++) = elem (i); |
|
147 |
|
148 return retval; |
|
149 } |
|
150 |
3219
|
151 octave_value_list |
3933
|
152 octave_value_list::index (idx_vector& i, int resize_ok) const |
3219
|
153 { |
3933
|
154 return octave_value_list (data.index (i, resize_ok, octave_value ())); |
3219
|
155 } |
|
156 |
3932
|
157 octave_value_list& |
|
158 octave_value_list::assign (const idx_vector& i, |
|
159 const octave_value_list& rhs) |
|
160 { |
|
161 data.set_index (i); |
|
162 ::assign (data, rhs.data); |
|
163 return *this; |
|
164 } |
|
165 |
2872
|
166 bool |
|
167 octave_value_list::all_strings_p (void) const |
1968
|
168 { |
|
169 int n = length (); |
517
|
170 |
1968
|
171 for (int i = 0; i < n; i++) |
|
172 if (! elem(i).is_string ()) |
|
173 return 0; |
1746
|
174 |
1968
|
175 return 1; |
517
|
176 } |
|
177 |
1968
|
178 string_vector |
3523
|
179 octave_value_list::make_argv (const std::string& fcn_name) const |
517
|
180 { |
1968
|
181 string_vector argv; |
|
182 |
2872
|
183 if (all_strings_p ()) |
1968
|
184 { |
3180
|
185 int len = length (); |
|
186 |
|
187 int total_nr = 0; |
|
188 |
|
189 for (int i = 0; i < len; i++) |
3264
|
190 { |
3523
|
191 // An empty std::string ("") has zero columns and zero rows (a |
3264
|
192 // change that was made for Matlab contemptibility. |
|
193 |
|
194 int n = elem(i).rows (); |
|
195 |
|
196 total_nr += n ? n : 1; |
|
197 } |
3180
|
198 |
|
199 argv.resize (total_nr+1); |
|
200 |
1968
|
201 argv[0] = fcn_name; |
517
|
202 |
3180
|
203 int k = 1; |
|
204 for (int i = 0; i < len; i++) |
|
205 { |
|
206 int nr = elem(i).rows (); |
|
207 |
3264
|
208 if (nr < 2) |
3180
|
209 argv[k++] = elem(i).string_value (); |
|
210 else |
|
211 { |
|
212 string_vector tmp = elem(i).all_strings (); |
|
213 |
|
214 for (int j = 0; j < nr; j++) |
|
215 argv[k++] = tmp[j]; |
|
216 } |
|
217 } |
1968
|
218 } |
|
219 else |
|
220 error ("%s: expecting all arguments to be strings", fcn_name.c_str ()); |
517
|
221 |
1968
|
222 return argv; |
517
|
223 } |
|
224 |
|
225 /* |
|
226 ;;; Local Variables: *** |
|
227 ;;; mode: C++ *** |
|
228 ;;; End: *** |
|
229 */ |