Changeset 5072c67 in mainline


Ignore:
Timestamp:
2018-07-05T21:41:20Z (6 years ago)
Author:
Dzejrou <dzejrou@…>
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)
Message:

cpp: finished constructors, fixed iterators, added misc operations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/cpp/include/impl/deque.hpp

    r806ce18 r5072c67  
    6161                using iterator_category = random_access_iterator_tag;
    6262
    63                 deque_const_iterator(deque<T, Allocator>& deq, size_type idx)
     63                deque_const_iterator(const deque<T, Allocator>& deq, size_type idx)
    6464                    : deq_{deq}, idx_{idx}
    6565                { /* DUMMY BODY */ }
     
    136136                }
    137137
     138                difference_type operator-(const deque_const_iterator& rhs)
     139                {
     140                    return idx_ - rhs.idx_;
     141                }
     142
    138143                size_type idx() const
    139144                {
     
    142147
    143148            private:
    144                 deque<T, Allocator>& deq_;
     149                const deque<T, Allocator>& deq_;
    145150                size_type idx_;
    146151        };
     
    258263                }
    259264
     265                difference_type operator-(const deque_iterator& rhs)
     266                {
     267                    return idx_ - rhs.idx_;
     268                }
     269
    260270                size_type idx() const
    261271                {
     
    324334
    325335            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_;
    328346            }
    329347
    330348            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_;
    333359            }
    334360
    335361            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);
    339370            }
    340371
    341372            deque(const deque& other)
    342             {
    343                 // TODO: implement
    344             }
     373                : deque{other.begin(), other.end(), other.allocator_}
     374            { /* DUMMY BODY */ }
    345375
    346376            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();
    349388            }
    350389
    351390            deque(const deque& other, const allocator_type& alloc)
    352             {
    353                 // TODO: implement
    354             }
     391                : deque{other.begin(), other.end(), alloc}
     392            { /* DUMMY BODY */ }
    355393
    356394            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();
    359406            }
    360407
    361408            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());
    364415            }
    365416
     
    377428                noexcept(allocator_traits<allocator_type>::is_always_equal::value)
    378429            {
    379                 // TODO: implement
     430                swap(other);
     431                other.clear();
    380432            }
    381433
     
    512564            reference at(size_type idx)
    513565            {
    514                 // TODO: implement
     566                // TODO: bounds checking
     567                return operator[](idx);
    515568            }
    516569
    517570            const_reference at(size_type idx) const
    518571            {
    519                 // TODO: implement
     572                // TODO: bounds checking
     573                return operator[](idx);
    520574            }
    521575
    522576            reference front()
    523577            {
    524                 // TODO: implement
     578                return *begin();
    525579            }
    526580
    527581            const_reference front() const
    528582            {
    529                 // TODO: implement
     583                return *cbegin();
    530584            }
    531585
    532586            reference back()
    533587            {
    534                 // TODO: implement
     588                auto tmp = end();
     589
     590                return *(--tmp);
    535591            }
    536592
    537593            const_reference back() const
    538594            {
    539                 // TODO: implement
     595                auto tmp = cend();
     596
     597                return *(--tmp);
    540598            }
    541599
     
    687745            void clear() noexcept
    688746            {
    689                 fini_();
     747                if (data_)
     748                    fini_();
    690749
    691750                front_bucket_ = default_front_;
     
    733792            }
    734793
     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
    735825            void fini_()
    736826            {
Note: See TracChangeset for help on using the changeset viewer.