Changeset 1001
- Timestamp:
- Jul 10, 2003, 10:55:19 PM (18 years ago)
- Location:
- to-imperative/trunk/runtime
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/runtime/rf_table.cc
r983 r1001 22 22 if ((_newNode->hashKey) == (x->hashKey)) { 23 23 flag = Expr::compare(_newNode->key, x->key); 24 // if ((newNode->key) == (x->key)) 25 if (!flag) 24 if (!flag) // case 0 : == 26 25 return; 27 // if ((newNode->key) < (x->key)) 28 if (flag == -1){ 26 if (flag == -1){ // case -1 : < 29 27 x = x->left; 30 28 continue; … … 45 43 if ((_newNode->hashKey) == (x->hashKey)) { 46 44 flag = Expr::compare(_newNode->key, x->key); 47 // if ((newNode->key) < (x->key)) 48 if (flag == -1) { 45 if (flag == -1) { // case -1 : < 49 46 y->left = _newNode; 50 47 return; … … 103 100 TableNode *y; 104 101 InsertNode(_x); 105 _x->color = RED;106 while ((_x != root) && (_x->p->color == RED)) {102 _x->color = TableNode::RED; 103 while ((_x != root) && (_x->p->color == TableNode::RED)) { 107 104 if (_x->p == _x->p->p->left) { 108 105 y = _x->p->p->right; 109 if ((y != TableNode::nil) && (y->color == RED)) {110 _x->p->color = BLACK;111 y->color = BLACK;112 _x->p->p->color = RED;106 if ((y != TableNode::nil) && (y->color == TableNode::RED)) { 107 _x->p->color = TableNode::BLACK; 108 y->color = TableNode::BLACK; 109 _x->p->p->color = TableNode::RED; 113 110 _x = _x->p->p; 114 111 } else { … … 117 114 LeftRotate_RB(_x); 118 115 } 119 _x->p->color = BLACK;120 _x->p->p->color = RED;116 _x->p->color = TableNode::BLACK; 117 _x->p->p->color =TableNode::RED; 121 118 RightRotate_RB(_x->p->p); 122 119 } 123 120 } else { 124 121 y=_x->p->p->left; 125 if ((y != TableNode::nil) && (y->color == RED)) {126 _x->p->color = BLACK;127 y->color = BLACK;128 _x->p->p->color = RED;122 if ((y != TableNode::nil) && (y->color == TableNode::RED)) { 123 _x->p->color = TableNode::BLACK; 124 y->color = TableNode::BLACK; 125 _x->p->p->color = TableNode::RED; 129 126 _x = _x->p->p; 130 127 } else { … … 133 130 RightRotate_RB(_x); 134 131 } 135 _x->p->color = BLACK;136 _x->p->p->color = RED;132 _x->p->color = TableNode::BLACK; 133 _x->p->p->color = TableNode::RED; 137 134 LeftRotate_RB(_x->p->p); 138 135 } 139 136 } 140 137 } 141 root->color= BLACK;138 root->color=TableNode::BLACK; 142 139 } 143 140 … … 168 165 _z->key = y->key; 169 166 } 170 if (y->color == BLACK)167 if (y->color == TableNode::BLACK) 171 168 DeleteFixup_RB(x); 172 169 return y; … … 177 174 { 178 175 TableNode* w; 179 while ((_x != root) && (_x->color == BLACK)) {176 while ((_x != root) && (_x->color == TableNode::BLACK)) { 180 177 if (_x == _x->p->left) { 181 178 w = _x->p->right; 182 if (w->color == RED) {183 w->color = BLACK;184 _x->p->color = RED;179 if (w->color == TableNode::RED) { 180 w->color = TableNode::BLACK; 181 _x->p->color = TableNode::RED; 185 182 LeftRotate_RB(_x->p); 186 183 w = _x->p->right; 187 184 } 188 if ((w->left->color == BLACK) && (w->right->color ==BLACK)) {189 w->color = RED;185 if ((w->left->color == TableNode::BLACK) && (w->right->color == TableNode::BLACK)) { 186 w->color = TableNode::RED; 190 187 _x = _x->p; 191 188 } else { 192 if (w->right->color == BLACK) {193 w->left->color = BLACK;194 w->color = RED;189 if (w->right->color == TableNode::BLACK) { 190 w->left->color = TableNode::BLACK; 191 w->color = TableNode::RED; 195 192 RightRotate_RB(w); 196 193 w = _x->p->right; 197 194 } 198 195 w->color = _x->p->color; 199 _x->p->color = BLACK;200 w->right->color = BLACK;196 _x->p->color = TableNode::BLACK; 197 w->right->color = TableNode::BLACK; 201 198 LeftRotate_RB(_x->p); 202 199 _x=root; … … 204 201 } else { 205 202 w = _x->p->left; 206 if (w->color == RED) {207 w->color = BLACK;208 _x->p->color = RED;203 if (w->color == TableNode::RED) { 204 w->color = TableNode::BLACK; 205 _x->p->color = TableNode::RED; 209 206 RightRotate_RB(_x->p); 210 207 w = _x->p->left; 211 208 } 212 if ((w->right->color == BLACK) && (w->left->color ==BLACK)) {213 w->color = RED;209 if ((w->right->color == TableNode::BLACK) && (w->left->color == TableNode::BLACK)) { 210 w->color = TableNode::RED; 214 211 _x = _x->p; 215 212 } else { 216 if (w->left->color == BLACK) {217 w->right->color = BLACK;218 w->color = RED;213 if (w->left->color == TableNode::BLACK) { 214 w->right->color = TableNode::BLACK; 215 w->color = TableNode::RED; 219 216 LeftRotate_RB(w); 220 217 w = _x->p->left; 221 218 } 222 219 w->color = _x->p->color; 223 _x->p->color = BLACK;224 w->left->color = BLACK;220 _x->p->color = TableNode::BLACK; 221 w->left->color = TableNode::BLACK; 225 222 RightRotate_RB(_x->p); 226 223 _x=root; … … 228 225 } 229 226 } 230 _x->color= BLACK;227 _x->color=TableNode::BLACK; 231 228 } 232 229 … … 243 240 node = node->left; 244 241 else 245 node = node->right; 246 // must be CompareExpr 242 node = node->right; 247 243 while ((node != TableNode::nil) && (hash == node->hashKey)) { 248 244 flag = Expr::compare(_key, node->key); 249 // if ( key == node->key) 250 if (!flag) 245 if (!flag) // case 0 : == 251 246 return (node); 252 // if (key < node->key) 253 if (flag == -1) 247 if (flag == -1) // case -1 : < 254 248 node = node->left; 255 249 else … … 260 254 261 255 262 void CopyTree (TableNode * _tab, TableNode*_copy)256 void CopyTree (TableNode const& _tab, TableNode& _copy) 263 257 { 264 258 TableNode* child; 265 _copy ->hashKey = _tab->hashKey;266 _copy ->key = _tab->key;267 _copy ->val = _tab->val;268 _copy ->color = _tab->color;269 if (_tab ->left == TableNode::nil)270 _copy ->left = TableNode::nil;259 _copy.hashKey = _tab.hashKey; 260 _copy.key = _tab.key; 261 _copy.val = _tab.val; 262 _copy.color = _tab.color; 263 if (_tab.left == TableNode::nil) 264 _copy.left = TableNode::nil; 271 265 else { 272 266 child = new TableNode; 273 child->p = _copy;274 _copy ->left = child;275 CopyTree( _tab->left,child);276 } 277 if (_tab ->right == TableNode::nil)278 _copy ->right = TableNode::nil;267 child->p = &_copy; 268 _copy.left = child; 269 CopyTree(*(_tab.left), *child); 270 } 271 if (_tab.right == TableNode::nil) 272 _copy.right = TableNode::nil; 279 273 else { 280 child = new (TableNode);281 child->p = _copy;282 _copy ->right = child;283 CopyTree( _tab->right,child);284 } 285 } 286 287 bool TableEqual(TableNode * _tab1, TableNode*_tab2)274 child = new TableNode; 275 child->p = &_copy; 276 _copy.right = child; 277 CopyTree( *(_tab.right), *child); 278 } 279 } 280 281 bool TableEqual(TableNode const& _tab1, TableNode const& _tab2) 288 282 { 289 if ( _tab1== TableNode::nil) {290 if ( _tab2== TableNode::nil)283 if ((&_tab1) == TableNode::nil) { 284 if ((&_tab2) == TableNode::nil) 291 285 return true; 292 286 return false; 293 287 } 294 288 else { 295 if ( _tab2== TableNode::nil)289 if ((&_tab2) == TableNode::nil) 296 290 return false; 297 if ( (_tab1 ->hashKey == _tab2->hashKey) &&298 (_tab1 ->color == _tab2->color) &&299 (_tab1 ->key == _tab2->key) &&300 (_tab1 ->val == _tab2->val) &&301 (TableEqual( _tab1->right, _tab2->right)) &&302 (TableEqual( _tab1->left, _tab2->left)) )291 if ( (_tab1.hashKey == _tab2.hashKey) && 292 (_tab1.color == _tab2.color) && 293 (_tab1.key == _tab2.key) && 294 (_tab1.val == _tab2.val) && 295 (TableEqual(*_tab1.right, *_tab2.right)) && 296 (TableEqual(*_tab1.left, *_tab2.left)) ) 303 297 return true; 304 298 else … … 318 312 319 313 320 void ListNode (TableNode *_node, Expr& _res)321 { 322 if (_node != TableNode::nil) {323 ListNode( _node->left, _res);324 ListNode(_node->right, _res);325 _res = _res + (_node->key)();326 }314 void ListNode (TableNode const& _node, Expr& _res) 315 { 316 if (_node.left != TableNode::nil) 317 ListNode( * _node.left, _res); 318 if (_node.right != TableNode::nil) 319 ListNode( * _node.right, _res); 320 _res = _res + (_node.key)(); 327 321 } 328 322 -
to-imperative/trunk/runtime/rf_table.hh
r983 r1001 12 12 13 13 class TableNode; 14 15 16 typedef enum {BLACK, RED} node_color;17 14 18 15 class Table : … … 33 30 34 31 public : 32 35 33 inline Table (); 36 34 inline ~Table(); … … 51 49 friend class Table; 52 50 53 private: 51 private: 54 52 55 53 int hashKey; 56 node_colorcolor;54 enum {BLACK, RED} color; 57 55 Expr key; // key-expression 58 56 Expr val; // value-expression … … 65 63 friend TableNode* TreeSuccessor (TableNode*) ; 66 64 friend TableNode* TreeMinimum (TableNode*) ; 67 friend void ListNode (TableNode *, Expr&) ;68 friend void CopyTree (TableNode *, TableNode*) ;65 friend void ListNode (TableNode const&, Expr&) ; 66 friend void CopyTree (TableNode const&, TableNode&) ; 69 67 friend void DeleteTree (TableNode*) ; 70 friend bool TableEqual (TableNode *, TableNode*) ;68 friend bool TableEqual (TableNode const&, TableNode const&) ; 71 69 }; 72 70 -
to-imperative/trunk/runtime/rf_table.ih
r983 r1001 19 19 flag = true; 20 20 TableNode::nil = new TableNode; 21 TableNode::nil->color = BLACK;21 TableNode::nil->color = TableNode::BLACK; 22 22 } 23 23 root = TableNode::nil; … … 38 38 try { 39 39 Table const& t = static_cast <Table const&>(_table); 40 return TableEqual( root,t.root);40 return TableEqual(*root, *t.root); 41 41 } 42 42 catch (std::bad_cast&) … … 57 57 { 58 58 Expr res = Expr(); 59 ListNode(root, res); 59 if (root != TableNode::nil) 60 ListNode(*root, res); 60 61 return res; 61 62 } … … 125 126 node->p = TableNode::nil; 126 127 _new_table.root = node; // p - root 127 CopyTree( root,node); // p - root128 CopyTree(*root, *node); // p - root 128 129 } 129 130 } … … 143 144 x->p = TableNode::nil; 144 145 root = x; 145 CopyTree( _source_table.root,root);146 CopyTree(*_source_table.root, *root); 146 147 } 147 148 }
Note: See TracChangeset
for help on using the changeset viewer.