tree.h 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  1. /****************************************************************************
  2. * include/nuttx/tree.h
  3. *
  4. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  25. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. ****************************************************************************/
  28. /* This file defines data structures for different types of trees:
  29. * splay trees and red-black trees.
  30. *
  31. * A splay tree is a self-organizing data structure. Every operation
  32. * on the tree causes a splay to happen. The splay moves the requested
  33. * node to the root of the tree and partly rebalances it.
  34. *
  35. * This has the benefit that request locality causes faster lookups as
  36. * the requested nodes move to the top of the tree. On the other hand,
  37. * every lookup causes memory writes.
  38. *
  39. * The Balance Theorem bounds the total access time for m operations
  40. * and n inserts on an initially empty tree as O((m + n)lg n). The
  41. * amortized cost for a sequence of m accesses to a splay tree is O(lg n);
  42. *
  43. * A red-black tree is a binary search tree with the node color as an
  44. * extra attribute. It fulfils a set of conditions:
  45. * - every search path from the root to a leaf consists of the
  46. * same number of black nodes,
  47. * - each red node (except for the root) has a black parent,
  48. * - each leaf node is black.
  49. *
  50. * Every operation on a red-black tree is bounded as O(lg n).
  51. * The maximum height of a red-black tree is 2lg (n+1).
  52. */
  53. /****************************************************************************
  54. * Included Files
  55. ****************************************************************************/
  56. #ifndef __INCLUDE_NUTTX_TREE_H
  57. #define __INCLUDE_NUTTX_TREE_H
  58. /****************************************************************************
  59. * Pre-processor Definitions
  60. ****************************************************************************/
  61. #define SPLAY_HEAD(name, type) \
  62. struct name \
  63. { \
  64. struct type *sph_root; /* root of the tree */ \
  65. }
  66. #define SPLAY_INITIALIZER(root) \
  67. { NULL }
  68. #define SPLAY_INIT(root) \
  69. do \
  70. { \
  71. (root)->sph_root = NULL; \
  72. } while (0)
  73. #define SPLAY_ENTRY(type) \
  74. struct \
  75. { \
  76. struct type *spe_left; /* left element */ \
  77. struct type *spe_right; /* right element */ \
  78. }
  79. #define SPLAY_LEFT(elm, field) (elm)->field.spe_left
  80. #define SPLAY_RIGHT(elm, field) (elm)->field.spe_right
  81. #define SPLAY_ROOT(head) (head)->sph_root
  82. #define SPLAY_EMPTY(head) (SPLAY_ROOT(head) == NULL)
  83. /* SPLAY_ROTATE_{LEFT,RIGHT} expect that tmp hold SPLAY_{RIGHT,LEFT} */
  84. #define SPLAY_ROTATE_RIGHT(head, tmp, field) \
  85. do \
  86. { \
  87. SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(tmp, field); \
  88. SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
  89. (head)->sph_root = tmp; \
  90. } \
  91. while (0)
  92. #define SPLAY_ROTATE_LEFT(head, tmp, field) \
  93. do \
  94. { \
  95. SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(tmp, field); \
  96. SPLAY_LEFT(tmp, field) = (head)->sph_root; \
  97. (head)->sph_root = tmp; \
  98. } \
  99. while (0)
  100. #define SPLAY_LINKLEFT(head, tmp, field) \
  101. do \
  102. { \
  103. SPLAY_LEFT(tmp, field) = (head)->sph_root; \
  104. tmp = (head)->sph_root; \
  105. (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
  106. } \
  107. while (0)
  108. #define SPLAY_LINKRIGHT(head, tmp, field) \
  109. do \
  110. { \
  111. SPLAY_RIGHT(tmp, field) = (head)->sph_root; \
  112. tmp = (head)->sph_root; \
  113. (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
  114. } \
  115. while (0)
  116. #define SPLAY_ASSEMBLE(head, node, left, right, field) \
  117. do \
  118. { \
  119. SPLAY_RIGHT(left, field) = SPLAY_LEFT((head)->sph_root, field); \
  120. SPLAY_LEFT(right, field) = SPLAY_RIGHT((head)->sph_root, field); \
  121. SPLAY_LEFT((head)->sph_root, field) = SPLAY_RIGHT(node, field); \
  122. SPLAY_RIGHT((head)->sph_root, field) = SPLAY_LEFT(node, field); \
  123. } \
  124. while (0)
  125. /* Generates prototypes and inline functions */
  126. #define SPLAY_PROTOTYPE(name, type, field, cmp) \
  127. void name##_SPLAY(struct name *, struct type *); \
  128. void name##_SPLAY_MINMAX(struct name *, int); \
  129. struct type *name##_SPLAY_INSERT(struct name *, struct type *); \
  130. struct type *name##_SPLAY_REMOVE(struct name *, struct type *); \
  131. \
  132. /* Finds the node with the same key as elm */ \
  133. static __inline struct type * name##_SPLAY_FIND(struct name *head, struct type *elm) \
  134. { \
  135. if (SPLAY_EMPTY(head)) \
  136. { \
  137. return(NULL); \
  138. } \
  139. \
  140. name##_SPLAY(head, elm); \
  141. if ((cmp)(elm, (head)->sph_root) == 0) \
  142. { \
  143. return (head->sph_root); \
  144. } \
  145. return (NULL); \
  146. } \
  147. \
  148. static __inline struct type * name##_SPLAY_NEXT(struct name *head, struct type *elm) \
  149. { \
  150. name##_SPLAY(head, elm); \
  151. if (SPLAY_RIGHT(elm, field) != NULL) \
  152. { \
  153. elm = SPLAY_RIGHT(elm, field); \
  154. while (SPLAY_LEFT(elm, field) != NULL) \
  155. { \
  156. elm = SPLAY_LEFT(elm, field); \
  157. } \
  158. } \
  159. else \
  160. { \
  161. elm = NULL; \
  162. } \
  163. \
  164. return (elm); \
  165. } \
  166. \
  167. static __inline struct type *name##_SPLAY_MIN_MAX(struct name *head, int val) \
  168. { \
  169. name##_SPLAY_MINMAX(head, val); \
  170. return (SPLAY_ROOT(head)); \
  171. }
  172. /* Main splay operation.
  173. * Moves node close to the key of elm to top
  174. */
  175. #define SPLAY_GENERATE(name, type, field, cmp) \
  176. struct type * \
  177. name##_SPLAY_INSERT(struct name *head, struct type *elm) \
  178. { \
  179. if (SPLAY_EMPTY(head)) \
  180. { \
  181. SPLAY_LEFT(elm, field) = SPLAY_RIGHT(elm, field) = NULL; \
  182. } \
  183. else \
  184. { \
  185. int __comp; \
  186. name##_SPLAY(head, elm); \
  187. __comp = (cmp)(elm, (head)->sph_root); \
  188. if(__comp < 0) \
  189. { \
  190. SPLAY_LEFT(elm, field) = SPLAY_LEFT((head)->sph_root, field); \
  191. SPLAY_RIGHT(elm, field) = (head)->sph_root; \
  192. SPLAY_LEFT((head)->sph_root, field) = NULL; \
  193. } \
  194. else if (__comp > 0) \
  195. { \
  196. SPLAY_RIGHT(elm, field) = SPLAY_RIGHT((head)->sph_root, field); \
  197. SPLAY_LEFT(elm, field) = (head)->sph_root; \
  198. SPLAY_RIGHT((head)->sph_root, field) = NULL; \
  199. } \
  200. else \
  201. { \
  202. return ((head)->sph_root); \
  203. } \
  204. } \
  205. \
  206. (head)->sph_root = (elm); \
  207. return (NULL); \
  208. } \
  209. \
  210. struct type *name##_SPLAY_REMOVE(struct name *head, struct type *elm) \
  211. { \
  212. struct type *__tmp; \
  213. if (SPLAY_EMPTY(head)) \
  214. { \
  215. return (NULL); \
  216. } \
  217. \
  218. name##_SPLAY(head, elm); \
  219. if ((cmp)(elm, (head)->sph_root) == 0) \
  220. { \
  221. if (SPLAY_LEFT((head)->sph_root, field) == NULL) \
  222. { \
  223. (head)->sph_root = SPLAY_RIGHT((head)->sph_root, field); \
  224. } \
  225. else \
  226. { \
  227. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  228. (head)->sph_root = SPLAY_LEFT((head)->sph_root, field); \
  229. name##_SPLAY(head, elm); \
  230. SPLAY_RIGHT((head)->sph_root, field) = __tmp; \
  231. } \
  232. \
  233. return (elm); \
  234. } \
  235. \
  236. return (NULL); \
  237. } \
  238. \
  239. void name##_SPLAY(struct name *head, struct type *elm) \
  240. { \
  241. struct type __node, *__left, *__right, *__tmp; \
  242. int __comp; \
  243. \
  244. SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL; \
  245. __left = __right = &__node; \
  246. \
  247. while ((__comp = (cmp)(elm, (head)->sph_root))) \
  248. { \
  249. if (__comp < 0) \
  250. { \
  251. __tmp = SPLAY_LEFT((head)->sph_root, field); \
  252. if (__tmp == NULL) \
  253. { \
  254. break; \
  255. } \
  256. \
  257. if ((cmp)(elm, __tmp) < 0) \
  258. { \
  259. SPLAY_ROTATE_RIGHT(head, __tmp, field); \
  260. if (SPLAY_LEFT((head)->sph_root, field) == NULL) \
  261. { \
  262. break; \
  263. } \
  264. } \
  265. \
  266. SPLAY_LINKLEFT(head, __right, field); \
  267. } \
  268. else if (__comp > 0) \
  269. { \
  270. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  271. if (__tmp == NULL) \
  272. { \
  273. break; \
  274. } \
  275. \
  276. if ((cmp)(elm, __tmp) > 0) \
  277. { \
  278. SPLAY_ROTATE_LEFT(head, __tmp, field); \
  279. if (SPLAY_RIGHT((head)->sph_root, field) == NULL) \
  280. { \
  281. break; \
  282. } \
  283. } \
  284. \
  285. SPLAY_LINKRIGHT(head, __left, field); \
  286. } \
  287. } \
  288. \
  289. SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
  290. } \
  291. \
  292. /* Splay with either the minimum or the maximum element \
  293. * Used to find minimum or maximum element in tree. \
  294. */ \
  295. \
  296. void name##_SPLAY_MINMAX(struct name *head, int __comp) \
  297. { \
  298. struct type __node, *__left, *__right, *__tmp; \
  299. \
  300. SPLAY_LEFT(&__node, field) = SPLAY_RIGHT(&__node, field) = NULL; \
  301. __left = __right = &__node; \
  302. \
  303. while (1) \
  304. { \
  305. if (__comp < 0) \
  306. { \
  307. __tmp = SPLAY_LEFT((head)->sph_root, field); \
  308. if (__tmp == NULL) \
  309. { \
  310. break; \
  311. } \
  312. \
  313. if (__comp < 0) \
  314. { \
  315. SPLAY_ROTATE_RIGHT(head, __tmp, field); \
  316. if (SPLAY_LEFT((head)->sph_root, field) == NULL) \
  317. { \
  318. break; \
  319. } \
  320. } \
  321. \
  322. SPLAY_LINKLEFT(head, __right, field); \
  323. } \
  324. else if (__comp > 0) \
  325. { \
  326. __tmp = SPLAY_RIGHT((head)->sph_root, field); \
  327. if (__tmp == NULL) \
  328. { \
  329. break; \
  330. } \
  331. \
  332. if (__comp > 0) \
  333. { \
  334. SPLAY_ROTATE_LEFT(head, __tmp, field); \
  335. if (SPLAY_RIGHT((head)->sph_root, field) == NULL) \
  336. { \
  337. break; \
  338. } \
  339. } \
  340. \
  341. SPLAY_LINKRIGHT(head, __left, field); \
  342. } \
  343. } \
  344. \
  345. SPLAY_ASSEMBLE(head, &__node, __left, __right, field); \
  346. }
  347. #define SPLAY_NEGINF -1
  348. #define SPLAY_INF 1
  349. #define SPLAY_INSERT(name, x, y) name##_SPLAY_INSERT(x, y)
  350. #define SPLAY_REMOVE(name, x, y) name##_SPLAY_REMOVE(x, y)
  351. #define SPLAY_FIND(name, x, y) name##_SPLAY_FIND(x, y)
  352. #define SPLAY_NEXT(name, x, y) name##_SPLAY_NEXT(x, y)
  353. #define SPLAY_MIN(name, x) \
  354. (SPLAY_EMPTY(x) ? NULL : name##_SPLAY_MIN_MAX(x, SPLAY_NEGINF))
  355. #define SPLAY_MAX(name, x) \
  356. (SPLAY_EMPTY(x) ? NULL : name##_SPLAY_MIN_MAX(x, SPLAY_INF))
  357. #define SPLAY_FOREACH(x, name, head) \
  358. for ((x) = SPLAY_MIN(name, head); \
  359. (x) != NULL; \
  360. (x) = SPLAY_NEXT(name, head, x))
  361. /* Macros that define a red-black tree */
  362. #define RB_HEAD(name, type) \
  363. struct name \
  364. { \
  365. struct type *rbh_root; /* root of the tree */ \
  366. }
  367. #define RB_INITIALIZER(root) \
  368. { NULL }
  369. #define RB_INIT(root) \
  370. do \
  371. { \
  372. (root)->rbh_root = NULL; \
  373. } \
  374. while (0)
  375. #define RB_BLACK 0
  376. #define RB_RED 1
  377. #define RB_ENTRY(type) \
  378. struct \
  379. { \
  380. struct type *rbe_left; /* left element */ \
  381. struct type *rbe_right; /* right element */ \
  382. struct type *rbe_parent; /* parent element */ \
  383. int rbe_color; /* node color */ \
  384. }
  385. #define RB_LEFT(elm, field) (elm)->field.rbe_left
  386. #define RB_RIGHT(elm, field) (elm)->field.rbe_right
  387. #define RB_PARENT(elm, field) (elm)->field.rbe_parent
  388. #define RB_COLOR(elm, field) (elm)->field.rbe_color
  389. #define RB_ROOT(head) (head)->rbh_root
  390. #define RB_EMPTY(head) (RB_ROOT(head) == NULL)
  391. #define RB_SET(elm, parent, field) \
  392. do \
  393. { \
  394. RB_PARENT(elm, field) = parent; \
  395. RB_LEFT(elm, field) = RB_RIGHT(elm, field) = NULL; \
  396. RB_COLOR(elm, field) = RB_RED; \
  397. } \
  398. while (0)
  399. #define RB_SET_BLACKRED(black, red, field) \
  400. do \
  401. { \
  402. RB_COLOR(black, field) = RB_BLACK; \
  403. RB_COLOR(red, field) = RB_RED; \
  404. } \
  405. while (0)
  406. #ifndef RB_AUGMENT
  407. # define RB_AUGMENT(x) do {} while (0)
  408. #endif
  409. #define RB_ROTATE_LEFT(head, elm, tmp, field) \
  410. do \
  411. { \
  412. (tmp) = RB_RIGHT(elm, field); \
  413. if ((RB_RIGHT(elm, field) = RB_LEFT(tmp, field))) \
  414. { \
  415. RB_PARENT(RB_LEFT(tmp, field), field) = (elm); \
  416. } \
  417. \
  418. RB_AUGMENT(elm); \
  419. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) \
  420. { \
  421. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  422. { \
  423. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  424. } \
  425. \
  426. else \
  427. { \
  428. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  429. } \
  430. } \
  431. else \
  432. { \
  433. (head)->rbh_root = (tmp); \
  434. } \
  435. \
  436. RB_LEFT(tmp, field) = (elm); \
  437. RB_PARENT(elm, field) = (tmp); \
  438. RB_AUGMENT(tmp); \
  439. if ((RB_PARENT(tmp, field))) \
  440. { \
  441. RB_AUGMENT(RB_PARENT(tmp, field)); \
  442. } \
  443. } \
  444. while (0)
  445. #define RB_ROTATE_RIGHT(head, elm, tmp, field) \
  446. do \
  447. { \
  448. (tmp) = RB_LEFT(elm, field); \
  449. if ((RB_LEFT(elm, field) = RB_RIGHT(tmp, field))) \
  450. { \
  451. RB_PARENT(RB_RIGHT(tmp, field), field) = (elm); \
  452. } \
  453. \
  454. RB_AUGMENT(elm); \
  455. if ((RB_PARENT(tmp, field) = RB_PARENT(elm, field))) \
  456. { \
  457. if ((elm) == RB_LEFT(RB_PARENT(elm, field), field)) \
  458. { \
  459. RB_LEFT(RB_PARENT(elm, field), field) = (tmp); \
  460. } \
  461. else \
  462. { \
  463. RB_RIGHT(RB_PARENT(elm, field), field) = (tmp); \
  464. } \
  465. } \
  466. else \
  467. { \
  468. (head)->rbh_root = (tmp); \
  469. } \
  470. \
  471. RB_RIGHT(tmp, field) = (elm); \
  472. RB_PARENT(elm, field) = (tmp); \
  473. RB_AUGMENT(tmp); \
  474. if ((RB_PARENT(tmp, field))) \
  475. { \
  476. RB_AUGMENT(RB_PARENT(tmp, field)); \
  477. } \
  478. } \
  479. while (0)
  480. /* Generates prototypes and inline functions */
  481. #define RB_PROTOTYPE(name, type, field, cmp) \
  482. RB_PROTOTYPE_INTERNAL(name, type, field, cmp,)
  483. #define RB_PROTOTYPE_STATIC(name, type, field, cmp) \
  484. RB_PROTOTYPE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
  485. #define RB_PROTOTYPE_INTERNAL(name, type, field, cmp, attr) \
  486. attr void name##_RB_INSERT_COLOR(struct name *, struct type *); \
  487. attr void name##_RB_REMOVE_COLOR(struct name *, struct type *, struct type *); \
  488. attr struct type *name##_RB_REMOVE(struct name *, struct type *); \
  489. attr struct type *name##_RB_INSERT(struct name *, struct type *); \
  490. attr struct type *name##_RB_FIND(struct name *, struct type *); \
  491. attr struct type *name##_RB_NFIND(struct name *, struct type *); \
  492. attr struct type *name##_RB_NEXT(struct type *); \
  493. attr struct type *name##_RB_PREV(struct type *); \
  494. attr struct type *name##_RB_MINMAX(struct name *, int);
  495. /* Main rb operation.
  496. * Moves node close to the key of elm to top
  497. */
  498. #define RB_GENERATE(name, type, field, cmp) \
  499. RB_GENERATE_INTERNAL(name, type, field, cmp,)
  500. #define RB_GENERATE_STATIC(name, type, field, cmp) \
  501. RB_GENERATE_INTERNAL(name, type, field, cmp, __attribute__((__unused__)) static)
  502. #define RB_GENERATE_INTERNAL(name, type, field, cmp, attr) \
  503. attr void name##_RB_INSERT_COLOR(struct name *head, struct type *elm) \
  504. { \
  505. struct type *parent, *gparent, *tmp; \
  506. while ((parent = RB_PARENT(elm, field)) && \
  507. RB_COLOR(parent, field) == RB_RED) \
  508. { \
  509. gparent = RB_PARENT(parent, field); \
  510. if (parent == RB_LEFT(gparent, field)) \
  511. { \
  512. tmp = RB_RIGHT(gparent, field); \
  513. if (tmp && RB_COLOR(tmp, field) == RB_RED) \
  514. { \
  515. RB_COLOR(tmp, field) = RB_BLACK; \
  516. RB_SET_BLACKRED(parent, gparent, field); \
  517. elm = gparent; \
  518. continue; \
  519. } \
  520. \
  521. if (RB_RIGHT(parent, field) == elm) \
  522. { \
  523. RB_ROTATE_LEFT(head, parent, tmp, field); \
  524. tmp = parent; \
  525. parent = elm; \
  526. elm = tmp; \
  527. } \
  528. \
  529. RB_SET_BLACKRED(parent, gparent, field); \
  530. RB_ROTATE_RIGHT(head, gparent, tmp, field); \
  531. } \
  532. else \
  533. { \
  534. tmp = RB_LEFT(gparent, field); \
  535. if (tmp && RB_COLOR(tmp, field) == RB_RED) \
  536. { \
  537. RB_COLOR(tmp, field) = RB_BLACK; \
  538. RB_SET_BLACKRED(parent, gparent, field); \
  539. elm = gparent; \
  540. continue; \
  541. } \
  542. \
  543. if (RB_LEFT(parent, field) == elm) \
  544. { \
  545. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  546. tmp = parent; \
  547. parent = elm; \
  548. elm = tmp; \
  549. } \
  550. \
  551. RB_SET_BLACKRED(parent, gparent, field); \
  552. RB_ROTATE_LEFT(head, gparent, tmp, field); \
  553. } \
  554. } \
  555. \
  556. RB_COLOR(head->rbh_root, field) = RB_BLACK; \
  557. } \
  558. \
  559. attr void name##_RB_REMOVE_COLOR(struct name *head, struct type *parent, struct type *elm) \
  560. { \
  561. struct type *tmp; \
  562. \
  563. while ((elm == NULL || RB_COLOR(elm, field) == RB_BLACK) && \
  564. elm != RB_ROOT(head)) \
  565. { \
  566. if (RB_LEFT(parent, field) == elm) \
  567. { \
  568. tmp = RB_RIGHT(parent, field); \
  569. if (RB_COLOR(tmp, field) == RB_RED) \
  570. { \
  571. RB_SET_BLACKRED(tmp, parent, field); \
  572. RB_ROTATE_LEFT(head, parent, tmp, field); \
  573. tmp = RB_RIGHT(parent, field); \
  574. } \
  575. \
  576. if ((RB_LEFT(tmp, field) == NULL || \
  577. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  578. (RB_RIGHT(tmp, field) == NULL || \
  579. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) \
  580. {\
  581. RB_COLOR(tmp, field) = RB_RED; \
  582. elm = parent; \
  583. parent = RB_PARENT(elm, field); \
  584. } \
  585. else \
  586. { \
  587. if (RB_RIGHT(tmp, field) == NULL || \
  588. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK) \
  589. { \
  590. struct type *oleft; \
  591. if ((oleft = RB_LEFT(tmp, field))) \
  592. { \
  593. RB_COLOR(oleft, field) = RB_BLACK; \
  594. } \
  595. \
  596. RB_COLOR(tmp, field) = RB_RED; \
  597. RB_ROTATE_RIGHT(head, tmp, oleft, field); \
  598. tmp = RB_RIGHT(parent, field); \
  599. } \
  600. \
  601. RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
  602. RB_COLOR(parent, field) = RB_BLACK; \
  603. if (RB_RIGHT(tmp, field)) \
  604. RB_COLOR(RB_RIGHT(tmp, field), field) = RB_BLACK; \
  605. RB_ROTATE_LEFT(head, parent, tmp, field); \
  606. elm = RB_ROOT(head); \
  607. break; \
  608. } \
  609. } \
  610. else \
  611. { \
  612. tmp = RB_LEFT(parent, field); \
  613. if (RB_COLOR(tmp, field) == RB_RED) \
  614. { \
  615. RB_SET_BLACKRED(tmp, parent, field); \
  616. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  617. tmp = RB_LEFT(parent, field); \
  618. } \
  619. \
  620. if ((RB_LEFT(tmp, field) == NULL || \
  621. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) &&\
  622. (RB_RIGHT(tmp, field) == NULL || \
  623. RB_COLOR(RB_RIGHT(tmp, field), field) == RB_BLACK)) \
  624. {\
  625. RB_COLOR(tmp, field) = RB_RED; \
  626. elm = parent; \
  627. parent = RB_PARENT(elm, field); \
  628. } \
  629. else \
  630. { \
  631. if (RB_LEFT(tmp, field) == NULL || \
  632. RB_COLOR(RB_LEFT(tmp, field), field) == RB_BLACK) \
  633. { \
  634. struct type *oright; \
  635. if ((oright = RB_RIGHT(tmp, field))) \
  636. { \
  637. RB_COLOR(oright, field) = RB_BLACK; \
  638. } \
  639. \
  640. RB_COLOR(tmp, field) = RB_RED; \
  641. RB_ROTATE_LEFT(head, tmp, oright, field); \
  642. tmp = RB_LEFT(parent, field); \
  643. } \
  644. \
  645. RB_COLOR(tmp, field) = RB_COLOR(parent, field); \
  646. RB_COLOR(parent, field) = RB_BLACK; \
  647. if (RB_LEFT(tmp, field)) \
  648. { \
  649. RB_COLOR(RB_LEFT(tmp, field), field) = RB_BLACK; \
  650. } \
  651. \
  652. RB_ROTATE_RIGHT(head, parent, tmp, field); \
  653. elm = RB_ROOT(head); \
  654. break; \
  655. } \
  656. } \
  657. } \
  658. \
  659. if (elm) \
  660. { \
  661. RB_COLOR(elm, field) = RB_BLACK; \
  662. } \
  663. } \
  664. \
  665. attr struct type *name##_RB_REMOVE(struct name *head, struct type *elm) \
  666. { \
  667. struct type *child, *parent, *old = elm; \
  668. int color; \
  669. \
  670. if (RB_LEFT(elm, field) == NULL) \
  671. { \
  672. child = RB_RIGHT(elm, field); \
  673. } \
  674. else if (RB_RIGHT(elm, field) == NULL) \
  675. { \
  676. child = RB_LEFT(elm, field); \
  677. } \
  678. else \
  679. { \
  680. struct type *left; \
  681. \
  682. elm = RB_RIGHT(elm, field); \
  683. while ((left = RB_LEFT(elm, field))) \
  684. { \
  685. elm = left; \
  686. } \
  687. \
  688. child = RB_RIGHT(elm, field); \
  689. parent = RB_PARENT(elm, field); \
  690. color = RB_COLOR(elm, field); \
  691. if (child) \
  692. { \
  693. RB_PARENT(child, field) = parent; \
  694. } \
  695. \
  696. if (parent) \
  697. { \
  698. if (RB_LEFT(parent, field) == elm) \
  699. { \
  700. RB_LEFT(parent, field) = child; \
  701. } \
  702. else \
  703. { \
  704. RB_RIGHT(parent, field) = child; \
  705. } \
  706. \
  707. RB_AUGMENT(parent); \
  708. } \
  709. else \
  710. { \
  711. RB_ROOT(head) = child; \
  712. } \
  713. \
  714. if (RB_PARENT(elm, field) == old) \
  715. { \
  716. parent = elm; \
  717. } \
  718. \
  719. (elm)->field = (old)->field; \
  720. if (RB_PARENT(old, field)) \
  721. { \
  722. if (RB_LEFT(RB_PARENT(old, field), field) == old) \
  723. { \
  724. RB_LEFT(RB_PARENT(old, field), field) = elm; \
  725. } \
  726. else \
  727. { \
  728. RB_RIGHT(RB_PARENT(old, field), field) = elm; \
  729. } \
  730. \
  731. RB_AUGMENT(RB_PARENT(old, field)); \
  732. } \
  733. else \
  734. { \
  735. RB_ROOT(head) = elm; \
  736. } \
  737. \
  738. RB_PARENT(RB_LEFT(old, field), field) = elm; \
  739. if (RB_RIGHT(old, field)) \
  740. { \
  741. RB_PARENT(RB_RIGHT(old, field), field) = elm; \
  742. } \
  743. \
  744. if (parent) \
  745. { \
  746. left = parent; \
  747. do \
  748. { \
  749. RB_AUGMENT(left); \
  750. } \
  751. while ((left = RB_PARENT(left, field))); \
  752. } \
  753. \
  754. goto color; \
  755. } \
  756. \
  757. parent = RB_PARENT(elm, field); \
  758. color = RB_COLOR(elm, field); \
  759. if (child) \
  760. { \
  761. RB_PARENT(child, field) = parent; \
  762. } \
  763. \
  764. if (parent) \
  765. { \
  766. if (RB_LEFT(parent, field) == elm) \
  767. { \
  768. RB_LEFT(parent, field) = child; \
  769. } \
  770. else \
  771. { \
  772. RB_RIGHT(parent, field) = child; \
  773. } \
  774. \
  775. RB_AUGMENT(parent); \
  776. } \
  777. else \
  778. { \
  779. RB_ROOT(head) = child; \
  780. } \
  781. \
  782. color: \
  783. if (color == RB_BLACK) \
  784. { \
  785. name##_RB_REMOVE_COLOR(head, parent, child); \
  786. } \
  787. \
  788. return (old); \
  789. } \
  790. \
  791. /* Inserts a node into the RB tree */ \
  792. \
  793. attr struct type *name##_RB_INSERT(struct name *head, struct type *elm) \
  794. { \
  795. struct type *tmp; \
  796. struct type *parent = NULL; \
  797. int comp = 0; \
  798. \
  799. tmp = RB_ROOT(head); \
  800. while (tmp) \
  801. { \
  802. parent = tmp; \
  803. comp = (cmp)(elm, parent); \
  804. if (comp < 0) \
  805. { \
  806. tmp = RB_LEFT(tmp, field); \
  807. } \
  808. else if (comp > 0) \
  809. { \
  810. tmp = RB_RIGHT(tmp, field); \
  811. } \
  812. else \
  813. { \
  814. return (tmp); \
  815. } \
  816. \
  817. } \
  818. \
  819. RB_SET(elm, parent, field); \
  820. if (parent != NULL) \
  821. { \
  822. if (comp < 0) \
  823. { \
  824. RB_LEFT(parent, field) = elm; \
  825. } \
  826. else \
  827. { \
  828. RB_RIGHT(parent, field) = elm; \
  829. } \
  830. \
  831. RB_AUGMENT(parent); \
  832. } \
  833. else \
  834. { \
  835. RB_ROOT(head) = elm; \
  836. } \
  837. \
  838. name##_RB_INSERT_COLOR(head, elm); \
  839. return (NULL); \
  840. } \
  841. \
  842. /* Finds the node with the same key as elm */ \
  843. \
  844. attr struct type *name##_RB_FIND(struct name *head, struct type *elm) \
  845. { \
  846. struct type *tmp = RB_ROOT(head); \
  847. int comp; \
  848. \
  849. while (tmp) \
  850. { \
  851. comp = cmp(elm, tmp); \
  852. if (comp < 0) \
  853. { \
  854. tmp = RB_LEFT(tmp, field); \
  855. } \
  856. else if (comp > 0) \
  857. { \
  858. tmp = RB_RIGHT(tmp, field); \
  859. } \
  860. else \
  861. { \
  862. return (tmp); \
  863. } \
  864. } \
  865. \
  866. return (NULL); \
  867. } \
  868. \
  869. /* Finds the first node greater than or equal to the search key */ \
  870. \
  871. attr struct type *name##_RB_NFIND(struct name *head, struct type *elm) \
  872. { \
  873. struct type *tmp = RB_ROOT(head); \
  874. struct type *res = NULL; \
  875. int comp; \
  876. \
  877. while (tmp) \
  878. { \
  879. comp = cmp(elm, tmp); \
  880. if (comp < 0) \
  881. { \
  882. res = tmp; \
  883. tmp = RB_LEFT(tmp, field); \
  884. } \
  885. else if (comp > 0) \
  886. { \
  887. tmp = RB_RIGHT(tmp, field); \
  888. } \
  889. else \
  890. { \
  891. return (tmp); \
  892. } \
  893. } \
  894. \
  895. return (res); \
  896. } \
  897. \
  898. /* ARGSUSED */ \
  899. \
  900. attr struct type *name##_RB_NEXT(struct type *elm) \
  901. { \
  902. if (RB_RIGHT(elm, field)) \
  903. { \
  904. elm = RB_RIGHT(elm, field); \
  905. while (RB_LEFT(elm, field)) \
  906. { \
  907. elm = RB_LEFT(elm, field); \
  908. } \
  909. } \
  910. else \
  911. { \
  912. if (RB_PARENT(elm, field) && \
  913. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  914. { \
  915. elm = RB_PARENT(elm, field); \
  916. } \
  917. else \
  918. { \
  919. while (RB_PARENT(elm, field) && \
  920. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  921. { \
  922. elm = RB_PARENT(elm, field); \
  923. } \
  924. \
  925. elm = RB_PARENT(elm, field); \
  926. } \
  927. } \
  928. \
  929. return (elm); \
  930. } \
  931. \
  932. /* ARGSUSED */ \
  933. \
  934. attr struct type *name##_RB_PREV(struct type *elm) \
  935. { \
  936. if (RB_LEFT(elm, field)) \
  937. { \
  938. elm = RB_LEFT(elm, field); \
  939. while (RB_RIGHT(elm, field)) \
  940. { \
  941. elm = RB_RIGHT(elm, field); \
  942. } \
  943. } \
  944. else \
  945. { \
  946. if (RB_PARENT(elm, field) && \
  947. (elm == RB_RIGHT(RB_PARENT(elm, field), field))) \
  948. { \
  949. elm = RB_PARENT(elm, field); \
  950. } \
  951. else \
  952. { \
  953. while (RB_PARENT(elm, field) && \
  954. (elm == RB_LEFT(RB_PARENT(elm, field), field))) \
  955. { \
  956. elm = RB_PARENT(elm, field); \
  957. } \
  958. \
  959. elm = RB_PARENT(elm, field); \
  960. } \
  961. } \
  962. \
  963. return (elm); \
  964. } \
  965. \
  966. attr struct type *name##_RB_MINMAX(struct name *head, int val) \
  967. { \
  968. struct type *tmp = RB_ROOT(head); \
  969. struct type *parent = NULL; \
  970. \
  971. while (tmp) \
  972. { \
  973. parent = tmp; \
  974. if (val < 0) \
  975. { \
  976. tmp = RB_LEFT(tmp, field); \
  977. } \
  978. else \
  979. { \
  980. tmp = RB_RIGHT(tmp, field); \
  981. } \
  982. } \
  983. \
  984. return (parent); \
  985. }
  986. #define RB_NEGINF -1
  987. #define RB_INF 1
  988. #define RB_INSERT(name, x, y) name##_RB_INSERT(x, y)
  989. #define RB_REMOVE(name, x, y) name##_RB_REMOVE(x, y)
  990. #define RB_FIND(name, x, y) name##_RB_FIND(x, y)
  991. #define RB_NFIND(name, x, y) name##_RB_NFIND(x, y)
  992. #define RB_NEXT(name, x, y) name##_RB_NEXT(y)
  993. #define RB_PREV(name, x, y) name##_RB_PREV(y)
  994. #define RB_MIN(name, x) name##_RB_MINMAX(x, RB_NEGINF)
  995. #define RB_MAX(name, x) name##_RB_MINMAX(x, RB_INF)
  996. #define RB_FOREACH(x, name, head) \
  997. for ((x) = RB_MIN(name, head); \
  998. (x) != NULL; \
  999. (x) = name##_RB_NEXT(x))
  1000. #define RB_FOREACH_SAFE(x, name, head, y) \
  1001. for ((x) = RB_MIN(name, head); \
  1002. ((x) != NULL) && ((y) = name##_RB_NEXT(x), 1); \
  1003. (x) = (y))
  1004. #define RB_FOREACH_REVERSE(x, name, head) \
  1005. for ((x) = RB_MAX(name, head); \
  1006. (x) != NULL; \
  1007. (x) = name##_RB_PREV(x))
  1008. #define RB_FOREACH_REVERSE_SAFE(x, name, head, y) \
  1009. for ((x) = RB_MAX(name, head); \
  1010. ((x) != NULL) && ((y) = name##_RB_PREV(x), 1); \
  1011. (x) = (y))
  1012. #endif /* __INCLUDE_NUTTX_TREE_H */