Changeset bb1d15c in mainline
- Timestamp:
- 2018-07-05T21:41:24Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- c300bb5
- Parents:
- 2e53e83d
- git-author:
- Dzejrou <dzejrou@…> (2018-05-16 23:14:11)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:24)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/list.hpp
r2e53e83d rbb1d15c 30 30 #define LIBCPP_LIST 31 31 32 #include <__bits/insert_iterator.hpp> 33 #include <__bits/list.hpp> 32 34 #include <cstdlib> 33 #include <__bits/list.hpp>34 35 #include <iterator> 35 36 #include <memory> … … 43 44 namespace aux 44 45 { 46 template<class T> 47 class list_iterator; 48 45 49 template<class T> 46 50 class list_const_iterator … … 53 57 using size_type = typename list<T>::size_type; 54 58 55 using iterator_category = forward_iterator_tag;59 using iterator_category = bidirectional_iterator_tag; 56 60 57 61 list_const_iterator(list_node<value_type>* node = nullptr, 58 list_node<value_type>* head = nullptr) 59 : current_{node}, head_{head} 62 list_node<value_type>* head = nullptr, 63 bool end = true) 64 : current_{node}, head_{head}, end_{end} 60 65 { /* DUMMY BODY */ } 61 66 … … 65 70 list_const_iterator& operator=(list_const_iterator&&) = default; 66 71 72 list_const_iterator(const list_iterator<T>& other) 73 : current_{other.current_}, head_{other.head_} 74 { /* DUMMY BODY */ } 75 67 76 reference operator*() const 68 77 { … … 72 81 list_const_iterator& operator++() 73 82 { 74 if ( current_)83 if (!end_ && current_) 75 84 { 76 85 if (current_->next == head_) 77 current_ = nullptr;86 end_ = true; 78 87 else 79 88 current_ = current_->next; … … 85 94 list_const_iterator operator++(int) 86 95 { 87 auto bckp = current_; 88 89 if (current_) 96 auto old = *this; 97 ++(*this); 98 99 return old; 100 } 101 102 list_const_iterator& operator--() 103 { 104 if (end_) 105 end_ = false; 106 else if (current_) 90 107 { 91 if (current_ ->next == head_)92 current_ = nullptr;108 if (current_ != head_) 109 current_ = current_->prev; 93 110 else 94 current_ = current_->next;111 end_ = true; 95 112 } 96 113 97 return list_const_iterator{bckp}; 114 return *this; 115 } 116 117 list_const_iterator operator--(int) 118 { 119 auto old = *this; 120 --(*this); 121 122 return old; 98 123 } 99 124 … … 123 148 } 124 149 150 bool end() const 151 { 152 return end_; 153 } 154 125 155 private: 126 156 list_node<value_type>* current_; 127 157 list_node<value_type>* head_; 158 bool end_; 128 159 }; 129 160 … … 131 162 bool operator==(const list_const_iterator<T>& lhs, const list_const_iterator<T>& rhs) 132 163 { 133 return lhs.node() == rhs.node();164 return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end()); 134 165 } 135 166 … … 150 181 using size_type = typename list<T>::size_type; 151 182 152 using iterator_category = forward_iterator_tag;183 using iterator_category = bidirectional_iterator_tag; 153 184 154 185 list_iterator(list_node<value_type>* node = nullptr, 155 list_node<value_type>* head = nullptr) 156 : current_{node}, head_{head} 186 list_node<value_type>* head = nullptr, 187 bool end = true) 188 : current_{node}, head_{head}, end_{end} 157 189 { /* DUMMY BODY */ } 158 190 … … 162 194 list_iterator& operator=(list_iterator&&) = default; 163 195 164 reference operator*() 196 reference operator*() const 165 197 { 166 198 return current_->value; … … 169 201 list_iterator& operator++() 170 202 { 171 if ( current_)203 if (!end_ && current_) 172 204 { 173 205 if (current_->next == head_) 174 current_ = nullptr;206 end_ = true; 175 207 else 176 208 current_ = current_->next; … … 182 214 list_iterator operator++(int) 183 215 { 184 auto bckp = current_; 185 186 if (current_) 216 auto old = *this; 217 ++(*this); 218 219 return old; 220 } 221 222 list_iterator& operator--() 223 { 224 if (end_) 225 end_ = false; 226 else if (current_) 187 227 { 188 if (current_ ->next == head_)189 current_ = nullptr;228 if (current_ != head_) 229 current_ = current_->prev; 190 230 else 191 current_ = current_->next;231 end_ = true; 192 232 } 193 233 194 return list_iterator{bckp}; 234 return *this; 235 } 236 237 list_iterator operator--(int) 238 { 239 auto old = *this; 240 --(*this); 241 242 return old; 195 243 } 196 244 … … 225 273 } 226 274 275 bool end() const 276 { 277 return end_; 278 } 279 227 280 private: 228 281 list_node<value_type>* current_; 229 282 list_node<value_type>* head_; 283 bool end_; 230 284 }; 231 285 … … 233 287 bool operator==(const list_iterator<T>& lhs, const list_iterator<T>& rhs) 234 288 { 235 return lhs.node() == rhs.node();289 return (lhs.node() == rhs.node()) && (lhs.end() == rhs.end()); 236 290 } 237 291 … … 283 337 { 284 338 init_( 285 aux::insert_iterator<value_type>{ value_type{}},286 aux::insert_iterator<value_type>{size_ }339 aux::insert_iterator<value_type>{size_type{}, value_type{}}, 340 aux::insert_iterator<value_type>{size_, value_type{}} 287 341 ); 288 342 } … … 293 347 { 294 348 init_( 295 aux::insert_iterator<value_type>{ val},296 aux::insert_iterator<value_type>{n }349 aux::insert_iterator<value_type>{size_type{}, val}, 350 aux::insert_iterator<value_type>{n, value_type{}} 297 351 ); 298 352 } … … 316 370 { 317 371 other.head_ = nullptr; 372 other.size_ = size_type{}; 318 373 } 319 374 320 375 list(const list& other, const allocator_type alloc) 321 : allocator_{alloc}, head_{nullptr}, size_{ other.size_}322 { 376 : allocator_{alloc}, head_{nullptr}, size_{} 377 { // Size is set in init_. 323 378 init_(other.begin(), other.end()); 324 379 } … … 330 385 { 331 386 other.head_ = nullptr; 387 other.size_ = size_type{}; 332 388 } 333 389 … … 350 406 351 407 init_(other.begin(), other.end()); 408 409 return *this; 352 410 } 353 411 … … 357 415 fini_(); 358 416 417 head_ = move(other.head_); 418 size_ = move(other.size_); 359 419 allocator_ = move(other.allocator_); 360 420 361 init_(362 make_move_iterator(other.begin()),363 make_move_iterator(other.end()) 364 );421 other.head_ = nullptr; 422 other.size_ = size_type{}; 423 424 return *this; 365 425 } 366 426 … … 370 430 371 431 init_(init.begin(), init.end()); 432 433 return *this; 372 434 } 373 435 … … 385 447 386 448 init_( 387 aux::insert_iterator<value_type>{ val},388 aux::insert_iterator<value_type>{n }449 aux::insert_iterator<value_type>{size_type{}, val}, 450 aux::insert_iterator<value_type>{n, value_type{}} 389 451 ); 390 452 } … … 404 466 iterator begin() noexcept 405 467 { 406 return iterator{head_, head_ };468 return iterator{head_, head_, size_ == 0U}; 407 469 } 408 470 … … 414 476 iterator end() noexcept 415 477 { 416 return iterator{ nullptr, head_};478 return iterator{get_last_(), head_, true}; 417 479 } 418 480 … … 444 506 const_iterator cbegin() const noexcept 445 507 { 446 return const_iterator{head_, head_ };508 return const_iterator{head_, head_, size_ == 0U}; 447 509 } 448 510 449 511 const_iterator cend() const noexcept 450 512 { 451 return const_iterator{ nullptr, head_};513 return const_iterator{get_last_(), head_, true}; 452 514 } 453 515 … … 608 670 head_ = head_->prev; 609 671 610 return iterator{node->prev, head_ };672 return iterator{node->prev, head_, false}; 611 673 } 612 674 … … 625 687 return insert( 626 688 position, 627 aux::insert_iterator<value_type>{ 0u, val},628 aux::insert_iterator<value_type>{n }689 aux::insert_iterator<value_type>{size_type{}, val}, 690 aux::insert_iterator<value_type>{n, value_type{}} 629 691 ); 630 692 } … … 642 704 } 643 705 644 return iterator{position.node()->next, head_ };706 return iterator{position.node()->next, head_, false}; 645 707 } 646 708 … … 675 737 delete node; 676 738 677 return iterator{next, head_ };739 return iterator{next, head_, size_ == 0U}; 678 740 } 679 741 … … 703 765 } 704 766 705 return iterator{next, head_ };767 return iterator{next, head_, size_ == 0U}; 706 768 } 707 769 … … 1033 1095 } 1034 1096 1035 aux::list_node<value_type>* get_last_() 1097 aux::list_node<value_type>* get_last_() const 1036 1098 { 1037 1099 if (!head_)
Note:
See TracChangeset
for help on using the changeset viewer.