GNU Octave  4.2.1
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
ovl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2017 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 #if defined (HAVE_CONFIG_H)
25 # include "config.h"
26 #endif
27 
28 #include "error.h"
29 #include "ovl.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 {
130 
131  octave_idx_type len = length ();
132 
133  if (offset < 0 || offset >= len)
134  {
135  if (! (rep_length == 0 && offset == len))
136  error ("octave_value_list::splice: invalid OFFSET");
137  }
138 
139  if (rep_length < 0 || rep_length + offset > len)
140  error ("octave_value_list::splice: invalid LENGTH");
141 
142  octave_idx_type lst_len = lst.length ();
143 
144  octave_idx_type new_len = len - rep_length + lst_len;
145 
146  retval.resize (new_len);
147 
148  octave_idx_type k = 0;
149 
150  for (octave_idx_type i = 0; i < offset; i++)
151  retval(k++) = elem (i);
152 
153  for (octave_idx_type i = 0; i < lst_len; i++)
154  retval(k++) = lst (i);
155 
156  for (octave_idx_type i = offset + rep_length; i < len; i++)
157  retval(k++) = elem (i);
158 
159  return retval;
160 }
161 
162 bool
164 {
165  octave_idx_type n = length ();
166 
167  for (octave_idx_type i = 0; i < n; i++)
168  if (! elem(i).is_string ())
169  return false;
170 
171  return true;
172 }
173 
174 bool
176 {
177  octave_idx_type n = length ();
178 
179  for (octave_idx_type i = 0; i < n; i++)
180  {
181  dim_vector dv = elem(i).dims ();
182  if (! dv.all_ones ())
183  return false;
184  }
185 
186  return true;
187 }
188 
189 bool
191 {
192  octave_idx_type n = length ();
193 
194  for (octave_idx_type i = 0; i < n; i++)
195  if (elem (i).is_cell ())
196  return true;
197 
198  return false;
199 }
200 
201 bool
203 {
204  octave_idx_type n = length ();
205 
206  for (octave_idx_type i = 0; i < n; i++)
207  if (elem(i).is_magic_colon ())
208  return true;
209 
210  return false;
211 }
212 
215 {
217 
218  if (! all_strings_p ())
219  error ("%s: all arguments must be strings", fcn_name.c_str ());
220 
221  octave_idx_type len = length ();
222 
223  octave_idx_type total_nr = 0;
224 
225  for (octave_idx_type i = 0; i < len; i++)
226  {
227  // An empty std::string ("") has zero columns and zero rows
228  // (a change that was made for Matlab contemptibility.
229 
230  octave_idx_type n = elem (i).rows ();
231 
232  total_nr += n ? n : 1;
233  }
234 
235  octave_idx_type k = 0;
236  if (! fcn_name.empty ())
237  {
238  argv.resize (total_nr+1);
239  argv[0] = fcn_name;
240  k = 1;
241  }
242  else
243  argv.resize (total_nr);
244 
245  for (octave_idx_type i = 0; i < len; i++)
246  {
247  octave_idx_type nr = elem (i).rows ();
248 
249  if (nr < 2)
250  argv[k++] = elem (i).string_value ();
251  else
252  {
254 
255  for (octave_idx_type j = 0; j < nr; j++)
256  argv[k++] = tmp[j];
257  }
258  }
259 
260  return argv;
261 }
262 
263 void
265 {
266  octave_idx_type len = length ();
267  const Array<octave_value>& cdata = data;
268 
269  for (octave_idx_type i = 0; i < len; i++)
270  {
271  // This is optimized so that we don't force a copy unless necessary.
272  octave_value tmp = cdata(i).storable_value ();
273  if (! tmp.is_copy_of (cdata (i)))
274  data(i) = tmp;
275  }
276 }
bool any_cell(void) const
Definition: ovl.cc:190
octave_idx_type nel
Definition: oct-lvalue.h:111
octave_idx_type rows(void) const
Definition: ov.h:489
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:5068
octave_idx_type length(void) const
Definition: ovl.h:96
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:85
for large enough k
Definition: lu.cc:606
void error(const char *fmt,...)
Definition: error.cc:570
bool all_ones(void) const
Definition: dim-vector.h:377
bool is_copy_of(const octave_value &val) const
Definition: ov.h:1272
void make_storable_values(void)
Definition: ovl.cc:264
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:97
string_vector argv
Definition: load-save.cc:635
octave_value & elem(octave_idx_type n)
Definition: ovl.h:162
string_vector make_argv(const std::string &="") const
Definition: ovl.cc:214
std::string string_value(bool force=false) const
Definition: ov.h:908
octave_value_list & reverse(void)
Definition: ovl.cc:111
void resize(const dim_vector &dv, const T &rfv)
Definition: Array.cc:1028
bool has_magic_colon(void) const
Definition: ovl.cc:202
double tmp
Definition: data.cc:6300
octave_value retval
Definition: data.cc:6294
octave_value_list splice(octave_idx_type offset, octave_idx_type len, const octave_value_list &lst=octave_value_list()) const
Definition: ovl.cc:126
octave_value_list(void)
Definition: ovl.h:45
Array< octave_value > data
Definition: ovl.h:156
dim_vector dims(void) const
Definition: ov.h:486
string_vector string_vector_value(bool pad=false) const
Definition: ov.h:911
bool all_strings_p(void) const
Definition: ovl.cc:163
=val(i)}if ode{val(i)}occurs in table i
Definition: lookup.cc:239
p
Definition: lu.cc:138
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:100
void assign(const idx_vector &i, const Array< T > &rhs, const T &rfv)
Indexed assignment (always with resize & fill).
Definition: Array.cc:1133
bool all_scalars(void) const
Definition: ovl.cc:175
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:87
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
dim_vector dv
Definition: sub2ind.cc:263
octave_value_list & prepend(const octave_value &val)
Definition: ovl.cc:67