1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
|
/* The MIT License
Copyright (c) 2021 Genome Research Ltd (GRL).
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SPLAYSORT_H
#define SPLAYSORT_H
#define SPLAYSORT_INIT(name, type_t, __sort_lt) \
typedef struct splaynode_##name { \
type_t value; \
struct splaynode_##name *left; \
struct splaynode_##name *right; \
struct splaynode_##name *parent; \
} splaynode_##name; \
\
void rotate_left_##name(splaynode_##name *node); \
void rotate_right_##name(splaynode_##name *node); \
int splay_sort_##name(size_t n, type_t array[] ); \
int splay_flatten_##name(splaynode_##name *node, type_t dest[], size_t n);\
splaynode_##name *splay_tree_##name(splaynode_##name *node); \
splaynode_##name *splay_insert_##name(splaynode_##name *node, \
type_t value, \
splaynode_##name *node_ptr); \
\
void rotate_left_##name(splaynode_##name *node) { \
splaynode_##name *parent = node->parent; \
splaynode_##name *grandparent = parent->parent; \
parent->right = node->left; \
if (node->left != NULL) { \
node->left->parent = parent; \
} \
node->left = parent; \
parent->parent = node; \
node->parent = grandparent; \
\
if (grandparent != NULL) { \
if (grandparent->left == parent) { \
grandparent->left = node; \
} else { \
grandparent->right = node; \
} \
} \
} \
\
void rotate_right_##name(splaynode_##name *node) { \
splaynode_##name *parent = node->parent; \
splaynode_##name *grandparent = parent->parent; \
parent->left = node->right; \
\
if (node->right != NULL) { \
node->right->parent = parent; \
} \
node->right = parent; \
parent->parent = node; \
node->parent = grandparent; \
\
if (grandparent != NULL) { \
if (grandparent->left == parent) { \
grandparent->left = node; \
} else { \
grandparent->right = node; \
} \
} \
} \
int splay_sort_##name(size_t n, type_t array[] ) { \
if (n < 1) { \
return 0; \
} \
int i; \
splaynode_##name *node_pool = malloc(sizeof(splaynode_##name) * n); \
if (node_pool == NULL) return -1; \
splaynode_##name *head = node_pool; \
head->value = array[0]; \
head->left = NULL; head->right = NULL; head->parent = NULL; \
for (i = 1; i < n; i++) { \
head = splay_insert_##name(head, array[i], node_pool + i ); \
} \
\
if (splay_flatten_##name(head, array, n) == -1) { \
free(node_pool); \
return -1; \
} \
free(node_pool); \
return 0; \
} \
\
int splay_flatten_##name(splaynode_##name *head, type_t *dest, size_t n) {\
int sp = 0, i = 0; \
splaynode_##name *current = head; \
splaynode_##name **stack = malloc(sizeof(current)*n); \
if (stack == NULL) return -1; \
\
do { \
while (current != NULL && sp < n) { \
stack[sp++] = current; \
current = current->left; \
} \
if (sp != 0) { \
sp--; \
dest[i++] = stack[sp]->value; \
current = stack[sp]->right; \
} \
} while (!(current == NULL && sp == 0)); \
\
free(stack); \
return 0; \
} \
splaynode_##name *splay_insert_##name(splaynode_##name *head, \
type_t value, \
splaynode_##name *node_ptr) { \
splaynode_##name *parent = NULL; \
while (head != NULL) { \
parent = head; \
if (__sort_lt(value, head->value)) { \
head = head->left; \
} else { \
head = head->right; \
} \
} \
splaynode_##name *new_node = node_ptr; \
new_node->value = value; \
new_node->left = NULL; \
new_node->right = NULL; \
new_node->parent = parent; \
if (parent) { \
if (__sort_lt(value, parent->value)) { \
parent->left = new_node; \
} else { \
parent->right = new_node; \
} \
} \
new_node = splay_tree_##name(new_node); \
return new_node; \
} \
\
splaynode_##name *splay_tree_##name(splaynode_##name *node) { \
splaynode_##name *parent = node->parent; \
\
if (node->parent == NULL) { \
return node; \
} \
if (node == parent->left) { \
if (parent->parent == NULL) { \
/* zig */ \
rotate_right_##name(node); \
} else if (parent->parent->left == parent) { \
/* left zig zig */ \
rotate_right_##name(node); \
rotate_right_##name(node); \
} else { \
/* right left zig zag */ \
rotate_right_##name(node); \
rotate_left_##name(node); \
} \
} else { \
if (parent->parent == NULL) { \
/* zig */ \
rotate_left_##name(node); \
} else if (parent->parent->right == parent) { \
/* right zig zig */ \
rotate_left_##name(node); \
rotate_left_##name(node); \
} else { \
/* left right zig zag */ \
rotate_left_##name(node); \
rotate_right_##name(node); \
} \
} \
\
if (node->parent != NULL) { \
return splay_tree_##name(node); \
} \
return node; \
} \
#define splaysort(name, n, array) splay_sort_##name(n, array)
#endif
|