SimiLie
Loading...
Searching...
No Matches
portable_stl.hpp
1// SPDX-FileCopyrightText: 2024 Baptiste Legouix
2// SPDX-License-Identifier: MIT
3
4#pragma once
5
6#include <Kokkos_Core.hpp>
7
8namespace sil {
9
10namespace misc {
11
12namespace detail {
13
14// Not sure why KOKKOS_FUNCTION is required despite constexpr
15template <class InputIt, class T = typename std::iterator_traits<InputIt>::value_type>
16KOKKOS_FUNCTION constexpr InputIt find(InputIt first, InputIt last, const T& value)
17{
18 for (; first != last; ++first)
19 if (*first == value)
20 return first;
21
22 return last;
23}
24
25/*
26template <class InputIt, class UnaryPred>
27KOKKOS_FUNCTION constexpr InputIt find_if(InputIt first, InputIt last, UnaryPred p)
28{
29 for (; first != last; ++first)
30 if (p(*first))
31 return first;
32
33 return last;
34}
35
36template <class InputIt, class UnaryPred>
37KOKKOS_FUNCTION constexpr InputIt find_if_not(InputIt first, InputIt last, UnaryPred q)
38{
39 for (; first != last; ++first)
40 if (!q(*first))
41 return first;
42
43 return last;
44}
45
46template <class InputIt, class UnaryPred>
47KOKKOS_FUNCTION constexpr bool all_of(InputIt first, InputIt last, UnaryPred p)
48{
49 return find_if_not(first, last, p) == last;
50}
51*/
52
53template <class T>
54constexpr std::remove_reference_t<T>&& move(T&& t) noexcept
55{
56 return static_cast<typename std::remove_reference<T>::type&&>(t);
57}
58
59template <class InputIt, class OutputIt>
60KOKKOS_FUNCTION OutputIt move(InputIt first, InputIt last, OutputIt d_first)
61{
62 for (; first != last; ++d_first, ++first)
63 *d_first = move(*first);
64
65 return d_first;
66}
67
68template <class I>
69KOKKOS_FUNCTION constexpr std::size_t bounded_advance(I& i, std::size_t n, I const bound)
70{
71 for (; n > 0 && i != bound; --n, void(++i)) {
72 ;
73 }
74
75 return n;
76}
77
78template <class ForwardIt>
79KOKKOS_FUNCTION ForwardIt shift_left(ForwardIt first, ForwardIt last, std::size_t n)
80{
81 if (n <= 0) {
82 return last;
83 }
84
85 auto mid = first;
86 if (bounded_advance(mid, n, last)) {
87 return first;
88 }
89
90 return move(move(mid), move(last), move(first));
91}
92
93template <class InputIt, class OutputIt>
94constexpr OutputIt copy(InputIt first, InputIt last, OutputIt d_first)
95{
96 for (; first != last; (void)++first, (void)++d_first)
97 *d_first = *first;
98
99 return d_first;
100}
101
102
103template <typename It, typename Compare = std::less<>>
104constexpr void sort(It begin, It end, Compare comp = Compare())
105{
106 for (It i = begin; i != end; ++i) {
107 for (It j = begin; j < end - 1; ++j) {
108 if (comp(*(j + 1), *j)) {
109 Kokkos::kokkos_swap(*j, *(j + 1));
110 }
111 }
112 }
113}
114
115} // namespace detail
116
117} // namespace misc
118
119} // namespace sil
The top-level namespace of SimiLie.
Definition csr.hpp:14