GNU Octave  4.0.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-2015 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.
34 
35 octave_value_list::octave_value_list (const std::list<octave_value_list>& lst)
36 {
37  octave_idx_type n = 0;
38  octave_idx_type 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 }
bool any_cell(void) const
Definition: oct-obj.cc:196
octave_idx_type rows(void) const
Definition: ov.h:473
octave_idx_type length(void) const
Definition: oct-obj.h:89
octave_value_list & append(const octave_value &val)
Definition: oct-obj.cc:85
void error(const char *fmt,...)
Definition: error.cc:476
bool all_ones(void) const
Definition: dim-vector.h:349
bool is_copy_of(const octave_value &val) const
Definition: ov.h:1087
void make_storable_values(void)
Definition: oct-obj.cc:272
string_vector all_strings(bool pad=false) const
Definition: ov.h:894
octave_value & elem(octave_idx_type n)
Definition: oct-obj.h:161
std::string string_value(bool force=false) const
Definition: ov.h:897
octave_value_list & reverse(void)
Definition: oct-obj.cc:111
void resize(octave_idx_type n, const std::string &rfv=std::string())
Definition: str-vec.h:91
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1033
bool has_magic_colon(void) const
Definition: oct-obj.cc:208
octave_value_list splice(octave_idx_type offset, octave_idx_type len, const octave_value_list &lst=octave_value_list()) const
Definition: oct-obj.cc:126
octave_value_list(void)
Definition: oct-obj.h:42
Array< octave_value > data
Definition: oct-obj.h:155
dim_vector dims(void) const
Definition: ov.h:470
bool all_strings_p(void) const
Definition: oct-obj.cc:169
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: oct-obj.h:93
void assign(const idx_vector &i, const Array< T > &rhs, const T &rfv)
Indexed assignment (always with resize & fill).
Definition: Array.cc:1140
bool all_scalars(void) const
Definition: oct-obj.cc:181
string_vector make_argv(const std::string &=std::string()) const
Definition: oct-obj.cc:220
octave_value_list & prepend(const octave_value &val)
Definition: oct-obj.cc:67