rbt_datasrc.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906
  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. const RBNode<T>* successor() const;
  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, const T& data, RBNode<T>* nullnode = NULL);
  81. /// the class isn't left to be inherited
  82. ~RBNode();
  83. //@}
  84. /// \brief copy the DNS related data to another node except the sub domain
  85. /// tree
  86. void cloneDNSData(RBNode<T>& node);
  87. /// \brief when copy the DNS data from one node to another, except the
  88. /// RRsets, name etc,
  89. /// also needs to maintain the down and up relationship, which includes
  90. /// set the down point of current node and up point of sub domain tree
  91. void setDownTree(RBTree<T>* down);
  92. /// data to maintain the rbtree balance
  93. RBNode<T>* parent_;
  94. RBNode<T>* left_;
  95. RBNode<T>* right_;
  96. RBTreeColor color_;
  97. /// data to carry dns info
  98. Name name_;
  99. /// this will make type T should have default constructor
  100. /// without any parameters
  101. T data_;
  102. RBTree<T>* down_;
  103. ///the node won't be returned to end user, if the node is shadow.
  104. ///shadow node is created by rbtree for inner use, it's opaque to
  105. ///end user.
  106. bool is_shadow_;
  107. };
  108. template <typename T>
  109. RBNode<T>::RBNode(const Name& name, const T& data, RBNode* nullnode) :
  110. parent_(nullnode),
  111. left_(nullnode),
  112. right_(nullnode),
  113. color_(RED),
  114. name_(name),
  115. data_(data),
  116. down_(NULL),
  117. is_shadow_(false) {
  118. }
  119. template <typename T>
  120. RBNode<T>::RBNode(const Name& name, RBNode* nullnode) :
  121. parent_(nullnode),
  122. left_(nullnode),
  123. right_(nullnode),
  124. color_(RED),
  125. name_(name),
  126. down_(NULL),
  127. is_shadow_(false) {
  128. }
  129. template <typename T>
  130. RBNode<T>::~RBNode() {
  131. delete down_;
  132. }
  133. template <typename T>
  134. const RBNode<T>*
  135. RBNode<T>::successor() const {
  136. const RBNode<T>* current = this;
  137. // if it has right node, the successor is the left-most node
  138. if (right_ != right_->right_) {
  139. current = right_;
  140. while (current->left_ != current->left_->left_) {
  141. current = current->left_;
  142. }
  143. return (current);
  144. }
  145. // otherwise return the parent without left child or
  146. // current node is not its right child
  147. const RBNode<T>* s = current->parent_;
  148. while (s != s->left_ && current == s->right_) {
  149. current = s;
  150. s = s->parent_;
  151. }
  152. return (s);
  153. }
  154. template <typename T>
  155. void
  156. RBNode<T>::cloneDNSData(RBNode<T>& node) {
  157. node.name_ = name_;
  158. node.data_ = data_;
  159. node.is_shadow_ = is_shadow_;
  160. }
  161. template <typename T>
  162. void
  163. RBNode<T>::setDownTree(RBTree<T>* down) {
  164. down_ = down;
  165. if (down != NULL) {
  166. down->up_ = this;
  167. }
  168. }
  169. /// \brief \c RBTree class represents all the domains with the same suffix,
  170. /// so it can be used to store the domains in one zone.
  171. ///
  172. /// \c RBTree is a generic red black tree, and contains all the nodes with
  173. /// the same suffix, since each
  174. /// name may have sub domain names so \c RBTree is a recursive data structure
  175. /// or tree in tree.
  176. /// So for one zone, several RBTrees may be involved. But from outside, the sub
  177. /// tree is opaque for end users.
  178. template <typename T>
  179. class RBTree : public boost::noncopyable {
  180. friend class RBNode<T>;
  181. public:
  182. /// \brief The return value for the \c find() method
  183. /// - EXACTMATCH: return the node in the tree exactly same with the target
  184. /// - PARTIALMATCH: return the node which is an ancestor of the target
  185. /// which also is the longest match
  186. /// - NOTFOUND: other conditions except EXACTMATCH & FINDREFERRAL
  187. enum FindResult{EXACTMATCH, PARTIALMATCH, NOTFOUND};
  188. /// \name Constructor and Destructor
  189. //@{
  190. RBTree();
  191. /// \b Note: RBTree is not intended to be inherited so the destructor
  192. /// is not virtual
  193. ~RBTree();
  194. //@}
  195. /// \name Inquery methods
  196. //@{
  197. /// \brief Find the node with the name
  198. /// \param name Target to be found
  199. /// \param node Point to the node when the return vaule is \c not
  200. /// NOTFOUND, if the return value is NOTFOUND, the value of node is
  201. /// \c unknown
  202. FindResult find(const Name& name, RBNode<T>** node) const;
  203. FindResult find(const Name& name, const 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>::find(const Name& name, const RBNode<T>** node) const {
  322. const RBTree<T> *tree;
  323. RBNode<T> *target_node;
  324. RBTree<T>::FindResult ret = findHelper(name, &tree, &target_node);
  325. if (ret != NOTFOUND)
  326. *node = target_node;
  327. return ret;
  328. }
  329. template <typename T>
  330. typename RBTree<T>::FindResult
  331. RBTree<T>::findHelper(const Name& name, const RBTree<T>** tree,
  332. RBNode<T>** ret) const
  333. {
  334. RBNode<T>* node = root_;
  335. while (node != NULLNODE) {
  336. const NameComparisonResult compare_result = name.compare(node->name_);
  337. const NameComparisonResult::NameRelation relation =
  338. compare_result.getRelation();
  339. if (relation == NameComparisonResult::EQUAL) {
  340. if (node->is_shadow_) {
  341. return (RBTree<T>::NOTFOUND);
  342. } else {
  343. *tree = this;
  344. *ret = node;
  345. return (RBTree<T>::EXACTMATCH);
  346. }
  347. } else {
  348. const int common_label_count = compare_result.getCommonLabels();
  349. // common label count equal one means, there is no common between
  350. // two names
  351. if (common_label_count == 1) {
  352. node = (compare_result.getOrder() < 0) ?
  353. node->left_ : node->right_;
  354. } else if (NameComparisonResult::SUBDOMAIN == relation) {
  355. if (node->is_shadow_) {
  356. assert(node->down_ != NULL);
  357. return (node->down_->findHelper(name - node->name_, tree,
  358. ret));
  359. } else {
  360. typename RBTree<T>::FindResult result =
  361. RBTree<T>::NOTFOUND;
  362. if (node->down_ != NULL) {
  363. result = node->down_->findHelper(name - node->name_,
  364. tree, ret);
  365. }
  366. // if not found in sub domain tree, so current node is the
  367. // longest match
  368. // otherwise return the result in sub domin tree
  369. if (RBTree<T>::NOTFOUND == result) {
  370. *tree = this;
  371. *ret = node;
  372. return (RBTree<T>::PARTIALMATCH);
  373. } else {
  374. return (result);
  375. }
  376. }
  377. } else {
  378. return (RBTree<T>::NOTFOUND);
  379. }
  380. }
  381. }
  382. return (RBTree<T>::NOTFOUND);
  383. }
  384. template <typename T>
  385. int
  386. RBTree<T>::getNodeCount() const {
  387. return (getNodeCountHelper(root_));
  388. }
  389. template <typename T>
  390. int
  391. RBTree<T>::getNodeCountHelper(const RBNode<T> *node) const {
  392. if (NULLNODE == node) {
  393. return (0);
  394. }
  395. const int sub_tree_node_count =
  396. node->down_ ? node->down_->getNodeCount() : 0;
  397. return (1 + sub_tree_node_count + getNodeCountHelper(node->left_) +
  398. getNodeCountHelper(node->right_));
  399. }
  400. template <typename T>
  401. int
  402. RBTree<T>::getNameCount() const {
  403. return (getNameCountHelper(root_));
  404. }
  405. template <typename T>
  406. int
  407. RBTree<T>::getNameCountHelper(const RBNode<T> *node) const {
  408. if (NULLNODE == node) {
  409. return (0);
  410. }
  411. const int sub_tree_name_count =
  412. node->down_ ? node->down_->getNameCount() : 0;
  413. return ((node->is_shadow_ ? 0 : 1) + sub_tree_name_count +
  414. getNameCountHelper(node->left_) +
  415. getNameCountHelper(node->right_));
  416. }
  417. template <typename T>
  418. int
  419. RBTree<T>::insert(const Name& name, RBNode<T>** new_node) {
  420. RBNode<T>* parent = NULLNODE;
  421. RBNode<T>* current = root_;
  422. int order = -1;
  423. while (current != NULLNODE) {
  424. parent = current;
  425. const NameComparisonResult compare_result =
  426. name.compare(current->name_);
  427. const NameComparisonResult::NameRelation relation =
  428. compare_result.getRelation();
  429. if (relation == NameComparisonResult::EQUAL) {
  430. if (new_node != NULL) {
  431. *new_node = current;
  432. }
  433. // if the node is a common suffix not user inserted, return 0
  434. // otherwise return 1
  435. if (current->is_shadow_) {
  436. current->is_shadow_ = false;
  437. return (0);
  438. } else {
  439. return (1);
  440. }
  441. } else {
  442. const int common_label_count = compare_result.getCommonLabels();
  443. if (common_label_count == 1) {
  444. order = compare_result.getOrder();
  445. current = order < 0 ? current->left_ : current->right_;
  446. } else {
  447. // insert sub domain to sub tree
  448. if (relation == NameComparisonResult::SUBDOMAIN) {
  449. if (NULL == current->down_) {
  450. try {
  451. RBTree<T>* new_sub_tree = new RBTree();
  452. int ret = new_sub_tree->insert(name -
  453. current->name_,
  454. new_node);
  455. if (-1 == ret) {
  456. delete new_sub_tree;
  457. return (-1);
  458. }
  459. current->setDownTree(new_sub_tree);
  460. return (ret);
  461. } catch (std::bad_alloc&) {
  462. return (-1);
  463. }
  464. } else {
  465. return (current->down_->insert(name - current->name_,
  466. new_node));
  467. }
  468. } else {
  469. // for super domain or has common label domain, create
  470. // common node first then insert current name and new name
  471. // into the sub tree
  472. const Name common_ancestor = name.split(
  473. name.getLabelCount() - common_label_count,
  474. common_label_count);
  475. const Name sub_name = current->name_ - common_ancestor;
  476. try {
  477. // create new sub domain tree, and ty to insert
  478. // (current_name - common_ancestor) and (name - common_ancestor)
  479. RBTree<T>* new_sub_tree = new RBTree();
  480. RBNode<T>* sub_root;
  481. if (-1 == new_sub_tree->insert(sub_name, &sub_root)) {
  482. delete new_sub_tree;
  483. return (-1);
  484. }
  485. int ret = 0;
  486. if (name.getLabelCount() != common_label_count) {
  487. ret = new_sub_tree->insert(name - common_ancestor,
  488. new_node);
  489. if (-1 == ret) {
  490. delete new_sub_tree;
  491. return (-1);
  492. }
  493. }
  494. RBTree<T>* down_old = current->down_;
  495. current->setDownTree(new_sub_tree);
  496. current->name_ = common_ancestor;
  497. current->cloneDNSData(*sub_root);
  498. sub_root->setDownTree(down_old);
  499. sub_root->name_ = sub_name;
  500. current->is_shadow_ = true;
  501. if (name.getLabelCount() == common_label_count) {
  502. *new_node = current;
  503. current->is_shadow_ = false;
  504. return (0);
  505. } else {
  506. return (ret);
  507. }
  508. } catch (std::bad_alloc&) {
  509. return (-1);
  510. }
  511. }
  512. }
  513. }
  514. }
  515. try {
  516. RBNode<T>* node = new RBNode<T>(name, NULLNODE);
  517. node->parent_ = parent;
  518. if (parent == NULLNODE) {
  519. root_ = node;
  520. } else if (order < 0) {
  521. parent->left_ = node;
  522. } else {
  523. parent->right_ = node;
  524. }
  525. insertRebalance(node);
  526. if (new_node != NULL) {
  527. *new_node = node;
  528. }
  529. } catch (std::bad_alloc&) {
  530. return (-1);
  531. }
  532. ++node_count_;
  533. return (0);
  534. }
  535. template <typename T>
  536. void
  537. RBTree<T>::insertRebalance(RBNode<T>* node) {
  538. RBNode<T>* uncle;
  539. while (node->parent_->color_ == RED) {
  540. if (node->parent_ == node->parent_->parent_->left_) {
  541. uncle = node->parent_->parent_->right_;
  542. if (uncle->color_ == RED) {
  543. node->parent_->color_ = BLACK;
  544. uncle->color_ = BLACK;
  545. node->parent_->parent_->color_ = RED;
  546. node = node->parent_->parent_;
  547. } else {
  548. if (node == node->parent_->right_) {
  549. node = node->parent_;
  550. leftRotate(node);
  551. }
  552. node->parent_->color_ = BLACK;
  553. node->parent_->parent_->color_ = RED;
  554. rightRotate(node->parent_->parent_);
  555. }
  556. } else {
  557. uncle = node->parent_->parent_->left_;
  558. if (uncle->color_ == RED) {
  559. node->parent_->color_ = BLACK;
  560. uncle->color_ = BLACK;
  561. node->parent_->parent_->color_ = RED;
  562. node = node->parent_->parent_;
  563. } else {
  564. if (node == node->parent_->left_) {
  565. node = node->parent_;
  566. rightRotate(node);
  567. }
  568. node->parent_->color_ = BLACK;
  569. node->parent_->parent_->color_ = RED;
  570. leftRotate(node->parent_->parent_);
  571. }
  572. }
  573. }
  574. root_->color_ = BLACK;
  575. }
  576. template <typename T>
  577. RBNode<T>*
  578. RBTree<T>::leftRotate(RBNode<T>* p) {
  579. RBNode<T>* c = p->right_;
  580. p->right_ = c->left_;
  581. if (c->left_ != NULLNODE) {
  582. c->left_->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->left_ = p;
  593. p->parent_ = c;
  594. return (c);
  595. }
  596. template <typename T>
  597. RBNode<T>*
  598. RBTree<T>::rightRotate(RBNode<T>* p) {
  599. RBNode<T>* c = p->left_;
  600. p->left_ = c->right_;
  601. if (c->right_ != NULLNODE) {
  602. c->right_->parent_ = p;
  603. }
  604. c->parent_ = p->parent_;
  605. if (p->parent_ == NULLNODE) {
  606. root_ = c;
  607. } else if (p == p->parent_->left_) {
  608. p->parent_->left_ = c;
  609. } else {
  610. p->parent_->right_ = c;
  611. }
  612. c->right_ = p;
  613. p->parent_ = c;
  614. return (c);
  615. }
  616. template <typename T>
  617. int
  618. RBTree<T>::erase(const Name& name) {
  619. RBNode<T>* node;
  620. const RBTree<T>* ctree;
  621. if (findHelper(name, &ctree, &node) != RBTree<T>::EXACTMATCH) {
  622. return (1);
  623. }
  624. // for node with downpointer, set it to shadow
  625. if (node->down_ != NULL) {
  626. assert(node->is_shadow_ == false);
  627. node->is_shadow_ = true;
  628. return (0);
  629. }
  630. RBTree<T>* tree = const_cast<RBTree<T>*>(ctree);
  631. tree->eraseNode(node);
  632. if (NULL != tree->up_) {
  633. // merge down to up
  634. if (1 == tree->node_count_ && tree->up_->is_shadow_) {
  635. RBNode<T>* up = tree->up_;
  636. const Name merged_name = tree->root_->name_.concatenate(up->name_);
  637. tree->root_->cloneDNSData(*up);
  638. up->setDownTree(tree->root_->down_);
  639. tree->root_->setDownTree(NULL);
  640. up->name_ = merged_name;
  641. up->is_shadow_ = false;
  642. delete tree;
  643. } else if (0 == tree->node_count_) { // delete empty tree
  644. tree->up_->setDownTree(NULL);
  645. delete tree;
  646. }
  647. }
  648. return (0);
  649. }
  650. template <typename T>
  651. void
  652. RBTree<T>::eraseNode(RBNode<T> *node) {
  653. RBNode<T>* y = NULLNODE;
  654. RBNode<T>* x = NULLNODE;
  655. if (node->left_ == NULLNODE || node->right_ == NULLNODE) {
  656. y = node;
  657. } else {
  658. y = const_cast<RBNode<T>*>(node->successor());
  659. }
  660. if (y->left_ != NULLNODE) {
  661. x = y->left_;
  662. } else {
  663. x = y->right_;
  664. }
  665. x->parent_ = y->parent_;
  666. if (y->parent_ == NULLNODE) {
  667. root_ = x;
  668. } else if (y == y->parent_->left_) {
  669. y->parent_->left_ = x;
  670. } else {
  671. y->parent_->right_ = x;
  672. }
  673. if (y != node) {
  674. y->cloneDNSData(*node);
  675. node->setDownTree(y->down_);
  676. y->down_ = NULL;
  677. }
  678. if (y->color_ == BLACK) {
  679. deleteRebalance(x);
  680. }
  681. y->left_ = NULL;
  682. y->right_ = NULL;
  683. delete y;
  684. --node_count_;
  685. }
  686. template <typename T>
  687. void
  688. RBTree<T>::deleteRebalance(RBNode<T>* node) {
  689. RBNode<T>* w = NULLNODE;
  690. while (node != root_ && node->color_ == BLACK) {
  691. if (node == node->parent_->left_) {
  692. w = node->parent_->right_;
  693. if (w->color_ == RED) {
  694. w->color_ = BLACK;
  695. node->parent_->color_ = RED;
  696. leftRotate(node->parent_);
  697. w = node->parent_->right_;
  698. }
  699. if (w->left_->color_ == BLACK && w->right_->color_ == BLACK) {
  700. w->color_ = RED;
  701. node = node->parent_;
  702. } else {
  703. if (w->right_->color_ == BLACK) {
  704. w->left_->color_ = BLACK;
  705. w->color_ = RED;
  706. rightRotate(w);
  707. w = node->parent_->right_;
  708. }
  709. w->color_ = node->parent_->color_;
  710. node->parent_->color_ = BLACK;
  711. w->right_->color_ = BLACK;
  712. leftRotate(node->parent_);
  713. node = root_;
  714. }
  715. } else {
  716. w = node->parent_->left_;
  717. if (w->color_ == RED) {
  718. w->color_ = BLACK;
  719. node->parent_->color_ = RED;
  720. rightRotate(node->parent_);
  721. w = node->parent_->left_;
  722. }
  723. if (w->right_->color_ == BLACK && w->left_->color_ == BLACK) {
  724. w->color_ = RED;
  725. node = node->parent_;
  726. } else {
  727. if (w->left_->color_ == BLACK) {
  728. w->right_->color_ = BLACK;
  729. w->color_ = RED;
  730. leftRotate(w);
  731. w = node->parent_->left_;
  732. }
  733. w->color_ = node->parent_->color_;
  734. node->parent_->color_ = BLACK;
  735. w->left_->color_ = BLACK;
  736. rightRotate(node->parent_);
  737. node = root_;
  738. }
  739. }
  740. }
  741. node->color_ = BLACK;
  742. }
  743. #define INDNET(os, depth) do { \
  744. int i = 0; \
  745. for (; i < (depth) * 5; ++i) { \
  746. (os) << " "; \
  747. } \
  748. } while(0)
  749. template <typename T>
  750. void
  751. RBTree<T>::printTree(std::ostream &os, int depth) const {
  752. INDNET(os, depth);
  753. os << "tree has node " << node_count_ << "\n";
  754. printTreeHelper(os, root_, depth);
  755. }
  756. template <typename T>
  757. void
  758. RBTree<T>::printTreeHelper(std::ostream& os, const RBNode<T>* node,
  759. int depth) const
  760. {
  761. INDNET(os, depth);
  762. os << node->name_.toText() << " ("
  763. << ((node->color_ == BLACK) ? "black" : "red") << ")\n";
  764. os << ((node->is_shadow_) ? "[invisible] \n" : "\n");
  765. if (node->down_) {
  766. assert(node->down_->up_ == node);
  767. INDNET(os, depth + 1);
  768. os << "begin down from "<< node->name_.toText() << "\n";
  769. node->down_->printTree(os, depth + 1);
  770. INDNET(os, depth + 1);
  771. os << "end down from" << node->name_.toText() <<"\n";
  772. }
  773. if (node->left_ != NULLNODE) {
  774. printTreeHelper(os, node->left_, depth + 1);
  775. } else {
  776. INDNET(os, depth + 1);
  777. os << "NULL\n";
  778. }
  779. if (node->right_ != NULLNODE) {
  780. printTreeHelper(os, node->right_, depth + 1);
  781. } else {
  782. INDNET(os, depth + 1);
  783. os << "NULL\n";
  784. }
  785. }
  786. }
  787. }
  788. #endif // _RBTREE_H
  789. // Local Variables:
  790. // mode: c++
  791. // End: