GADGET-4
cxxsort.h
Go to the documentation of this file.
1/*******************************************************************************
2 * \copyright This file is part of the GADGET4 N-body/SPH code developed
3 * \copyright by Volker Springel. Copyright (C) 2014-2020 by Volker Springel
4 * \copyright (vspringel@mpa-garching.mpg.de) and all contributing authors.
5 *******************************************************************************/
6
11#ifndef GADGET4_CXXSORT_H
12#define GADGET4_CXXSORT_H
13
14#include <algorithm>
15
16#include "../data/allvars.h"
17#include "../data/mymalloc.h"
18#include "../logs/logs.h"
19
20template <typename T, typename Tcomp>
21void mycxxsort_internal_serial(T *begin, T *end, T *buf, bool res_into_buf, Tcomp comp)
22{
23 std::size_t n = end - begin;
24 if(n <= 1)
25 {
26 if((n == 1) && res_into_buf)
27 *buf = *begin;
28 return;
29 }
30
31 mycxxsort_internal_serial(begin, begin + n / 2, buf, !res_into_buf, comp);
32 mycxxsort_internal_serial(begin + n / 2, end, buf + n / 2, !res_into_buf, comp);
33
34 res_into_buf ? std::merge(begin, begin + n / 2, begin + n / 2, begin + n, buf, comp)
35 : std::merge(buf, buf + n / 2, buf + n / 2, buf + n, begin, comp);
36}
37
38template <typename T, typename Tcomp>
39double mycxxsort(T *begin, T *end, Tcomp comp)
40{
41 if(end - begin <= 1)
42 return 0.;
43
44 double t0 = Logs.second();
45
46 T *buf = (T *)Mem.mymalloc("buf", (end - begin) * sizeof(T));
47
48 mycxxsort_internal_serial(begin, end, buf, false, comp);
49
50 Mem.myfree(buf);
51
52 return Logs.timediff(t0, Logs.second());
53}
54
55#endif
double timediff(double t0, double t1)
Definition: logs.cc:488
double second(void)
Definition: logs.cc:471
double mycxxsort(T *begin, T *end, Tcomp comp)
Definition: cxxsort.h:39
void mycxxsort_internal_serial(T *begin, T *end, T *buf, bool res_into_buf, Tcomp comp)
Definition: cxxsort.h:21
logs Logs
Definition: main.cc:43
memory Mem
Definition: main.cc:44