<T>LAPACK 0.1.1
C++ Template Linear Algebra PACKage
Loading...
Searching...
No Matches
stdvector.hpp
Go to the documentation of this file.
1
3//
4// Copyright (c) 2021-2023, University of Colorado Denver. All rights reserved.
5//
6// This file is part of <T>LAPACK.
7// <T>LAPACK is free software: you can redistribute it and/or modify it under
8// the terms of the BSD 3-Clause license. See the accompanying LICENSE file.
9
10#ifndef TLAPACK_STDVECTOR_HH
11#define TLAPACK_STDVECTOR_HH
12
13#include <vector>
14
15#if __cplusplus >= 202002L
16 #include <span>
17#else
18namespace std {
19template <typename T>
20class span {
21 T* ptr_;
22 std::size_t len_;
23
24 public:
25 template <class It>
26 constexpr span(It first, std::size_t count) : ptr_(&(*first)), len_(count)
27 {}
28 constexpr T& operator[](std::size_t idx) const { return ptr_[idx]; }
29
30 constexpr std::size_t size() const noexcept { return len_; }
31 constexpr T* data() const noexcept { return ptr_; }
32};
33} // namespace std
34#endif
35
37
38namespace tlapack {
39
40namespace traits {
41 namespace internal {
42 template <typename>
43 struct is_std_vector : std::false_type {};
44
45 template <typename T, typename A>
46 struct is_std_vector<std::vector<T, A>> : std::true_type {};
47
48 template <typename T>
49 struct is_std_vector<std::span<T>> : std::true_type {};
50 } // namespace internal
51
52 template <typename T>
53 inline constexpr bool is_stdvector_type = internal::is_std_vector<T>::value;
54} // namespace traits
55
56// -----------------------------------------------------------------------------
57// blas functions to access std::vector properties
58
59// Size
60template <class T, class Allocator>
61constexpr auto size(const std::vector<T, Allocator>& x) noexcept
62{
63 return x.size();
64}
65template <class T>
66constexpr auto size(const std::span<T>& x) noexcept
67{
68 return x.size();
69}
70
71// -----------------------------------------------------------------------------
72// blas functions to access std::vector block operations
73
74// slice
75template <class vec_t,
76 class SliceSpec,
77 std::enable_if_t<traits::is_stdvector_type<vec_t>, int> = 0>
78constexpr auto slice(const vec_t& v, SliceSpec&& rows) noexcept
79{
80 assert((rows.first >= 0 && (std::size_t)rows.first < size(v)) ||
81 rows.first == rows.second);
82 assert(rows.second >= 0 && (std::size_t)rows.second <= size(v));
83 assert(rows.first <= rows.second);
84
85 using T = type_t<vec_t>;
86 return std::span<T>((T*)v.data() + rows.first, rows.second - rows.first);
87}
88
89} // namespace tlapack
90
91#endif // TLAPACK_STDVECTOR_HH
typename traits::real_type_traits< Types..., int >::type real_type
The common real type of the list of types.
Definition scalar_type_traits.hpp:113
Definition stdvector.hpp:43