@based/db 0.2.5 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/db.h +1 -1
  2. package/dist/lib/darwin_aarch64/include/selva/fields.h +6 -3
  3. package/dist/lib/darwin_aarch64/include/selva/types.h +2 -1
  4. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  5. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  6. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  7. package/dist/lib/darwin_aarch64/libnode-v25.node +0 -0
  8. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  9. package/dist/lib/linux_aarch64/include/cdefs.h +324 -0
  10. package/dist/lib/linux_aarch64/include/jemalloc.h +468 -0
  11. package/dist/lib/linux_aarch64/include/libdeflate.h +345 -0
  12. package/dist/lib/linux_aarch64/include/libdeflate_strings.h +35 -0
  13. package/dist/lib/linux_aarch64/include/linker_set.h +109 -0
  14. package/dist/lib/linux_aarch64/include/queue.h +627 -0
  15. package/dist/lib/linux_aarch64/include/selva/_export.h +7 -0
  16. package/dist/lib/linux_aarch64/include/selva/align.h +9 -0
  17. package/dist/lib/linux_aarch64/include/selva/backoff_timeout.h +29 -0
  18. package/dist/lib/linux_aarch64/include/selva/bitmap.h +95 -0
  19. package/dist/lib/linux_aarch64/include/selva/colvec.h +71 -0
  20. package/dist/lib/linux_aarch64/include/selva/crc32c.h +17 -0
  21. package/dist/lib/linux_aarch64/include/selva/ctime.h +135 -0
  22. package/dist/lib/linux_aarch64/include/selva/db.h +450 -0
  23. package/dist/lib/linux_aarch64/include/selva/endian.h +301 -0
  24. package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +27 -0
  25. package/dist/lib/linux_aarch64/include/selva/fast_memcmp.h +18 -0
  26. package/dist/lib/linux_aarch64/include/selva/fast_memmem.h +11 -0
  27. package/dist/lib/linux_aarch64/include/selva/fast_parsei.h +36 -0
  28. package/dist/lib/linux_aarch64/include/selva/fields.h +344 -0
  29. package/dist/lib/linux_aarch64/include/selva/gmtime.h +137 -0
  30. package/dist/lib/linux_aarch64/include/selva/hll.h +85 -0
  31. package/dist/lib/linux_aarch64/include/selva/lpf.h +28 -0
  32. package/dist/lib/linux_aarch64/include/selva/mblen.h +40 -0
  33. package/dist/lib/linux_aarch64/include/selva/membar.h +32 -0
  34. package/dist/lib/linux_aarch64/include/selva/node_id_set.h +43 -0
  35. package/dist/lib/linux_aarch64/include/selva/poptop.h +114 -0
  36. package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +49 -0
  37. package/dist/lib/linux_aarch64/include/selva/selva_lang.h +112 -0
  38. package/dist/lib/linux_aarch64/include/selva/selva_math.h +37 -0
  39. package/dist/lib/linux_aarch64/include/selva/selva_string.h +683 -0
  40. package/dist/lib/linux_aarch64/include/selva/sort.h +140 -0
  41. package/dist/lib/linux_aarch64/include/selva/strsearch.h +43 -0
  42. package/dist/lib/linux_aarch64/include/selva/thread.h +37 -0
  43. package/dist/lib/linux_aarch64/include/selva/timestamp.h +25 -0
  44. package/dist/lib/linux_aarch64/include/selva/types.h +116 -0
  45. package/dist/lib/linux_aarch64/include/selva/vector.h +35 -0
  46. package/dist/lib/linux_aarch64/include/selva_error.h +140 -0
  47. package/dist/lib/linux_aarch64/include/selva_lang_code.h +160 -0
  48. package/dist/lib/linux_aarch64/include/tree.h +852 -0
  49. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  50. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  51. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  52. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  53. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  54. package/dist/lib/linux_aarch64/libnode-v25.node +0 -0
  55. package/dist/lib/linux_aarch64/libselva.so +0 -0
  56. package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
  57. package/dist/lib/linux_x86_64/include/cdefs.h +324 -0
  58. package/dist/lib/linux_x86_64/include/jemalloc.h +468 -0
  59. package/dist/lib/linux_x86_64/include/libdeflate.h +345 -0
  60. package/dist/lib/linux_x86_64/include/libdeflate_strings.h +35 -0
  61. package/dist/lib/linux_x86_64/include/linker_set.h +109 -0
  62. package/dist/lib/linux_x86_64/include/queue.h +627 -0
  63. package/dist/lib/linux_x86_64/include/selva/_export.h +7 -0
  64. package/dist/lib/linux_x86_64/include/selva/align.h +9 -0
  65. package/dist/lib/linux_x86_64/include/selva/backoff_timeout.h +29 -0
  66. package/dist/lib/linux_x86_64/include/selva/bitmap.h +95 -0
  67. package/dist/lib/linux_x86_64/include/selva/colvec.h +71 -0
  68. package/dist/lib/linux_x86_64/include/selva/crc32c.h +17 -0
  69. package/dist/lib/linux_x86_64/include/selva/ctime.h +135 -0
  70. package/dist/lib/linux_x86_64/include/selva/db.h +450 -0
  71. package/dist/lib/linux_x86_64/include/selva/endian.h +301 -0
  72. package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +27 -0
  73. package/dist/lib/linux_x86_64/include/selva/fast_memcmp.h +18 -0
  74. package/dist/lib/linux_x86_64/include/selva/fast_memmem.h +11 -0
  75. package/dist/lib/linux_x86_64/include/selva/fast_parsei.h +36 -0
  76. package/dist/lib/linux_x86_64/include/selva/fields.h +344 -0
  77. package/dist/lib/linux_x86_64/include/selva/gmtime.h +137 -0
  78. package/dist/lib/linux_x86_64/include/selva/hll.h +85 -0
  79. package/dist/lib/linux_x86_64/include/selva/lpf.h +28 -0
  80. package/dist/lib/linux_x86_64/include/selva/mblen.h +40 -0
  81. package/dist/lib/linux_x86_64/include/selva/membar.h +32 -0
  82. package/dist/lib/linux_x86_64/include/selva/node_id_set.h +43 -0
  83. package/dist/lib/linux_x86_64/include/selva/poptop.h +114 -0
  84. package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +49 -0
  85. package/dist/lib/linux_x86_64/include/selva/selva_lang.h +112 -0
  86. package/dist/lib/linux_x86_64/include/selva/selva_math.h +37 -0
  87. package/dist/lib/linux_x86_64/include/selva/selva_string.h +683 -0
  88. package/dist/lib/linux_x86_64/include/selva/sort.h +140 -0
  89. package/dist/lib/linux_x86_64/include/selva/strsearch.h +43 -0
  90. package/dist/lib/linux_x86_64/include/selva/thread.h +37 -0
  91. package/dist/lib/linux_x86_64/include/selva/timestamp.h +25 -0
  92. package/dist/lib/linux_x86_64/include/selva/types.h +116 -0
  93. package/dist/lib/linux_x86_64/include/selva/vector.h +35 -0
  94. package/dist/lib/linux_x86_64/include/selva_error.h +140 -0
  95. package/dist/lib/linux_x86_64/include/selva_lang_code.h +160 -0
  96. package/dist/lib/linux_x86_64/include/tree.h +852 -0
  97. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  98. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  99. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  100. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  101. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  102. package/dist/lib/linux_x86_64/libnode-v25.node +0 -0
  103. package/dist/lib/linux_x86_64/libselva.so +0 -0
  104. package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
  105. package/dist/src/client/modify/props/fixed.js +1 -1
  106. package/dist/src/client/modify/types.d.ts +1 -0
  107. package/dist/src/client/modify/types.js +1 -0
  108. package/dist/src/client/query/BasedDbQuery.d.ts +1 -0
  109. package/dist/src/client/query/BasedDbQuery.js +14 -0
  110. package/dist/src/client/query/BasedQueryResponse.js +1 -1
  111. package/dist/src/client/query/filter/filter.js +26 -17
  112. package/dist/src/client/query/filter/toByteCode.js +18 -9
  113. package/dist/src/client/query/include/utils.d.ts +2 -2
  114. package/dist/src/client/query/include/utils.js +7 -6
  115. package/dist/src/client/query/include/walk.js +20 -2
  116. package/dist/src/client/query/queryDefToReadSchema.js +1 -1
  117. package/dist/src/client/query/subscription/index.js +6 -6
  118. package/dist/src/client/query/subscription/toByteCode.js +11 -6
  119. package/dist/src/client/query/types.d.ts +17 -1
  120. package/dist/src/client/query/types.js +23 -0
  121. package/dist/src/server/schemaSelvaBuffer.js +8 -11
  122. package/dist/src/server/subscription.js +0 -12
  123. package/package.json +7 -7
