gslcpp
Modern-C++ Wrapper for GSL
Classes | Public Types | Public Member Functions | List of all members
gsl::vector< T, S > Struct Template Reference

Constructor-type for vector whose storage is owned by instance of vector. More...

#include <vector.hpp>

Inheritance diagram for gsl::vector< T, S >:
[legend]
Collaboration diagram for gsl::vector< T, S >:
[legend]

Classes

struct  sz_ok
 Struct defining whether size of other vector allow copying its data. More...
 

Public Types

using P = v_iface< T, S, v_stor >
 Type of ancestor.
 
- Public Types inherited from gsl::v_iface< T, 0, v_stor >
enum  
 
using E = T
 Element-type needed by gsl::v_iterator<v_iface>.
 
using iterator = v_iterator< v_iface >
 Type of iterator that points to mutable element.
 
using const_iterator = v_iterator< v_iface const >
 Type of iterator that points to immutable element.
 

Public Member Functions

 vector (vector const &src)
 Copy data from other vector of same type. More...
 
 vector (vector &&)=default
 Enable move-constructor in gsl::v_stor to work.
 
vectoroperator= (vector const &)=default
 Enable explicitly defined copy-constructor in gsl::v_iface. More...
 
template<size_t N, template< typename, size_t > class V, typename = enable_if_t<sz_ok<N>::V && !is_const_v<T>>>
 vector (v_iface< T const, N, V > const &src)
 Copy data from vector of different type (different element-constness, different compile-time number of elements, or different storage-interface). More...
 
template<size_t N, template< typename, size_t > class V, typename = enable_if_t<sz_ok<N>::V>>
 vector (v_iface< T, N, V > const &src)
 Copy data from vector of different type (different compile-time number of elements or different storage-interface). More...
 
 vector (T const *d, size_t n, size_t s=1)
 Copy from standard (decayed) C-array. More...
 
 vector (T const (&d)[S])
 Copy from non-decayed C-array. More...
 
 vector (std::initializer_list< T > i)
 Copy from initializer-list. More...
 
- Public Member Functions inherited from gsl::v_iface< T, 0, v_stor >
iterator begin ()
 Iterator that points to first element. More...
 
const_iterator begin () const
 Iterator that points to first element. More...
 
iterator end ()
 Iterator that points to element just past last element. More...
 
const_iterator end () const
 Iterator that points to element just past last element. More...
 
size_t size () const
 Size of vector. More...
 
T * data ()
 Pointer to first element in vector. More...
 
const T * data () const
 Pointer to first element in immutable vector. More...
 
get (size_t i) const
 Read element with bounds-checking. More...
 
void set (size_t i, T const &x)
 Write element with bounds-checking. More...
 
const T & operator[] (size_t i) const
 Read element without bounds-checking. More...
 
T & operator[] (size_t i)
 Write element without bounds-checking. More...
 
T * ptr (size_t i)
 Retrieve pointer to ith element with bounds-checking. More...
 
const T * ptr (size_t i) const
 Retrieve pointer to ith element with bounds-checking. More...
 
void set_all (T const &x)
 Set every element. More...
 
void set_zero ()
 Set every element to zero.
 
int set_basis (size_t i)
 Set element at offset i to unity and every other element to zero. More...
 
int fwrite (FILE *f) const
 Write non-portable binary-image of vector to file. More...
 
int fread (FILE *f)
 Read non-portable binary-image of vector from file. More...
 
int fprintf (FILE *flp, char const *fmt) const
 Write ASCII-formatted representation of vector to file. More...
 
int fscanf (FILE *f)
 Read ASCII-formatted representation of vector from file. More...
 
v_iface< element_t< T >, N, v_viewreal ()
 View of real-part of complex vector. More...
 
v_iface< element_t< T > const, N, v_viewreal () const
 View of real-part of complex vector. More...
 
v_iface< element_t< T >, N, v_viewimag ()
 View of imaginary-part of complex vector. More...
 
v_iface< element_t< T > const, N, v_viewimag () const
 View of imaginary-part of complex vector. More...
 
v_iface< T, N, v_viewsubvector (size_t n, size_t i=0, size_t s=1)
 View of subvector of vector. More...
 
v_iface< T const, N, v_viewsubvector (size_t n, size_t i=0, size_t s=1) const
 View of subvector of vector. More...
 
