ACTS
Experiment-independent tracking
Loading...
Searching...
No Matches
sort.hpp
1// This file is part of the ACTS project.
2//
3// Copyright (C) 2016 CERN for the benefit of the ACTS project
4//
5// This Source Code Form is subject to the terms of the Mozilla Public
6// License, v. 2.0. If a copy of the MPL was not distributed with this
7// file, You can obtain one at https://mozilla.org/MPL/2.0/.
8
9#pragma once
10
11// Project include(s).
12#include "detray/definitions/detail/qualifiers.hpp"
13#include "detray/utils/find_bound.hpp"
14#include "detray/utils/ranges/detail/iterator_functions.hpp"
15
16// System include(s).
17#include <algorithm>
18#include <functional>
19
20namespace detray::detail {
21
22template <std::random_access_iterator RandomIt>
23DETRAY_HOST_DEVICE inline void insertion_sort(RandomIt first, RandomIt last) {
24 for (auto it = first; it != last; it++) {
25 // Searching the upper bound, i.e., first
26 // element greater than *it from beginning
27 auto const insertion_point = detray::detail::upper_bound(first, it, *it);
28
29 // Shifting the unsorted part
30 std::ranges::rotate(insertion_point, it, it + 1);
31 }
32}
33
34// Function to sort the array
35template <template <typename...> class vector_t, typename TYPE>
36DETRAY_HOST_DEVICE inline void insertion_sort(vector_t<TYPE> &vec) {
37 detray::detail::insertion_sort(vec.begin(), vec.end());
38}
39
40template <std::random_access_iterator RandomIt, class Comp = std::less<void>>
41DETRAY_HOST_DEVICE inline void selection_sort(RandomIt first, RandomIt last,
42 Comp &&comp = Comp()) {
43 for (RandomIt i = first; i < (last - 1); ++i) {
44 RandomIt k = i;
45
46 for (RandomIt j = i + 1; j < last; ++j) {
47 if (comp(*j, *k)) {
48 k = j;
49 }
50 }
51
52 if (k != i) {
53 auto t = *i;
54 *i = *k;
55 *k = t;
56 }
57 }
58}
59
60// Function to sort the array
61template <template <typename...> class vector_t, typename TYPE>
62DETRAY_HOST_DEVICE inline void selection_sort(vector_t<TYPE> &vec) {
63 detray::detail::selection_sort(vec.begin(), vec.end());
64}
65
66} // namespace detray::detail
@ last
Use the last track state to reach target surface.
Definition TrackHelpers.hpp:41