rbt_datasrc.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef _RBTREE_H
  15. #define _RBTREE_H 1
  16. #include <dns/name.h>
  17. #include <dns/rrset.h>
  18. #include <dns/rrsetlist.h>
  19. #include <boost/shared_ptr.hpp>
  20. #include <boost/utility.hpp>
  21. #include <exception>
  22. #include <iostream>
  23. using namespace isc::dns;
  24. namespace isc {
  25. namespace datasrc {
  26. namespace {
  27. /// helper function to remove the base domain from super domain
  28. /// the precondition of this function is the super_name contains the
  29. /// sub_name
  30. Name
  31. operator-(const Name& super_name, const Name& sub_name) {
  32. return (super_name.split(0, super_name.getLabelCount() -
  33. sub_name.getLabelCount()));
  34. }
  35. }
  36. /// \brief Define rbtree color
  37. enum RBTreeColor {BLACK = 1, RED};
  38. template <typename T>
  39. class RBTree;
  40. /// \brief \c RBNode class represents one domain name in the domain space
  41. /// It has two roles, the first one is as one node in the \c RBTree,
  42. /// the second one is to store the data related to DNS. As for the first role,
  43. /// it has left, right, parent and color members
  44. /// which is used to keep the balance of the \c RBTree. As for the second role,
  45. // it stores the RRsets that belong to the domain name and a rbtree which
  46. /// includes all the subdomains of this node.
  47. /// The name stored in the node is relative related to its parent node.
  48. /// One special kind of node is non-terminal node
  49. /// which has subdomains with RRset but itself doesn't have any RRsets.
  50. ///
  51. /// \b Note: \c RBNode should be created or destroyed only by \c RBTree so
  52. /// constructor and destructor function aren't exposed.
  53. template <typename T>
  54. class RBNode : public boost::noncopyable {
  55. public:
  56. /// only /c RBTree can create and destroy \c RBNode
  57. friend class RBTree<T>;
  58. /// \name Test functions
  59. //@{
  60. /// \brief return the name of current node, it's relative to its parents
  61. //
  62. /// \todo Is it meaningful to return the absolute of the node?
  63. const Name& getName() const {return (name_); }
  64. // \breif return the data store in this node
  65. T& getData() { return (data_);}
  66. // \brief return next node whose name is bigger than current node
  67. RBNode<T>* successor();
  68. //@}
  69. /// \name Modify functions
  70. // \brief set the data stored in the node
  71. void setData(const T& data) { data_ = data;}
  72. private:
  73. /// \name Constructors and destructor
  74. //@{
  75. /// \param nullnode The null point for \c RBNode isnot use \c NULL, but
  76. /// use one specified
  77. /// default node singled as null node, this is intended to keep the code
  78. /// more unify
  79. RBNode(const Name &name, RBNode<T> *nullnode = NULL);
  80. RBNode(const Name& name, T& data,
  81. RBNode<T>* nullnode = NULL);
  82. /// the class isn't left to be inherited
  83. ~RBNode();
  84. //@}
  85. /// \brief copy the DNS related data to another node except the sub domain
  86. /// tree
  87. void cloneDNSData(RBNode<T>& node);
  88. /// \brief when copy the DNS data from one node to another, except the
  89. /// RRsets, name etc,
  90. /// also needs to maintain the down and up relationship, which includes
  91. /// set the down point of current node and up point of sub domain tree
  92. void setDownTree(RBTree<T>* down);
  93. /// data to maintain the rbtree balance
  94. RBNode<T>* parent_;
  95. RBNode<T>* left_;
  96. RBNode<T>* right_;
  97. RBTreeColor color_;
  98. /// data to carry dns info
  99. Name name_;
  100. /// this will make type T should have default constructor
  101. /// without any parameters
  102. T data_;
  103. RBTree<T>* down_;
  104. ///the node willn't return to end user, if the node is shadow
  105. ///shadow node is created by rbtree for inner use, it's opaque to
  106. ///end user.
  107. bool is_shadow_;
  108. };
  109. template <typename T>
  110. RBNode<T>::RBNode(const Name& name, T &data, RBNode* nullnode) :
  111. parent_(nullnode),
  112. left_(nullnode),
  113. right_(nullnode),
  114. color_(RED),
  115. name_(name),
  116. data_(data),
  117. down_(NULL),
  118. is_shadow_(false) {
  119. }
  120. template <typename T>
  121. RBNode<T>::RBNode(const Name& name, RBNode* nullnode) :
  122. parent_(nullnode),
  123. left_(nullnode),
  124. right_(nullnode),
  125. color_(RED),
  126. name_(name),
  127. down_(NULL),
  128. is_shadow_(false) {
  129. }
  130. template <typename T>
  131. RBNode<T>::~RBNode() {
  132. delete down_;
  133. }
  134. template <typename T>
  135. RBNode<T>*
  136. RBNode<T>::successor() {
  137. RBNode<T>* current = this;
  138. // if it has right node, the successor is the left-most node
  139. if (right_ != right_->right_) {
  140. current = right_;
  141. while (current->left_ != current->left_->left_) {
  142. current = current->left_;
  143. }
  144. return (current);
  145. }
  146. // otherwise return the parent without left child or
  147. // current node is not its right child
  148. RBNode<T>* s = current->parent_;
  149. while (s != s->left_ && current == s->right_) {
  150. current = s;
  151. s = s->parent_;
  152. }
  153. return (s);
  154. }
  155. template <typename T>
  156. void
  157. RBNode<T>::cloneDNSData(RBNode<T>& node) {
  158. node.name_ = name_;
  159. node.data_ = data_;
  160. node.is_shadow_ = is_shadow_;
  161. }
  162. template <typename T>
  163. void
  164. RBNode<T>::setDownTree(RBTree<T>* down) {
  165. down_ = down;
  166. if (down) {
  167. down->up_ = this;
  168. }
  169. }
  170. /// \brief \c RBTree class represents all the domains with the same suffix,
  171. /// so it can be used to store the domains in one zone.
  172. ///
  173. /// \c RBTree is a generic red black tree, and contains all the nodes with
  174. /// the same suffix, since each
  175. /// name may have sub domain names so \c RBTree is a recursive data structure
  176. /// or tree in tree.
  177. /// So for one zone, several RBTrees may be involved. But from outside, the sub
  178. /// tree is opaque for end users.
  179. template <typename T>
  180. class RBTree : public boost::noncopyable {
  181. friend class RBNode<T>;
  182. public:
  183. /// \brief The return value for the \c find() method
  184. /// - EXACTMATCH: return the node in the tree exactly same with the target
  185. /// - PARTIALMATCH: return the node which is an ancestor of the target
  186. /// which also is the longest match
  187. /// - NOTFOUND: other conditions except EXACTMATCH & FINDREFERRAL
  188. enum FindResult{EXACTMATCH, PARTIALMATCH, NOTFOUND};
  189. /// \name Constructor and Destructor
  190. //@{
  191. RBTree();
  192. /// \b Note: RBTree is not intended to be inherited so the destructor
  193. /// is not virtual
  194. ~RBTree();
  195. //@}
  196. /// \name Inquery methods
  197. //@{
  198. /// \brief Find the node with the name
  199. /// \param name Target to be found
  200. /// \param node Point to the node when the return vaule is \c not
  201. /// NOTFOUND, if the return value is NOTFOUND, the value of node is
  202. /// \c unknown
  203. FindResult find(const Name& name, RBNode<T>** node) const;
  204. /// \brief Get the total node count in the tree
  205. /// the node count including the node created common suffix node
  206. int getNodeCount() const;
  207. /// \brief Get the total names inserted into the tree
  208. int getNameCount() const;
  209. //@}
  210. /// \name Debug function
  211. //@{
  212. /// \brief print the nodes in the trees
  213. /// \todo is it better to return one string instead of print to the stdout?
  214. void printTree(std::ostream &os, int depth = 0) const;
  215. //@}
  216. /// \name Modify function
  217. //@{
  218. /// \brief Insert the domain name into the tree
  219. /// \param name The name to be inserted into the tree
  220. /// \param inserted_node If no node with the name in the tree,
  221. /// new \c RBNode will be created, otherwise nothing will be done.
  222. /// Anyway the pointer point to the node with the name will be assigned to
  223. /// inserted_node
  224. /// \return return 0 means no node exists in the tree with the name before
  225. /// insert; return 1 means already has the node with the given name
  226. /// return -1 means no memory left to allocate new node
  227. //
  228. /// To add an RRset into one node when it's not known whether the node
  229. /// already exists, it is better to call \c insert and then call the
  230. /// RBNode interface instead of calling \c find().
  231. int insert(const Name& name, RBNode<T>** inserted_node);
  232. /// \brief Erase the node with the domain name
  233. /// \return If no node with the name, return 1; otherwise return 0
  234. int erase(const Name& name);
  235. //@}
  236. private:
  237. /// \name RBTree balance functions
  238. //@{
  239. void deleteRebalance(RBNode<T>* node);
  240. void insertRebalance(RBNode<T>* node);
  241. RBNode<T>* rightRotate(RBNode<T>* p);
  242. RBNode<T>* leftRotate(RBNode<T>* p);
  243. //@}
  244. /// \name Helper functions
  245. //@{
  246. /// Each public function has related recursive helper function
  247. void eraseNode(RBNode<T>* node);
  248. FindResult findHelper(const Name& name, const RBTree<T>** tree,
  249. RBNode<T>** node) const;
  250. int getNodeCountHelper(const RBNode<T>* node) const;
  251. int getNameCountHelper(const RBNode<T>* node) const;
  252. void printTreeHelper(std::ostream &os, const RBNode<T>* node, int depth) const;
  253. //@}
  254. RBNode<T>* root_;
  255. RBNode<T>* NULLNODE;
  256. RBNode<T>* up_;
  257. /// the node count of current tree except the sub domain trees
  258. unsigned int node_count_;
  259. };
  260. /*
  261. with the following names:
  262. a x.d.e.f o.w.y.d.e.f
  263. b z.d.e.f p.w.y.d.e.f
  264. c g.h q.w.y.d.e.f
  265. the tree will looks like:
  266. b
  267. / \
  268. a d.e.f
  269. /|\
  270. c | g.h
  271. |
  272. w.y
  273. /|\
  274. x | z
  275. |
  276. p
  277. / \
  278. o q
  279. */
  280. template <typename T>
  281. RBTree<T>::RBTree() {
  282. NULLNODE = new RBNode<T>(Name(" "));
  283. NULLNODE->parent_ = NULLNODE->left_ = NULLNODE->right_ = NULLNODE;
  284. NULLNODE->color_ = BLACK;
  285. root_ = NULLNODE;
  286. node_count_ = 0;
  287. up_ = NULL;
  288. }
  289. template <typename T>
  290. RBTree<T>::~RBTree() {
  291. assert(root_ != NULL);
  292. delete NULLNODE;
  293. if (NULLNODE == root_) {
  294. return;
  295. }
  296. RBNode<T>* node = root_;
  297. while (root_->left_ != NULLNODE || root_->right_ != NULLNODE) {
  298. while (node->left_ != NULLNODE || node->right_ != NULLNODE) {
  299. node = (node->left_ != NULLNODE) ? node->left_ : node->right_;
  300. }
  301. RBNode<T>* parent = node->parent_;
  302. if (parent->left_ == node) {
  303. parent->left_ = NULLNODE;
  304. } else {
  305. parent->right_ = NULLNODE;
  306. }
  307. delete node;
  308. node = parent;
  309. }
  310. delete root_;
  311. root_ = NULL;
  312. }
  313. template <typename T>
  314. typename RBTree<T>::FindResult
  315. RBTree<T>::find(const Name& name, RBNode<T>** node) const {
  316. const RBTree<T>* tree;
  317. return (findHelper(name, &tree, node));
  318. }
  319. template <typename T>
  320. typename RBTree<T>::FindResult
  321. RBTree<T>::findHelper(const Name& name, const RBTree<T>** tree, RBNode<T>** ret) const {
  322. RBNode<T>* node = root_;
  323. while (node != NULLNODE) {
  324. NameComparisonResult compare_result = name.compare(node->name_);
  325. NameComparisonResult::NameRelation relation =
  326. compare_result.getRelation();
  327. if (relation == NameComparisonResult::EQUAL) {
  328. if (node->is_shadow_) {
  329. return (RBTree<T>::NOTFOUND);
  330. } else {
  331. *tree = this;
  332. *ret = node;
  333. return (RBTree<T>::EXACTMATCH);
  334. }
  335. } else {
  336. int common_label_count = compare_result.getCommonLabels();
  337. // common label count equal one means, there is no common between
  338. // two names
  339. if (common_label_count == 1) {
  340. node = (compare_result.getOrder() < 0) ?
  341. node->left_ : node->right_;
  342. } else if (NameComparisonResult::SUBDOMAIN == relation) {
  343. if (node->is_shadow_) {
  344. assert(node->down_);
  345. return (node->down_->findHelper(name - node->name_, tree,
  346. ret));
  347. } else {
  348. RBTree<T>::FindResult result = RBTree<T>::NOTFOUND;
  349. if (node->down_) {
  350. result = node->down_->findHelper(name - node->name_, tree,
  351. ret);
  352. }
  353. // if not found in sub domain tree, so current node is the longest match
  354. // otherwise return the result in sub domin tree
  355. if (RBTree<T>::NOTFOUND == result) {
  356. *tree = this;
  357. *ret = node;
  358. return RBTree<T>::PARTIALMATCH;
  359. } else {
  360. return result;
  361. }
  362. }
  363. } else {
  364. return (RBTree<T>::NOTFOUND);
  365. }
  366. }
  367. }
  368. return (RBTree<T>::NOTFOUND);
  369. }
  370. template <typename T>
  371. int
  372. RBTree<T>::getNodeCount() const {
  373. return (getNodeCountHelper(root_));
  374. }
  375. template <typename T>
  376. int
  377. RBTree<T>::getNodeCountHelper(const RBNode<T> *node) const {
  378. if (NULLNODE == node) {
  379. return (0);
  380. }
  381. int sub_tree_node_count = node->down_ ? node->down_->getNodeCount() : 0;
  382. return (1 + sub_tree_node_count + getNodeCountHelper(node->left_) +
  383. getNodeCountHelper(node->right_));
  384. }
  385. template <typename T>
  386. int
  387. RBTree<T>::getNameCount() const {
  388. return (getNameCountHelper(root_));
  389. }
  390. template <typename T>
  391. int
  392. RBTree<T>::getNameCountHelper(const RBNode<T> *node) const {
  393. if (NULLNODE == node) {
  394. return (0);
  395. }
  396. int sub_tree_name_count = node->down_ ? node->down_->getNameCount() : 0;
  397. return ((node->is_shadow_ ? 0 : 1) + sub_tree_name_count + getNameCountHelper(node->left_) +
  398. getNameCountHelper(node->right_));
  399. }
  400. template <typename T>
  401. int
  402. RBTree<T>::insert(const Name& name, RBNode<T>** new_node) {
  403. RBNode<T>* parent = NULLNODE;
  404. RBNode<T>* current = root_;
  405. int order = -1;
  406. while (current != NULLNODE) {
  407. parent = current;
  408. NameComparisonResult compare_result = name.compare(current->name_);
  409. NameComparisonResult::NameRelation relation =
  410. compare_result.getRelation();
  411. if (relation == NameComparisonResult::EQUAL) {
  412. if (new_node) {
  413. *new_node = current;
  414. }
  415. // if the node is a common suffix not user inserted, return 0
  416. // otherwise return 1
  417. if (current->is_shadow_) {
  418. current->is_shadow_ = false;
  419. return (0);
  420. } else {
  421. return (1);
  422. }
  423. } else {
  424. int common_label_count = compare_result.getCommonLabels();
  425. if (common_label_count == 1) {
  426. order = compare_result.getOrder();
  427. current = order < 0 ? current->left_ : current->right_;
  428. } else {
  429. // insert sub domain to sub tree
  430. if (relation == NameComparisonResult::SUBDOMAIN) {
  431. if (NULL == current->down_) {
  432. try {
  433. RBTree<T>* new_sub_tree = new RBTree();
  434. int ret = new_sub_tree->insert(name - current->name_,
  435. new_node);
  436. if (-1 == ret) {
  437. delete new_sub_tree;
  438. return (-1);
  439. }
  440. current->setDownTree(new_sub_tree);
  441. return (ret);
  442. } catch (std::bad_alloc &) {
  443. return (-1);
  444. }
  445. } else {
  446. return current->down_->insert(name - current->name_,
  447. new_node);
  448. }
  449. } else {
  450. // for super domain or has common label domain, create
  451. // common node first then insert current name and new name
  452. // into the sub tree
  453. Name common_ancestor = name.split(
  454. name.getLabelCount() - common_label_count,
  455. common_label_count);
  456. Name sub_name = current->name_ - common_ancestor;
  457. try {
  458. // create new sub domain tree, and ty to insert
  459. // (current_name - common_ancestor) and (name - common_ancestor)
  460. RBTree<T>* new_sub_tree = new RBTree();
  461. RBNode<T>* sub_root;
  462. if ( -1 == new_sub_tree->insert(sub_name, &sub_root)) {
  463. delete new_sub_tree;
  464. return (-1);
  465. }
  466. int ret = 0;
  467. if (name.getLabelCount() != common_label_count) {
  468. ret = new_sub_tree->insert(name - common_ancestor, new_node);
  469. if (-1 == ret) {
  470. delete new_sub_tree;
  471. return (-1);
  472. }
  473. }
  474. RBTree<T>* down_old = current->down_;
  475. current->setDownTree(new_sub_tree);
  476. current->name_ = common_ancestor;
  477. current->cloneDNSData(*sub_root);
  478. sub_root->setDownTree(down_old);
  479. sub_root->name_ = sub_name;
  480. current->is_shadow_ = true;
  481. if (name.getLabelCount() == common_label_count) {
  482. *new_node = current;
  483. current->is_shadow_ = false;
  484. return (0);
  485. } else {
  486. return ret;
  487. }
  488. } catch (std::bad_alloc &) {
  489. return (-1);
  490. }
  491. }
  492. }
  493. }
  494. }
  495. try {
  496. RBNode<T>* node = new RBNode<T>(name, NULLNODE);
  497. node->parent_ = parent;
  498. if (parent == NULLNODE) {
  499. root_ = node;
  500. } else if (order < 0) {
  501. parent->left_ = node;
  502. } else {
  503. parent->right_ = node;
  504. }
  505. insertRebalance(node);
  506. if (new_node) {
  507. *new_node = node;
  508. }
  509. } catch (std::bad_alloc &) {
  510. return (-1);
  511. }
  512. ++node_count_;
  513. return (0);
  514. }
  515. template <typename T>
  516. void
  517. RBTree<T>::insertRebalance(RBNode<T>* node) {
  518. RBNode<T>* uncle;
  519. while (node->parent_->color_ == RED) {
  520. if (node->parent_ == node->parent_->parent_->left_) {
  521. uncle = node->parent_->parent_->right_;
  522. if (uncle->color_ == RED) {
  523. node->parent_->color_ = BLACK;
  524. uncle->color_ = BLACK;
  525. node->parent_->parent_->color_ = RED;
  526. node = node->parent_->parent_;
  527. } else {
  528. if (node == node->parent_->right_) {
  529. node = node->parent_;
  530. leftRotate(node);
  531. }
  532. node->parent_->color_ = BLACK;
  533. node->parent_->parent_->color_ = RED;
  534. rightRotate(node->parent_->parent_);
  535. }
  536. } else {
  537. uncle = node->parent_->parent_->left_;
  538. if (uncle->color_ == RED) {
  539. node->parent_->color_ = BLACK;
  540. uncle->color_ = BLACK;
  541. node->parent_->parent_->color_ = RED;
  542. node = node->parent_->parent_;
  543. } else {
  544. if (node == node->parent_->left_) {
  545. node = node->parent_;
  546. rightRotate(node);
  547. }
  548. node->parent_->color_ = BLACK;
  549. node->parent_->parent_->color_ = RED;
  550. leftRotate(node->parent_->parent_);
  551. }
  552. }
  553. }
  554. root_->color_ = BLACK;
  555. }
  556. template <typename T>
  557. RBNode<T>*
  558. RBTree<T>::leftRotate(RBNode<T>* p) {
  559. RBNode<T>* c = p->right_;
  560. p->right_ = c->left_;
  561. if (c->left_ != NULLNODE) {
  562. c->left_->parent_ = p;
  563. }
  564. c->parent_ = p->parent_;
  565. if (p->parent_ == NULLNODE) {
  566. root_ = c;
  567. } else if (p == p->parent_->left_) {
  568. p->parent_->left_ = c;
  569. } else {
  570. p->parent_->right_ = c;
  571. }
  572. c->left_ = p;
  573. p->parent_ = c;
  574. return (c);
  575. }
  576. template <typename T>
  577. RBNode<T>*
  578. RBTree<T>::rightRotate(RBNode<T>* p) {
  579. RBNode<T>* c = p->left_;
  580. p->left_ = c->right_;
  581. if (c->right_ != NULLNODE) {
  582. c->right_->parent_ = p;
  583. }
  584. c->parent_ = p->parent_;
  585. if (p->parent_ == NULLNODE) {
  586. root_ = c;
  587. } else if (p == p->parent_->left_) {
  588. p->parent_->left_ = c;
  589. } else {
  590. p->parent_->right_ = c;
  591. }
  592. c->right_ = p;
  593. p->parent_ = c;
  594. return (c);
  595. }
  596. template <typename T>
  597. int
  598. RBTree<T>::erase(const Name& name) {
  599. RBNode<T>* node;
  600. const RBTree<T>* ctree;
  601. if (findHelper(name, &ctree, &node) != RBTree<T>::EXACTMATCH) {
  602. return (1);
  603. }
  604. // for node with downpointer, set it to shadow
  605. if (node->down_ != NULL) {
  606. assert(node->is_shadow_ == false);
  607. node->is_shadow_ = true;
  608. return (0);
  609. }
  610. RBTree<T> *tree = const_cast<RBTree<T> *>(ctree);
  611. tree->eraseNode(node);
  612. if (NULL != tree->up_) {
  613. // merge down to up
  614. if (1 == tree->node_count_ && tree->up_->is_shadow_) {
  615. RBNode<T>* up = tree->up_;
  616. Name merged_name = tree->root_->name_.concatenate(up->name_);
  617. tree->root_->cloneDNSData(*up);
  618. up->setDownTree(tree->root_->down_);
  619. tree->root_->setDownTree(NULL);
  620. up->name_ = merged_name;
  621. up->is_shadow_ = false;
  622. delete tree;
  623. } else if (0 == tree->node_count_) { // delete empty tree
  624. tree->up_->setDownTree(NULL);
  625. delete tree;
  626. }
  627. }
  628. return 0;
  629. }
  630. template <typename T>
  631. void
  632. RBTree<T>::eraseNode(RBNode<T> *node) {
  633. RBNode<T>* y = NULLNODE;
  634. RBNode<T>* x = NULLNODE;
  635. if (node->left_ == NULLNODE || node->right_ == NULLNODE) {
  636. y = node;
  637. } else {
  638. y = node->successor();
  639. }
  640. if (y->left_ != NULLNODE) {
  641. x = y->left_;
  642. } else {
  643. x = y->right_;
  644. }
  645. x->parent_ = y->parent_;
  646. if (y->parent_ == NULLNODE) {
  647. root_ = x;
  648. } else if (y == y->parent_->left_) {
  649. y->parent_->left_ = x;
  650. } else {
  651. y->parent_->right_ = x;
  652. }
  653. if (y != node) {
  654. y->cloneDNSData(*node);
  655. node->setDownTree(y->down_);
  656. y->down_ = NULL;
  657. }
  658. if (y->color_ == BLACK) {
  659. deleteRebalance(x);
  660. }
  661. y->left_ = NULL;
  662. y->right_ = NULL;
  663. delete y;
  664. --node_count_;
  665. }
  666. template <typename T>
  667. void
  668. RBTree<T>::deleteRebalance(RBNode<T>* node) {
  669. RBNode<T>* w = NULLNODE;
  670. while (node != root_ && node->color_ == BLACK) {
  671. if (node == node->parent_->left_) {
  672. w = node->parent_->right_;
  673. if (w->color_ == RED) {
  674. w->color_ = BLACK;
  675. node->parent_->color_ = RED;
  676. leftRotate(node->parent_);
  677. w = node->parent_->right_;
  678. }
  679. if (w->left_->color_ == BLACK && w->right_->color_ == BLACK) {
  680. w->color_ = RED;
  681. node = node->parent_;
  682. } else {
  683. if (w->right_->color_ == BLACK) {
  684. w->left_->color_ = BLACK;
  685. w->color_ = RED;
  686. rightRotate(w);
  687. w = node->parent_->right_;
  688. }
  689. w->color_ = node->parent_->color_;
  690. node->parent_->color_ = BLACK;
  691. w->right_->color_ = BLACK;
  692. leftRotate(node->parent_);
  693. node = root_;
  694. }
  695. } else {
  696. w = node->parent_->left_;
  697. if (w->color_ == RED) {
  698. w->color_ = BLACK;
  699. node->parent_->color_ = RED;
  700. rightRotate(node->parent_);
  701. w = node->parent_->left_;
  702. }
  703. if (w->right_->color_ == BLACK && w->left_->color_ == BLACK) {
  704. w->color_ = RED;
  705. node = node->parent_;
  706. } else {
  707. if (w->left_->color_ == BLACK) {
  708. w->right_->color_ = BLACK;
  709. w->color_ = RED;
  710. leftRotate(w);
  711. w = node->parent_->left_;
  712. }
  713. w->color_ = node->parent_->color_;
  714. node->parent_->color_ = BLACK;
  715. w->left_->color_ = BLACK;
  716. rightRotate(node->parent_);
  717. node = root_;
  718. }
  719. }
  720. }
  721. node->color_ = BLACK;
  722. }
  723. #define INDNET(os, depth) do { \
  724. int i = 0; \
  725. for (; i < (depth) * 5; ++i) { \
  726. (os) << " "; \
  727. } \
  728. } while(0)
  729. template <typename T>
  730. void
  731. RBTree<T>::printTree(std::ostream &os, int depth) const {
  732. INDNET(os, depth);
  733. os << "tree has node " << node_count_ << "\n";
  734. printTreeHelper(os, root_, depth);
  735. }
  736. template <typename T>
  737. void
  738. RBTree<T>::printTreeHelper(std::ostream &os, const RBNode<T>* node, int depth) const {
  739. INDNET(os, depth);
  740. os << node->name_.toText() << " ("
  741. << ((node->color_ == BLACK) ? "black" : "red") << ")\n";
  742. os << ((node->is_shadow_) ? "[invisible] \n" : "\n");
  743. if (node->down_) {
  744. assert(node->down_->up_ == node);
  745. INDNET(os, depth + 1);
  746. os << "begin down from "<< node->name_.toText() << "\n";
  747. node->down_->printTree(os, depth + 1);
  748. INDNET(os, depth + 1);
  749. os << "end down from" << node->name_.toText() <<"\n";
  750. }
  751. if (node->left_ != NULLNODE) {
  752. printTreeHelper(os, node->left_, depth + 1);
  753. } else {
  754. INDNET(os, depth + 1);
  755. os << "NULL\n";
  756. }
  757. if (node->right_ != NULLNODE) {
  758. printTreeHelper(os, node->right_, depth + 1);
  759. } else {
  760. INDNET(os, depth + 1);
  761. os << "NULL\n";
  762. }
  763. }
  764. }
  765. }
  766. #endif // _RBTREE_H
  767. // Local Variables:
  768. // mode: c++
  769. // End: