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