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