Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
alg_primitives.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
20#include <utility>
21
22namespace shambase {
23
34 template<class T, class Comp>
35 inline void ptr_insert_sort(T *data, u32 start, u32 end, Comp &&comp) {
36 for (u32 i = start + 1; i < end; ++i) {
37 auto key = data[i];
38 u32 j = i;
39 while (j > start && comp(key, data[j - 1])) {
40 data[j] = data[j - 1];
41 --j;
42 }
43 data[j] = key;
44 }
45 };
46
47 template<int I, int ArrSize>
49 template<typename K, typename Comp>
50 inline static void Sort(K *keys, const u8 *segment_boundary, Comp comp) {
51#pragma unroll
52 for (int i = 1 & I; i < ArrSize - 1; i += 2)
53 if (!segment_boundary[i] && comp(keys[i + 1], keys[i])) {
54 std::swap(keys[i], keys[i + 1]);
55 }
57 }
58 };
59
60 template<int I>
62 template<typename K, typename Comp>
63 inline static void Sort(K *keys, const u8 *segment_boundary, Comp comp) {}
64 };
65
78 template<class T, int ArrSize, class Comp>
83
84} // namespace shambase
std::uint8_t u8
8 bit unsigned integer
std::uint32_t u32
32 bit unsigned integer
namespace for basic c++ utilities
void odd_even_transpose_sort_segment_flags(T *data, const u8 *segment_boundary, Comp comp)
Odd-even transpose sort with segment boundaries.
void throw_with_loc(std::string message, SourceLocation loc=SourceLocation{})
Throw an exception and append the source location to it.
void ptr_insert_sort(T *data, u32 start, u32 end, Comp &&comp)
Simple insertion sort on pointer range.