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