<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/> <title>C++ API frePPLe: name.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> <link href="../styles.css" rel="stylesheet" type="text/css"> </head> <body> <div id="container"> <div id="menubar"> <div id="logo" align="center"> <br/><img src='../frepple.bmp' alt="frepple" /><br/> <a href='http://www.frepple.com/'> <strong>a Free<br/>Production Planning<br/>Library</strong> </a> </div> <div id="menu"> <br/> <h3><a href='../Main/HomePage.html'>Main</a></h3> <h3><a href='../UI/Main.html'>User Manual</a></h3> <h3><a href='../Tutorial/Main.html'>Tutorial</a></h3> <h3><a href='../Frepple/Main.html'>Reference Manual</a></h3> <h3><a href='../Main/FAQ.html'>FAQ</a></h3> <h3><a href='index.html'>C++ API</a></h3> <br/> </div> </div> <div id="content"> <br/> <!-- Generated by Doxygen 1.6.1 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.html"><span>Main Page</span></a></li> <li><a href="pages.html"><span>Related Pages</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>File Members</span></a></li> </ul> </div> <div class="navpath"><a class="el" href="dir_88117dff2735251a69c8eb9084477714.html">src</a> » <a class="el" href="dir_dc63a72a31e1c272514e3670d5c54e12.html">utils</a> </div> </div> <div class="contents"> <h1>name.cpp</h1><a href="a00222.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/***************************************************************************</span> <a name="l00002"></a>00002 <span class="comment"> file : $URL: https://frepple.svn.sourceforge.net/svnroot/frepple/trunk/src/utils/name.cpp $</span> <a name="l00003"></a>00003 <span class="comment"> version : $LastChangedRevision: 1315 $ $LastChangedBy: jdetaeye $</span> <a name="l00004"></a>00004 <span class="comment"> date : $LastChangedDate: 2010-07-17 18:08:53 +0200 (Sat, 17 Jul 2010) $</span> <a name="l00005"></a>00005 <span class="comment"> ***************************************************************************/</span> <a name="l00006"></a>00006 <a name="l00007"></a>00007 <span class="comment">/***************************************************************************</span> <a name="l00008"></a>00008 <span class="comment"> * *</span> <a name="l00009"></a>00009 <span class="comment"> * Copyright (C) 2007-2010 by Johan De Taeye *</span> <a name="l00010"></a>00010 <span class="comment"> * *</span> <a name="l00011"></a>00011 <span class="comment"> * This library is free software; you can redistribute it and/or modify it *</span> <a name="l00012"></a>00012 <span class="comment"> * under the terms of the GNU Lesser General Public License as published *</span> <a name="l00013"></a>00013 <span class="comment"> * by the Free Software Foundation; either version 2.1 of the License, or *</span> <a name="l00014"></a>00014 <span class="comment"> * (at your option) any later version. *</span> <a name="l00015"></a>00015 <span class="comment"> * *</span> <a name="l00016"></a>00016 <span class="comment"> * This library is distributed in the hope that it will be useful, *</span> <a name="l00017"></a>00017 <span class="comment"> * but WITHOUT ANY WARRANTY; without even the implied warranty of *</span> <a name="l00018"></a>00018 <span class="comment"> * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *</span> <a name="l00019"></a>00019 <span class="comment"> * General Public License for more details. *</span> <a name="l00020"></a>00020 <span class="comment"> * *</span> <a name="l00021"></a>00021 <span class="comment"> * You should have received a copy of the GNU Lesser General Public *</span> <a name="l00022"></a>00022 <span class="comment"> * License along with this library; if not, write to the Free Software *</span> <a name="l00023"></a>00023 <span class="comment"> * Foundation Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,*</span> <a name="l00024"></a>00024 <span class="comment"> * USA *</span> <a name="l00025"></a>00025 <span class="comment"> * *</span> <a name="l00026"></a>00026 <span class="comment"> ***************************************************************************/</span> <a name="l00027"></a>00027 <a name="l00028"></a><a class="code" href="a00222.html#a342983a479ae7a921be5face83fdb029">00028</a> <span class="preprocessor">#define FREPPLE_CORE</span> <a name="l00029"></a>00029 <span class="preprocessor"></span><span class="preprocessor">#include "<a class="code" href="a00252.html" title="Header file for auxilary classes.">frepple/utils.h</a>"</span> <a name="l00030"></a>00030 <a name="l00031"></a>00031 <span class="keyword">namespace </span>frepple <a name="l00032"></a>00032 { <a name="l00033"></a>00033 <span class="keyword">namespace </span>utils <a name="l00034"></a>00034 { <a name="l00035"></a>00035 <a name="l00036"></a><a class="code" href="a00181.html#ad31e0db332aa55c29567d28b4f47a2d4">00036</a> <a class="code" href="a00252.html#a99b94ab8c2f2a32277559c7b2b0a054c">DECLARE_EXPORT</a> <span class="keywordtype">void</span> <a class="code" href="a00181.html#ad31e0db332aa55c29567d28b4f47a2d4">Tree::clear</a>() <a name="l00037"></a>00037 { <a name="l00038"></a>00038 <span class="comment">// Tree is already empty</span> <a name="l00039"></a>00039 <span class="keywordflow">if</span> (<a class="code" href="a00181.html#a8c11861060dcf9ee45054b73c26ee77d">empty</a>()) <span class="keywordflow">return</span>; <a name="l00040"></a>00040 <a name="l00041"></a>00041 <span class="comment">// Locking the tree is not required: the delete method locks</span> <a name="l00042"></a>00042 <span class="comment">// the tree during the deletion.</span> <a name="l00043"></a>00043 <span class="comment">//ScopeMutexLock l(treeaccess);</span> <a name="l00044"></a>00044 <a name="l00045"></a>00045 <span class="comment">// Erase all elements</span> <a name="l00046"></a>00046 <span class="keywordflow">for</span> (<a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* x = <a class="code" href="a00181.html#ad44c460c1fb9648e83d703e6cdc64d19">begin</a>(); x != <a class="code" href="a00181.html#a3c12d98627c9d46864dfd6b5e994878c">end</a>(); x = <a class="code" href="a00181.html#ad44c460c1fb9648e83d703e6cdc64d19">begin</a>()) <a name="l00047"></a>00047 { <a name="l00048"></a>00048 <a class="code" href="a00120.html" title="Object is the abstract base class for the main entities.">Object</a> *o = <span class="keyword">dynamic_cast<</span><a class="code" href="a00120.html" title="Object is the abstract base class for the main entities.">Object</a>*<span class="keyword">></span>(x); <a name="l00049"></a>00049 <span class="keywordflow">if</span> (o && o-><a class="code" href="a00120.html#a2e5ff5e5d9c32647f692abd04a0d72d2">getType</a>().<a class="code" href="a00115.html#ae3e00d415acf61ed63fe0854ebc55ee3">raiseEvent</a>(o, <a class="code" href="a00257.html#aec6d4a238b0cd9ffc8be8f10fd9b652fa6a5f44a606844db345f7c98cd402ecfb">SIG_REMOVE</a>)) <a name="l00050"></a>00050 <span class="keyword">delete</span>(x); <span class="comment">// The destructor calls the erase method</span> <a name="l00051"></a>00051 <span class="keywordflow">else</span> <a name="l00052"></a>00052 <span class="keywordflow">throw</span> <a class="code" href="a00048.html" title="An exception of this type is thrown when data errors are found.">DataException</a>(<span class="stringliteral">"Can't delete object"</span>); <a name="l00053"></a>00053 } <a name="l00054"></a>00054 } <a name="l00055"></a>00055 <a name="l00056"></a>00056 <a name="l00057"></a><a class="code" href="a00181.html#ad86b27fab36285d7564f42dfed3570d7">00057</a> <a class="code" href="a00182.html" title="This class represents a node in the tree.">Tree::TreeNode</a>* <a class="code" href="a00181.html#afa8f41efd3bf1ba8345458a875cc9da7">Tree::insert</a>(<a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* z, <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a> *hint) <a name="l00058"></a>00058 { <a name="l00059"></a>00059 <span class="keywordflow">if</span> (!z) <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Inserting null pointer in tree"</span>); <a name="l00060"></a>00060 <a name="l00061"></a>00061 <span class="comment">// Lock the tree</span> <a name="l00062"></a>00062 <a class="code" href="a00163.html" title="This is a convenience class that makes it easy (and exception-safe) to lock a mutex...">ScopeMutexLock</a> l(treeaccess); <a name="l00063"></a>00063 <a name="l00064"></a>00064 <span class="comment">// Use the hint to create the proper starting point in the tree</span> <a name="l00065"></a>00065 <span class="keywordtype">int</span> comp; <a name="l00066"></a>00066 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* y; <a name="l00067"></a>00067 <span class="keywordflow">if</span> (!hint) <a name="l00068"></a>00068 { <a name="l00069"></a>00069 <span class="comment">// Effectively no hint given</span> <a name="l00070"></a>00070 hint = header.parent; <a name="l00071"></a>00071 y = &header; <a name="l00072"></a>00072 } <a name="l00073"></a>00073 <span class="keywordflow">else</span> <a name="l00074"></a>00074 { <a name="l00075"></a>00075 <span class="comment">// Check if the hint is a good starting point to descend</span> <a name="l00076"></a>00076 <span class="keywordflow">while</span> (hint->parent) <a name="l00077"></a>00077 { <a name="l00078"></a>00078 comp = z->nm.compare(hint->parent->nm); <a name="l00079"></a>00079 <span class="keywordflow">if</span> ((comp>0 && hint == hint->parent->left) <a name="l00080"></a>00080 || (comp<0 && hint == hint->parent->right)) <a name="l00081"></a>00081 <span class="comment">// Move the hint up to the parent node</span> <a name="l00082"></a>00082 <span class="comment">// If I'm left child of my parent && new node needs right insert</span> <a name="l00083"></a>00083 <span class="comment">// or I'm right child of my parent && new node needs left insert</span> <a name="l00084"></a>00084 hint = hint->parent; <a name="l00085"></a>00085 <span class="keywordflow">else</span> <a name="l00086"></a>00086 <span class="keywordflow">break</span>; <a name="l00087"></a>00087 } <a name="l00088"></a>00088 y = hint->parent; <a name="l00089"></a>00089 } <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">// Step down the tree till we found a proper empty leaf node in the tree</span> <a name="l00092"></a>00092 <span class="keywordflow">for</span> (; hint; hint = comp<0 ? hint->left : hint->right) <a name="l00093"></a>00093 { <a name="l00094"></a>00094 y = hint; <a name="l00095"></a>00095 <span class="comment">// Exit the function if the key is already found</span> <a name="l00096"></a>00096 comp = z->nm.compare(hint->nm); <a name="l00097"></a>00097 <span class="keywordflow">if</span> (!comp) <span class="keywordflow">return</span> hint; <a name="l00098"></a>00098 } <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="keywordflow">if</span> (y == &header || z->nm < y->nm) <a name="l00101"></a>00101 { <a name="l00102"></a>00102 <span class="comment">// Inserting as a new left child</span> <a name="l00103"></a>00103 y->left = z; <span class="comment">// also makes leftmost() = z when y == header</span> <a name="l00104"></a>00104 <span class="keywordflow">if</span> (y == &header) <a name="l00105"></a>00105 { <a name="l00106"></a>00106 <span class="comment">// Inserting a first element in the list</span> <a name="l00107"></a>00107 header.parent = z; <a name="l00108"></a>00108 header.right = z; <a name="l00109"></a>00109 } <a name="l00110"></a>00110 <span class="comment">// maintain leftmost() pointing to min node</span> <a name="l00111"></a>00111 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (y == header.left) header.left = z; <a name="l00112"></a>00112 } <a name="l00113"></a>00113 <span class="keywordflow">else</span> <a name="l00114"></a>00114 { <a name="l00115"></a>00115 <span class="comment">// Inserting as a new right child</span> <a name="l00116"></a>00116 y->right = z; <a name="l00117"></a>00117 <span class="comment">// Maintain rightmost() pointing to max node.</span> <a name="l00118"></a>00118 <span class="keywordflow">if</span> (y == header.right) header.right = z; <a name="l00119"></a>00119 } <a name="l00120"></a>00120 <a name="l00121"></a>00121 <span class="comment">// Set parent and child pointers of the new node</span> <a name="l00122"></a>00122 z->parent = y; <a name="l00123"></a>00123 z->left = NULL; <a name="l00124"></a>00124 z->right = NULL; <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="comment">// Increase the node count</span> <a name="l00127"></a>00127 ++count; <a name="l00128"></a>00128 <a name="l00129"></a>00129 <span class="comment">// Rebalance the tree</span> <a name="l00130"></a>00130 rebalance(z); <a name="l00131"></a>00131 <span class="keywordflow">return</span> z; <a name="l00132"></a>00132 } <a name="l00133"></a>00133 <a name="l00134"></a>00134 <a name="l00135"></a>00135 <span class="keywordtype">void</span> Tree::rebalance(TreeNode* x) <a name="l00136"></a>00136 { <a name="l00137"></a>00137 x->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00138"></a>00138 <a name="l00139"></a>00139 <span class="keywordflow">while</span> (x != header.parent && x->parent->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00140"></a>00140 { <a name="l00141"></a>00141 <span class="keywordflow">if</span> (x->parent == x->parent->parent->left) <a name="l00142"></a>00142 { <a name="l00143"></a>00143 TreeNode* y = x->parent->parent->right; <a name="l00144"></a>00144 <span class="keywordflow">if</span> (y && y->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00145"></a>00145 { <a name="l00146"></a>00146 x->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00147"></a>00147 y->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00148"></a>00148 x->parent->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00149"></a>00149 x = x->parent->parent; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 <span class="keywordflow">else</span> <a name="l00152"></a>00152 { <a name="l00153"></a>00153 <span class="keywordflow">if</span> (x == x->parent->right) <a name="l00154"></a>00154 { <a name="l00155"></a>00155 x = x->parent; <a name="l00156"></a>00156 rotateLeft(x); <a name="l00157"></a>00157 } <a name="l00158"></a>00158 x->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00159"></a>00159 x->parent->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00160"></a>00160 rotateRight(x->parent->parent); <a name="l00161"></a>00161 } <a name="l00162"></a>00162 } <a name="l00163"></a>00163 <span class="keywordflow">else</span> <a name="l00164"></a>00164 { <a name="l00165"></a>00165 TreeNode* y = x->parent->parent->left; <a name="l00166"></a>00166 <span class="keywordflow">if</span> (y && y->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00167"></a>00167 { <a name="l00168"></a>00168 x->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00169"></a>00169 y->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00170"></a>00170 x->parent->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00171"></a>00171 x = x->parent->parent; <a name="l00172"></a>00172 } <a name="l00173"></a>00173 <span class="keywordflow">else</span> <a name="l00174"></a>00174 { <a name="l00175"></a>00175 <span class="keywordflow">if</span> (x == x->parent->left) <a name="l00176"></a>00176 { <a name="l00177"></a>00177 x = x->parent; <a name="l00178"></a>00178 rotateRight(x); <a name="l00179"></a>00179 } <a name="l00180"></a>00180 x->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00181"></a>00181 x->parent->parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00182"></a>00182 rotateLeft(x->parent->parent); <a name="l00183"></a>00183 } <a name="l00184"></a>00184 } <a name="l00185"></a>00185 } <a name="l00186"></a>00186 header.parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00187"></a>00187 } <a name="l00188"></a>00188 <a name="l00189"></a>00189 <a name="l00190"></a><a class="code" href="a00181.html#af97a2348168870409e23348bdb409970">00190</a> <span class="keywordtype">void</span> <a class="code" href="a00181.html#af97a2348168870409e23348bdb409970">Tree::erase</a>(<a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* z) <a name="l00191"></a>00191 { <a name="l00192"></a>00192 <span class="comment">// A colorless node was never inserted in the tree, and shouldn't be</span> <a name="l00193"></a>00193 <span class="comment">// removed from it either...</span> <a name="l00194"></a>00194 <span class="keywordflow">if</span> (!z || z->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a77de76fed566d26afbd93930e7a53807">none</a>) <span class="keywordflow">return</span>; <a name="l00195"></a>00195 <a name="l00196"></a>00196 <span class="comment">// Lock the tree</span> <a name="l00197"></a>00197 <a class="code" href="a00163.html" title="This is a convenience class that makes it easy (and exception-safe) to lock a mutex...">ScopeMutexLock</a> l(treeaccess); <a name="l00198"></a>00198 <a name="l00199"></a>00199 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* y = z; <a name="l00200"></a>00200 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* x = NULL; <a name="l00201"></a>00201 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* x_parent = NULL; <a name="l00202"></a>00202 --count; <a name="l00203"></a>00203 <span class="keywordflow">if</span> (y->left == NULL) <span class="comment">// z has at most one non-null child. y == z.</span> <a name="l00204"></a>00204 x = y->right; <span class="comment">// x might be null.</span> <a name="l00205"></a>00205 <span class="keywordflow">else</span> <a name="l00206"></a>00206 <span class="keywordflow">if</span> (y->right == NULL) <span class="comment">// z has exactly one non-null child. y == z.</span> <a name="l00207"></a>00207 x = y->left; <span class="comment">// x is not null.</span> <a name="l00208"></a>00208 <span class="keywordflow">else</span> <a name="l00209"></a>00209 { <a name="l00210"></a>00210 <span class="comment">// z has two non-null children. Set y to</span> <a name="l00211"></a>00211 y = y->right; <span class="comment">// z's successor. x might be null.</span> <a name="l00212"></a>00212 <span class="keywordflow">while</span> (y->left != NULL) y = y->left; <a name="l00213"></a>00213 x = y->right; <a name="l00214"></a>00214 } <a name="l00215"></a>00215 <span class="keywordflow">if</span> (y != z) <a name="l00216"></a>00216 { <a name="l00217"></a>00217 <span class="comment">// relink y in place of z. y is z's successor</span> <a name="l00218"></a>00218 z->left->parent = y; <a name="l00219"></a>00219 y->left = z->left; <a name="l00220"></a>00220 <span class="keywordflow">if</span> (y != z->right) <a name="l00221"></a>00221 { <a name="l00222"></a>00222 x_parent = y->parent; <a name="l00223"></a>00223 <span class="keywordflow">if</span> (x) x->parent = y->parent; <a name="l00224"></a>00224 y->parent->left = x; <span class="comment">// y must be a child of left</span> <a name="l00225"></a>00225 y->right = z->right; <a name="l00226"></a>00226 z->right->parent = y; <a name="l00227"></a>00227 } <a name="l00228"></a>00228 <span class="keywordflow">else</span> <a name="l00229"></a>00229 x_parent = y; <a name="l00230"></a>00230 <span class="keywordflow">if</span> (header.parent == z) header.parent = y; <a name="l00231"></a>00231 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (z->parent->left == z) z->parent->left = y; <a name="l00232"></a>00232 <span class="keywordflow">else</span> z->parent->right = y; <a name="l00233"></a>00233 y->parent = z->parent; <a name="l00234"></a>00234 std::swap(y->color, z->color); <a name="l00235"></a>00235 y = z; <a name="l00236"></a>00236 <span class="comment">// y now points to node to be actually deleted</span> <a name="l00237"></a>00237 } <a name="l00238"></a>00238 <span class="keywordflow">else</span> <a name="l00239"></a>00239 { <span class="comment">// y == z</span> <a name="l00240"></a>00240 x_parent = y->parent; <a name="l00241"></a>00241 <span class="keywordflow">if</span> (x) x->parent = y->parent; <a name="l00242"></a>00242 <span class="keywordflow">if</span> (header.parent == z) header.parent = x; <a name="l00243"></a>00243 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (z->parent->left == z) z->parent->left = x; <a name="l00244"></a>00244 <span class="keywordflow">else</span> z->parent->right = x; <a name="l00245"></a>00245 <span class="keywordflow">if</span> (header.left == z) <a name="l00246"></a>00246 { <a name="l00247"></a>00247 <span class="keywordflow">if</span> (z->right == NULL) <span class="comment">// z->left must be null also</span> <a name="l00248"></a>00248 header.left = z->parent; <a name="l00249"></a>00249 <span class="comment">// makes leftmost == header if z == root</span> <a name="l00250"></a>00250 <span class="keywordflow">else</span> <a name="l00251"></a>00251 header.left = minimum(x); <a name="l00252"></a>00252 } <a name="l00253"></a>00253 <span class="keywordflow">if</span> (header.right == z) <a name="l00254"></a>00254 { <a name="l00255"></a>00255 <span class="keywordflow">if</span> (z->left == NULL) <span class="comment">// z->right must be null also</span> <a name="l00256"></a>00256 header.right = z->parent; <a name="l00257"></a>00257 <span class="comment">// makes rightmost == header if z == root</span> <a name="l00258"></a>00258 <span class="keywordflow">else</span> <span class="comment">// x == z->left</span> <a name="l00259"></a>00259 header.right = maximum(x); <a name="l00260"></a>00260 } <a name="l00261"></a>00261 } <a name="l00262"></a>00262 <span class="keywordflow">if</span> (y->color != <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00263"></a>00263 { <a name="l00264"></a>00264 <span class="keywordflow">while</span> (x != header.parent && (x == NULL || x->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>)) <a name="l00265"></a>00265 <span class="keywordflow">if</span> (x == x_parent->left) <a name="l00266"></a>00266 { <a name="l00267"></a>00267 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* w = x_parent->right; <a name="l00268"></a>00268 <span class="keywordflow">if</span> (w->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00269"></a>00269 { <a name="l00270"></a>00270 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00271"></a>00271 x_parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00272"></a>00272 rotateLeft(x_parent); <a name="l00273"></a>00273 w = x_parent->right; <a name="l00274"></a>00274 } <a name="l00275"></a>00275 <span class="keywordflow">if</span> ((w->left == NULL || w->left->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>) && <a name="l00276"></a>00276 (w->right == NULL || w->right->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>)) <a name="l00277"></a>00277 { <a name="l00278"></a>00278 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00279"></a>00279 x = x_parent; <a name="l00280"></a>00280 x_parent = x_parent->parent; <a name="l00281"></a>00281 } <a name="l00282"></a>00282 <span class="keywordflow">else</span> <a name="l00283"></a>00283 { <a name="l00284"></a>00284 <span class="keywordflow">if</span> (w->right == NULL || w->right->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>) <a name="l00285"></a>00285 { <a name="l00286"></a>00286 w->left->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00287"></a>00287 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00288"></a>00288 rotateRight(w); <a name="l00289"></a>00289 w = x_parent->right; <a name="l00290"></a>00290 } <a name="l00291"></a>00291 w->color = x_parent->color; <a name="l00292"></a>00292 x_parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00293"></a>00293 <span class="keywordflow">if</span> (w->right) w->right->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00294"></a>00294 rotateLeft(x_parent); <a name="l00295"></a>00295 <span class="keywordflow">break</span>; <a name="l00296"></a>00296 } <a name="l00297"></a>00297 } <a name="l00298"></a>00298 <span class="keywordflow">else</span> <a name="l00299"></a>00299 { <a name="l00300"></a>00300 <span class="comment">// same as above, with right <-> left.</span> <a name="l00301"></a>00301 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* w = x_parent->left; <a name="l00302"></a>00302 <span class="keywordflow">if</span> (w->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00303"></a>00303 { <a name="l00304"></a>00304 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00305"></a>00305 x_parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00306"></a>00306 rotateRight(x_parent); <a name="l00307"></a>00307 w = x_parent->left; <a name="l00308"></a>00308 } <a name="l00309"></a>00309 <span class="keywordflow">if</span> ((w->right == NULL || w->right->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>) && <a name="l00310"></a>00310 (w->left == NULL || w->left->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>)) <a name="l00311"></a>00311 { <a name="l00312"></a>00312 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00313"></a>00313 x = x_parent; <a name="l00314"></a>00314 x_parent = x_parent->parent; <a name="l00315"></a>00315 } <a name="l00316"></a>00316 <span class="keywordflow">else</span> <a name="l00317"></a>00317 { <a name="l00318"></a>00318 <span class="keywordflow">if</span> (w->left == NULL || w->left->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>) <a name="l00319"></a>00319 { <a name="l00320"></a>00320 w->right->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00321"></a>00321 w->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>; <a name="l00322"></a>00322 rotateLeft(w); <a name="l00323"></a>00323 w = x_parent->left; <a name="l00324"></a>00324 } <a name="l00325"></a>00325 w->color = x_parent->color; <a name="l00326"></a>00326 x_parent->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00327"></a>00327 <span class="keywordflow">if</span> (w->left) w->left->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00328"></a>00328 rotateRight(x_parent); <a name="l00329"></a>00329 <span class="keywordflow">break</span>; <a name="l00330"></a>00330 } <a name="l00331"></a>00331 } <a name="l00332"></a>00332 <span class="keywordflow">if</span> (x) x->color = <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39aa79abd32771092aea18764ad563a4470">black</a>; <a name="l00333"></a>00333 } <a name="l00334"></a>00334 } <a name="l00335"></a>00335 <a name="l00336"></a>00336 <a name="l00337"></a>00337 <span class="keywordtype">void</span> Tree::rotateLeft(TreeNode* x) <a name="l00338"></a>00338 { <a name="l00339"></a>00339 TreeNode* y = x->right; <a name="l00340"></a>00340 x->right = y->left; <a name="l00341"></a>00341 <span class="keywordflow">if</span> (y->left != NULL) y->left->parent = x; <a name="l00342"></a>00342 y->parent = x->parent; <a name="l00343"></a>00343 <a name="l00344"></a>00344 <span class="keywordflow">if</span> (x == header.parent) <a name="l00345"></a>00345 <span class="comment">// x was the root</span> <a name="l00346"></a>00346 header.parent = y; <a name="l00347"></a>00347 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (x == x->parent->left) <a name="l00348"></a>00348 <span class="comment">// x was on the left of its parent</span> <a name="l00349"></a>00349 x->parent->left = y; <a name="l00350"></a>00350 <span class="keywordflow">else</span> <a name="l00351"></a>00351 <span class="comment">// x was on the right of its parent</span> <a name="l00352"></a>00352 x->parent->right = y; <a name="l00353"></a>00353 y->left = x; <a name="l00354"></a>00354 x->parent = y; <a name="l00355"></a>00355 } <a name="l00356"></a>00356 <a name="l00357"></a>00357 <a name="l00358"></a>00358 <span class="keywordtype">void</span> Tree::rotateRight(TreeNode* x) <a name="l00359"></a>00359 { <a name="l00360"></a>00360 TreeNode* y = x->left; <a name="l00361"></a>00361 x->left = y->right; <a name="l00362"></a>00362 <span class="keywordflow">if</span> (y->right != NULL) y->right->parent = x; <a name="l00363"></a>00363 y->parent = x->parent; <a name="l00364"></a>00364 <a name="l00365"></a>00365 <span class="keywordflow">if</span> (x == header.parent) <a name="l00366"></a>00366 <span class="comment">// x was the root</span> <a name="l00367"></a>00367 header.parent = y; <a name="l00368"></a>00368 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (x == x->parent->right) <a name="l00369"></a>00369 <span class="comment">// x was on the right of its parent</span> <a name="l00370"></a>00370 x->parent->right = y; <a name="l00371"></a>00371 <span class="keywordflow">else</span> <a name="l00372"></a>00372 <span class="comment">// x was on the left of its parent</span> <a name="l00373"></a>00373 x->parent->left = y; <a name="l00374"></a>00374 y->right = x; <a name="l00375"></a>00375 x->parent = y; <a name="l00376"></a>00376 } <a name="l00377"></a>00377 <a name="l00378"></a>00378 <a name="l00379"></a><a class="code" href="a00181.html#ad0759a0db4bb6b71307d42a344e76f85">00379</a> <a class="code" href="a00252.html#a99b94ab8c2f2a32277559c7b2b0a054c">DECLARE_EXPORT</a> <span class="keywordtype">void</span> <a class="code" href="a00181.html#ad0759a0db4bb6b71307d42a344e76f85">Tree::verify</a>()<span class="keyword"> const</span> <a name="l00380"></a>00380 <span class="keyword"></span>{ <a name="l00381"></a>00381 <span class="comment">// Checks for an empty tree</span> <a name="l00382"></a>00382 <span class="keywordflow">if</span> (<a class="code" href="a00181.html#a8c11861060dcf9ee45054b73c26ee77d">empty</a>() || <a class="code" href="a00181.html#ad44c460c1fb9648e83d703e6cdc64d19">begin</a>() == <a class="code" href="a00181.html#a3c12d98627c9d46864dfd6b5e994878c">end</a>()) <a name="l00383"></a>00383 { <a name="l00384"></a>00384 <span class="keywordtype">bool</span> ok = (<a class="code" href="a00181.html#a8c11861060dcf9ee45054b73c26ee77d">empty</a>() && <a name="l00385"></a>00385 <a class="code" href="a00181.html#ad44c460c1fb9648e83d703e6cdc64d19">begin</a>() == <a class="code" href="a00181.html#a3c12d98627c9d46864dfd6b5e994878c">end</a>() && <a name="l00386"></a>00386 header.left == &header && <a name="l00387"></a>00387 header.right == &header); <a name="l00388"></a>00388 <span class="keywordflow">if</span> (!ok) <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Invalid empty tree"</span>); <a name="l00389"></a>00389 <span class="keywordflow">return</span>; <a name="l00390"></a>00390 } <a name="l00391"></a>00391 <a name="l00392"></a>00392 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> len = countBlackNodes(header.left); <a name="l00393"></a>00393 <span class="keywordtype">size_t</span> counter = 0; <a name="l00394"></a>00394 <span class="keywordflow">for</span> (<a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* x = <a class="code" href="a00181.html#ad44c460c1fb9648e83d703e6cdc64d19">begin</a>(); x != <a class="code" href="a00181.html#a3c12d98627c9d46864dfd6b5e994878c">end</a>(); x = x->increment()) <a name="l00395"></a>00395 { <a name="l00396"></a>00396 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* L = x->left; <a name="l00397"></a>00397 <a class="code" href="a00182.html" title="This class represents a node in the tree.">TreeNode</a>* R = x->right; <a name="l00398"></a>00398 ++counter; <a name="l00399"></a>00399 <a name="l00400"></a>00400 <span class="keywordflow">if</span> (x->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a77de76fed566d26afbd93930e7a53807">none</a>) <a name="l00401"></a>00401 <span class="comment">// Nodes must have a color</span> <a name="l00402"></a>00402 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Colorless node included in a tree"</span>); <a name="l00403"></a>00403 <a name="l00404"></a>00404 <span class="keywordflow">if</span> (x->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) <a name="l00405"></a>00405 <span class="keywordflow">if</span> ((L && L->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>) || (R && R->color == <a class="code" href="a00181.html#a1d874ef9d1fb1325aec8069d9a578f39a8efd9fdac156d90ea714f4085c33f9c4">red</a>)) <a name="l00406"></a>00406 <span class="comment">// A red node can have only NULL and black children</span> <a name="l00407"></a>00407 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Wrong color on node"</span>); <a name="l00408"></a>00408 <a name="l00409"></a>00409 <span class="keywordflow">if</span> (L && x->nm<L->nm) <a name="l00410"></a>00410 <span class="comment">// Left child isn't smaller</span> <a name="l00411"></a>00411 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Wrong sorting on left node"</span>); <a name="l00412"></a>00412 <a name="l00413"></a>00413 <span class="keywordflow">if</span> (R && R->nm<x->nm) <a name="l00414"></a>00414 <span class="comment">// Right child isn't bigger</span> <a name="l00415"></a>00415 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Wrong sorting on right node"</span>); <a name="l00416"></a>00416 <a name="l00417"></a>00417 <span class="keywordflow">if</span> (!L && !R && countBlackNodes(x) != len) <a name="l00418"></a>00418 <span class="comment">// All leaf nodes have the same number of black nodes on their path</span> <a name="l00419"></a>00419 <span class="comment">// to the root</span> <a name="l00420"></a>00420 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Unbalanced count of black nodes"</span>); <a name="l00421"></a>00421 } <a name="l00422"></a>00422 <a name="l00423"></a>00423 <span class="comment">// Check whether the header has a good pointer to the leftmost element</span> <a name="l00424"></a>00424 <span class="keywordflow">if</span> (header.left != minimum(header.parent)) <a name="l00425"></a>00425 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Incorrect tree minimum"</span>); <a name="l00426"></a>00426 <a name="l00427"></a>00427 <span class="comment">// Check whether the header has a good pointer to the rightmost element</span> <a name="l00428"></a>00428 <span class="keywordflow">if</span> (header.right != maximum(header.parent)) <a name="l00429"></a>00429 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Incorrect tree maximum"</span>); <a name="l00430"></a>00430 <a name="l00431"></a>00431 <span class="comment">// Check whether the measured node count match the expectation?</span> <a name="l00432"></a>00432 <span class="keywordflow">if</span> (counter != <a class="code" href="a00181.html#a7ae0f783f775996f5ac010769caa9af8">size</a>()) <a name="l00433"></a>00433 <span class="keywordflow">throw</span> <a class="code" href="a00111.html" title="An exception of this type is thrown when the library gets in an inconsistent state...">LogicException</a>(<span class="stringliteral">"Incorrect tree size"</span>); <a name="l00434"></a>00434 <a name="l00435"></a>00435 <span class="comment">// If we reach this point, then this tree is healthy</span> <a name="l00436"></a>00436 } <a name="l00437"></a>00437 <a name="l00438"></a>00438 } <span class="comment">// end namespace</span> <a name="l00439"></a>00439 } <span class="comment">// end namespace</span> </pre></div></div> <hr size="1"/><address style="align: right;"><small>Documentation generated for frePPLe by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"/></a></small></address> </div> </div> </body> </html>