gslcpp
Modern-C++ Wrapper for GSL
vector-view.hpp
Go to the documentation of this file.
1 /// \file include/gslcpp/vector-view.hpp
2 /// \copyright 2022 Thomas E. Vaughan, all rights reserved.
3 /// \brief Definition for gsl::vector_view.
4 
5 #pragma once
6 #include "vec/v-iface.hpp" // v_iface
7 #include "wrap/vector-view-array.hpp" // w_vector_view_array
8 
9 /// Namespace for C++-interface to GSL.
10 namespace gsl {
11 
12 
13 /// Constructor-type for vector-by-reference whose storage is *not* owned by
14 /// instance of view.
15 ///
16 /// Despite its name, %vector_view allows both viewing and modifying data that
17 /// it refers to. These data are presented as if they were in a virtual vector
18 /// represented by the intance.
19 ///
20 /// %vector_view has its interface to storage given by gsl::v_view, and most of
21 /// the ordinary vector-interface is given by gsl::v_iface.
22 ///
23 /// %vector_view inherits these and provides template-constructors for an
24 /// instance of type gsl::v_iface<T, S, v_view>.
25 ///
26 /// - One constructor is from a normal, decayed pointer and a length (and
27 /// optionally a stride).
28 ///
29 /// - The other is from a non-decayed C-style array (which still retains
30 /// information about its length).
31 ///
32 /// Template-value-parameter `S` indicates the number of elements in the vector
33 /// at compile-time. If `S` be zero, then the number of elements in the vector
34 /// is determined at run-time.
35 ///
36 /// When using %vector_view, one typically does not need to specify
37 /// template-parameters:
38 ///
39 /// ~~~{.cpp}
40 /// #include <gslcpp/vector-view.hpp>
41 ///
42 /// int main() {
43 /// double g[]= {1, 2, 3, 4, 5, 6};
44 ///
45 /// // Deduce vector_view<double, 6> with stride=1:
46 /// gsl::vector_view v1= g;
47 ///
48 /// // Deduce vector_view<double, 0> with three elements and stride=2:
49 /// gsl::vector_view v2(g, 3, 2);
50 ///
51 /// v2[1]= 10; // Now g[2] is no longer 3 but 10.
52 ///
53 /// // Deduce vector_view<double const, 0> with three elements and stride=2:
54 /// double const *h= g;
55 /// gsl::vector_view v3(h, 3, 2); // Only fetching allowed, not setting.
56 ///
57 /// return 0;
58 /// }
59 /// ~~~
60 ///
61 /// @tparam T Type of each element in vector.
62 /// @tparam S Compile-time number of elements (0 for number set at run-time).
63 template<typename T, size_t S= 0>
64 struct vector_view: public v_iface<T, S, v_view> {
65  using P= v_iface<T, S, v_view>; ///< Type of ancestor.
66  using P::P;
67 
68  /// Initialize view of standard (decayed) C-array.
69  ///
70  /// Arguments are ordered differently from those given to
71  /// gsl_vector_viewiew_array_with_stride(). Putting stride at *end* allows
72  /// it to have default value of 1.
73  ///
74  /// \param b Pointer to first element of array and of view.
75  /// \param n Number of elements in view.
76  /// \param s Stride of elements relative to array.
77  vector_view(T *b, size_t n, size_t s= 1): P(w_vector_view_array(b, s, n)) {}
78 
79  /// Initialize view of non-decayed C-array.
80  /// \param b Reference to non-decayed C-array.
81  vector_view(T (&b)[S]): P(w_vector_view_array(b, 1, S)) {}
82 };
83 
84 
85 } // namespace gsl
86 
87 // EOF
gsl::vector_view::vector_view
vector_view(T(&b)[S])
Initialize view of non-decayed C-array.
Definition: vector-view.hpp:81
gsl::v_iface
Interface for every kind of vector.
Definition: v-iface.hpp:62
gsl::v_view
Interface to vector-storage not owned by interface.
Definition: v-view.hpp:17
gsl::vector_view::vector_view
vector_view(T *b, size_t n, size_t s=1)
Initialize view of standard (decayed) C-array.
Definition: vector-view.hpp:77
gsl::vector_view
Constructor-type for vector-by-reference whose storage is not owned by instance of view.
Definition: vector-view.hpp:64
gsl
Namespace for C++-interface to GSL.
Definition: v-iface.hpp:51