v_iface< T, N, v_viewview ()
 View of vector. More...
 
v_iface< T const, N, v_viewview () const
 View of vector. More...
 
int swap_elements (size_t i, size_t j)
 Swap elements within this vector. More...
 
int reverse ()
 Reverse order of elements. More...
 
int add (v_iface< T, ON, OV > const &b)
 Add contents of b into this vector in place. More...
 
int sub (v_iface< T, ON, OV > const &b)
 Subtract contents of b from this vector in place. More...
 
int mul (v_iface< T, ON, OV > const &b)
 Multiply contents of b into this vector in place. More...
 
int div (v_iface< T, ON, OV > const &b)
 Divide contents of b into this vector in place. More...
 
v_ifaceoperator+= (v_iface< T, ON, OV > const &b)
 Add contents of b into this vector in place. More...
 
v_ifaceoperator+= (T const &x)
 Add constant into each element of this vector in place. More...
 
v_ifaceoperator-= (v_iface< T, ON, OV > const &b)
 Subtract contents of b from this vector in place. More...
 
v_ifaceoperator*= (v_iface< T, ON, OV > const &b)
 Multiply contents of b into this vector in place. More...
 
v_ifaceoperator*= (T const &x)
 Multiply scalar into this vector in place. More...
 
v_ifaceoperator/= (v_iface< T, ON, OV > const &b)
 Divide contents of b into this vector in place. More...
 
v_ifaceoperator= (v_iface< T, ON, OV > const &b)
 Copy contents of b into this vector. More...
 
v_ifaceoperator= (v_iface const &b)
 Copy contents of b into this vector. More...
 
 v_iface (v_iface &&)=default
 Enable move-constructor in gsl::v_stor to work.
 
int scale (T const &x)
 Multiply scalar into this vector in place. More...
 
int add_constant (T const &x)
 Add constant into each element of this vector in place. More...
 
sum () const
 Sum of elements. More...
 
max () const
 Greatest value of any element. More...
 
min () const
 Least value of any element. More...
 
void minmax (T &min, T &max) const
 Greatest value and least value of any element. More...
 
size_t max_index () const
 Offset of greatest value. More...
 
size_t min_index () const
 Offset of least value. More...
 
void minmax_index (size_t &imin, size_t &imax) const
 Offset of least value and offset of greatest value. More...
 
bool isnull () const
 True only if every element have zero value. More...
 
bool ispos () const
 True only if every element be positive. More...
 
bool isneg () const
 True only if every element be negative. More...
 
bool isnonneg () const
 True only if every element be non-negative. More...
 
- Public Member Functions inherited from gsl::v_stor< T, N >
 v_stor (size_t n=S)
 Initialize GSL's view of static storage, but do not initialize elements. More...
 
auto * v ()
 Pointer to GSL's interface to vector. More...
 
const auto * v () const
 Pointer to GSL's interface to vector. More...
 

Detailed Description

template<typename T, size_t S = 0>
struct gsl::vector< T, S >

Constructor-type for vector whose storage is owned by instance of vector.

vector has its interface to storage given by gsl::v_stor, and most of the ordinary vector-interface is given by gsl::v_iface.

vector inherits these and provides template-constructors for an instance of type gsl::v_iface<T, S, v_stor>.

Each constructor copies data from the source or (in the case of the move-constructor, when both the source is an r-value with S = 0, and the destination has S = 0) moves the data into the instance.

Template-value-parameter S indicates the number of elements in the vector at compile-time. If S be zero, then the number of elements in the vector is determined at run-time.

Although move-construction is provided, move-assignment is not provided. Once a chunk of memory is allocated to a vector, that same chunk, and only ever that chunk, is associated with the vector until it is destroyed.

When using vector, one typically does not need to specify template-parameters:

int main() {
double g[]= {1, 2, 3, 4, 5, 6};
// Deduce vector<double, 6> with stride=1, and copy the data:
gsl::vector v1= g;
// Deduce vector<double, 0> with three elements and stride=2, and copy
// the data:
gsl::vector v2(g, 3, 2);
// Deduce vector<double const, 0> with three elements and stride=2, and
// copy the data:
double const *h= g;
gsl::vector v3(h, 3, 2);
return 0;
}
Template Parameters
TType of each element in vector.
SCompile-time number of elements (0 for number set at run-time).

