GNU Octave  4.4.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ovl.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1994-2018 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
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License 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 <https://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 (const auto& ovl : lst)
42  {
43  n++;
44  nel += ovl.length ();
45  }
46 
47  // Optimize single-element case
48  if (n == 1)
49  data = lst.front ().data;
50  else if (nel > 0)
51  {
52  data.resize (dim_vector (1, nel));
53  octave_idx_type k = 0;
54  for (const auto& ovl : lst)
55  {
56  data.assign (idx_vector (k, k + ovl.length ()), ovl.data);
57  k += ovl.length ();
58  }
59  assert (k == nel);
60  }
61 
62 }
63 
66 {
67  octave_idx_type n = length ();
68 
69  resize (n + 1);
70 
71  while (n > 0)
72  {
73  elem (n) = elem (n - 1);
74  n--;
75  }
76 
77  elem (0) = val;
78 
79  return *this;
80 }
81 
84 {
85  octave_idx_type n = length ();
86 
87  resize (n + 1);
88 
89  elem (n) = val;
90 
91  return *this;
92 }
93 
96 {
97  octave_idx_type len = length ();
98  octave_idx_type lst_len = lst.length ();
99 
100  resize (len + lst_len);
101 
102  for (octave_idx_type i = 0; i < lst_len; i++)
103  elem (len + i) = lst (i);
104 
105  return *this;
106 }
107 
110 {
111  octave_idx_type n = length ();
112 
113  for (octave_idx_type i = 0; i < n / 2; i++)
114  {
115  octave_value tmp = elem (i);
116  elem (i) = elem (n - i - 1);
117  elem (n - i - 1) = tmp;
118  }
119 
120  return *this;
121 }
122 
125  const octave_value_list& lst) const
126 {
128 
129  octave_idx_type len = length ();
130 
131  if (offset < 0 || offset >= len)
132  {
133  if (! (rep_length == 0 && offset == len))
134  error ("octave_value_list::splice: invalid OFFSET");
135  }
136 
137  if (rep_length < 0 || rep_length + offset > len)
138  error ("octave_value_list::splice: invalid LENGTH");
139 
140  octave_idx_type lst_len = lst.length ();
141 
142  octave_idx_type new_len = len - rep_length + lst_len;
143 
144  retval.resize (new_len);
145 
146  octave_idx_type k = 0;
147 
148  for (octave_idx_type i = 0; i < offset; i++)
149  retval(k++) = elem (i);
150 
151  for (octave_idx_type i = 0; i < lst_len; i++)
152  retval(k++) = lst (i);
153 
154  for (octave_idx_type i = offset + rep_length; i < len; i++)
155  retval(k++) = elem (i);
156 
157  return retval;
158 }
159 
160 bool
162 {
163  octave_idx_type n = length ();
164 
165  for (octave_idx_type i = 0; i < n; i++)
166  if (! elem(i).is_string ())
167  return false;
168 
169  return true;
170 }
171 
172 bool
174 {
175  octave_idx_type n = length ();
176 
177  for (octave_idx_type i = 0; i < n; i++)
178  {
179  dim_vector dv = elem(i).dims ();
180  if (! dv.all_ones ())
181  return false;
182  }
183 
184  return true;
185 }
186 
187 bool
189 {
190  octave_idx_type n = length ();
191 
192  for (octave_idx_type i = 0; i < n; i++)
193  if (elem (i).iscell ())
194  return true;
195 
196  return false;
197 }
198 
199 bool
201 {
202  octave_idx_type n = length ();
203 
204  for (octave_idx_type i = 0; i < n; i++)
205  if (elem(i).is_magic_colon ())
206  return true;
207 
208  return false;
209 }
210 
213 {
215 
216  if (! all_strings_p ())
217  error ("%s: all arguments must be strings", fcn_name.c_str ());
218 
219  octave_idx_type len = length ();
220 
221  octave_idx_type total_nr = 0;
222 
223  for (octave_idx_type i = 0; i < len; i++)
224  {
225  // An empty std::string ("") has zero columns and zero rows
226  // (a change that was made for Matlab contemptibility.
227 
228  octave_idx_type n = elem (i).rows ();
229 
230  total_nr += n ? n : 1;
231  }
232 
233  octave_idx_type k = 0;
234  if (! fcn_name.empty ())
235  {
236  argv.resize (total_nr+1);
237  argv[0] = fcn_name;
238  k = 1;
239  }
240  else
241  argv.resize (total_nr);
242 
243  for (octave_idx_type i = 0; i < len; i++)
244  {
245  octave_idx_type nr = elem (i).rows ();
246 
247  if (nr < 2)
248  argv[k++] = elem (i).string_value ();
249  else
250  {
252 
253  for (octave_idx_type j = 0; j < nr; j++)
254  argv[k++] = tmp[j];
255  }
256  }
257 
258  return argv;
259 }
260 
261 void
263 {
264  octave_idx_type len = length ();
265  const Array<octave_value>& cdata = data;
266 
267  for (octave_idx_type i = 0; i < len; i++)
268  {
269  // This is optimized so that we don't force a copy unless necessary.
270  octave_value tmp = cdata(i).storable_value ();
271  if (! tmp.is_copy_of (cdata (i)))
272  data(i) = tmp;
273  }
274 }
std::string string_value(bool force=false) const
Definition: ov.h:955
const T * data(void) const
Definition: Array.h:582
identity matrix If supplied two scalar respectively For allows like xample val
Definition: data.cc:4986
octave_value_list & append(const octave_value &val)
Definition: ovl.cc:83
for large enough k
Definition: lu.cc:617
bool has_magic_colon(void) const
Definition: ovl.cc:200
void error(const char *fmt,...)
Definition: error.cc:578
octave_value_list splice(octave_idx_type offset, octave_idx_type len, const octave_value_list &lst=octave_value_list()) const
Definition: ovl.cc:124
void make_storable_values(void)
Definition: ovl.cc:262
void resize(octave_idx_type n, const std::string &rfv="")
Definition: str-vec.h:97
string_vector argv
Definition: load-save.cc:648
octave_value resize(const dim_vector &dv, bool fill=false) const
Definition: ov.h:511
octave_value & elem(octave_idx_type n)
Definition: ovl.h:162
bool any_cell(void) const
Definition: ovl.cc:188
octave_value_list & reverse(void)
Definition: ovl.cc:109
dim_vector dims(void) const
Definition: ov.h:469
string_vector make_argv(const std::string &="") const
Definition: ovl.cc:212
void resize(const dim_vector &dv, const T &rfv)
Resizing (with fill).
Definition: Array.cc:1010
octave_idx_type rows(void) const
Definition: ov.h:472
double tmp
Definition: data.cc:6252
octave_value retval
Definition: data.cc:6246
octave_value_list(void)
Definition: ovl.h:45
bool all_scalars(void) const
Definition: ovl.cc:173
Array< octave_value > data
Definition: ovl.h:156
bool all_ones(void) const
Definition: dim-vector.h:351
OCTAVE_EXPORT octave_value_list isa nd deftypefn *return ovl(args(0).isinteger())
octave_idx_type length(void) const
Definition: ovl.h:96
bool all_strings_p(void) const
Definition: ovl.cc:161
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:1115
for i
Definition: data.cc:5264
string_vector string_vector_value(bool pad=false) const
Definition: ov.h:958
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:888
dim_vector dv
Definition: sub2ind.cc:263
octave_value_list & prepend(const octave_value &val)
Definition: ovl.cc:65