@@ -0,0 +1,852 @@
1
+ /* $NetBSD: tree.h,v 1.8 2004/03/28 19:38:30 provos Exp $ */
2
+ /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
3
+ /* $FreeBSD$ */
4
+
5
+ /*-
6
+ * SPDX-License-Identifier: BSD-2-Clause
7
+ *
8
+ * Copyright 2002 Niels Provos <provos@citi.umich.edu>
9
+ * Copyright (c) 2022-2025 SAULX
10
+ * All rights reserved.
11
+ *
12
+ * Redistribution and use in source and binary forms, with or without
13
+ * modification, are permitted provided that the following conditions
14
+ * are met:
15
+ * 1. Redistributions of source code must retain the above copyright
16
+ * notice, this list of conditions and the following disclaimer.
17
+ * 2. Redistributions in binary form must reproduce the above copyright
18
+ * notice, this list of conditions and the following disclaimer in the
19
+ * documentation and/or other materials provided with the distribution.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+ #ifndef _SYS_TREE_H_
34
+ #define _SYS_TREE_H_
35
+
36
+ #include <stdint.h>
37
+
38
+ /*
39
+ * This file defines data structures for different types of trees:
40
+ * splay trees and rank-balanced trees.
41
+ *
42
+ * A splay tree is a self-organizing data structure. Every operation
43
+ * on the tree causes a splay to happen. The splay moves the requested
44
+ * node to the root of the tree and partly rebalances it.
45
+ *
46
+ * This has the benefit that request locality causes faster lookups as
47
+ * the requested nodes move to the top of the tree. On the other hand,
48
+ * every lookup causes memory writes.
49
+ *
50
+ * The Balance Theorem bounds the total access time for m operations
51
+ * and n inserts on an initially empty tree as O((m + n)lg n). The
52
+ * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
53
+ *
54
+ * A rank-balanced tree is a binary search tree with an integer
55
+ * rank-difference as an attribute of each pointer from parent to child.
56
+ * The sum of the rank-differences on any path from a node down to null is
57
+ * the same, and defines the rank of that node. The rank of the null node
58
+ * is -1.
59
+ *
60
+ * Different additional conditions define different sorts of balanced
61
+ * trees, including "red-black" and "AVL" trees. The set of conditions
62
+ * applied here are the "weak-AVL" conditions of Haeupler, Sen and Tarjan:
63
+ * - every rank-difference is 1 or 2.
64
+ * - the rank of any leaf is 1.
65
+ *
66
+ * For historical reasons, rank differences that are even are associated
67
+ * with the color red (Rank-Even-Difference), and the child that a red edge
68
+ * points to is called a red child.
69
+ *
70
+ * Every operation on a rank-balanced tree is bounded as O(lg n).
71
+ * The maximum height of a rank-balanced tree is 2lg (n+1).
72
+ */
73
+
74
+ #define SPLAY_HEAD(name, type) \
75
+ struct name { \
76
+ struct type *sph_root; /* root of the tree */ \
77
+ }
78
+
79
+ #define SPLAY_INITIALIZER(root) \
80
+ { nullptr }
81
+
82
+ #define SPLAY_INIT(root) do { \
83
+ (root)->sph_root = nullptr; \
84
+ } while (/*CONSTCOND*/ 0)
85
+
86
+ #define SPLAY_ENTRY(type) \
87
+ struct { \
88
+ struct type *spe_left; /* left element */ \
89
+ struct type *spe_right; /* right element */ \
90
+ }
91
+
92
+ #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
93
+ #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
94
+ #define SPLAY_ROOT(head) (head)->sph_root
95
+ #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == nullptr)
96
+
97
+ /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
98
+ #define SPLAY_ROTATE_RIGHT(head, tmp, field) do { \
99
+ SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
100
+ SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
101
+ (head)->sph_root = tmp; \
102
+ } while (/*CONSTCOND*/ 0)
103
+
104
+ #define SPLAY_ROTATE_LEFT(head, tmp, field) do { \
105
+ SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
106
+ SPLAY_LEFT(tmp, field) = (head)->sph_root; \
107
+ (head)->sph_root = tmp; \
108
+ } while (/*CONSTCOND*/ 0)
109
+
110
+ #define SPLAY_LINKLEFT(head, tmp, field) do { \
111
+ SPLAY_LEFT(tmp, field) = (head)->sph_root; \
112
+ tmp = (head)->sph_root; \
113
+ (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
114
+ } while (/*CONSTCOND*/ 0)
115
+
116
+ #define SPLAY_LINKRIGHT(head, tmp, field) do { \
117
+ SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
118
+ tmp = (head)->sph_root; \
119
+ (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
120
+ } while (/*CONSTCOND*/ 0)
121
+
122
+ #define SPLAY_ASSEMBLE(head, node, left, right, field) do { \
123
+ SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
124
+ SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field);\
125
+ SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
126
+ SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
127
+ } while (/*CONSTCOND*/ 0)
128
+
129
+ /* Generates prototypes and inline functions */
130
+
131
+ #define SPLAY_PROTOTYPE(name, type, field, cmp) \
132
+ void name##_SPLAY(struct name *, struct type *); \
133
+ void name##_SPLAY_MINMAX(struct name *, int); \
134
+ struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
135
+ struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
136
+ \
137
+ /* Finds the node with the same key as elm */ \
138
+ static __unused __inline struct type * \
139
+ name##_SPLAY_FIND(struct name *head, struct type *elm) \
140
+ { \
141
+ if (SPLAY_EMPTY(head)) \
142
+ return nullptr; \
143
+ name##_SPLAY(head, elm); \
144
+ if ((cmp)(elm, (head)->sph_root) == 0) \
145
+ return (head->sph_root); \
146
+ return nullptr; \
147
+ } \
148
+ \
149
+ static __unused __inline struct type * \
150
+ name##_SPLAY_NEXT(struct name *head, struct type *elm) \
151
+ { \
152
+ name##_SPLAY(head, elm); \
153
+ if (SPLAY_RIGHT(elm, field) != nullptr) { \
154
+ elm = SPLAY_RIGHT(elm, field); \
155
+ while (SPLAY_LEFT(elm, field) != nullptr) { \
156
+ elm = SPLAY_LEFT(elm, field); \
157
+ } \
158
+ } else \
159
+ elm = nullptr; \
160
+ return (elm); \
161
+ } \
162
+ \
163
+ static __unused __inline struct type * \
164
+ name##_SPLAY_MIN_MAX(struct name *head, int val) \
165
+ { \
166
+ name##_SPLAY_MINMAX(head, val); \
167
+ return (SPLAY_ROOT(head)); \
168
+ }
169
+
170
+ /* Main splay operation.
171
+ * Moves node close to the key of elm to top
172
+ */
173
+ #define SPLAY_GENERATE(name, type, field, cmp) \
174
+ struct type * \
175
+ name##_SPLAY_INSERT(struct name *head, struct type *elm) \
176
+ { \
177
+ if (SPLAY_EMPTY(head)) { \
178
+ SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = nullptr; \
179
+ } else { \
180
+ __typeof(cmp((void *)1, (void *)1)) __comp; \
181
+ name##_SPLAY(head, elm); \
182
+ __comp = (cmp)(elm, (head)->sph_root); \
183
+ if (__comp < 0) { \
184
+ SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field);\
185
+ SPLAY_RIGHT(elm, field) = (head)->sph_root; \
186
+ SPLAY_LEFT((head)->sph_root, field) = nullptr; \
187
+ } else if (__comp > 0) { \
188
+ SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field);\
189
+ SPLAY_LEFT(elm, field) = (head)->sph_root; \
190
+ SPLAY_RIGHT((head)->sph_root, field) = nullptr; \
191
+ } else \
192
+ return ((head)->sph_root); \
193
+ } \
194
+ (head)->sph_root = (elm); \
195
+ return nullptr; \
196
+ } \
197
+ \
198
+ struct type * \
199
+ name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
200
+ { \
201
+ struct type *__tmp; \
202
+ if (SPLAY_EMPTY(head)) \
203
+ return nullptr; \
204
+ name##_SPLAY(head, elm); \
205
+ if ((cmp)(elm, (head)->sph_root) == 0) { \
206
+ if (SPLAY_LEFT((head)->sph_root, field) == nullptr) { \
207
+ (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field);\
208
+ } else { \
209
+ __tmp = SPLAY_RIGHT((head)->sph_root, field); \
210
+ (head)->sph_root = SPLAY_LEFT((head)->sph_root, field);\
211
+ name##_SPLAY(head, elm); \
212
+ SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
213
+ } \
214
+ return (elm); \
215
+ } \
216
+ return nullptr; \
217
+ } \
218
+ \
219
+ void \
220
+ name##_SPLAY(struct name *head, struct type *elm) \
221
+ { \
222
+ struct type __node, *__left, *__right, *__tmp; \
223
+ __typeof(cmp((void *)1, (void *)1)) __comp; \
224
+ \
225
+ SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = nullptr;\
226
+ __left = __right = &__node; \
227
+ \
228
+ while ((__comp = (cmp)(elm, (head)->sph_root)) != 0) { \
229
+ if (__comp < 0) { \
230
+ __tmp = SPLAY_LEFT((head)->sph_root, field); \
231
+ if (__tmp == nullptr) \
232
+ break; \
233
+ if ((cmp)(elm, __tmp) < 0){ \
234
+ SPLAY_ROTATE_RIGHT(head, __tmp, field); \
235
+ if (SPLAY_LEFT((head)->sph_root, field) == nullptr)\
236
+ break; \
237
+ } \
238
+ SPLAY_LINKLEFT(head, __right, field); \
239
+ } else if (__comp > 0) { \
240
+ __tmp = SPLAY_RIGHT((head)->sph_root, field); \
241
+ if (__tmp == nullptr) \
242
+ break; \
243
+ if ((cmp)(elm, __tmp) > 0){ \
244
+ SPLAY_ROTATE_LEFT(head, __tmp, field); \
245
+ if (SPLAY_RIGHT((head)->sph_root, field) == nullptr)\
246
+ break; \
247
+ } \
248
+ SPLAY_LINKRIGHT(head, __left, field); \
249
+ } \
250
+ } \
251
+ SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
252
+ } \
253
+ \
254
+ /* Splay with either the minimum or the maximum element \
255
+ * Used to find minimum or maximum element in tree. \
256
+ */ \
257
+ void name##_SPLAY_MINMAX(struct name *head, int __comp) \
258
+ { \
259
+ struct type __node, *__left, *__right, *__tmp; \
260
+ \
261
+ SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = nullptr;\
262
+ __left = __right = &__node; \
263
+ \
264
+ while (1) { \
265
+ if (__comp < 0) { \
266
+ __tmp = SPLAY_LEFT((head)->sph_root, field); \
267
+ if (__tmp == nullptr) \
268
+ break; \
269
+ if (__comp < 0){ \
270
+ SPLAY_ROTATE_RIGHT(head, __tmp, field); \
271
+ if (SPLAY_LEFT((head)->sph_root, field) == nullptr)\
272
+ break; \
273
+ } \
274
+ SPLAY_LINKLEFT(head, __right, field); \
275
+ } else if (__comp > 0) { \
276
+ __tmp = SPLAY_RIGHT((head)->sph_root, field); \
277
+ if (__tmp == nullptr) \
278
+ break; \
279
+ if (__comp > 0) { \
280
+ SPLAY_ROTATE_LEFT(head, __tmp, field); \
281
+ if (SPLAY_RIGHT((head)->sph_root, field) == nullptr)\
282
+ break; \
283
+ } \
284
+ SPLAY_LINKRIGHT(head, __left, field); \
285
+ } \
286
+ } \
287
+ SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
288
+ }
289
+
290
+ #define SPLAY_NEGINF -1
291
+ #define SPLAY_INF 1
292
+
293
+ #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
294
+ #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
295
+ #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
296
+ #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
297
+ #define SPLAY_MIN(name, x) (SPLAY_EMPTY(x) ? nullptr \
298
+ : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
299
+ #define SPLAY_MAX(name, x) (SPLAY_EMPTY(x) ? nullptr \
300
+ : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
301
+
302
+ #define SPLAY_FOREACH(x, name, head) \
303
+ for ((x) = SPLAY_MIN(name, head); \
304
+ (x) != nullptr; \
305
+ (x) = SPLAY_NEXT(name, head, x))
306
+
307
+ /* Macros that define a rank-balanced tree */
308
+ #define RB_HEAD(name, type) \
309
+ struct name { \
310
+ struct type *rbh_root; /* root of the tree */ \
311
+ }
312
+
313
+ #define RB_INITIALIZER(root) \
314
+ { nullptr }
315
+
316
+ #define RB_INIT(root) do { \
317
+ (root)->rbh_root = nullptr; \
318
+ } while (/*CONSTCOND*/ 0)
319
+
320
+ #define RB_ENTRY(type) \
321
+ struct { \
322
+ struct type *rbe_left; /* left element */ \
323
+ struct type *rbe_right; /* right element */ \
324
+ union { \
325
+ struct type *rbe_parent; /* parent element */ \
326
+ uintptr_t rbe_parent_bits; \
327
+ }; \
328
+ }
329
+
330
+ #define RB_LEFT(elm, field) (elm)->field.rbe_left
331
+ #define RB_RIGHT(elm, field) (elm)->field.rbe_right
332
+
333
+ /*
334
+ * With the expectation that any object of struct type has an
335
+ * address that is a multiple of 4, and that therefore the
336
+ * 2 least significant bits of a pointer to struct type are
337
+ * always zero, this implementation sets those bits to indicate
338
+ * that the left or right child of the tree node is "red".
339
+ */
340
+ #define RB_UP(elm, field) (elm)->field.rbe_parent
341
+ #define RB_UP_BITS(elm, field) (elm)->field.rbe_parent_bits
342
+ #define RB_BITS(elm, field) (RB_UP_BITS(elm, field))
343
+ #define RB_RED_L ((uintptr_t)1)
344
+ #define RB_RED_R ((uintptr_t)2)
345
+ #define RB_RED_MASK ((uintptr_t)3)
346
+ #define RB_FLIP_LEFT(elm, field) (RB_BITS(elm, field) ^= RB_RED_L)
347
+ #define RB_FLIP_RIGHT(elm, field) (RB_BITS(elm, field) ^= RB_RED_R)
348
+ #define RB_RED_LEFT(elm, field) ((RB_BITS(elm, field) & RB_RED_L) != 0)
349
+ #define RB_RED_RIGHT(elm, field) ((RB_BITS(elm, field) & RB_RED_R) != 0)
350
+ #define RB_PARENT(elm, field) ((__typeof(RB_UP(elm, field))) \
351
+ (RB_BITS(elm, field) & ~RB_RED_MASK))
352
+ #define RB_ROOT(head) (head)->rbh_root
353
+ #define RB_EMPTY(head) (RB_ROOT(head) == nullptr)
354
+
355
+ #define RB_SET_PARENT(dst, src, field) do { \
356
+ RB_BITS(dst, field) &= RB_RED_MASK; \
357
+ RB_BITS(dst, field) |= (uintptr_t)src; \
358
+ } while (/*CONSTCOND*/ 0)
359
+
360
+ #define RB_SET(elm, parent, field) do { \
361
+ RB_UP(elm, field) = parent; \
362
+ RB_LEFT(elm, field) = RB_RIGHT(elm, field) = nullptr; \
363
+ } while (/*CONSTCOND*/ 0)
364
+
365
+ #define RB_COLOR(elm, field) (RB_PARENT(elm, field) == nullptr ? 0 : \
366
+ RB_LEFT(RB_PARENT(elm, field), field) == elm ? \
367
+ RB_RED_LEFT(RB_PARENT(elm, field), field) : \
368
+ RB_RED_RIGHT(RB_PARENT(elm, field), field))
369
+
370
+ /*
371
+ * Something to be invoked in a loop at the root of every modified subtree,
372
+ * from the bottom up to the root, to update augmented node data.
373
+ */
374
+ #ifndef RB_AUGMENT
375
+ #define RB_AUGMENT(x) break
376
+ #endif
377
+
378
+ #define RB_SWAP_CHILD(head, out, in, field) do { \
379
+ if (RB_PARENT(out, field) == nullptr) \
380
+ RB_ROOT(head) = (in); \
381
+ else if ((out) == RB_LEFT(RB_PARENT(out, field), field)) \
382
+ RB_LEFT(RB_PARENT(out, field), field) = (in); \
383
+ else \
384
+ RB_RIGHT(RB_PARENT(out, field), field) = (in); \
385
+ } while (/*CONSTCOND*/ 0)
386
+
387
+ #define RB_ROTATE_LEFT(head, elm, tmp, field) do { \
388
+ (tmp) = RB_RIGHT(elm, field); \
389
+ if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field)) != nullptr) { \
390
+ RB_SET_PARENT(RB_RIGHT(elm, field), elm, field); \
391
+ } \
392
+ RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
393
+ RB_SWAP_CHILD(head, elm, tmp, field); \
394
+ RB_LEFT(tmp, field) = (elm); \
395
+ RB_SET_PARENT(elm, tmp, field); \
396
+ RB_AUGMENT(elm); \
397
+ } while (/*CONSTCOND*/ 0)
398
+
399
+ #define RB_ROTATE_RIGHT(head, elm, tmp, field) do { \
400
+ (tmp) = RB_LEFT(elm, field); \
401
+ if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field)) != nullptr) { \
402
+ RB_SET_PARENT(RB_LEFT(elm, field), elm, field); \
403
+ } \
404
+ RB_SET_PARENT(tmp, RB_PARENT(elm, field), field); \
405
+ RB_SWAP_CHILD(head, elm, tmp, field); \
406
+ RB_RIGHT(tmp, field) = (elm); \
407
+ RB_SET_PARENT(elm, tmp, field); \
408
+ RB_AUGMENT(elm); \
409
+ } while (/*CONSTCOND*/ 0)
410
+
411
+ /* Generates prototypes and inline functions */
412
+ #define RB_PROTOTYPE(name, type, field, cmp) \
413
+ RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
414
+ #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
415
+ RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __unused static)
416
+ #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
417
+ RB_PROTOTYPE_INSERT_COLOR(name, type, attr); \
418
+ RB_PROTOTYPE_REMOVE_COLOR(name, type, attr); \
419
+ RB_PROTOTYPE_INSERT_FINISH(name, type, attr); \
420
+ RB_PROTOTYPE_INSERT(name, type, attr); \
421
+ RB_PROTOTYPE_INSERT_NEXT(name, type, attr); \
422
+ RB_PROTOTYPE_REMOVE(name, type, attr); \
423
+ RB_PROTOTYPE_FIND(name, type, attr); \
424
+ RB_PROTOTYPE_NFIND(name, type, attr); \
425
+ RB_PROTOTYPE_NEXT(name, type, attr); \
426
+ RB_PROTOTYPE_PREV(name, type, attr); \
427
+ RB_PROTOTYPE_MINMAX(name, type, attr); \
428
+ RB_PROTOTYPE_REINSERT(name, type, attr);
429
+ #define RB_PROTOTYPE_INSERT_COLOR(name, type, attr) \
430
+ attr void name##_RB_INSERT_COLOR(struct name *, struct type *, struct type *)
431
+ #define RB_PROTOTYPE_REMOVE_COLOR(name, type, attr) \
432
+ attr void name##_RB_REMOVE_COLOR(struct name *, \
433
+ struct type *, struct type *)
434
+ #define RB_PROTOTYPE_INSERT_FINISH(name, type, attr) \
435
+ attr struct type *name##_RB_INSERT_FINISH(struct name *, \
436
+ struct type *, struct type **, struct type *)
437
+ #define RB_PROTOTYPE_REMOVE(name, type, attr) \
438
+ attr struct type *name##_RB_REMOVE(struct name *, struct type *)
439
+ #define RB_PROTOTYPE_INSERT(name, type, attr) \
440
+ attr struct type *name##_RB_INSERT(struct name *, struct type *)
441
+ #define RB_PROTOTYPE_INSERT_NEXT(name, type, attr) \
442
+ attr void name##_RB_INSERT_NEXT(struct name *, struct type *, struct type *)
443
+ #define RB_PROTOTYPE_FIND(name, type, attr) \
444
+ attr struct type *name##_RB_FIND(struct name *, struct type *)
445
+ #define RB_PROTOTYPE_NFIND(name, type, attr) \
446
+ attr struct type *name##_RB_NFIND(struct name *, struct type *)
447
+ #define RB_PROTOTYPE_NEXT(name, type, attr) \
448
+ attr struct type *name##_RB_NEXT(struct type *)
449
+ #define RB_PROTOTYPE_PREV(name, type, attr) \
450
+ attr struct type *name##_RB_PREV(struct type *)
451
+ #define RB_PROTOTYPE_MINMAX(name, type, attr) \
452
+ attr struct type *name##_RB_MINMAX(struct name *, int)
453
+ #define RB_PROTOTYPE_REINSERT(name, type, attr) \
454
+ attr struct type *name##_RB_REINSERT(struct name *, struct type *)
455
+
456
+ /* Main rb operation.
457
+ * Moves node close to the key of elm to top
458
+ */
459
+ #define RB_GENERATE(name, type, field, cmp) \
460
+ RB_GENERATE_INTERNAL(name, type, field, cmp,)
461
+ #define RB_GENERATE_STATIC(name, type, field, cmp) \
462
+ RB_GENERATE_INTERNAL(name, type, field, cmp, __unused static)
463
+ #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
464
+ RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
465
+ RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
466
+ RB_GENERATE_INSERT_FINISH(name, type, field, attr) \
467
+ RB_GENERATE_INSERT(name, type, field, cmp, attr) \
468
+ RB_GENERATE_INSERT_NEXT(name, type, field, cmp, attr) \
469
+ RB_GENERATE_REMOVE(name, type, field, attr) \
470
+ RB_GENERATE_FIND(name, type, field, cmp, attr) \
471
+ RB_GENERATE_NFIND(name, type, field, cmp, attr) \
472
+ RB_GENERATE_NEXT(name, type, field, attr) \
473
+ RB_GENERATE_PREV(name, type, field, attr) \
474
+ RB_GENERATE_MINMAX(name, type, field, attr) \
475
+ RB_GENERATE_REINSERT(name, type, field, cmp, attr)
476
+
477
+
478
+ #define RB_GENERATE_INSERT_COLOR(name, type, field, attr) \
479
+ attr void \
480
+ name##_RB_INSERT_COLOR(struct name *head, struct type *parent, struct type *elm) \
481
+ { \
482
+ struct type *child; \
483
+ do { \
484
+ if (RB_LEFT(parent, field) == elm) { \
485
+ if (RB_RED_LEFT(parent, field)) { \
486
+ RB_FLIP_LEFT(parent, field); \
487
+ return; \
488
+ } \
489
+ RB_FLIP_RIGHT(parent, field); \
490
+ if (RB_RED_RIGHT(parent, field)) { \
491
+ elm = parent; \
492
+ continue; \
493
+ } \
494
+ if (!RB_RED_RIGHT(elm, field)) { \
495
+ RB_FLIP_LEFT(elm, field); \
496
+ RB_ROTATE_LEFT(head, elm, child, field);\
497
+ if (RB_RED_LEFT(child, field)) \
498
+ RB_FLIP_RIGHT(elm, field); \
499
+ else if (RB_RED_RIGHT(child, field)) \
500
+ RB_FLIP_LEFT(parent, field); \
501
+ elm = child; \
502
+ } \
503
+ RB_ROTATE_RIGHT(head, parent, elm, field); \
504
+ } else { \
505
+ if (RB_RED_RIGHT(parent, field)) { \
506
+ RB_FLIP_RIGHT(parent, field); \
507
+ return; \
508
+ } \
509
+ RB_FLIP_LEFT(parent, field); \
510
+ if (RB_RED_LEFT(parent, field)) { \
511
+ elm = parent; \
512
+ continue; \
513
+ } \
514
+ if (!RB_RED_LEFT(elm, field)) { \
515
+ RB_FLIP_RIGHT(elm, field); \
516
+ RB_ROTATE_RIGHT(head, elm, child, field);\
517
+ if (RB_RED_RIGHT(child, field)) \
518
+ RB_FLIP_LEFT(elm, field); \
519
+ else if (RB_RED_LEFT(child, field)) \
520
+ RB_FLIP_RIGHT(parent, field); \
521
+ elm = child; \
522
+ } \
523
+ RB_ROTATE_LEFT(head, parent, elm, field); \
524
+ } \
525
+ RB_BITS(elm, field) &= ~RB_RED_MASK; \
526
+ break; \
527
+ } while ((parent = RB_PARENT(elm, field)) != nullptr); \
528
+ }
529
+
530
+ #define RB_GENERATE_REMOVE_COLOR(name, type, field, attr) \
531
+ attr void \
532
+ name##_RB_REMOVE_COLOR(struct name *head, \
533
+ struct type *parent, struct type *elm) \
534
+ { \
535
+ struct type *sib; \
536
+ if (RB_LEFT(parent, field) == elm && \
537
+ RB_RIGHT(parent, field) == elm) { \
538
+ RB_BITS(parent, field) &= ~RB_RED_MASK; \
539
+ elm = parent; \
540
+ parent = RB_PARENT(elm, field); \
541
+ if (parent == nullptr) \
542
+ return; \
543
+ } \
544
+ do { \
545
+ if (RB_LEFT(parent, field) == elm) { \
546
+ if (!RB_RED_LEFT(parent, field)) { \
547
+ RB_FLIP_LEFT(parent, field); \
548
+ return; \
549
+ } \
550
+ if (RB_RED_RIGHT(parent, field)) { \
551
+ RB_FLIP_RIGHT(parent, field); \
552
+ elm = parent; \
553
+ continue; \
554
+ } \
555
+ sib = RB_RIGHT(parent, field); \
556
+ if ((~RB_BITS(sib, field) & RB_RED_MASK) == 0) {\
557
+ RB_BITS(sib, field) &= ~RB_RED_MASK; \
558
+ elm = parent; \
559
+ continue; \
560
+ } \
561
+ RB_FLIP_RIGHT(sib, field); \
562
+ if (RB_RED_LEFT(sib, field)) \
563
+ RB_FLIP_LEFT(parent, field); \
564
+ else if (!RB_RED_RIGHT(sib, field)) { \
565
+ RB_FLIP_LEFT(parent, field); \
566
+ RB_ROTATE_RIGHT(head, sib, elm, field); \
567
+ if (RB_RED_RIGHT(elm, field)) \
568
+ RB_FLIP_LEFT(sib, field); \
569
+ if (RB_RED_LEFT(elm, field)) \
570
+ RB_FLIP_RIGHT(parent, field); \
571
+ RB_BITS(elm, field) |= RB_RED_MASK; \
572
+ sib = elm; \
573
+ } \
574
+ RB_ROTATE_LEFT(head, parent, sib, field); \
575
+ } else { \
576
+ if (!RB_RED_RIGHT(parent, field)) { \
577
+ RB_FLIP_RIGHT(parent, field); \
578
+ return; \
579
+ } \
580
+ if (RB_RED_LEFT(parent, field)) { \
581
+ RB_FLIP_LEFT(parent, field); \
582
+ elm = parent; \
583
+ continue; \
584
+ } \
585
+ sib = RB_LEFT(parent, field); \
586
+ if ((~RB_BITS(sib, field) & RB_RED_MASK) == 0) {\
587
+ RB_BITS(sib, field) &= ~RB_RED_MASK; \
588
+ elm = parent; \
589
+ continue; \
590
+ } \
591
+ RB_FLIP_LEFT(sib, field); \
592
+ if (RB_RED_RIGHT(sib, field)) \
593
+ RB_FLIP_RIGHT(parent, field); \
594
+ else if (!RB_RED_LEFT(sib, field)) { \
595
+ RB_FLIP_RIGHT(parent, field); \
596
+ RB_ROTATE_LEFT(head, sib, elm, field); \
597
+ if (RB_RED_LEFT(elm, field)) \
598
+ RB_FLIP_RIGHT(sib, field); \
599
+ if (RB_RED_RIGHT(elm, field)) \
600
+ RB_FLIP_LEFT(parent, field); \
601
+ RB_BITS(elm, field) |= RB_RED_MASK; \
602
+ sib = elm; \
603
+ } \
604
+ RB_ROTATE_RIGHT(head, parent, sib, field); \
605
+ } \
606
+ break; \
607
+ } while ((parent = RB_PARENT(elm, field)) != nullptr); \
608
+ }
609
+
610
+ #define RB_GENERATE_REMOVE(name, type, field, attr) \
611
+ attr struct type * \
612
+ name##_RB_REMOVE(struct name *head, struct type *elm) \
613
+ { \
614
+ struct type *child, *old, *parent, *right; \
615
+ \
616
+ old = elm; \
617
+ parent = RB_PARENT(elm, field); \
618
+ right = RB_RIGHT(elm, field); \
619
+ if (RB_LEFT(elm, field) == nullptr) \
620
+ elm = child = right; \
621
+ else if (right == nullptr) \
622
+ elm = child = RB_LEFT(elm, field); \
623
+ else { \
624
+ if ((child = RB_LEFT(right, field)) == nullptr) { \
625
+ child = RB_RIGHT(right, field); \
626
+ RB_RIGHT(old, field) = child; \
627
+ parent = elm = right; \
628
+ } else { \
629
+ do \
630
+ elm = child; \
631
+ while ((child = RB_LEFT(elm, field)) != nullptr); \
632
+ child = RB_RIGHT(elm, field); \
633
+ parent = RB_PARENT(elm, field); \
634
+ RB_LEFT(parent, field) = child; \
635
+ RB_SET_PARENT(RB_RIGHT(old, field), elm, field);\
636
+ } \
637
+ RB_SET_PARENT(RB_LEFT(old, field), elm, field); \
638
+ elm->field = old->field; \
639
+ } \
640
+ RB_SWAP_CHILD(head, old, elm, field); \
641
+ if (child != nullptr) \
642
+ RB_SET_PARENT(child, parent, field); \
643
+ if (parent != nullptr) \
644
+ name##_RB_REMOVE_COLOR(head, parent, child); \
645
+ while (parent != nullptr) { \
646
+ RB_AUGMENT(parent); \
647
+ parent = RB_PARENT(parent, field); \
648
+ } \
649
+ return (old); \
650
+ }
651
+
652
+ #define RB_GENERATE_INSERT_FINISH(name, type, field, attr) \
653
+ /* Inserts a node into the RB tree */ \
654
+ attr struct type * \
655
+ name##_RB_INSERT_FINISH(struct name *head, struct type *parent, \
656
+ struct type **pptr, struct type *elm) \
657
+ { \
658
+ RB_SET(elm, parent, field); \
659
+ *pptr = elm; \
660
+ if (parent != nullptr) name##_RB_INSERT_COLOR(head, parent, elm); \
661
+ while (elm != nullptr) { \
662
+ RB_AUGMENT(elm); \
663
+ elm = RB_PARENT(elm, field); \
664
+ } \
665
+ return nullptr; \
666
+ }
667
+
668
+ #define RB_GENERATE_INSERT(name, type, field, cmp, attr) \
669
+ /* Inserts a node into the RB tree */ \
670
+ attr struct type * \
671
+ name##_RB_INSERT(struct name *head, struct type *elm) \
672
+ { \
673
+ struct type *tmp; \
674
+ struct type **tmpp = &RB_ROOT(head); \
675
+ struct type *parent = nullptr; \
676
+ while ((tmp = *tmpp) != nullptr) { \
677
+ parent = tmp; \
678
+ __typeof(cmp((void *)1, (void *)1)) comp = (cmp)(elm, parent); \
679
+ if (comp < 0) tmpp = &RB_LEFT(parent, field); \
680
+ else if (comp > 0) tmpp = &RB_RIGHT(parent, field); \
681
+ else return (parent); \
682
+ } \
683
+ return (name##_RB_INSERT_FINISH(head, parent, tmpp, elm)); \
684
+ }
685
+
686
+ #define RB_GENERATE_INSERT_NEXT(name, type, field, cmp, attr) \
687
+ attr void \
688
+ name##_RB_INSERT_NEXT(struct name *head, struct type * restrict elm, struct type * restrict next) \
689
+ { \
690
+ struct type *tmp; \
691
+ struct type **tmpp = &RB_RIGHT(elm, field); \
692
+ while ((tmp = *tmpp) != nullptr) { \
693
+ elm = tmp; \
694
+ tmpp = &RB_LEFT(elm, field); \
695
+ } \
696
+ (void)(name##_RB_INSERT_FINISH(head, elm, tmpp, next)); \
697
+ }
698
+
699
+ #define RB_GENERATE_FIND(name, type, field, cmp, attr) \
700
+ /* Finds the node with the same key as elm */ \
701
+ attr struct type * \
702
+ name##_RB_FIND(struct name *head, struct type *elm) \
703
+ { \
704
+ struct type *tmp = RB_ROOT(head); \
705
+ __typeof(cmp((void *)1, (void *)1)) comp; \
706
+ while (tmp) { \
707
+ comp = cmp(elm, tmp); \
708
+ if (comp < 0) \
709
+ tmp = RB_LEFT(tmp, field); \
710
+ else if (comp > 0) \
711
+ tmp = RB_RIGHT(tmp, field); \
712
+ else \
713
+ return (tmp); \
714
+ } \
715
+ return nullptr; \
716
+ }
717
+
718
+ #define RB_GENERATE_NFIND(name, type, field, cmp, attr) \
719
+ /* Finds the first node greater than or equal to the search key */ \
720
+ attr struct type * \
721
+ name##_RB_NFIND(struct name *head, struct type *elm) \
722
+ { \
723
+ struct type *tmp = RB_ROOT(head); \
724
+ struct type *res = nullptr; \
725
+ __typeof(cmp((void *)1, (void *)1)) comp; \
726
+ while (tmp) { \
727
+ comp = cmp(elm, tmp); \
728
+ if (comp < 0) { \
729
+ res = tmp; \
730
+ tmp = RB_LEFT(tmp, field); \
731
+ } \
732
+ else if (comp > 0) \
733
+ tmp = RB_RIGHT(tmp, field); \
734
+ else \
735
+ return (tmp); \
736
+ } \
737
+ return (res); \
738
+ }
739
+
740
+ #define RB_GENERATE_NEXT(name, type, field, attr) \
741
+ /* ARGSUSED */ \
742
+ attr struct type * \
743
+ name##_RB_NEXT(struct type *elm) \
744
+ { \
745
+ if (RB_RIGHT(elm, field)) { \
746
+ elm = RB_RIGHT(elm, field); \
747
+ while (RB_LEFT(elm, field)) \
748
+ elm = RB_LEFT(elm, field); \
749
+ } else { \
750
+ while (RB_PARENT(elm, field) && \
751
+ (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
752
+ elm = RB_PARENT(elm, field); \
753
+ elm = RB_PARENT(elm, field); \
754
+ } \
755
+ return (elm); \
756
+ }
757
+
758
+ #define RB_GENERATE_PREV(name, type, field, attr) \
759
+ /* ARGSUSED */ \
760
+ attr struct type * \
761
+ name##_RB_PREV(struct type *elm) \
762
+ { \
763
+ if (RB_LEFT(elm, field)) { \
764
+ elm = RB_LEFT(elm, field); \
765
+ while (RB_RIGHT(elm, field)) \
766
+ elm = RB_RIGHT(elm, field); \
767
+ } else { \
768
+ while (RB_PARENT(elm, field) && \
769
+ (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
770
+ elm = RB_PARENT(elm, field); \
771
+ elm = RB_PARENT(elm, field); \
772
+ } \
773
+ return (elm); \
774
+ }
775
+
776
+ #define RB_GENERATE_MINMAX(name, type, field, attr) \
777
+ attr struct type * \
778
+ name##_RB_MINMAX(struct name *head, int val) \
779
+ { \
780
+ struct type *tmp = RB_ROOT(head); \
781
+ struct type *parent = nullptr; \
782
+ while (tmp) { \
783
+ parent = tmp; \
784
+ if (val < 0) \
785
+ tmp = RB_LEFT(tmp, field); \
786
+ else \
787
+ tmp = RB_RIGHT(tmp, field); \
788
+ } \
789
+ return (parent); \
790
+ }
791
+
792
+ #define RB_GENERATE_REINSERT(name, type, field, cmp, attr) \
793
+ attr struct type * \
794
+ name##_RB_REINSERT(struct name *head, struct type *elm) \
795
+ { \
796
+ struct type *cmpelm; \
797
+ if (((cmpelm = RB_PREV(name, head, elm)) != nullptr && \
798
+ cmp(cmpelm, elm) >= 0) || \
799
+ ((cmpelm = RB_NEXT(name, head, elm)) != nullptr && \
800
+ cmp(elm, cmpelm) >= 0)) { \
801
+ /* XXXLAS: Remove/insert is heavy handed. */ \
802
+ RB_REMOVE(name, head, elm); \
803
+ return (RB_INSERT(name, head, elm)); \
804
+ } \
805
+ return nullptr; \
806
+ } \
807
+
808
+ #define RB_NEGINF -1
809
+ #define RB_INF 1
810
+
811
+ #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
812
+ #define RB_INSERT_NEXT(name, x, y, z) name##_RB_INSERT_NEXT(x, y, z)
813
+ #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
814
+ #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
815
+ #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
816
+ #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
817
+ #define RB_PREV(name, x, y) name##_RB_PREV(y)
818
+ #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
819
+ #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
820
+ #define RB_REINSERT(name, x, y) name##_RB_REINSERT(x, y)
821
+
822
+ #define RB_FOREACH(x, name, head) \
823
+ for ((x) = RB_MIN(name, head); \
824
+ (x) != nullptr; \
825
+ (x) = name##_RB_NEXT(x))
826
+
827
+ #define RB_FOREACH_FROM(x, name, y) \
828
+ for ((x) = (y); \
829
+ ((x) != nullptr) && ((y) = name##_RB_NEXT(x), (x) != nullptr); \
830
+ (x) = (y))
831
+
832
+ #define RB_FOREACH_SAFE(x, name, head, y) \
833
+ for ((x) = RB_MIN(name, head); \
834
+ ((x) != nullptr) && ((y) = name##_RB_NEXT(x), (x) != nullptr); \
835
+ (x) = (y))
836
+
837
+ #define RB_FOREACH_REVERSE(x, name, head) \
838
+ for ((x) = RB_MAX(name, head); \
839
+ (x) != nullptr; \
840
+ (x) = name##_RB_PREV(x))
841
+
842
+ #define RB_FOREACH_REVERSE_FROM(x, name, y) \
843
+ for ((x) = (y); \
844
+ ((x) != nullptr) && ((y) = name##_RB_PREV(x), (x) != nullptr); \
845
+ (x) = (y))
846
+
847
+ #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
848
+ for ((x) = RB_MAX(name, head); \
849
+ ((x) != nullptr) && ((y) = name##_RB_PREV(x), (x) != nullptr); \
850
+ (x) = (y))
851
+
852
+ #endif /* _SYS_TREE_H_ */