GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ovl.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1994-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_ovl_h)
27 #define octave_ovl_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 #include <vector>
33 #include <initializer_list>
34 
35 #include "str-vec.h"
36 #include "Array.h"
37 
38 #include "ov.h"
39 #include "Cell.h"
40 
41 class
42 OCTINTERP_API
44 {
45 public:
46 
47  octave_value_list () = default;
48 
50  : m_data (n), m_names () { }
51 
53  : m_data (n, val), m_names () { }
54 
56  : m_data (1, tc), m_names () { }
57 
58  template<template <typename...> class OV_Container>
59  octave_value_list (const OV_Container<octave_value>& args)
60  : m_data (args.begin (), args.end ()), m_names () { }
61 
63  : m_data (a.numel ()), m_names ()
64  {
65  for (octave_idx_type i = 0; i < a.numel (); i++)
66  m_data[i] = a(i);
67  }
68 
70  : m_data (c.numel ()), m_names ()
71  {
72  for (octave_idx_type i = 0; i < c.numel (); i++)
73  m_data[i] = c(i);
74  }
75 
76  octave_value_list (const octave_value_list& obj) = default;
77 
79 
80  // Concatenation constructors.
81  octave_value_list (const std::list<octave_value>&);
82  octave_value_list (const std::list<octave_value_list>&);
83 
84  ~octave_value_list () = default;
85 
86  octave_value_list& operator = (const octave_value_list& obj) = default;
87 
88  octave_value_list& operator = (octave_value_list&& obj) = default;
89 
91  {
92  Array<octave_value> retval;
93 
94  if (! m_data.empty ())
95  {
96  retval.resize (dim_vector (1, length ()));
97 
98  for (octave_idx_type i = 0; i < retval.numel (); i++)
99  retval.xelem (i) = m_data[i];
100  }
101 
102  return retval;
103  }
104 
105  Cell cell_value () const { return array_value (); }
106 
107  // Assignment will resize on range errors.
108 
109  octave_value& operator () (octave_idx_type n) { return elem (n); }
110 
111  const octave_value& operator () (octave_idx_type n) const { return elem (n); }
112 
113  octave_idx_type length () const { return m_data.size (); }
114 
115  bool empty () const { return length () == 0; }
116 
118  {
119  m_data.resize (n, rfv);
120  }
121 
122  octave_value_list& prepend (const octave_value& val);
123 
124  octave_value_list& append (const octave_value& val);
125 
126  octave_value_list& append (const octave_value_list& lst);
127 
128  octave_value_list& reverse ();
129 
131  slice (octave_idx_type offset, octave_idx_type len, bool tags = false) const
132  {
133  // linear_slice uses begin/end indices instead of offset and length.
134  // Avoid calling with upper bound out of range.
135 
136  octave_idx_type tlen = len > 0 ? len : 0;
137  std::vector<octave_value> slice_data (tlen);
138  auto beg = m_data.begin () + offset;
139  auto end = beg + len;
140  std::copy (beg, end, slice_data.begin ());
141 
142  octave_value_list retval = slice_data;
143 
144  if (tags && len > 0 && m_names.numel () > 0)
145  retval.m_names = m_names.linear_slice (offset, std::min (offset + len,
146  m_names.numel ()));
147 
148  return retval;
149  }
150 
152  splice (octave_idx_type offset, octave_idx_type len,
153  const octave_value_list& lst = octave_value_list ()) const;
154 
155  bool all_strings_p () const;
156 
157  bool all_scalars () const;
158 
159  bool any_cell () const;
160 
161  bool has_magic_colon () const;
162 
163  string_vector make_argv (const std::string& = "") const;
164 
165  void stash_name_tags (const string_vector& nm) { m_names = nm; }
166 
167  string_vector name_tags () const { return m_names; }
168 
169  void make_storable_values ();
170 
171  octave_value& xelem (octave_idx_type i) { return m_data[i]; }
172 
173  void clear () { m_data.clear (); }
174 
176  {
177  if (length ())
178  return m_data.front ();
179 
180  return octave_value ();
181  }
182 
183 private:
184 
185  std::vector<octave_value> m_data;
186 
187  // The list of strings can be used to tag each element of m_data with a name.
188  // By default, it is empty.
189  string_vector m_names;
190 
191  // elem will automatically resize array for out-of-bounds requests.
193  {
194  if (n >= length ())
195  resize (n + 1);
196 
197  return m_data[n];
198  }
199 
200  const octave_value& elem (octave_idx_type n) const { return m_data[n]; }
201 
202 };
203 
204 
205 //! Construct an octave_value_list with less typing.
206 //!
207 //! Historically, this made it easier to create an octave_value_list
208 //! from multiple octave_value arguments. It is no longer useful since
209 //! octave_value_list has now a constructor accepting an initializer_list
210 //! so all it does is save some typing. The following are equivalent:
211 //!
212 //! @code{.cc}
213 //! return octave_value_list ({ov0, ov1, ov2});
214 //! return ovl (ov0, ov1, ov2);
215 //! @endcode
216 
217 template<typename... OV_Args>
218 inline octave_value_list
219 ovl (const OV_Args& ... args)
220 {
221  return octave_value_list (std::initializer_list<octave_value> ({args...}));
222 }
223 
224 #endif
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array-base.cc:1023
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:524
octave_idx_type numel() const
Number of elements in the array.
Definition: Array.h:414
Definition: Cell.h:43
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
octave_value & xelem(octave_idx_type i)
Definition: ovl.h:171
octave_value_list(const OV_Container< octave_value > &args)
Definition: ovl.h:59
octave_value_list(const octave_value_list &obj)=default
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition: ovl.h:117
Array< octave_value > array_value() const
Definition: ovl.h:90
void stash_name_tags(const string_vector &nm)
Definition: ovl.h:165
~octave_value_list()=default
octave_value_list(const Cell &c)
Definition: ovl.h:69
octave_value_list()=default
octave_value_list(octave_idx_type n, const octave_value &val)
Definition: ovl.h:52
octave_value_list(octave_idx_type n)
Definition: ovl.h:49
octave_value_list(octave_value_list &&obj)=default
octave_value_list(const octave_value &tc)
Definition: ovl.h:55
bool empty() const
Definition: ovl.h:115
Cell cell_value() const
Definition: ovl.h:105
octave_value first_or_nil_ov() const
Definition: ovl.h:175
string_vector name_tags() const
Definition: ovl.h:167
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
Definition: ovl.h:131
void clear()
Definition: ovl.h:173
octave_value_list(const Array< octave_value > &a)
Definition: ovl.h:62
octave_idx_type length() const
Definition: ovl.h:113
string_vector linear_slice(octave_idx_type lo, octave_idx_type up) const
Definition: str-vec.h:140
octave_idx_type n
Definition: mx-inlines.cc:761
T::size_type numel(const T &str)
Definition: oct-string.cc:74
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:219
F77_RET_T len
Definition: xerbla.cc:61