Changeset 932
- Timestamp:
- Jul 1, 2003, 5:34:45 PM (18 years ago)
- Location:
- to-imperative/trunk/runtime
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/runtime/rf_table.cc
r922 r932 13 13 /* Constructor: create NIL-Table and initialize p - root of RB-tree */ 14 14 15 Table :: Table():16 15 Table :: Table() : 16 Object () 17 17 { 18 static int flag=0; 19 20 if (!flag) 21 { 22 NIL = static_cast <Table *> (new(Table)); 23 NIL->color=BLACK; 24 NIL->right=NIL; 25 NIL->left=NIL; 26 NIL->p=NIL; 27 flag=1; 28 } 29 p=NIL; /* p - root */ 30 } 31 32 33 /* Destructor: deleting of this-RBtree */ 34 35 Table :: ~Table() 36 { 37 DeleteTree(p); 38 } 39 40 41 void Table :: Bind (Expr const& key, Expr const& val) 18 static int flag=0; 19 20 printf("Table() \n"); 21 if (!flag) 22 { 23 NIL = static_cast <Table *> (new(Table)); 24 NIL->color=BLACK; 25 NIL->right=NIL; 26 NIL->left=NIL; 27 NIL->p=NIL; 28 flag=1; 29 } 30 p=NIL; /* p - root */ 31 } 32 33 34 void Table::Bind (Expr const& key, Expr const& val) 42 35 { 43 Table *newNode; 44 if ((newNode =IterativeTreeSearch(key))==NIL) 45 { 46 newNode = static_cast <Table *> (new(Table)); 47 newNode->key=key; 48 newNode->hashKey=Expr_hash(key); 49 newNode->left=NIL; 50 newNode->right=NIL; 51 newNode->p=NIL; 52 newNode->val=val; 53 RBinsert(newNode); 54 } 55 else 56 newNode->val=val; 57 } 58 59 36 Table *newNode; 37 if ((newNode =IterativeTreeSearch(key))==NIL) 38 { 39 newNode = static_cast <Table *> (new(Table)); 40 newNode->key=key; 41 newNode->hashKey=Expr_hash(key); 42 newNode->left=NIL; 43 newNode->right=NIL; 44 newNode->p=NIL; 45 newNode->val=val; 46 RBinsert(newNode); 47 } 48 else 49 newNode->val=val; 50 } 60 51 61 52 62 53 void Table :: Unbind(Expr& key) 63 54 { 64 Table *newNode; 65 66 if ((newNode = IterativeTreeSearch(key))==NIL); 67 // printf("error hash not exists\n"); 68 else 69 { 70 newNode= RBdelete(newNode); 71 delete(newNode); 72 } 55 Table *newNode; 56 57 if ((newNode = IterativeTreeSearch(key))!=NIL) 58 { 59 newNode= RBdelete(newNode); 60 delete(newNode); 61 } 73 62 } 74 63 75 64 76 65 int Table :: Lookup (Expr const& key, Expr& val) 77 { 78 79 80 81 82 83 84 85 66 { Table *newNode; 67 68 if ((newNode = IterativeTreeSearch(key))==NIL) 69 return 0; /* fail(0) */ 70 else 71 { 72 val=newNode->val; 73 return 1; 74 } 86 75 } 87 76 … … 90 79 91 80 int Table:: In_table(Expr const& key) 92 { Table *newNode; 93 94 if ((newNode = IterativeTreeSearch(key))==NIL) 95 return 0; 96 else 97 return 1; 98 } 99 100 101 Expr Table :: Domain () 102 { 103 Expr res = Expr(); 104 InorderTreeWalk(p, res); /* p - root */ 105 return res; 106 } 107 81 { 82 Table *newNode; 83 84 if ((newNode = IterativeTreeSearch(key))==NIL) 85 return 0; 86 else 87 return 1; 88 } 108 89 109 90 /* Table_copy : this - sourceTable, newTable - result */ 110 91 111 92 void Table :: Table_copy(Table& newTable) 112 93 { Table *node; 113 114 115 116 117 118 119 120 94 95 if (p!=NIL) /* p - root */ 96 { 97 node=static_cast <Table *> (new(Table)); 98 node->p=NIL; 99 newTable.p=node; /* p - root */ 100 CopyTree(p,node); /* p - root */ 101 } 121 102 } 122 103 … … 127 108 { Table *x; 128 109 129 if (p!=NIL) /* p - root */ 130 { 131 DeleteTree(p); 132 p=NIL; /* p - root*/ 133 } 134 if (SourceTable.p!= NIL) /* p - root */ 135 { 136 x= static_cast <Table *> (new(Table)); 137 x->p=NIL; 138 p=x; /* p - root */ 139 CopyTree(SourceTable.p,p); /* p - root */ 140 } 141 } 142 110 if (p!=NIL) /* p - root */ 111 { 112 DeleteTree(p); 113 p=NIL; /* p - root*/ 114 } 115 if (SourceTable.p!= NIL) /* p - root */ 116 { 117 x= static_cast <Table *> (new(Table)); 118 x->p=NIL; 119 p=x; /* p - root */ 120 CopyTree(SourceTable.p,p); /* p - root */ 121 } 122 } 143 123 144 124 /* inserting of element newNode in this-Table */ … … 148 128 Table *x, *y; 149 129 int flag; 150 130 151 131 y=NIL; 152 132 x=p; /* root */ … … 155 135 y=x; 156 136 if ((newNode->hashKey) == (x->hashKey)) 157 158 159 //if ((newNode->key) == (x->key))160 161 162 // if ((newNode->key) < (x->key))163 164 165 166 167 168 169 170 171 172 137 { 138 flag=Expr::compare(newNode->key, x->key); 139 // if ((newNode->key) == (x->key)) 140 if (!flag) 141 return; 142 // if ((newNode->key) < (x->key)) 143 if (flag==-1) 144 { 145 x=x->left; 146 continue; 147 } 148 else 149 { 150 x=x->right; 151 continue; 152 } 173 153 } 174 154 if ((newNode->hashKey)<(x->hashKey)) … … 179 159 newNode->p=y; 180 160 if (y==NIL) 181 161 p=newNode; /* p - root */ 182 162 else 183 163 if ((newNode->hashKey) == (x->hashKey)) 184 185 186 //if ((newNode->key) < (x->key))187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 164 { 165 flag=Expr::compare(newNode->key, x->key); 166 // if ((newNode->key) < (x->key)) 167 if (flag==-1) 168 { 169 y->left=newNode; 170 return; 171 } 172 else 173 { 174 y->right=newNode; 175 return; 176 } 177 } 178 if ((newNode->hashKey) < (y->hashKey)) 179 y->left = newNode; 180 else 181 y->right = newNode; 202 182 } 203 183 … … 250 230 { Table *y; 251 231 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 } 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 232 TreeInsert(x); 233 x->color=RED; 234 while ((x!=p)&&(x->p->color==RED)) /* p - root */ 235 { 236 if (x->p == x->p->p->left) 237 { 238 y=x->p->p->right; 239 if ((y!=NIL) && (y->color==RED)) 240 { 241 x->p->color=BLACK; 242 y->color=BLACK; 243 x->p->p->color=RED; 244 x=x->p->p; 245 } 246 else 247 { 248 if (x==x->p->right) 249 { 250 x=x->p; 251 LeftRotate(x); 252 } 253 x->p->color=BLACK; 254 x->p->p->color=RED; 255 RightRotate(x->p->p); 256 } 257 } 258 else 259 { 260 y=x->p->p->left; 261 if ((y!=NIL)&&(y->color==RED)) 262 { 263 x->p->color=BLACK; 264 y->color=BLACK; 265 x->p->p->color=RED; 266 x=x->p->p; 267 } 268 else 269 { 270 if (x==x->p->left) 271 { 272 x=x->p; 273 RightRotate(x); 274 } 275 x->p->color=BLACK; 276 x->p->p->color=RED; 277 LeftRotate(x->p->p); 278 } 279 } 280 } 281 p->color=BLACK; /* p - root */ 302 282 } 303 283 … … 309 289 310 290 if ((z->left==NIL) || (z->right==NIL)) 311 312 else 313 291 y=z; 292 else 293 y=TreeSuccessor(z); 314 294 if (y->left!=NIL) 315 316 else 317 295 x=y->left; 296 else 297 x=y->right; 318 298 x->p=y->p; 319 299 if (y->p==NIL) 320 321 else 322 323 324 325 300 p=x; /* p - root */ 301 else 302 if (y==y->p->left) 303 y->p->left=x; 304 else 305 y->p->right=x; 326 306 if (y!=z) 327 307 { 328 329 330 // = foe Expr331 308 z->hashKey=y->hashKey; 309 z->val=y->val; 310 // = foe Expr 311 z->key=y->key; 332 312 } 333 313 if (y->color==BLACK) 334 314 RBdeleteFixup(x); 335 315 return y; 336 316 } … … 342 322 while ((x!=p) && (x->color==BLACK)) /* p - root */ 343 323 { 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 324 if (x==x->p->left) 325 { 326 w=x->p->right; 327 if (w->color==RED) 328 { 329 w->color=BLACK; 330 x->p->color=RED; 331 LeftRotate(x->p); 332 w=x->p->right; 333 } 334 if ((w->left->color==BLACK)&&(w->right->color==BLACK)) 335 { 336 w->color=RED; 337 x=x->p; 338 } 339 else 340 { 341 if (w->right->color==BLACK) 342 { 343 w->left->color=BLACK; 344 w->color=RED; 345 RightRotate(w); 346 w=x->p->right; 347 } 348 w->color=x->p->color; 349 x->p->color=BLACK; 350 w->right->color=BLACK; 351 LeftRotate(x->p); 352 x=p; /* p - root */ 353 } 354 } 355 else 356 { 357 w=x->p->left; 358 if (w->color==RED) 359 { 360 w->color=BLACK; 361 x->p->color=RED; 362 RightRotate(x->p); 363 w=x->p->left; 364 } 365 if ((w->right->color==BLACK)&&(w->left->color==BLACK)) 366 { 367 w->color=RED; 368 x=x->p; 369 } 370 else 371 { 372 if (w->left->color==BLACK) 373 { 374 w->right->color=BLACK; 375 w->color=RED; 376 LeftRotate(w); 377 w=x->p->left; 378 } 379 w->color=x->p->color; 380 x->p->color=BLACK; 381 w->left->color=BLACK; 382 RightRotate(x->p); 383 x=p; /* p - root */ 384 } 385 } 406 386 } 407 387 x->color=BLACK; … … 413 393 Table* Table :: IterativeTreeSearch (Expr const& key) 414 394 { Table *node; 415 416 417 418 419 420 421 422 423 424 // must be CompareExpr425 426 427 428 //if ( key == node->key)429 430 431 //if (key < node->key)432 433 434 435 node = node->right;436 437 395 int hash, flag; 396 397 hash=Expr_hash(key); 398 node = p; /* root */ 399 while ((node!=NIL)&&(node->hashKey!=hash)) 400 if (hash < node->hashKey) 401 node=node->left; 402 else 403 node=node->right; 404 // must be CompareExpr 405 while ((node!=NIL) && (hash == node->hashKey)) 406 { 407 flag=Expr::compare(key, node->key); 408 // if ( key == node->key) 409 if (!flag) 410 return (node); 411 // if (key < node->key) 412 if (flag==-1) 413 node = node->left; 414 else 415 node = node->right; 416 } 417 return (NIL); 438 418 } 439 419 … … 442 422 { Table *child; 443 423 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 424 copy->hashKey=tab->hashKey; 425 copy->key=tab->key; 426 copy->val=tab->val; 427 copy->color=tab->color; 428 if (tab->left==NIL) 429 copy->left=NIL; 430 else 431 { 432 child=static_cast<Table *> (new(Table)); 433 child->p=copy; 434 copy->left=child; 435 CopyTree(tab->left, child); 436 } 437 if (tab->right==NIL) 438 copy->right=NIL; 439 else 440 { 441 child=static_cast<Table *> (new(Table)); 442 child->p=copy; 443 copy->right=child; 444 CopyTree(tab->right, child); 445 } 466 446 } 467 447 … … 469 449 bool TableEqual(Table* tab1, Table* tab2) 470 450 { 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 451 if (tab1==NIL) 452 { 453 if (tab2==NIL) 454 return true; 455 return false; 456 } 457 else 458 { 459 if (tab2==NIL) 460 return false; 461 if ( (tab1->hashKey==tab2->hashKey) && 462 (tab1->color==tab2->color) && 463 (tab1->key==tab2->key) && 464 (tab1->val==tab2->val) && 465 (TableEqual(tab1->right, tab2->right)) && 466 (TableEqual(tab1->left, tab2->left)) ) 467 return true; 468 else 469 return false; 470 } 491 471 } 492 472 … … 494 474 void DeleteTree(Table *node) 495 475 { 496 497 498 499 500 501 } 502 476 if (node->left!=NIL) 477 DeleteTree(node->left); 478 if (node->right!=NIL) 479 DeleteTree(node->right); 480 delete(node); 481 } 482 503 483 504 484 /* return key for Domain */ 505 void InorderTreeWalk (Table* node, Expr& _res)485 void InorderTreeWalk (Table* node, Expr& _res) 506 486 { if (node!=NIL) 507 487 { InorderTreeWalk(node->left, _res); 508 509 488 InorderTreeWalk(node->right, _res); 489 _res = _res + (node->key)(); 510 490 } 511 491 } -
to-imperative/trunk/runtime/rf_table.hh
r922 r932 42 42 Table* RBdelete(Table *); 43 43 44 44 public : 45 45 46 47 ~Table();48 49 50 51 Expr Domain ();52 53 54 46 Table (); 47 inline ~Table(); 48 void Unbind(Expr& key); 49 int Lookup (Expr const& key, Expr& val); 50 int In_table(Expr const& key); 51 inline Expr Domain (); 52 void Table_copy(Table&); 53 void Replace_Table (Table const&); 54 void Bind(Expr const& key, Expr const& val); 55 55 56 57 58 56 inline unsigned get_type () const; 57 inline uint32_t hash () const; 58 inline bool operator==(Object const& _obj) const; 59 59 60 61 62 63 64 65 60 friend Table* TreeSuccessor(Table* node); 61 friend Table* TreeMinimum(Table* node); 62 friend void InorderTreeWalk(Table* node, Expr& res); 63 friend void CopyTree(Table *tab, Table *copy); 64 friend void DeleteTree(Table *node); 65 friend bool TableEqual(Table*, Table*); 66 66 }; 67 67 -
to-imperative/trunk/runtime/rf_table.ih
r922 r932 27 27 inline bool Table::operator == (Object const& _table) const 28 28 { 29 30 31 32 33 34 35 36 29 try { 30 Table const& _t=static_cast <Table const&>(_table); 31 return TableEqual(p, _t.p); 32 } 33 catch (std::bad_cast&) 34 { 35 return false; 36 } 37 37 } 38 39 40 /* Destructor: deleting of this-RBtree */ 41 42 inline Table :: ~Table() 43 { 44 DeleteTree(p); 45 } 46 47 48 inline Expr Table :: Domain () 49 { 50 Expr res = Expr(); 51 InorderTreeWalk(p, res); /* p - root */ 52 return res; 53 } 54 38 55 39 56 }
Note: See TracChangeset
for help on using the changeset viewer.