|
| 1 | +/*********************************************************************** |
| 2 | + * Copyright (c) 2021 Russell O'Connor, Jonas Nick * |
| 3 | + * Distributed under the MIT software license, see the accompanying * |
| 4 | + * file COPYING or https://www.opensource.org/licenses/mit-license.php.* |
| 5 | + ***********************************************************************/ |
| 6 | + |
| 7 | +#ifndef SECP256K1_HSORT_IMPL_H |
| 8 | +#define SECP256K1_HSORT_IMPL_H |
| 9 | + |
| 10 | +#include "hsort.h" |
| 11 | + |
| 12 | +/* An array is a heap when, for all non-zero indexes i, the element at index i |
| 13 | + * compares as less than or equal to the element at index parent(i) = (i-1)/2. |
| 14 | + */ |
| 15 | + |
| 16 | +static SECP256K1_INLINE size_t secp256k1_heap_child1(size_t i) { |
| 17 | + VERIFY_CHECK(i <= (SIZE_MAX - 1)/2); |
| 18 | + return 2*i + 1; |
| 19 | +} |
| 20 | + |
| 21 | +static SECP256K1_INLINE size_t secp256k1_heap_child2(size_t i) { |
| 22 | + VERIFY_CHECK(i <= SIZE_MAX/2 - 1); |
| 23 | + return secp256k1_heap_child1(i)+1; |
| 24 | +} |
| 25 | + |
| 26 | +static SECP256K1_INLINE void secp256k1_heap_swap64(unsigned char *a, unsigned char *b, size_t len) { |
| 27 | + unsigned char tmp[64]; |
| 28 | + VERIFY_CHECK(len <= 64); |
| 29 | + memcpy(tmp, a, len); |
| 30 | + memmove(a, b, len); |
| 31 | + memcpy(b, tmp, len); |
| 32 | +} |
| 33 | + |
| 34 | +static SECP256K1_INLINE void secp256k1_heap_swap(unsigned char *arr, size_t i, size_t j, size_t stride) { |
| 35 | + unsigned char *a = arr + i*stride; |
| 36 | + unsigned char *b = arr + j*stride; |
| 37 | + size_t len = stride; |
| 38 | + while (64 < len) { |
| 39 | + secp256k1_heap_swap64(a + (len - 64), b + (len - 64), 64); |
| 40 | + len -= 64; |
| 41 | + } |
| 42 | + secp256k1_heap_swap64(a, b, len); |
| 43 | +} |
| 44 | + |
| 45 | +/* This function accepts an array arr containing heap_size elements, each of |
| 46 | + * size stride. The elements in the array at indices >i satisfy the max-heap |
| 47 | + * property, i.e., for any element at index j (where j > i), all of its children |
| 48 | + * are smaller than the element itself. The purpose of the function is to update |
| 49 | + * the array so that all elements at indices >=i satisfy the max-heap |
| 50 | + * property. */ |
| 51 | +static SECP256K1_INLINE void secp256k1_heap_down(unsigned char *arr, size_t i, size_t heap_size, size_t stride, |
| 52 | + int (*cmp)(const void *, const void *, void *), void *cmp_data) { |
| 53 | + while (i < heap_size/2) { |
| 54 | + VERIFY_CHECK(i <= SIZE_MAX/2 - 1); |
| 55 | + /* Proof: |
| 56 | + * i < heap_size/2 |
| 57 | + * i + 1 <= heap_size/2 |
| 58 | + * 2*i + 2 <= heap_size <= SIZE_MAX |
| 59 | + * 2*i <= SIZE_MAX - 2 |
| 60 | + */ |
| 61 | + |
| 62 | + VERIFY_CHECK(secp256k1_heap_child1(i) < heap_size); |
| 63 | + /* Proof: |
| 64 | + * i < heap_size/2 |
| 65 | + * i + 1 <= heap_size/2 |
| 66 | + * 2*i + 2 <= heap_size |
| 67 | + * 2*i + 1 < heap_size |
| 68 | + * child1(i) < heap_size |
| 69 | + */ |
| 70 | + |
| 71 | + /* Let [x] be notation for the contents at arr[x*stride]. |
| 72 | + * |
| 73 | + * If [child1(i)] > [i] and [child2(i)] > [i], |
| 74 | + * swap [i] with the larger child to ensure the new parent is larger |
| 75 | + * than both children. When [child1(i)] == [child2(i)], swap [i] with |
| 76 | + * [child2(i)]. |
| 77 | + * Else if [child1(i)] > [i], swap [i] with [child1(i)]. |
| 78 | + * Else if [child2(i)] > [i], swap [i] with [child2(i)]. |
| 79 | + */ |
| 80 | + if (secp256k1_heap_child2(i) < heap_size |
| 81 | + && 0 <= cmp(arr + secp256k1_heap_child2(i)*stride, arr + secp256k1_heap_child1(i)*stride, cmp_data)) { |
| 82 | + if (0 < cmp(arr + secp256k1_heap_child2(i)*stride, arr + i*stride, cmp_data)) { |
| 83 | + secp256k1_heap_swap(arr, i, secp256k1_heap_child2(i), stride); |
| 84 | + i = secp256k1_heap_child2(i); |
| 85 | + } else { |
| 86 | + /* At this point we have [child2(i)] >= [child1(i)] and we have |
| 87 | + * [child2(i)] <= [i], and thus [child1(i)] <= [i] which means |
| 88 | + * that the next comparison can be skipped. */ |
| 89 | + return; |
| 90 | + } |
| 91 | + } else if (0 < cmp(arr + secp256k1_heap_child1(i)*stride, arr + i*stride, cmp_data)) { |
| 92 | + secp256k1_heap_swap(arr, i, secp256k1_heap_child1(i), stride); |
| 93 | + i = secp256k1_heap_child1(i); |
| 94 | + } else { |
| 95 | + return; |
| 96 | + } |
| 97 | + } |
| 98 | + /* heap_size/2 <= i |
| 99 | + * heap_size/2 < i + 1 |
| 100 | + * heap_size < 2*i + 2 |
| 101 | + * heap_size <= 2*i + 1 |
| 102 | + * heap_size <= child1(i) |
| 103 | + * Thus child1(i) and child2(i) are now out of bounds and we are at a leaf. |
| 104 | + */ |
| 105 | +} |
| 106 | + |
| 107 | +/* In-place heap sort. */ |
| 108 | +static void secp256k1_hsort(void *ptr, size_t count, size_t size, |
| 109 | + int (*cmp)(const void *, const void *, void *), |
| 110 | + void *cmp_data) { |
| 111 | + size_t i; |
| 112 | + |
| 113 | + for (i = count/2; 0 < i; --i) { |
| 114 | + secp256k1_heap_down(ptr, i-1, count, size, cmp, cmp_data); |
| 115 | + } |
| 116 | + for (i = count; 1 < i; --i) { |
| 117 | + /* Extract the largest value from the heap */ |
| 118 | + secp256k1_heap_swap(ptr, 0, i-1, size); |
| 119 | + |
| 120 | + /* Repair the heap condition */ |
| 121 | + secp256k1_heap_down(ptr, 0, i-1, size, cmp, cmp_data); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +#endif |
0 commit comments