GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
str-vec.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 1996-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_str_vec_h)
27#define octave_str_vec_h 1
28
29#include "octave-config.h"
30
31#include <iosfwd>
32#include <list>
33#include <string>
34
35#include "Array.h"
36
38{
39public:
40
41 string_vector () = default;
42
43 explicit string_vector (octave_idx_type n) : m_data (dim_vector (n, 1)) { }
44
45 string_vector (const char *s) : m_data (dim_vector (1, 1), s) { }
46
47 string_vector (const std::string& s) : m_data (dim_vector (1, 1), s) { }
48
49 string_vector (const string_vector&) = default;
50
52
53 //! Constructor for STL containers of std::string.
54 //!
55 //! Templated constructor for any template class with std::string as the
56 //! first parameter, and begin, end, and size methods, i.e., a class with
57 //! similar interface as the STL containers.
58
59 template<template <typename...> class String_Container, typename... Other>
60 string_vector (const String_Container<std::string, Other...>& lst);
61
63 : m_data (s.as_column ()) { }
64
65 string_vector (const char * const *s);
66
67 string_vector (const char * const *s, octave_idx_type n);
68
69 string_vector& operator = (const string_vector&) = default;
70
71 string_vector& operator = (string_vector&&) = default;
72
73 ~string_vector () = default;
74
75 bool empty () const { return numel () == 0; }
76
78 {
80 octave_idx_type longest = 0;
81
82 for (octave_idx_type i = 0; i < n; i++)
83 {
84 octave_idx_type tmp = elem (i).length ();
85
86 if (tmp > longest)
87 longest = tmp;
88 }
89
90 return longest;
91 }
92
93 void resize (octave_idx_type n, const std::string& rfv = "")
94 {
95 m_data.resize (dim_vector (n, 1), rfv);
96 }
97
98 octave_idx_type numel () const { return m_data.numel (); }
99
100 bool isempty () const { return m_data.isempty (); }
101
102 std::string& elem (octave_idx_type i) { return m_data.elem (i); }
103
104 std::string elem (octave_idx_type i) const { return m_data.elem (i); }
105
106 std::string& xelem (octave_idx_type i) { return m_data.xelem (i); }
107
108 std::string xelem (octave_idx_type i) const { return m_data.xelem (i); }
109
110 std::string& operator[] (octave_idx_type i) { return elem (i); }
111
112 std::string operator[] (octave_idx_type i) const { return elem (i); }
113
114 std::string& operator () (octave_idx_type i) { return elem (i); }
115
116 std::string operator () (octave_idx_type i) const { return elem (i); }
117
118 string_vector& sort (bool make_uniq = false);
119
120 string_vector& uniq ();
121
122 string_vector& append (const std::string& s);
123
124 string_vector& append (const string_vector& sv);
125
126 std::string join (const std::string& sep = "") const;
127
128 char ** c_str_vec () const;
129
130 std::list<std::string> std_list () const;
131
132 static void delete_c_str_vec (const char * const*);
133
134 std::ostream&
135 list_in_columns (std::ostream&, int width = 0,
136 const std::string& prefix = "") const;
137
139 {
140 return string_vector (m_data.linear_slice (lo, up));
141 }
142
143 template <typename U, typename F> Array<U> map (F fcn) const
144 {
145 return m_data.map<U> (fcn);
146 }
147
148private:
149
150 Array<std::string> m_data;
151};
152
153
154template<template <typename...> class String_Container, typename... Other>
155string_vector::string_vector (const String_Container<std::string, Other...>&
156 lst)
157 : m_data ()
158{
159 resize (lst.size ());
160
161 octave_idx_type i = 0;
162 for (const std::string& s : lst)
163 elem (i++) = s;
164}
165
166#endif
N Dimensional Array with copy-on-write semantics.
Definition Array.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:90
~string_vector()=default
std::string elem(octave_idx_type i) const
Definition str-vec.h:104
std::string & xelem(octave_idx_type i)
Definition str-vec.h:106
string_vector(const char *s)
Definition str-vec.h:45
string_vector(const Array< std::string > &s)
Definition str-vec.h:62
void resize(octave_idx_type n, const std::string &rfv="")
Definition str-vec.h:93
string_vector(octave_idx_type n)
Definition str-vec.h:43
octave_idx_type max_length() const
Definition str-vec.h:77
std::string xelem(octave_idx_type i) const
Definition str-vec.h:108
string_vector linear_slice(octave_idx_type lo, octave_idx_type up) const
Definition str-vec.h:138
bool isempty() const
Definition str-vec.h:100
Array< U > map(F fcn) const
Definition str-vec.h:143
string_vector(const std::string &s)
Definition str-vec.h:47
bool empty() const
Definition str-vec.h:75
octave_idx_type numel() const
Definition str-vec.h:98
string_vector(const string_vector &)=default
string_vector()=default
std::string & elem(octave_idx_type i)
Definition str-vec.h:102
string_vector(string_vector &&)=default
#define OCTAVE_API
Definition main.in.cc:55
T::size_type numel(const T &str)
Definition oct-string.cc:74