tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
Index_to_container.hpp
Go to the documentation of this file.
1#ifndef TATAMI_INDEX_TO_CONTAINER_HPP
2#define TATAMI_INDEX_TO_CONTAINER_HPP
3
4#include <type_traits>
5#include <limits>
6#include <cstddef>
7
8#include "sanisizer/sanisizer.hpp"
9
10#include "copy.hpp"
11
17namespace tatami {
18
22template<typename Left_, typename Right_> // provided only for back-compatibility.
23bool safe_non_negative_equal(const Left_ l, const Right_ r) {
24 return l >= 0 && r >= 0 && sanisizer::is_equal(l, r);
25}
26
27// By the time we get any kind of Index_ in a tatami context, it should represent a dimension extent, so we can assume that 'x' is non-negative.
28// We also know that all uses of Index_ must fit into a size_t in order for the Extractor::fetch calls to address the user-supplied arrays.
29// By casting away larger types, we allow sanisizer functions to eliminate the run-time overflow checks completely.
30template<typename Index_>
31auto attest_for_Index(Index_ x) {
32 return sanisizer::attest_gez(sanisizer::attest_max_by_type<std::size_t>(x));
33}
48template<typename Container_, typename Index_>
49Index_ can_cast_Index_to_container_size(const Index_ x) {
50 sanisizer::can_cast<I<decltype(std::declval<Container_>().size())> >(attest_for_Index(x));
51 return x;
52}
53
64template<typename Container_, typename Index_>
65I<decltype(std::declval<Container_>().size())> cast_Index_to_container_size(const Index_ x) {
67}
68
81template<typename Container_, typename Index_, typename ... Args_>
82Container_ create_container_of_Index_size(const Index_ x, Args_&& ... args) {
83 return sanisizer::create<Container_>(attest_for_Index(x), std::forward<Args_>(args)...);
84}
85
98template<typename Container_, typename Index_, typename ... Args_>
99void resize_container_to_Index_size(Container_& container, const Index_ x, Args_&& ... args) {
100 container.resize(cast_Index_to_container_size<Container_>(x), std::forward<Args_>(args)...);
101}
102
103}
104
105#endif
Copy data from one buffer to another.
Flexible representations for matrix data.
Definition Extractor.hpp:15
Index_ can_cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:49
void resize_container_to_Index_size(Container_ &container, const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:99
I< decltype(std::declval< Container_ >().size())> cast_Index_to_container_size(const Index_ x)
Definition Index_to_container.hpp:65
Container_ create_container_of_Index_size(const Index_ x, Args_ &&... args)
Definition Index_to_container.hpp:82