| 1 | /* |
| 2 | * Implement Heap sort -- direct and indirect sorting |
| 3 | * Based on descriptions in Sedgewick "Algorithms in C" |
| 4 | * |
| 5 | * Copyright (C) 1999 Thomas Walter |
| 6 | * |
| 7 | * 18 February 2000: Modified for GSL by Brian Gough |
| 8 | * |
| 9 | * This is free software; you can redistribute it and/or modify it |
| 10 | * under the terms of the GNU General Public License as published by the |
| 11 | * Free Software Foundation; either version 3, or (at your option) any |
| 12 | * later version. |
| 13 | * |
| 14 | * This source is distributed in the hope that it will be useful, but WITHOUT |
| 15 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 17 | * for more details. |
| 18 | */ |
| 19 | |
| 20 | static inline void FUNCTIONcxx . . $ `./templates_on.h:233:9`! (index, downheap) (size_tcxx . . $ size_t# * p, const BASEcxx . . $ `./templates_on.h:114:9`! * data, const size_tcxx . . $ size_t# stride, const size_tcxx . . $ size_t# N, size_tcxx . . $ size_t# k); |
| 21 | |
| 22 | static inline void |
| 23 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (index, downheap) (size_tcxx . . $ size_t# * p, const BASEcxx . . $ `./templates_on.h:114:9`! * data, const size_tcxx . . $ size_t# stride, const size_tcxx . . $ size_t# N, size_tcxx . . $ size_t# k) |
| 24 | { |
| 25 | const size_tcxx . . $ size_t# pki = p[k]; |
| 26 | |
| 27 | while (k <= N / 2) |
| 28 | { |
| 29 | size_tcxx . . $ size_t# j = 2 * k; |
| 30 | |
| 31 | if (j < N && data[p[j] * stride] < data[p[j + 1] * stride]) |
| 32 | { |
| 33 | j++; |
| 34 | } |
| 35 | |
| 36 | if (!(data[pki * stride] < data[p[j] * stride])) /* avoid infinite loop if nan */ |
| 37 | { |
| 38 | break; |
| 39 | } |
| 40 | |
| 41 | p[k] = p[j]; |
| 42 | |
| 43 | k = j; |
| 44 | } |
| 45 | |
| 46 | p[k] = pki; |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (gsl_sort, index) (size_tcxx . . $ size_t# * p, const BASEcxx . . $ `./templates_on.h:114:9`! * data, const size_tcxx . . $ size_t# stride, const size_tcxx . . $ size_t# n) |
| 51 | { |
| 52 | size_tcxx . . $ size_t# N; |
| 53 | size_tcxx . . $ size_t# i, k; |
| 54 | |
| 55 | if (n == 0) |
| 56 | { |
| 57 | return; /* No data to sort */ |
| 58 | } |
| 59 | |
| 60 | /* set permutation to identity */ |
| 61 | |
| 62 | for (i = 0 ; i < n ; i++) |
| 63 | { |
| 64 | p[i] = i ; |
| 65 | } |
| 66 | |
| 67 | /* We have n_data elements, last element is at 'n_data-1', first at |
| 68 | '0' Set N to the last element number. */ |
| 69 | |
| 70 | N = n - 1; |
| 71 | |
| 72 | k = N / 2; |
| 73 | k++; /* Compensate the first use of 'k--' */ |
| 74 | do |
| 75 | { |
| 76 | k--; |
| 77 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (index, downheap) (p, data, stride, N, k); |
| 78 | } |
| 79 | while (k > 0); |
| 80 | |
| 81 | while (N > 0) |
| 82 | { |
| 83 | /* first swap the elements */ |
| 84 | size_tcxx . . $ size_t# tmp = p[0]; |
| 85 | p[0] = p[N]; |
| 86 | p[N] = tmp; |
| 87 | |
| 88 | /* then process the heap */ |
| 89 | N--; |
| 90 | |
| 91 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (index, downheap) (p, data, stride, N, 0); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | int |
| 96 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (gsl_sort_vector, index) (gsl_permutationcxx . . $ gsl_permutation# * permutation, const TYPEcxx . . $ `./templates_on.h:234:9`! (gsl_vector) * v) |
| 97 | { |
| 98 | if (permutation->sizecxx . . $ gsl_permutation_struct#size. != v->sizecxx . . $ $anonymous_type_9a7b30797aee9e14_0#size.) |
| 99 | { |
| 100 | GSL_ERRORcxx . . $ `external/gsl/gsl_errno.h:104:9`! ("permutation and vector lengths are not equal", GSL_EBADLEN); |
| 101 | } |
| 102 | |
| 103 | FUNCTIONcxx . . $ `./templates_on.h:233:9`! (gsl_sort, index) (permutation->datacxx . . $ gsl_permutation_struct#data., v->datacxx . . $ $anonymous_type_9a7b30797aee9e14_0#data., v->stridecxx . . $ $anonymous_type_9a7b30797aee9e14_0#stride., v->sizecxx . . $ $anonymous_type_9a7b30797aee9e14_0#size.) ; |
| 104 | |
| 105 | return GSL_SUCCESScxx . . $ GSL_SUCCESS. ; |
| 106 | } |