Changeset 5072c67 in mainline
- Timestamp:
- 2018-07-05T21:41:20Z (7 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- a44c35d
- Parents:
- 806ce18
- git-author:
- Dzejrou <dzejrou@…> (2018-04-11 19:14:16)
- git-committer:
- Dzejrou <dzejrou@…> (2018-07-05 21:41:20)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/cpp/include/impl/deque.hpp
r806ce18 r5072c67 61 61 using iterator_category = random_access_iterator_tag; 62 62 63 deque_const_iterator( deque<T, Allocator>& deq, size_type idx)63 deque_const_iterator(const deque<T, Allocator>& deq, size_type idx) 64 64 : deq_{deq}, idx_{idx} 65 65 { /* DUMMY BODY */ } … … 136 136 } 137 137 138 difference_type operator-(const deque_const_iterator& rhs) 139 { 140 return idx_ - rhs.idx_; 141 } 142 138 143 size_type idx() const 139 144 { … … 142 147 143 148 private: 144 deque<T, Allocator>& deq_;149 const deque<T, Allocator>& deq_; 145 150 size_type idx_; 146 151 }; … … 258 263 } 259 264 265 difference_type operator-(const deque_iterator& rhs) 266 { 267 return idx_ - rhs.idx_; 268 } 269 260 270 size_type idx() const 261 271 { … … 324 334 325 335 explicit deque(size_type n, const allocator_type& alloc = allocator_type{}) 326 { 327 // TODO: implement 336 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{}, 337 front_bucket_{}, back_bucket_{}, bucket_count_{}, 338 bucket_capacity_{}, size_{n}, data_{} 339 { 340 prepare_for_size_(n); 341 init_(); 342 343 for (size_type i = 0; i < size_; ++i) 344 allocator_.construct(&(*this)[i]); 345 back_bucket_idx_ = size_ % bucket_size_; 328 346 } 329 347 330 348 deque(size_type n, const value_type& value, const allocator_type& alloc = allocator_type{}) 331 { 332 // TODO: implement 349 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{}, 350 front_bucket_{}, back_bucket_{}, bucket_count_{}, 351 bucket_capacity_{}, size_{n}, data_{} 352 { 353 prepare_for_size_(n); 354 init_(); 355 356 for (size_type i = 0; i < size_; ++i) 357 (*this)[i] = value; 358 back_bucket_idx_ = size_ % bucket_size_; 333 359 } 334 360 335 361 template<class InputIterator> 336 deque(InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type{}) 337 { 338 // TODO: implement 362 deque(InputIterator first, InputIterator last, 363 const allocator_type& alloc = allocator_type{}) 364 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, 365 back_bucket_idx_{}, front_bucket_{}, back_bucket_{}, 366 bucket_count_{}, bucket_capacity_{}, size_{}, 367 data_{} 368 { 369 copy_from_range_(first, last); 339 370 } 340 371 341 372 deque(const deque& other) 342 { 343 // TODO: implement 344 } 373 : deque{other.begin(), other.end(), other.allocator_} 374 { /* DUMMY BODY */ } 345 375 346 376 deque(deque&& other) 347 { 348 // TODO: implement 377 : allocator_{move(other.allocator_)}, 378 front_bucket_idx_{other.front_bucket_idx_}, 379 back_bucket_idx_{other.front_bucket_idx_}, 380 front_bucket_{other.front_bucket_}, 381 back_bucket_{other.back_bucket_}, 382 bucket_count_{other.bucket_count_}, 383 bucket_capacity_{other.bucket_capacity_}, 384 size_{other.size_}, data_{other.data_} 385 { 386 other.data_ = nullptr; 387 other.clear(); 349 388 } 350 389 351 390 deque(const deque& other, const allocator_type& alloc) 352 { 353 // TODO: implement 354 } 391 : deque{other.begin(), other.end(), alloc} 392 { /* DUMMY BODY */ } 355 393 356 394 deque(deque&& other, const allocator_type& alloc) 357 { 358 // TODO: implement 395 : allocator_{alloc}, 396 front_bucket_idx_{other.front_bucket_idx_}, 397 back_bucket_idx_{other.front_bucket_idx_}, 398 front_bucket_{other.front_bucket_}, 399 back_bucket_{other.back_bucket_}, 400 bucket_count_{other.bucket_count_}, 401 bucket_capacity_{other.bucket_capacity_}, 402 size_{other.size_}, data_{other.data_} 403 { 404 other.data_ = nullptr; 405 other.clear(); 359 406 } 360 407 361 408 deque(initializer_list<T> init, const allocator_type& alloc = allocator_type{}) 362 { 363 // TODO: implement 409 : allocator_{alloc}, front_bucket_idx_{bucket_size_}, 410 back_bucket_idx_{}, front_bucket_{}, back_bucket_{}, 411 bucket_count_{}, bucket_capacity_{}, size_{}, 412 data_{} 413 { 414 copy_from_range_(init.begin(), init.end()); 364 415 } 365 416 … … 377 428 noexcept(allocator_traits<allocator_type>::is_always_equal::value) 378 429 { 379 // TODO: implement 430 swap(other); 431 other.clear(); 380 432 } 381 433 … … 512 564 reference at(size_type idx) 513 565 { 514 // TODO: implement 566 // TODO: bounds checking 567 return operator[](idx); 515 568 } 516 569 517 570 const_reference at(size_type idx) const 518 571 { 519 // TODO: implement 572 // TODO: bounds checking 573 return operator[](idx); 520 574 } 521 575 522 576 reference front() 523 577 { 524 // TODO: implement578 return *begin(); 525 579 } 526 580 527 581 const_reference front() const 528 582 { 529 // TODO: implement583 return *cbegin(); 530 584 } 531 585 532 586 reference back() 533 587 { 534 // TODO: implement 588 auto tmp = end(); 589 590 return *(--tmp); 535 591 } 536 592 537 593 const_reference back() const 538 594 { 539 // TODO: implement 595 auto tmp = cend(); 596 597 return *(--tmp); 540 598 } 541 599 … … 687 745 void clear() noexcept 688 746 { 689 fini_(); 747 if (data_) 748 fini_(); 690 749 691 750 front_bucket_ = default_front_; … … 733 792 } 734 793 794 void prepare_for_size_(size_type size) 795 { 796 if (data_) 797 fini_(); 798 799 if (size < bucket_size_) // Always front_bucket_ != back_bucket_. 800 bucket_count_ = bucket_capacity_ = 2; 801 else if (size % bucket_size_ == 0) 802 bucket_count_ = bucket_capacity_ = size / bucket_size_ + 1; 803 else 804 bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2; 805 806 front_bucket_ = 0; 807 back_bucket_ = bucket_capacity_ - 1; 808 } 809 810 template<class Iterator> 811 void copy_from_range_(Iterator first, Iterator last) 812 { 813 size_ = distance(first, last); 814 prepare_for_size_(size_); 815 init_(); 816 817 auto it = begin(); 818 while (first != last) 819 *it++ = *first++; 820 821 // Remainder is the amount of elements in the last bucket. 822 back_bucket_idx_ = size_ % bucket_size_; 823 } 824 735 825 void fini_() 736 826 {
Note:
See TracChangeset
for help on using the changeset viewer.