Debian Codegraph @

samtools/splaysort.h

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