GNU Octave 7.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-2022 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
42OCTINTERP_API
44{
45public:
46
47 octave_value_list (void) = 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 (void) = 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 {
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 (void) 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 (void) const { return m_data.size (); }
114
115 bool empty (void) 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 (void);
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 (void) const;
156
157 bool all_scalars (void) const;
158
159 bool any_cell (void) const;
160
161 bool has_magic_colon (void) 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 (void) const { return m_names; }
168
169 void make_storable_values (void);
170
171 octave_value& xelem (octave_idx_type i) { return m_data[i]; }
172
173 void clear (void) { m_data.clear (); }
174
175private:
176
177 std::vector<octave_value> m_data;
178
179 // The list of strings can be used to tag each element of m_data with a name.
180 // By default, it is empty.
182
183 // elem will automatically resize array for out-of-bounds requests.
185 {
186 if (n >= length ())
187 resize (n + 1);
188
189 return m_data[n];
190 }
191
192 const octave_value& elem (octave_idx_type n) const { return m_data[n]; }
193
194};
195
196
197//! Construct an octave_value_list with less typing.
198//!
199//! Historically, this made it easier to create an octave_value_list
200//! from multiple octave_value arguments. It is no longer useful since
201//! octave_value_list has now a constructor accepting an initializer_list
202//! so all it does is save some typing. The following are equivalent:
203//!
204//! @code{.cc}
205//! return octave_value_list ({ov0, ov1, ov2});
206//! return ovl (ov0, ov1, ov2);
207//! @endcode
208
209template<typename... OV_Args>
211ovl (const OV_Args&... args)
212{
213 return octave_value_list (std::initializer_list<octave_value> ({args...}));
214}
215
216#endif
static int elem
Definition: __contourc__.cc:54
charNDArray min(char d, const charNDArray &m)
Definition: chNDArray.cc:207
T & xelem(octave_idx_type n)
Size of the specified dimension.
Definition: Array.h:504
octave_idx_type numel(void) const
Number of elements in the array.
Definition: Array.h:411
OCTARRAY_API void resize(const dim_vector &dv, const T &rfv)
Size of the specified dimension.
Definition: Array.cc:1010
Definition: Cell.h:43
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
~octave_value_list(void)=default
Array< octave_value > array_value(void) const
Definition: ovl.h:90
Cell cell_value(void) const
Definition: ovl.h:105
octave_value & elem(octave_idx_type n)
Definition: ovl.h:184
bool empty(void) const
Definition: ovl.h:115
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
void stash_name_tags(const string_vector &nm)
Definition: ovl.h:165
octave_idx_type length(void) const
Definition: ovl.h:113
octave_value_list(const Cell &c)
Definition: ovl.h:69
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
void clear(void)
Definition: ovl.h:173
octave_value & xelem(octave_idx_type i)
Definition: ovl.h:171
string_vector name_tags(void) 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
string_vector m_names
Definition: ovl.h:181
const octave_value & elem(octave_idx_type n) const
Definition: ovl.h:192
octave_value_list(const Array< octave_value > &a)
Definition: ovl.h:62
octave_value_list(void)=default
std::vector< octave_value > m_data
Definition: ovl.h:177
string_vector linear_slice(octave_idx_type lo, octave_idx_type up) const
Definition: str-vec.h:140
T::size_type numel(const T &str)
Definition: oct-string.cc:71
octave_value_list ovl(const OV_Args &... args)
Construct an octave_value_list with less typing.
Definition: ovl.h:211
F77_RET_T len
Definition: xerbla.cc:61