Definition at line 67 of file vector.hpp.

Constructor & Destructor Documentation

◆ vector() [1/6]

template<typename T , size_t S = 0>
gsl::vector< T, S >::vector ( vector< T, S > const &  src)
inline

Copy data from other vector of same type.

Parameters
srcReference to other vector.

Definition at line 73 of file vector.hpp.

73 : P(src.v()->size) { memcpy(*this, src); }

◆ vector() [2/6]

template<typename T , size_t S = 0>
template<size_t N, template< typename, size_t > class V, typename = enable_if_t<sz_ok<N>::V && !is_const_v<T>>>
gsl::vector< T, S >::vector ( v_iface< T const, N, V > const &  src)
inline

Copy data from vector of different type (different element-constness, different compile-time number of elements, or different storage-interface).

Template Parameters
NNumber of elements in other vector.
VType of other vector's interface to storage.
Parameters
srcReference to other vector.

Definition at line 99 of file vector.hpp.

99  : P(src.v()->size) {
100  memcpy(*this, src);
101  }

◆ vector() [3/6]

template<typename T , size_t S = 0>
template<size_t N, template< typename, size_t > class V, typename = enable_if_t<sz_ok<N>::V>>
gsl::vector< T, S >::vector ( v_iface< T, N, V > const &  src)
inline

Copy data from vector of different type (different compile-time number of elements or different storage-interface).

Template Parameters
NNumber of elements in other vector.
VType of other vector's interface to storage.
Parameters
srcReference to other vector.

Definition at line 113 of file vector.hpp.

113  : P(src.v()->size) {
114  memcpy(*this, src);
115  }

◆ vector() [4/6]

template<typename T , size_t S = 0>
gsl::vector< T, S >::vector ( T const *  d,
size_t  n,
size_t  s = 1 
)
inline

Copy from standard (decayed) C-array.

Arguments are ordered differently from those given to gsl_vector_viewiew_array_with_stride(). Putting stride at end allows it to have default value of 1.

Parameters
dPointer to first element of array and of vector.
nNumber of elements in vector.
sStride of elements relative to array.

Definition at line 126 of file vector.hpp.

126  : P(n) {
127  auto const cview= w_vector_view_array(d, s, n);
128  memcpy(*this, v_iface<T const, 0, v_view>(cview));
129  }

◆ vector() [5/6]

template<typename T , size_t S = 0>
gsl::vector< T, S >::vector ( T const (&)  d[S])
inline

Copy from non-decayed C-array.

Parameters
dReference to non-decayed C-array.

Definition at line 133 of file vector.hpp.

133  : P(S) {
134  auto const cview= w_vector_view_array(d, 1, S);
135  memcpy(*this, v_iface<T const, S, v_view>(cview));
136  }

◆ vector() [6/6]

template<typename T , size_t S = 0>
gsl::vector< T, S >::vector ( std::initializer_list< T >  i)
inline

Copy from initializer-list.

Parameters
iList of initializers.

Definition at line 140 of file vector.hpp.

140  : P(i.size()) {
141  auto const cview= w_vector_view_array(i.begin(), 1, i.size());
142  memcpy(*this, v_iface<T const, 0, v_view>(cview));
143  }

Member Function Documentation

◆ operator=()

template<typename T , size_t S = 0>
vector& gsl::vector< T, S >::operator= ( vector< T, S > const &  )
default

Enable explicitly defined copy-constructor in gsl::v_iface.

Returns
Reference to this instance after assignment.

The documentation for this struct was generated from the following file:
vector.hpp
Definition for gsl::vector.
gsl::w_vector_view_array
auto w_vector_view_array(double *b, size_t s, size_t n)
Initialize view of C-style array as vector.
Definition: vector-view-array.hpp:17
gsl::vector
Constructor-type for vector whose storage is owned by instance of vector.
Definition: vector.hpp:67
gsl::vector::P
v_iface< T, S, v_stor > P
Type of ancestor.
Definition: vector.hpp:68
gsl::memcpy
int memcpy(v_iface< T1, N1, V1 > &dst, v_iface< T2, N2, V2 > const &src)
Copy data from src, whose length must be same as that of dst.
Definition: v-iface.hpp:576