Changeset 983
- Timestamp:
- Jul 9, 2003, 12:45:35 PM (18 years ago)
- Location:
- to-imperative/trunk/runtime
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
to-imperative/trunk/runtime/rf_table.cc
r969 r983 9 9 TableNode* TableNode::nil = NULL; 10 10 11 // zaglushka12 int Expr_hash(Expr const&);13 14 15 void Table::Bind (Expr const& _key, Expr const& _val)16 {17 TableNode *newNode;18 if ((newNode = IterativeTreeSearch(_key)) == TableNode::nil) {19 newNode = new TableNode;20 newNode->key = _key;21 newNode->hashKey = Expr_hash(_key);22 newNode->left = TableNode::nil;23 newNode->right = TableNode::nil;24 newNode->p = TableNode::nil;25 newNode->val = _val;26 RB_Insert(newNode);27 }28 else29 newNode->val = _val;30 }31 32 void Table::Unbind (Expr& _key)33 {34 TableNode *newNode;35 if ((newNode = IterativeTreeSearch(_key)) != TableNode::nil) {36 newNode = RB_Delete(newNode);37 delete(newNode);38 }39 }40 41 42 bool Table::Lookup (Expr const& _key, Expr& _val)43 {44 TableNode *newNode;45 if ((newNode = IterativeTreeSearch(_key)) == TableNode::nil)46 return false;47 else {48 _val = newNode->val;49 return true;50 }51 }52 53 54 bool Table::InTable (Expr const& _key)55 {56 TableNode *newNode;57 if ((newNode = IterativeTreeSearch(_key)) == TableNode::nil)58 return false;59 else60 return true;61 }62 63 64 //65 // Table_copy : this - sourceTable, newTable - result66 void Table::TableCopy (Table& _new_table)67 {68 TableNode *node;69 if (root != TableNode::nil) {70 node = new TableNode;71 node->p = TableNode::nil;72 _new_table.root = node; // p - root73 CopyTree(root, node); // p - root74 }75 }76 77 78 //79 // ReplaceTable: this - TargetTable80 void Table::ReplaceTable (Table const& _source_table)81 {82 TableNode* x;83 if (root != TableNode::nil) {84 DeleteTree(root);85 root = TableNode::nil;86 }87 if (_source_table.root != TableNode::nil) {88 x = new TableNode;89 x->p = TableNode::nil;90 root = x;91 CopyTree(_source_table.root, root);92 }93 }94 95 96 11 // 97 12 // inserting of element newNode in this-Table 98 void Table:: TreeInsert(TableNode* _newNode)13 void Table::InsertNode (TableNode* _newNode) 99 14 { 100 15 TableNode* x; … … 146 61 147 62 148 void Table::LeftRotate (TableNode* _x)63 void Table::LeftRotate_RB (TableNode* _x) 149 64 { 150 65 TableNode* y; … … 165 80 166 81 167 void Table::RightRotate (TableNode* _y)82 void Table::RightRotate_RB (TableNode* _y) 168 83 { 169 84 TableNode* x; … … 184 99 185 100 186 void Table:: RB_Insert(TableNode* _x)101 void Table::InsertNode_RB (TableNode* _x) 187 102 { 188 103 TableNode *y; 189 TreeInsert(_x);104 InsertNode(_x); 190 105 _x->color = RED; 191 106 while ((_x != root) && (_x->p->color == RED)) { … … 200 115 if (_x == _x->p->right) { 201 116 _x = _x->p; 202 LeftRotate (_x);117 LeftRotate_RB(_x); 203 118 } 204 119 _x->p->color = BLACK; 205 120 _x->p->p->color =RED; 206 RightRotate (_x->p->p);121 RightRotate_RB(_x->p->p); 207 122 } 208 123 } else { … … 216 131 if (_x == _x->p->left) { 217 132 _x = _x->p; 218 RightRotate (_x);133 RightRotate_RB(_x); 219 134 } 220 135 _x->p->color = BLACK; 221 136 _x->p->p->color = RED; 222 LeftRotate (_x->p->p);137 LeftRotate_RB(_x->p->p); 223 138 } 224 139 } … … 228 143 229 144 230 TableNode* Table:: RB_Delete(TableNode *_z)145 TableNode* Table::DeleteNode_RB (TableNode *_z) 231 146 { 232 147 TableNode* x; … … 254 169 } 255 170 if (y->color == BLACK) 256 RB_DeleteFixup(x);171 DeleteFixup_RB(x); 257 172 return y; 258 173 } 259 174 260 175 261 void Table:: RB_DeleteFixup(TableNode* _x)176 void Table::DeleteFixup_RB (TableNode* _x) 262 177 { 263 178 TableNode* w; … … 268 183 w->color = BLACK; 269 184 _x->p->color = RED; 270 LeftRotate (_x->p);185 LeftRotate_RB(_x->p); 271 186 w = _x->p->right; 272 187 } … … 278 193 w->left->color = BLACK; 279 194 w->color = RED; 280 RightRotate (w);195 RightRotate_RB(w); 281 196 w = _x->p->right; 282 197 } … … 284 199 _x->p->color = BLACK; 285 200 w->right->color = BLACK; 286 LeftRotate (_x->p);201 LeftRotate_RB(_x->p); 287 202 _x=root; 288 203 } … … 292 207 w->color = BLACK; 293 208 _x->p->color = RED; 294 RightRotate (_x->p);209 RightRotate_RB(_x->p); 295 210 w = _x->p->left; 296 211 } … … 302 217 w->right->color = BLACK; 303 218 w->color = RED; 304 LeftRotate (w);219 LeftRotate_RB(w); 305 220 w = _x->p->left; 306 221 } … … 308 223 _x->p->color = BLACK; 309 224 w->left->color = BLACK; 310 RightRotate (_x->p);225 RightRotate_RB(_x->p); 311 226 _x=root; 312 227 } … … 317 232 318 233 319 TableNode* Table:: IterativeTreeSearch(Expr const& _key)234 TableNode* Table::SearchNode (Expr const& _key) 320 235 { 321 236 TableNode* node; … … 370 285 } 371 286 372 373 287 bool TableEqual(TableNode* _tab1, TableNode* _tab2) 374 288 { … … 404 318 405 319 406 void InorderTreeWalk(TableNode* _node, Expr& _res)320 void ListNode (TableNode* _node, Expr& _res) 407 321 { 408 322 if (_node != TableNode::nil) { 409 InorderTreeWalk(_node->left, _res);410 InorderTreeWalk(_node->right, _res);323 ListNode(_node->left, _res); 324 ListNode(_node->right, _res); 411 325 _res = _res + (_node->key)(); 412 326 } … … 436 350 437 351 438 // hash - zaglushka 439 int Expr_hash (Expr const& _key) 440 { 441 return _key.get_len(); 442 } 443 444 445 } 352 } -
to-imperative/trunk/runtime/rf_table.hh
r969 r983 24 24 TableNode* root; // root of RB-tree 25 25 26 void TreeInsert(TableNode*);27 void RB_Insert(TableNode*);28 void LeftRotate (TableNode*);29 void RightRotate (TableNode*);30 TableNode* IterativeTreeSearch(Expr const&);31 void RB_DeleteFixup(TableNode*);32 TableNode* RB_Delete(TableNode*);26 void InsertNode (TableNode*); 27 void InsertNode_RB (TableNode*); 28 void LeftRotate_RB (TableNode*); 29 void RightRotate_RB (TableNode*); 30 TableNode* SearchNode (Expr const&); 31 void DeleteFixup_RB (TableNode*); 32 TableNode* DeleteNode_RB (TableNode*); 33 33 34 34 public : 35 35 inline Table (); 36 36 inline ~Table(); 37 void Unbind (Expr& _key);38 bool Lookup (Expr const& _key, Expr& _val);39 bool InTable (Expr const& _key);37 inline void Unbind (Expr& _key); 38 inline bool Lookup (Expr const& _key, Expr& _val); 39 inline bool InTable (Expr const& _key); 40 40 inline Expr Domain (); 41 void TableCopy (Table& _tab);42 void ReplaceTable (Table const& _tab);43 void Bind (Expr const& _key, Expr const& _val);41 inline void TableCopy (Table& _tab); 42 inline void ReplaceTable (Table const& _tab); 43 inline void Bind (Expr const& _key, Expr const& _val); 44 44 inline unsigned get_type () const; 45 45 inline uint32_t hash () const; … … 65 65 friend TableNode* TreeSuccessor (TableNode*) ; 66 66 friend TableNode* TreeMinimum (TableNode*) ; 67 friend void InorderTreeWalk(TableNode*, Expr&) ;67 friend void ListNode (TableNode*, Expr&) ; 68 68 friend void CopyTree (TableNode*, TableNode*) ; 69 69 friend void DeleteTree (TableNode*) ; -
to-imperative/trunk/runtime/rf_table.ih
r969 r983 57 57 { 58 58 Expr res = Expr(); 59 InorderTreeWalk(root, res);59 ListNode(root, res); 60 60 return res; 61 61 } 62 63 // zaglushka - hash-function 64 inline int Expr_hash (Expr const& _key) 65 { 66 return _key.get_len(); 67 } 68 69 inline void Table::Bind (Expr const& _key, Expr const& _val) 70 { 71 TableNode *newNode; 72 if ((newNode = SearchNode(_key)) == TableNode::nil) { 73 newNode = new TableNode; 74 newNode->key = _key; 75 newNode->hashKey = Expr_hash(_key); 76 newNode->left = TableNode::nil; 77 newNode->right = TableNode::nil; 78 newNode->p = TableNode::nil; 79 newNode->val = _val; 80 InsertNode_RB(newNode); 81 } 82 else 83 newNode->val = _val; 84 } 85 86 inline void Table::Unbind (Expr& _key) 87 { 88 TableNode *newNode; 89 if ((newNode = SearchNode(_key)) != TableNode::nil) { 90 newNode = DeleteNode_RB(newNode); 91 delete(newNode); 92 } 93 } 94 95 96 inline bool Table::Lookup (Expr const& _key, Expr& _val) 97 { 98 TableNode *newNode; 99 if ((newNode = SearchNode(_key)) == TableNode::nil) 100 return false; 101 else { 102 _val = newNode->val; 103 return true; 104 } 105 } 106 107 108 inline bool Table::InTable (Expr const& _key) 109 { 110 TableNode *newNode; 111 if ((newNode = SearchNode(_key)) == TableNode::nil) 112 return false; 113 else 114 return true; 115 } 116 117 118 // 119 // Table_copy : this - sourceTable, newTable - result 120 inline void Table::TableCopy (Table& _new_table) 121 { 122 TableNode *node; 123 if (root != TableNode::nil) { 124 node = new TableNode; 125 node->p = TableNode::nil; 126 _new_table.root = node; // p - root 127 CopyTree(root, node); // p - root 128 } 129 } 130 131 132 // 133 // ReplaceTable: this - TargetTable 134 inline void Table::ReplaceTable (Table const& _source_table) 135 { 136 TableNode* x; 137 if (root != TableNode::nil) { 138 DeleteTree(root); 139 root = TableNode::nil; 140 } 141 if (_source_table.root != TableNode::nil) { 142 x = new TableNode; 143 x->p = TableNode::nil; 144 root = x; 145 CopyTree(_source_table.root, root); 146 } 147 } 148 62 149 63 150 }
Note: See TracChangeset
for help on using the changeset viewer.