GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-obj.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2013 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
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
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
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
19 along with Octave; see the file COPYING. If not, see
20 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include "error.h"
29 #include "oct-obj.h"
30 #include "Cell.h"
31 
32 // We are likely to have a lot of octave_value_list objects to allocate,
33 // so make the grow_size large.
35 
36 octave_value_list::octave_value_list (const std::list<octave_value_list>& lst)
37 {
38  octave_idx_type n = 0, nel = 0;
39 
40  // Determine number.
41  for (std::list<octave_value_list>::const_iterator p = lst.begin ();
42  p != lst.end (); p++)
43  {
44  n++;
45  nel += p->length ();
46  }
47 
48  // Optimize single-element case
49  if (n == 1)
50  data = lst.front ().data;
51  else if (nel > 0)
52  {
53  data.resize (dim_vector (1, nel));
54  octave_idx_type k = 0;
55  for (std::list<octave_value_list>::const_iterator p = lst.begin ();
56  p != lst.end (); p++)
57  {
58  data.assign (idx_vector (k, k + p->length ()), p->data);
59  k += p->length ();
60  }
61  assert (k == nel);
62  }
63 
64 }
65 
68 {
69  octave_idx_type n = length ();
70 
71  resize (n + 1);
72 
73  while (n > 0)
74  {
75  elem (n) = elem (n - 1);
76  n--;
77  }
78 
79  elem (0) = val;
80 
81  return *this;
82 }
83 
86 {
87  octave_idx_type n = length ();
88 
89  resize (n + 1);
90 
91  elem (n) = val;
92 
93  return *this;
94 }
95 
98 {
99  octave_idx_type len = length ();
100  octave_idx_type lst_len = lst.length ();
101 
102  resize (len + lst_len);
103 
104  for (octave_idx_type i = 0; i < lst_len; i++)
105  elem (len + i) = lst (i);
106 
107  return *this;
108 }
109 
112 {
113  octave_idx_type n = length ();
114 
115  for (octave_idx_type i = 0; i < n / 2; i++)
116  {
117  octave_value tmp = elem (i);
118  elem (i) = elem (n - i - 1);
119  elem (n - i - 1) = tmp;
120  }
121 
122  return *this;
123 }
124 
127  const octave_value_list& lst) const
128 {
129  octave_value_list retval;
130 
131  octave_idx_type len = length ();
132 
133  if (offset < 0 || offset >= len)
134  {
135  if (! (rep_length == 0 && offset == len))
136  {
137  error ("octave_value_list::splice: invalid OFFSET");
138  return retval;
139  }
140  }
141 
142  if (rep_length < 0 || rep_length + offset > len)
143  {
144  error ("octave_value_list::splice: invalid LENGTH");
145  return retval;
146  }
147 
148  octave_idx_type lst_len = lst.length ();
149 
150  octave_idx_type new_len = len - rep_length + lst_len;
151 
152  retval.resize (new_len);
153 
154  octave_idx_type k = 0;
155 
156  for (octave_idx_type i = 0; i < offset; i++)
157  retval(k++) = elem (i);
158 
159  for (octave_idx_type i = 0; i < lst_len; i++)
160  retval(k++) = lst (i);
161 
162  for (octave_idx_type i = offset + rep_length; i < len; i++)
163  retval(k++) = elem (i);
164 
165  return retval;
166 }
167 
168 bool
170 {
171  octave_idx_type n = length ();
172 
173  for (octave_idx_type i = 0; i < n; i++)
174  if (! elem(i).is_string ())
175  return false;
176 
177  return true;
178 }
179 
180 bool
182 {
183  octave_idx_type n = length ();
184 
185  for (octave_idx_type i = 0; i < n; i++)
186  {
187  dim_vector dv = elem(i).dims ();
188  if (! dv.all_ones ())
189  return false;
190  }
191 
192  return true;
193 }
194 
195 bool
197 {
198  octave_idx_type n = length ();
199 
200  for (octave_idx_type i = 0; i < n; i++)
201  if (elem (i).is_cell ())
202  return true;
203 
204  return false;
205 }
206 
207 bool
209 {
210  octave_idx_type n = length ();
211 
212  for (octave_idx_type i = 0; i < n; i++)
213  if (elem(i).is_magic_colon ())
214  return true;
215 
216  return false;
217 }
218 
220 octave_value_list::make_argv (const std::string& fcn_name) const
221 {
222  string_vector argv;
223 
224  if (all_strings_p ())
225  {
226  octave_idx_type len = length ();
227 
228  octave_idx_type total_nr = 0;
229 
230  for (octave_idx_type i = 0; i < len; i++)
231  {
232  // An empty std::string ("") has zero columns and zero rows (a
233  // change that was made for Matlab contemptibility.
234 
235  octave_idx_type n = elem(i).rows ();
236 
237  total_nr += n ? n : 1;
238  }
239 
240  octave_idx_type k = 0;
241  if (! fcn_name.empty ())
242  {
243  argv.resize (total_nr+1);
244  argv[0] = fcn_name;
245  k = 1;
246  }
247  else
248  argv.resize (total_nr);
249 
250  for (octave_idx_type i = 0; i < len; i++)
251  {
252  octave_idx_type nr = elem(i).rows ();
253 
254  if (nr < 2)
255  argv[k++] = elem(i).string_value ();
256  else
257  {
258  string_vector tmp = elem(i).all_strings ();
259 
260  for (octave_idx_type j = 0; j < nr; j++)
261  argv[k++] = tmp[j];
262  }
263  }
264  }
265  else
266  error ("%s: expecting all arguments to be strings", fcn_name.c_str ());
267 
268  return argv;
269 }
270 
271 void
273 {
274  octave_idx_type len = length ();
275  const Array<octave_value>& cdata = data;
276 
277  for (octave_idx_type i = 0; i < len; i++)
278  {
279  // This is optimized so that we don't force a copy unless necessary.
280  octave_value tmp = cdata(i).storable_value ();
281  if (! tmp.is_copy_of (cdata (i)))
282  data(i) = tmp;
283  }
284 }