tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
integer_comparisons.hpp
1#ifndef TATAMI_SAFE_NON_NEGATIVE_EQUAL_HPP
2#define TATAMI_SAFE_NON_NEGATIVE_EQUAL_HPP
3
4#include <type_traits>
5#include <limits>
6
7#include "sanisizer/sanisizer.hpp"
8
9namespace tatami {
10
14template<typename Left_, typename Right_> // provided only for back-compatibility.
15bool safe_non_negative_equal(Left_ l, Right_ r) {
16 return l >= 0 && r >= 0 && sanisizer::is_equal(l, r);
17}
18
19template<typename Container_, typename Index_>
20Index_ can_cast_Index_to_container_size(Index_ x) {
21 // If the Index_ type is larger than size_t, we cast it to size_t to provide can_cast() with some opportunities for compile-time optimization.
22 // This is because we 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.
23 // By casting away larger types, we allow can_cast() to eliminate the run-time overflow checks completely.
24 typedef typename std::conditional<std::numeric_limits<std::size_t>::max() < std::numeric_limits<Index_>::max(), std::size_t, Index_>::type Intermediate;
25 sanisizer::can_cast<decltype(std::declval<Container_>().size())>(static_cast<Intermediate>(x));
26 return x;
27}
28
29template<typename Container_, typename Index_>
30decltype(std::declval<Container_>().size()) cast_Index_to_container_size(Index_ x) {
31 return can_cast_Index_to_container_size<Container_>(x);
32}
33
34template<typename Container_, typename Index_>
35Container_ create_container_of_Index_size(Index_ x) {
36 // Same logic as described above.
37 typedef typename std::conditional<std::numeric_limits<std::size_t>::max() < std::numeric_limits<Index_>::max(), std::size_t, Index_>::type Intermediate;
38 return sanisizer::create<Container_>(static_cast<Intermediate>(x));
39}
40
41template<typename Container_, typename Index_>
42void resize_container_to_Index_size(Container_& container, Index_ x) {
43 container.resize(cast_Index_to_container_size<Container_>(x));
44}
45
46
51}
52
53#endif
Flexible representations for matrix data.
Definition Extractor.hpp:15