GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ovl.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1994-2025 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
41class OCTINTERP_API octave_value_list
42{
43public:
44
45 octave_value_list () = default;
46
48 : m_data (n), m_names () { }
49
51 : m_data (n, val), m_names () { }
52
54 : m_data (1, tc), m_names () { }
55
56 template<template <typename...> class OV_Container>
57 octave_value_list (const OV_Container<octave_value>& args)
58 : m_data (args.begin (), args.end ()), m_names () { }
59
61 : m_data (a.numel ()), m_names ()
62 {
63 for (octave_idx_type i = 0; i < a.numel (); i++)
64 m_data[i] = a(i);
65 }
66
68 : m_data (c.numel ()), m_names ()
69 {
70 for (octave_idx_type i = 0; i < c.numel (); i++)
71 m_data[i] = c(i);
72 }
73
74 octave_value_list (const octave_value_list& obj) = default;
75
77
78 // Concatenation constructors.
79 octave_value_list (const std::list<octave_value>&);
80 octave_value_list (const std::list<octave_value_list>&);
81
82 ~octave_value_list () = default;
83
84 octave_value_list& operator = (const octave_value_list& obj) = default;
85
86 octave_value_list& operator = (octave_value_list&& obj) = default;
87
89 {
91
92 if (! m_data.empty ())
93 {
94 retval.resize (dim_vector (1, length ()));
95
96 for (octave_idx_type i = 0; i < retval.numel (); i++)
97 retval.xelem (i) = m_data[i];
98 }
99
100 return retval;
101 }
102
103 Cell cell_value () const { return array_value (); }
104
105 // Assignment will resize on range errors.
106
107 octave_value& operator () (octave_idx_type n) { return elem (n); }
108
109 const octave_value& operator () (octave_idx_type n) const { return elem (n); }
110
111 octave_idx_type length () const { return m_data.size (); }
112
113 bool empty () const { return length () == 0; }
114
116 {
117 m_data.resize (n, rfv);
118 }
119
120 octave_value_list& prepend (const octave_value& val);
121
122 octave_value_list& append (const octave_value& val);
123
124 octave_value_list& append (const octave_value_list& lst);
125
126 octave_value_list& reverse ();
127
129 slice (octave_idx_type offset, octave_idx_type len, bool tags = false) const
130 {
131 // linear_slice uses begin/end indices instead of offset and length.
132 // Avoid calling with upper bound out of range.
133
134 octave_idx_type tlen = len > 0 ? len : 0;
135 std::vector<octave_value> slice_data (tlen);
136 auto beg = m_data.begin () + offset;
137 auto end = beg + len;
138 std::copy (beg, end, slice_data.begin ());
139
140 octave_value_list retval = slice_data;
141
142 if (tags && len > 0 && m_names.numel () > 0)
143 retval.m_names = m_names.linear_slice (offset, std::min (offset + len,
144 m_names.numel ()));
145
146 return retval;
147 }
148
150 splice (octave_idx_type offset, octave_idx_type len,
151 const octave_value_list& lst = octave_value_list ()) const;
152
153 bool all_strings_p () const;
154
155 bool all_scalars () const;
156
157 bool any_cell () const;
158
159 bool has_magic_colon () const;
160
161 string_vector make_argv (const std::string& = "") const;
162
163 void stash_name_tags (const string_vector& nm) { m_names = nm; }
164
165 string_vector name_tags () const { return m_names; }
166
167 void make_storable_values ();
168
169 octave_value& xelem (octave_idx_type i) { return m_data[i]; }
170
171 void clear () { m_data.clear (); }
172
174 {
175 if (length ())
176 return m_data.front ();
177
178 return octave_value ();
179 }
180
181private:
182
183 std::vector<octave_value> m_data;
184
185 // The list of strings can be used to tag each element of m_data with a name.
186 // By default, it is empty.
187 string_vector m_names;
188
189 // elem will automatically resize array for out-of-bounds requests.
191 {
192 if (n >= length ())
193 resize (n + 1);
194
195 return m_data[n];
196 }
197
198 const octave_value& elem (octave_idx_type n) const { return m_data[n]; }
199
200};
201
202
203//! Construct an octave_value_list with less typing.
204//!
205//! Historically, this made it easier to create an octave_value_list
206//! from multiple octave_value arguments. It is no longer useful since
207//! octave_value_list has now a constructor accepting an initializer_list
208//! so all it does is save some typing. The following are equivalent:
209//!
210//! @code{.cc}
211//! return octave_value_list ({ov0, ov1, ov2});
212//! return ovl (ov0, ov1, ov2);
213//! @endcode
214
215template<typename... OV_Args>
217ovl (const OV_Args& ... args)
218{
219 return octave_value_list (std::initializer_list<octave_value> ({args...}));
220}
221
222#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition Array.h:525
void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
octave_idx_type numel() const
Number of elements in the array.
Definition Array.h:418
Definition Cell.h:41
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
octave_value_list(const OV_Container< octave_value > &args)
Definition ovl.h:57
octave_value_list(const octave_value_list &obj)=default
void resize(octave_idx_type n, const octave_value &rfv=octave_value())
Definition ovl.h:115
void stash_name_tags(const string_vector &nm)
Definition ovl.h:163
~octave_value_list()=default
Array< octave_value > array_value() const
Definition ovl.h:88
octave_value_list(const Cell &c)
Definition ovl.h:67
octave_value_list()=default
octave_value_list(octave_idx_type n, const octave_value &val)
Definition ovl.h:50
octave_value_list(octave_idx_type n)
Definition ovl.h:47
octave_value_list(octave_value_list &&obj)=default
octave_value_list(const octave_value &tc)
Definition ovl.h:53
bool empty() const
Definition ovl.h:113
octave_value & xelem(octave_idx_type i)
Definition ovl.h:169
Cell cell_value() const
Definition ovl.h:103
octave_value first_or_nil_ov() const
Definition ovl.h:173
string_vector name_tags() const
Definition ovl.h:165
octave_value_list slice(octave_idx_type offset, octave_idx_type len, bool tags=false) const
Definition ovl.h:129
void clear()
Definition ovl.h:171
octave_value_list(const Array< octave_value > &a)
Definition ovl.h:60
octave_idx_type length() const
Definition ovl.h:111
string_vector linear_slice(octave_idx_type lo, octave_idx_type up) const
Definition str-vec.h:138
T::size_type numel(const T &str)
Definition oct-string.cc:74
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition ovl.h:217
F77_RET_T len
Definition xerbla.cc:61