rbtree.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*========================================================================
  2. //
  3. // rbtree.c
  4. //
  5. // Red Black tree implementation
  6. //
  7. //========================================================================
  8. // ####ECOSGPLCOPYRIGHTBEGIN####
  9. // -------------------------------------------
  10. // This file is part of eCos, the Embedded Configurable Operating System.
  11. // Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
  12. //
  13. // eCos is free software; you can redistribute it and/or modify it under
  14. // the terms of the GNU General Public License as published by the Free
  15. // Software Foundation; either version 2 or (at your option) any later
  16. // version.
  17. //
  18. // eCos is distributed in the hope that it will be useful, but WITHOUT
  19. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  20. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  21. // for more details.
  22. //
  23. // You should have received a copy of the GNU General Public License
  24. // along with eCos; if not, write to the Free Software Foundation, Inc.,
  25. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  26. //
  27. // As a special exception, if other files instantiate templates or use
  28. // macros or inline functions from this file, or you compile this file
  29. // and link it with other works to produce a work based on this file,
  30. // this file does not by itself cause the resulting work to be covered by
  31. // the GNU General Public License. However the source code for this file
  32. // must still be made available in accordance with section (3) of the GNU
  33. // General Public License v2.
  34. //
  35. // This exception does not invalidate any other reasons why a work based
  36. // on this file might be covered by the GNU General Public License.
  37. // -------------------------------------------
  38. // ####ECOSGPLCOPYRIGHTEND####
  39. //========================================================================
  40. //#####DESCRIPTIONBEGIN####
  41. //
  42. // Author(s): Niels Provos/OpenBSD
  43. // Contributors: dwmw2
  44. // Date: 2003-01-21
  45. // Purpose: This file provides an implementation of red-black trees.
  46. // Description: Derived from OpenBSD src/sys/sys/tree.h
  47. // Usage:
  48. //
  49. //####DESCRIPTIONEND####
  50. //
  51. //======================================================================
  52. */
  53. /* $OpenBSD: tree.h,v 1.7 2002/10/17 21:51:54 art Exp $ */
  54. /*
  55. * Copyright 2002 Niels Provos <provos@citi.umich.edu>
  56. * All rights reserved.
  57. *
  58. * Redistribution and use in source and binary forms, with or without
  59. * modification, are permitted provided that the following conditions
  60. * are met:
  61. * 1. Redistributions of source code must retain the above copyright
  62. * notice, this list of conditions and the following disclaimer.
  63. * 2. Redistributions in binary form must reproduce the above copyright
  64. * notice, this list of conditions and the following disclaimer in the
  65. * documentation and/or other materials provided with the distribution.
  66. *
  67. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  68. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  69. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  70. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  71. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  72. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  73. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  74. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  75. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  76. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  77. */
  78. /* Fields renamed to match Linux ones. */
  79. #include <linux/rbtree.h>
  80. #define RB_HEAD(head) (head)->rb_node
  81. #define RB_LEFT(elm) (elm)->rb_left
  82. #define RB_RIGHT(elm) (elm)->rb_right
  83. #define RB_PARENT(elm) (elm)->rb_parent
  84. #define RB_COLOR(elm) (elm)->rb_color
  85. #define RB_SET(elm, parent) do { \
  86. RB_PARENT(elm) = parent; \
  87. RB_LEFT(elm) = RB_RIGHT(elm) = NULL; \
  88. RB_COLOR(elm) = RB_RED; \
  89. } while (0)
  90. #define RB_SET_BLACKRED(black, red) do { \
  91. RB_COLOR(black) = RB_BLACK; \
  92. RB_COLOR(red) = RB_RED; \
  93. } while (0)
  94. #ifndef RB_AUGMENT
  95. #define RB_AUGMENT(x)
  96. #endif
  97. #define RB_ROTATE_LEFT(head, elm, tmp) do { \
  98. (tmp) = RB_RIGHT(elm); \
  99. if ((RB_RIGHT(elm) = RB_LEFT(tmp))) { \
  100. RB_PARENT(RB_LEFT(tmp)) = (elm); \
  101. } \
  102. RB_AUGMENT(elm); \
  103. if ((RB_PARENT(tmp) = RB_PARENT(elm))) { \
  104. if ((elm) == RB_LEFT(RB_PARENT(elm))) \
  105. RB_LEFT(RB_PARENT(elm)) = (tmp); \
  106. else \
  107. RB_RIGHT(RB_PARENT(elm)) = (tmp); \
  108. } else \
  109. (head)->rb_node = (tmp); \
  110. RB_LEFT(tmp) = (elm); \
  111. RB_PARENT(elm) = (tmp); \
  112. RB_AUGMENT(tmp); \
  113. if ((RB_PARENT(tmp))) \
  114. RB_AUGMENT(RB_PARENT(tmp)); \
  115. } while (0)
  116. #define RB_ROTATE_RIGHT(head, elm, tmp) do { \
  117. (tmp) = RB_LEFT(elm); \
  118. if ((RB_LEFT(elm) = RB_RIGHT(tmp))) { \
  119. RB_PARENT(RB_RIGHT(tmp)) = (elm); \
  120. } \
  121. RB_AUGMENT(elm); \
  122. if ((RB_PARENT(tmp) = RB_PARENT(elm))) { \
  123. if ((elm) == RB_LEFT(RB_PARENT(elm))) \
  124. RB_LEFT(RB_PARENT(elm)) = (tmp); \
  125. else \
  126. RB_RIGHT(RB_PARENT(elm)) = (tmp); \
  127. } else \
  128. (head)->rb_node = (tmp); \
  129. RB_RIGHT(tmp) = (elm); \
  130. RB_PARENT(elm) = (tmp); \
  131. RB_AUGMENT(tmp); \
  132. if ((RB_PARENT(tmp))) \
  133. RB_AUGMENT(RB_PARENT(tmp)); \
  134. } while(0)
  135. /* Note args swapped to match Linux */
  136. void rb_insert_color(struct rb_node *elm, struct rb_root *head)
  137. {
  138. struct rb_node *parent, *gparent, *tmp;
  139. while ((parent = RB_PARENT(elm)) &&
  140. RB_COLOR(parent) == RB_RED) {
  141. gparent = RB_PARENT(parent);
  142. if (parent == RB_LEFT(gparent)) {
  143. tmp = RB_RIGHT(gparent);
  144. if (tmp && RB_COLOR(tmp) == RB_RED) {
  145. RB_COLOR(tmp) = RB_BLACK;
  146. RB_SET_BLACKRED(parent, gparent);
  147. elm = gparent;
  148. continue;
  149. }
  150. if (RB_RIGHT(parent) == elm) {
  151. RB_ROTATE_LEFT(head, parent, tmp);
  152. tmp = parent;
  153. parent = elm;
  154. elm = tmp;
  155. }
  156. RB_SET_BLACKRED(parent, gparent);
  157. RB_ROTATE_RIGHT(head, gparent, tmp);
  158. } else {
  159. tmp = RB_LEFT(gparent);
  160. if (tmp && RB_COLOR(tmp) == RB_RED) {
  161. RB_COLOR(tmp) = RB_BLACK;
  162. RB_SET_BLACKRED(parent, gparent);
  163. elm = gparent;
  164. continue;
  165. }
  166. if (RB_LEFT(parent) == elm) {
  167. RB_ROTATE_RIGHT(head, parent, tmp);
  168. tmp = parent;
  169. parent = elm;
  170. elm = tmp;
  171. }
  172. RB_SET_BLACKRED(parent, gparent);
  173. RB_ROTATE_LEFT(head, gparent, tmp);
  174. }
  175. }
  176. RB_COLOR(head->rb_node) = RB_BLACK;
  177. }
  178. static void rb_remove_color(struct rb_root *head, struct rb_node *parent,
  179. struct rb_node *elm)
  180. {
  181. struct rb_node *tmp;
  182. while ((elm == NULL || RB_COLOR(elm) == RB_BLACK) &&
  183. elm != RB_HEAD(head)) {
  184. if (RB_LEFT(parent) == elm) {
  185. tmp = RB_RIGHT(parent);
  186. if (RB_COLOR(tmp) == RB_RED) {
  187. RB_SET_BLACKRED(tmp, parent);
  188. RB_ROTATE_LEFT(head, parent, tmp);
  189. tmp = RB_RIGHT(parent);
  190. }
  191. if ((RB_LEFT(tmp) == NULL ||
  192. RB_COLOR(RB_LEFT(tmp)) == RB_BLACK) &&
  193. (RB_RIGHT(tmp) == NULL ||
  194. RB_COLOR(RB_RIGHT(tmp)) == RB_BLACK)) {
  195. RB_COLOR(tmp) = RB_RED;
  196. elm = parent;
  197. parent = RB_PARENT(elm);
  198. } else {
  199. if (RB_RIGHT(tmp) == NULL ||
  200. RB_COLOR(RB_RIGHT(tmp)) == RB_BLACK) {
  201. struct rb_node *oleft;
  202. if ((oleft = RB_LEFT(tmp)))
  203. RB_COLOR(oleft) = RB_BLACK;
  204. RB_COLOR(tmp) = RB_RED;
  205. RB_ROTATE_RIGHT(head, tmp, oleft);
  206. tmp = RB_RIGHT(parent);
  207. }
  208. RB_COLOR(tmp) = RB_COLOR(parent);
  209. RB_COLOR(parent) = RB_BLACK;
  210. if (RB_RIGHT(tmp))
  211. RB_COLOR(RB_RIGHT(tmp)) = RB_BLACK;
  212. RB_ROTATE_LEFT(head, parent, tmp);
  213. elm = RB_HEAD(head);
  214. break;
  215. }
  216. } else {
  217. tmp = RB_LEFT(parent);
  218. if (RB_COLOR(tmp) == RB_RED) {
  219. RB_SET_BLACKRED(tmp, parent);
  220. RB_ROTATE_RIGHT(head, parent, tmp);
  221. tmp = RB_LEFT(parent);
  222. }
  223. if ((RB_LEFT(tmp) == NULL ||
  224. RB_COLOR(RB_LEFT(tmp)) == RB_BLACK) &&
  225. (RB_RIGHT(tmp) == NULL ||
  226. RB_COLOR(RB_RIGHT(tmp)) == RB_BLACK)) {
  227. RB_COLOR(tmp) = RB_RED;
  228. elm = parent;
  229. parent = RB_PARENT(elm);
  230. } else {
  231. if (RB_LEFT(tmp) == NULL ||
  232. RB_COLOR(RB_LEFT(tmp)) == RB_BLACK) {
  233. struct rb_node *oright;
  234. if ((oright = RB_RIGHT(tmp)))
  235. RB_COLOR(oright) = RB_BLACK;
  236. RB_COLOR(tmp) = RB_RED;
  237. RB_ROTATE_LEFT(head, tmp, oright);
  238. tmp = RB_LEFT(parent);
  239. }
  240. RB_COLOR(tmp) = RB_COLOR(parent);
  241. RB_COLOR(parent) = RB_BLACK;
  242. if (RB_LEFT(tmp))
  243. RB_COLOR(RB_LEFT(tmp)) = RB_BLACK;
  244. RB_ROTATE_RIGHT(head, parent, tmp);
  245. elm = RB_HEAD(head);
  246. break;
  247. }
  248. }
  249. }
  250. if (elm)
  251. RB_COLOR(elm) = RB_BLACK;
  252. }
  253. /* Note name changed. Guess why :) */
  254. void rb_erase(struct rb_node *elm, struct rb_root *head)
  255. {
  256. struct rb_node *child, *parent, *old = elm;
  257. int color;
  258. if (RB_LEFT(elm) == NULL)
  259. child = RB_RIGHT(elm);
  260. else if (RB_RIGHT(elm) == NULL)
  261. child = RB_LEFT(elm);
  262. else {
  263. struct rb_node *left;
  264. elm = RB_RIGHT(elm);
  265. while ((left = RB_LEFT(elm)))
  266. elm = left;
  267. child = RB_RIGHT(elm);
  268. parent = RB_PARENT(elm);
  269. color = RB_COLOR(elm);
  270. if (child)
  271. RB_PARENT(child) = parent;
  272. if (parent) {
  273. if (RB_LEFT(parent) == elm)
  274. RB_LEFT(parent) = child;
  275. else
  276. RB_RIGHT(parent) = child;
  277. RB_AUGMENT(parent);
  278. } else
  279. RB_HEAD(head) = child;
  280. if (RB_PARENT(elm) == old)
  281. parent = elm;
  282. *(elm) = *(old);
  283. if (RB_PARENT(old)) {
  284. if (RB_LEFT(RB_PARENT(old)) == old)
  285. RB_LEFT(RB_PARENT(old)) = elm;
  286. else
  287. RB_RIGHT(RB_PARENT(old)) = elm;
  288. RB_AUGMENT(RB_PARENT(old));
  289. } else
  290. RB_HEAD(head) = elm;
  291. RB_PARENT(RB_LEFT(old)) = elm;
  292. if (RB_RIGHT(old))
  293. RB_PARENT(RB_RIGHT(old)) = elm;
  294. if (parent) {
  295. left = parent;
  296. do {
  297. RB_AUGMENT(left);
  298. } while ((left = RB_PARENT(left)));
  299. }
  300. goto color;
  301. }
  302. parent = RB_PARENT(elm);
  303. color = RB_COLOR(elm);
  304. if (child)
  305. RB_PARENT(child) = parent;
  306. if (parent) {
  307. if (RB_LEFT(parent) == elm)
  308. RB_LEFT(parent) = child;
  309. else
  310. RB_RIGHT(parent) = child;
  311. RB_AUGMENT(parent);
  312. } else
  313. RB_HEAD(head) = child;
  314. color:
  315. if (color == RB_BLACK)
  316. rb_remove_color(head, parent, child);
  317. }
  318. struct rb_node *rb_next(struct rb_node *elm)
  319. {
  320. if (RB_RIGHT(elm)) {
  321. elm = RB_RIGHT(elm);
  322. while (RB_LEFT(elm))
  323. elm = RB_LEFT(elm);
  324. } else {
  325. if (RB_PARENT(elm) &&
  326. (elm == RB_LEFT(RB_PARENT(elm))))
  327. elm = RB_PARENT(elm);
  328. else {
  329. while (RB_PARENT(elm) &&
  330. (elm == RB_RIGHT(RB_PARENT(elm))))
  331. elm = RB_PARENT(elm);
  332. elm = RB_PARENT(elm);
  333. }
  334. }
  335. return (elm);
  336. }
  337. struct rb_node *rb_prev(struct rb_node *elm)
  338. {
  339. if (RB_LEFT(elm)) {
  340. elm = RB_LEFT(elm);
  341. while (RB_RIGHT(elm))
  342. elm = RB_RIGHT(elm);
  343. } else {
  344. if (RB_PARENT(elm) &&
  345. (elm == RB_RIGHT(RB_PARENT(elm))))
  346. elm = RB_PARENT(elm);
  347. else {
  348. while (RB_PARENT(elm) &&
  349. (elm == RB_LEFT(RB_PARENT(elm))))
  350. elm = RB_PARENT(elm);
  351. elm = RB_PARENT(elm);
  352. }
  353. }
  354. return (elm);
  355. }
  356. /* These ones are lifted from Linux -- but that's OK because I
  357. wrote them. dwmw2. */
  358. struct rb_node *rb_first(struct rb_root *root)
  359. {
  360. struct rb_node *n;
  361. n = root->rb_node;
  362. if (!n)
  363. return 0;
  364. while (n->rb_left)
  365. n = n->rb_left;
  366. return n;
  367. }
  368. void rb_replace_node(struct rb_node *victim, struct rb_node *new,
  369. struct rb_root *root)
  370. {
  371. struct rb_node *parent = victim->rb_parent;
  372. /* Set the surrounding nodes to point to the replacement */
  373. if (parent) {
  374. if (victim == parent->rb_left)
  375. parent->rb_left = new;
  376. else
  377. parent->rb_right = new;
  378. } else {
  379. root->rb_node = new;
  380. }
  381. if (victim->rb_left)
  382. victim->rb_left->rb_parent = new;
  383. if (victim->rb_right)
  384. victim->rb_right->rb_parent = new;
  385. /* Copy the pointers/colour from the victim to the replacement */
  386. *new = *victim;
  387. }