Changeset e9027b5 in mainline for uspace/lib/cpp/include/internal/hash_table.hpp
- Timestamp:
- 2018-07-05T21:41:21Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- f67b4ef
- Parents:
- 871cfe0c
- git-author:
- Dzejrou <dzejrou@…> (2018-04-23 18:58:08)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:21)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/internal/hash_table.hpp
r871cfe0c re9027b5 121 121 ); 122 122 } 123 124 template<class Table, class Key> 125 static typename Table::size_type erase(Table& table, const Key& key) 126 { 127 auto idx = table.get_bucket_idx_(key); 128 auto head = table.table_[idx].head; 129 auto current = head; 130 131 do 132 { 133 if (table.key_eq_(key, table.key_extractor_(current->value))) 134 { 135 --table.size_; 136 137 if (current == head) 138 { 139 if (current->next != head) 140 table.table_[idx].head = current->next; 141 else 142 table.table_[idx].head = nullptr; 143 } 144 145 current->unlink(); 146 delete current; 147 148 return 1; 149 } 150 else 151 current = current->next; 152 } 153 while (current != head); 154 155 return 0; 156 } 123 157 }; 124 158 … … 154 188 // link (if key is already in it, place the new copy 155 189 // next to it, otherwise just return head) 190 } 191 192 template<class Table, class Key> 193 static typename Table::size_type erase(Table& table, const Key& key) 194 { 195 // TODO: erase all items with given key 156 196 } 157 197 }; … … 169 209 using iterator_category = forward_iterator_tag; 170 210 171 hash_table_const_iterator( hash_table_bucket<value_type, size_type>* table = nullptr,211 hash_table_const_iterator(const hash_table_bucket<value_type, size_type>* table = nullptr, 172 212 size_type idx = size_type{}, size_type max_idx = size_type{}, 173 list_node<value_type>* current = nullptr)213 const list_node<value_type>* current = nullptr) 174 214 : table_{table}, idx_{idx}, max_idx_{max_idx}, current_{current} 175 215 { /* DUMMY BODY */ } … … 239 279 list_node<value_type>* node() 240 280 { 281 return const_cast<list_node<value_type>*>(current_); 282 } 283 284 const list_node<value_type>* node() const 285 { 241 286 return current_; 242 287 } 243 288 244 const list_node<value_type>* node() const245 { 246 return current_;289 size_type idx() const 290 { 291 return idx_; 247 292 } 248 293 249 294 private: 250 hash_table_bucket<value_type, size_type>* table_;295 const hash_table_bucket<value_type, size_type>* table_; 251 296 size_type idx_; 252 297 size_type max_idx_; 253 list_node<value_type>* current_;298 const list_node<value_type>* current_; 254 299 }; 255 300 … … 358 403 } 359 404 405 size_type idx() const 406 { 407 return idx_; 408 } 409 360 410 template<class ConstRef, class ConstPtr> 361 411 operator hash_table_const_iterator< … … 363 413 >() const 364 414 { 365 return hash_table_const_iterator {415 return hash_table_const_iterator<value_type, ConstRef, ConstPtr, size_type>{ 366 416 table_, idx_, max_idx_, current_ 367 417 }; … … 401 451 402 452 // TODO: requirement for forward iterator is default constructibility, fix others! 403 hash_table_const_local_iterator( list_node<value_type>* head = nullptr,404 list_node<value_type>* current = nullptr)453 hash_table_const_local_iterator(const list_node<value_type>* head = nullptr, 454 const list_node<value_type>* current = nullptr) 405 455 : head_{head}, current_{current} 406 456 { /* DUMMY BODY */ } … … 438 488 } 439 489 490 440 491 list_node<value_type>* node() 441 492 { 493 return const_cast<list_node<value_type>*>(current_); 494 } 495 496 const list_node<value_type>* node() const 497 { 442 498 return current_; 443 499 } 444 500 445 const list_node<value_type>* node() const446 {447 return current_;448 }449 450 501 private: 451 list_node<value_type>* head_;452 list_node<value_type>* current_;502 const list_node<value_type>* head_; 503 const list_node<value_type>* current_; 453 504 }; 454 505 … … 530 581 >() const 531 582 { 532 return hash_table_const_local_iterator{head_, current_}; 583 return hash_table_const_local_iterator< 584 value_type, ConstRef, ConstPtr 585 >{head_, current_}; 533 586 } 534 587 … … 659 712 660 713 ++size_; 714 // TODO: if we go over max load factor, rehash 661 715 } 662 716 … … 673 727 674 728 ++size_; 729 // TODO: if we go over max load factor, rehash 675 730 } 676 731 677 732 size_type erase(const key_type& key) 678 733 { 679 // TODO: implement734 return Policy::erase(*this, key); 680 735 } 681 736 682 737 iterator erase(const_iterator it) 683 738 { 684 // TODO: implement 739 auto node = it.node(); 740 auto idx = it.idx(); 741 742 /** 743 * Note: This way we will continue on the next bucket 744 * if this is the last element in its bucket. 745 */ 746 iterator res{table_, idx, size_, node}; 747 ++res; 748 749 if (table_[idx].head == node) 750 { 751 if (node->next != node) 752 table_[idx].head = node->next; 753 else 754 table_[idx].head = nullptr; 755 } 756 757 node->unlink(); 758 delete node; 759 760 return res; 685 761 } 686 762
Note:
See TracChangeset
for help on using the changeset viewer.