[7a7ecbd] | 1 | /*
|
---|
[3a06cc6] | 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
[7a7ecbd] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #ifndef LIBCPP_DEQUE
|
---|
| 30 | #define LIBCPP_DEQUE
|
---|
| 31 |
|
---|
[9019d85] | 32 | #include <algorithm>
|
---|
[7a7ecbd] | 33 | #include <initializer_list>
|
---|
[9019d85] | 34 | #include <internal/insert_iterator.hpp>
|
---|
[3a06cc6] | 35 | #include <iterator>
|
---|
[7a7ecbd] | 36 | #include <memory>
|
---|
| 37 |
|
---|
| 38 | namespace std
|
---|
| 39 | {
|
---|
[3a06cc6] | 40 | template<class T, class Allocator = allocator<T>>
|
---|
| 41 | class deque;
|
---|
[7a7ecbd] | 42 |
|
---|
| 43 | namespace aux
|
---|
| 44 | {
|
---|
[6e93323] | 45 | /**
|
---|
| 46 | * Note: We decided that these iterators contain a
|
---|
| 47 | * reference to the container and an index, which
|
---|
| 48 | * allows us to use the already implemented operator[]
|
---|
| 49 | * on deque and also allows us to conform to the requirement
|
---|
| 50 | * of the standard that functions such as push_back
|
---|
| 51 | * invalidate the .end() iterator.
|
---|
| 52 | */
|
---|
| 53 |
|
---|
[9019d85] | 54 | template<class T, class Allocator>
|
---|
| 55 | class deque_iterator;
|
---|
| 56 |
|
---|
[7a7ecbd] | 57 | template<class T, class Allocator>
|
---|
[6e93323] | 58 | class deque_const_iterator
|
---|
[7a7ecbd] | 59 | {
|
---|
[3a06cc6] | 60 | public:
|
---|
[6e93323] | 61 | using size_type = typename deque<T, Allocator>::size_type;
|
---|
| 62 | using value_type = typename deque<T, Allocator>::value_type;
|
---|
| 63 | using reference = typename deque<T, Allocator>::const_reference;
|
---|
| 64 | using difference_type = size_type;
|
---|
| 65 | using pointer = const value_type*;
|
---|
| 66 | using iterator_category = random_access_iterator_tag;
|
---|
| 67 |
|
---|
[5072c67] | 68 | deque_const_iterator(const deque<T, Allocator>& deq, size_type idx)
|
---|
[3a06cc6] | 69 | : deq_{deq}, idx_{idx}
|
---|
| 70 | { /* DUMMY BODY */ }
|
---|
| 71 |
|
---|
[9019d85] | 72 | deque_const_iterator(const deque_const_iterator& other)
|
---|
| 73 | : deq_{other.deq_}, idx_{other.idx_}
|
---|
| 74 | { /* DUMMY BODY */ }
|
---|
| 75 |
|
---|
[6e93323] | 76 | deque_const_iterator& operator=(const deque_const_iterator& other)
|
---|
| 77 | {
|
---|
| 78 | deq_ = other.deq_;
|
---|
| 79 | idx_ = other.idx_;
|
---|
| 80 |
|
---|
| 81 | return *this;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | reference operator*() const
|
---|
| 85 | {
|
---|
| 86 | return deq_[idx_];
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | pointer operator->() const
|
---|
| 90 | {
|
---|
| 91 | return addressof(deq_[idx_]);
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 | deque_const_iterator& operator++()
|
---|
| 95 | {
|
---|
| 96 | ++idx_;
|
---|
| 97 |
|
---|
| 98 | return *this;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | deque_const_iterator operator++(int)
|
---|
| 102 | {
|
---|
| 103 | return deque_const_iterator{deq_, idx_++};
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | deque_const_iterator& operator--()
|
---|
| 107 | {
|
---|
| 108 | --idx_;
|
---|
| 109 |
|
---|
| 110 | return *this;
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | deque_const_iterator operator--(int)
|
---|
| 114 | {
|
---|
| 115 | return deque_const_iterator{deq_, idx_--};
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | deque_const_iterator operator+(difference_type n)
|
---|
| 119 | {
|
---|
| 120 | return deque_const_iterator{deq_, idx_ + n};
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | deque_const_iterator& operator+=(difference_type n)
|
---|
| 124 | {
|
---|
| 125 | idx_ += n;
|
---|
| 126 |
|
---|
| 127 | return *this;
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | deque_const_iterator operator-(difference_type n)
|
---|
| 131 | {
|
---|
| 132 | return deque_const_iterator{deq_, idx_ - n};
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | deque_const_iterator& operator-=(difference_type n)
|
---|
| 136 | {
|
---|
| 137 | idx_ -= n;
|
---|
| 138 |
|
---|
| 139 | return *this;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | reference operator[](difference_type n) const
|
---|
| 143 | {
|
---|
| 144 | return deq_[idx_ + n];
|
---|
| 145 | }
|
---|
| 146 |
|
---|
[5072c67] | 147 | difference_type operator-(const deque_const_iterator& rhs)
|
---|
| 148 | {
|
---|
| 149 | return idx_ - rhs.idx_;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[6e93323] | 152 | size_type idx() const
|
---|
| 153 | {
|
---|
| 154 | return idx_;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
[9019d85] | 157 | operator deque_iterator<T, Allocator>()
|
---|
| 158 | {
|
---|
[f9ce7cd] | 159 | return deque_iterator{
|
---|
| 160 | const_cast<deque<T, Allocator>&>(deq_), idx_
|
---|
| 161 | };
|
---|
[9019d85] | 162 | }
|
---|
| 163 |
|
---|
[3a06cc6] | 164 | private:
|
---|
[5072c67] | 165 | const deque<T, Allocator>& deq_;
|
---|
[3a06cc6] | 166 | size_type idx_;
|
---|
[7a7ecbd] | 167 | };
|
---|
| 168 |
|
---|
| 169 | template<class T, class Allocator>
|
---|
[6e93323] | 170 | bool operator==(const deque_const_iterator<T, Allocator>& lhs,
|
---|
| 171 | const deque_const_iterator<T, Allocator>& rhs)
|
---|
| 172 | {
|
---|
| 173 | return lhs.idx() == rhs.idx();
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | template<class T, class Allocator>
|
---|
| 177 | bool operator!=(const deque_const_iterator<T, Allocator>& lhs,
|
---|
| 178 | const deque_const_iterator<T, Allocator>& rhs)
|
---|
[7a7ecbd] | 179 | {
|
---|
[6e93323] | 180 | return !(lhs == rhs);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | template<class T, class Allocator>
|
---|
| 184 | class deque_iterator
|
---|
| 185 | {
|
---|
| 186 | public:
|
---|
| 187 | using size_type = typename deque<T, Allocator>::size_type;
|
---|
| 188 | using value_type = typename deque<T, Allocator>::value_type;
|
---|
| 189 | using reference = typename deque<T, Allocator>::reference;
|
---|
| 190 | using difference_type = size_type;
|
---|
| 191 | using pointer = value_type*;
|
---|
| 192 | using iterator_category = random_access_iterator_tag;
|
---|
| 193 |
|
---|
| 194 | deque_iterator(deque<T, Allocator>& deq, size_type idx)
|
---|
| 195 | : deq_{deq}, idx_{idx}
|
---|
| 196 | { /* DUMMY BODY */ }
|
---|
| 197 |
|
---|
[9019d85] | 198 | deque_iterator(const deque_iterator& other)
|
---|
| 199 | : deq_{other.deq_}, idx_{other.idx_}
|
---|
| 200 | { /* DUMMY BODY */ }
|
---|
| 201 |
|
---|
[6e93323] | 202 | deque_iterator(const deque_const_iterator<T, Allocator>& other)
|
---|
| 203 | : deq_{other.deq_}, idx_{other.idx_}
|
---|
| 204 | { /* DUMMY BODY */ }
|
---|
| 205 |
|
---|
| 206 | deque_iterator& operator=(const deque_iterator& other)
|
---|
| 207 | {
|
---|
| 208 | deq_ = other.deq_;
|
---|
| 209 | idx_ = other.idx_;
|
---|
| 210 |
|
---|
| 211 | return *this;
|
---|
| 212 | }
|
---|
| 213 |
|
---|
| 214 | deque_iterator& operator=(const deque_const_iterator<T, Allocator>& other)
|
---|
| 215 | {
|
---|
| 216 | deq_ = other.deq_;
|
---|
| 217 | idx_ = other.idx_;
|
---|
| 218 |
|
---|
| 219 | return *this;
|
---|
| 220 | }
|
---|
| 221 |
|
---|
| 222 | reference operator*()
|
---|
| 223 | {
|
---|
| 224 | return deq_[idx_];
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | pointer operator->()
|
---|
| 228 | {
|
---|
| 229 | return addressof(deq_[idx_]);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | deque_iterator& operator++()
|
---|
| 233 | {
|
---|
| 234 | ++idx_;
|
---|
| 235 |
|
---|
| 236 | return *this;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | deque_iterator operator++(int)
|
---|
| 240 | {
|
---|
| 241 | return deque_iterator{deq_, idx_++};
|
---|
| 242 | }
|
---|
| 243 |
|
---|
| 244 | deque_iterator& operator--()
|
---|
| 245 | {
|
---|
| 246 | --idx_;
|
---|
| 247 |
|
---|
| 248 | return *this;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | deque_iterator operator--(int)
|
---|
| 252 | {
|
---|
| 253 | return deque_iterator{deq_, idx_--};
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | deque_iterator operator+(difference_type n)
|
---|
| 257 | {
|
---|
| 258 | return deque_iterator{deq_, idx_ + n};
|
---|
| 259 | }
|
---|
| 260 |
|
---|
| 261 | deque_iterator& operator+=(difference_type n)
|
---|
| 262 | {
|
---|
| 263 | idx_ += n;
|
---|
| 264 |
|
---|
| 265 | return *this;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | deque_iterator operator-(difference_type n)
|
---|
| 269 | {
|
---|
| 270 | return deque_iterator{deq_, idx_ - n};
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | deque_iterator& operator-=(difference_type n)
|
---|
| 274 | {
|
---|
| 275 | idx_ -= n;
|
---|
| 276 |
|
---|
| 277 | return *this;
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | reference operator[](difference_type n) const
|
---|
| 281 | {
|
---|
| 282 | return deq_[idx_ + n];
|
---|
| 283 | }
|
---|
| 284 |
|
---|
[5072c67] | 285 | difference_type operator-(const deque_iterator& rhs)
|
---|
| 286 | {
|
---|
| 287 | return idx_ - rhs.idx_;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[6e93323] | 290 | size_type idx() const
|
---|
| 291 | {
|
---|
| 292 | return idx_;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
[9019d85] | 295 | operator deque_const_iterator<T, Allocator>()
|
---|
| 296 | {
|
---|
| 297 | return deque_const_iterator{deq_, idx_};
|
---|
| 298 | }
|
---|
| 299 |
|
---|
[6e93323] | 300 | private:
|
---|
| 301 | deque<T, Allocator>& deq_;
|
---|
| 302 | size_type idx_;
|
---|
[7a7ecbd] | 303 | };
|
---|
[6e93323] | 304 |
|
---|
| 305 | template<class T, class Allocator>
|
---|
| 306 | bool operator==(const deque_iterator<T, Allocator>& lhs,
|
---|
| 307 | const deque_iterator<T, Allocator>& rhs)
|
---|
| 308 | {
|
---|
| 309 | return lhs.idx() == rhs.idx();
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | template<class T, class Allocator>
|
---|
| 313 | bool operator!=(const deque_iterator<T, Allocator>& lhs,
|
---|
| 314 | const deque_iterator<T, Allocator>& rhs)
|
---|
| 315 | {
|
---|
| 316 | return !(lhs == rhs);
|
---|
| 317 | }
|
---|
[7a7ecbd] | 318 | }
|
---|
| 319 |
|
---|
| 320 | /**
|
---|
| 321 | * 23.3.3 deque:
|
---|
| 322 | */
|
---|
| 323 |
|
---|
[3a06cc6] | 324 | template<class T, class Allocator>
|
---|
[7a7ecbd] | 325 | class deque
|
---|
| 326 | {
|
---|
| 327 | public:
|
---|
| 328 | using allocator_type = Allocator;
|
---|
| 329 | using value_type = T;
|
---|
| 330 | using reference = value_type&;
|
---|
| 331 | using const_reference = const value_type&;
|
---|
| 332 |
|
---|
| 333 | using iterator = aux::deque_iterator<T, Allocator>;
|
---|
| 334 | using const_iterator = aux::deque_const_iterator<T, Allocator>;
|
---|
| 335 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
| 336 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
| 337 |
|
---|
| 338 | using size_type = typename allocator_traits<allocator_type>::size_type;
|
---|
| 339 | using difference_type = typename allocator_traits<allocator_type>::difference_type;
|
---|
| 340 | using pointer = typename allocator_traits<allocator_type>::pointer;
|
---|
| 341 | using const_pointer = typename allocator_traits<allocator_type>::const_pointer;
|
---|
| 342 |
|
---|
| 343 | /**
|
---|
| 344 | * 23.3.3.2, construct/copy/destroy:
|
---|
| 345 | */
|
---|
| 346 |
|
---|
| 347 | deque()
|
---|
| 348 | : deque{allocator_type{}}
|
---|
| 349 | { /* DUMMY BODY */ }
|
---|
| 350 |
|
---|
| 351 | explicit deque(const allocator_type& alloc)
|
---|
[3a06cc6] | 352 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
| 353 | back_bucket_idx_{0}, front_bucket_{default_front_},
|
---|
| 354 | back_bucket_{default_back_}, bucket_count_{default_bucket_count_},
|
---|
| 355 | bucket_capacity_{default_bucket_capacity_}, size_{}, data_{}
|
---|
[7a7ecbd] | 356 | {
|
---|
[3a06cc6] | 357 | init_();
|
---|
[7a7ecbd] | 358 | }
|
---|
| 359 |
|
---|
| 360 | explicit deque(size_type n, const allocator_type& alloc = allocator_type{})
|
---|
[5072c67] | 361 | : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
|
---|
| 362 | front_bucket_{}, back_bucket_{}, bucket_count_{},
|
---|
| 363 | bucket_capacity_{}, size_{n}, data_{}
|
---|
[7a7ecbd] | 364 | {
|
---|
[5072c67] | 365 | prepare_for_size_(n);
|
---|
| 366 | init_();
|
---|
| 367 |
|
---|
| 368 | for (size_type i = 0; i < size_; ++i)
|
---|
| 369 | allocator_.construct(&(*this)[i]);
|
---|
| 370 | back_bucket_idx_ = size_ % bucket_size_;
|
---|
[7a7ecbd] | 371 | }
|
---|
| 372 |
|
---|
| 373 | deque(size_type n, const value_type& value, const allocator_type& alloc = allocator_type{})
|
---|
[5072c67] | 374 | : allocator_{alloc}, front_bucket_idx_{bucket_size_}, back_bucket_idx_{},
|
---|
| 375 | front_bucket_{}, back_bucket_{}, bucket_count_{},
|
---|
| 376 | bucket_capacity_{}, size_{n}, data_{}
|
---|
[7a7ecbd] | 377 | {
|
---|
[5072c67] | 378 | prepare_for_size_(n);
|
---|
| 379 | init_();
|
---|
| 380 |
|
---|
| 381 | for (size_type i = 0; i < size_; ++i)
|
---|
| 382 | (*this)[i] = value;
|
---|
| 383 | back_bucket_idx_ = size_ % bucket_size_;
|
---|
[7a7ecbd] | 384 | }
|
---|
| 385 |
|
---|
| 386 | template<class InputIterator>
|
---|
[5072c67] | 387 | deque(InputIterator first, InputIterator last,
|
---|
| 388 | const allocator_type& alloc = allocator_type{})
|
---|
| 389 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
| 390 | back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
|
---|
| 391 | bucket_count_{}, bucket_capacity_{}, size_{},
|
---|
| 392 | data_{}
|
---|
[7a7ecbd] | 393 | {
|
---|
[5072c67] | 394 | copy_from_range_(first, last);
|
---|
[7a7ecbd] | 395 | }
|
---|
| 396 |
|
---|
| 397 | deque(const deque& other)
|
---|
[5072c67] | 398 | : deque{other.begin(), other.end(), other.allocator_}
|
---|
| 399 | { /* DUMMY BODY */ }
|
---|
[7a7ecbd] | 400 |
|
---|
| 401 | deque(deque&& other)
|
---|
[5072c67] | 402 | : allocator_{move(other.allocator_)},
|
---|
| 403 | front_bucket_idx_{other.front_bucket_idx_},
|
---|
| 404 | back_bucket_idx_{other.front_bucket_idx_},
|
---|
| 405 | front_bucket_{other.front_bucket_},
|
---|
| 406 | back_bucket_{other.back_bucket_},
|
---|
| 407 | bucket_count_{other.bucket_count_},
|
---|
| 408 | bucket_capacity_{other.bucket_capacity_},
|
---|
| 409 | size_{other.size_}, data_{other.data_}
|
---|
[7a7ecbd] | 410 | {
|
---|
[5072c67] | 411 | other.data_ = nullptr;
|
---|
| 412 | other.clear();
|
---|
[7a7ecbd] | 413 | }
|
---|
| 414 |
|
---|
| 415 | deque(const deque& other, const allocator_type& alloc)
|
---|
[5072c67] | 416 | : deque{other.begin(), other.end(), alloc}
|
---|
| 417 | { /* DUMMY BODY */ }
|
---|
[7a7ecbd] | 418 |
|
---|
| 419 | deque(deque&& other, const allocator_type& alloc)
|
---|
[5072c67] | 420 | : allocator_{alloc},
|
---|
| 421 | front_bucket_idx_{other.front_bucket_idx_},
|
---|
| 422 | back_bucket_idx_{other.front_bucket_idx_},
|
---|
| 423 | front_bucket_{other.front_bucket_},
|
---|
| 424 | back_bucket_{other.back_bucket_},
|
---|
| 425 | bucket_count_{other.bucket_count_},
|
---|
| 426 | bucket_capacity_{other.bucket_capacity_},
|
---|
| 427 | size_{other.size_}, data_{other.data_}
|
---|
[7a7ecbd] | 428 | {
|
---|
[5072c67] | 429 | other.data_ = nullptr;
|
---|
| 430 | other.clear();
|
---|
[7a7ecbd] | 431 | }
|
---|
| 432 |
|
---|
| 433 | deque(initializer_list<T> init, const allocator_type& alloc = allocator_type{})
|
---|
[5072c67] | 434 | : allocator_{alloc}, front_bucket_idx_{bucket_size_},
|
---|
| 435 | back_bucket_idx_{}, front_bucket_{}, back_bucket_{},
|
---|
| 436 | bucket_count_{}, bucket_capacity_{}, size_{},
|
---|
| 437 | data_{}
|
---|
[7a7ecbd] | 438 | {
|
---|
[5072c67] | 439 | copy_from_range_(init.begin(), init.end());
|
---|
[7a7ecbd] | 440 | }
|
---|
| 441 |
|
---|
| 442 | ~deque()
|
---|
| 443 | {
|
---|
[3a06cc6] | 444 | fini_();
|
---|
[7a7ecbd] | 445 | }
|
---|
| 446 |
|
---|
| 447 | deque& operator=(const deque& other)
|
---|
| 448 | {
|
---|
[0f158be5] | 449 | if (data_)
|
---|
| 450 | fini_();
|
---|
| 451 |
|
---|
| 452 | copy_from_range_(other.begin(), other.end());
|
---|
| 453 |
|
---|
| 454 | return *this;
|
---|
[7a7ecbd] | 455 | }
|
---|
| 456 |
|
---|
| 457 | deque& operator=(deque&& other)
|
---|
| 458 | noexcept(allocator_traits<allocator_type>::is_always_equal::value)
|
---|
| 459 | {
|
---|
[5072c67] | 460 | swap(other);
|
---|
| 461 | other.clear();
|
---|
[7a7ecbd] | 462 | }
|
---|
| 463 |
|
---|
| 464 | deque& operator=(initializer_list<T> init)
|
---|
| 465 | {
|
---|
[0f158be5] | 466 | if (data_)
|
---|
| 467 | fini_();
|
---|
| 468 |
|
---|
| 469 | copy_from_range_(init.begin(), init.end());
|
---|
| 470 |
|
---|
| 471 | return *this;
|
---|
[7a7ecbd] | 472 | }
|
---|
| 473 |
|
---|
| 474 | template<class InputIterator>
|
---|
| 475 | void assign(InputIterator first, InputIterator last)
|
---|
| 476 | {
|
---|
[db05684] | 477 | copy_from_range_(first, last);
|
---|
[7a7ecbd] | 478 | }
|
---|
| 479 |
|
---|
| 480 | void assign(size_type n, const T& value)
|
---|
| 481 | {
|
---|
[db05684] | 482 | prepare_for_size_(n);
|
---|
| 483 | init_();
|
---|
| 484 | size_ = n;
|
---|
| 485 |
|
---|
| 486 | auto it = begin();
|
---|
| 487 | for (size_type i = size_type{}; i < n; ++i)
|
---|
| 488 | *it++ = value;
|
---|
[7a7ecbd] | 489 | }
|
---|
| 490 |
|
---|
| 491 | void assign(initializer_list<T> init)
|
---|
| 492 | {
|
---|
[db05684] | 493 | copy_from_range_(init.begin(), init.end());
|
---|
[7a7ecbd] | 494 | }
|
---|
| 495 |
|
---|
| 496 | allocator_type get_allocator() const noexcept
|
---|
| 497 | {
|
---|
| 498 | return allocator_;
|
---|
| 499 | }
|
---|
| 500 |
|
---|
| 501 | iterator begin() noexcept
|
---|
| 502 | {
|
---|
[6e93323] | 503 | return aux::deque_iterator{*this, 0};
|
---|
[7a7ecbd] | 504 | }
|
---|
| 505 |
|
---|
| 506 | const_iterator begin() const noexcept
|
---|
| 507 | {
|
---|
[6e93323] | 508 | return aux::deque_const_iterator{*this, 0};
|
---|
[7a7ecbd] | 509 | }
|
---|
| 510 |
|
---|
| 511 | iterator end() noexcept
|
---|
| 512 | {
|
---|
[6e93323] | 513 | return aux::deque_iterator{*this, size_};
|
---|
[7a7ecbd] | 514 | }
|
---|
| 515 |
|
---|
| 516 | const_iterator end() const noexcept
|
---|
| 517 | {
|
---|
[6e93323] | 518 | return aux::deque_const_iterator{*this, size_};
|
---|
[7a7ecbd] | 519 | }
|
---|
| 520 |
|
---|
| 521 | reverse_iterator rbegin() noexcept
|
---|
| 522 | {
|
---|
[6e93323] | 523 | return make_reverse_iterator(end());
|
---|
[7a7ecbd] | 524 | }
|
---|
| 525 |
|
---|
| 526 | const_reverse_iterator rbegin() const noexcept
|
---|
| 527 | {
|
---|
[6e93323] | 528 | return make_reverse_iterator(cend());
|
---|
[7a7ecbd] | 529 | }
|
---|
| 530 |
|
---|
| 531 | reverse_iterator rend() noexcept
|
---|
| 532 | {
|
---|
[6e93323] | 533 | return make_reverse_iterator(begin());
|
---|
[7a7ecbd] | 534 | }
|
---|
| 535 |
|
---|
| 536 | const_reverse_iterator rend() const noexcept
|
---|
| 537 | {
|
---|
[6e93323] | 538 | return make_reverse_iterator(cbegin());
|
---|
[7a7ecbd] | 539 | }
|
---|
| 540 |
|
---|
| 541 | const_iterator cbegin() const noexcept
|
---|
| 542 | {
|
---|
[6e93323] | 543 | return aux::deque_const_iterator{*this, 0};
|
---|
[7a7ecbd] | 544 | }
|
---|
| 545 |
|
---|
| 546 | const_iterator cend() const noexcept
|
---|
| 547 | {
|
---|
[6e93323] | 548 | return aux::deque_const_iterator{*this, size_};
|
---|
[7a7ecbd] | 549 | }
|
---|
| 550 |
|
---|
| 551 | const_reverse_iterator crbegin() const noexcept
|
---|
| 552 | {
|
---|
[6e93323] | 553 | return make_reverse_iterator(cend());
|
---|
[7a7ecbd] | 554 | }
|
---|
| 555 |
|
---|
| 556 | const_reverse_iterator crend() const noexcept
|
---|
| 557 | {
|
---|
[6e93323] | 558 | return make_reverse_iterator(cbegin());
|
---|
[7a7ecbd] | 559 | }
|
---|
| 560 |
|
---|
| 561 | /**
|
---|
| 562 | * 23.3.3.3, capacity:
|
---|
| 563 | */
|
---|
| 564 |
|
---|
| 565 | size_type size() const noexcept
|
---|
| 566 | {
|
---|
[3a06cc6] | 567 | return size_;
|
---|
[7a7ecbd] | 568 | }
|
---|
| 569 |
|
---|
| 570 | size_type max_size() const noexcept
|
---|
| 571 | {
|
---|
[db05684] | 572 | return allocator_traits<allocator_type>::max_size(allocator_);
|
---|
[7a7ecbd] | 573 | }
|
---|
| 574 |
|
---|
| 575 | void resize(size_type sz)
|
---|
| 576 | {
|
---|
[289c954a] | 577 | if (sz <= size_)
|
---|
| 578 | {
|
---|
| 579 | for (size_type i = 0; i < size_ - sz; ++i)
|
---|
| 580 | pop_back();
|
---|
| 581 | }
|
---|
| 582 | else
|
---|
| 583 | {
|
---|
| 584 | value_type value{};
|
---|
| 585 | for (size_type i = 0; i < sz - size_; ++i)
|
---|
| 586 | push_back(value);
|
---|
| 587 | }
|
---|
[7a7ecbd] | 588 | }
|
---|
| 589 |
|
---|
| 590 | void resize(size_type sz, const value_type& value)
|
---|
| 591 | {
|
---|
[289c954a] | 592 | if (sz <= size_)
|
---|
| 593 | {
|
---|
| 594 | for (size_type i = 0; i < size_ - sz; ++i)
|
---|
| 595 | pop_back();
|
---|
| 596 | }
|
---|
| 597 | else
|
---|
| 598 | {
|
---|
| 599 | for (size_type i = 0; i < sz - size_; ++i)
|
---|
| 600 | push_back(value);
|
---|
| 601 | }
|
---|
[7a7ecbd] | 602 | }
|
---|
| 603 |
|
---|
| 604 | void shrink_to_fit()
|
---|
| 605 | {
|
---|
[289c954a] | 606 | /**
|
---|
| 607 | * We lazily allocate buckets and eagerily deallocate them.
|
---|
| 608 | * We cannot go into smaller pieces as buckets have fixed size.
|
---|
| 609 | * Because of this, shrink_to_fit has no effect (as permitted
|
---|
| 610 | * by the standard).
|
---|
| 611 | */
|
---|
[7a7ecbd] | 612 | }
|
---|
| 613 |
|
---|
| 614 | bool empty() const noexcept
|
---|
| 615 | {
|
---|
[3a06cc6] | 616 | return size_ == size_type{};
|
---|
[7a7ecbd] | 617 | }
|
---|
| 618 |
|
---|
| 619 | reference operator[](size_type idx)
|
---|
| 620 | {
|
---|
[3a06cc6] | 621 | return data_[get_bucket_index_(idx)][get_element_index_(idx)];
|
---|
[7a7ecbd] | 622 | }
|
---|
| 623 |
|
---|
[3a06cc6] | 624 | const_reference operator[](size_type idx) const
|
---|
[7a7ecbd] | 625 | {
|
---|
[3a06cc6] | 626 | return data_[get_bucket_index_(idx)][get_element_index_(idx)];
|
---|
[7a7ecbd] | 627 | }
|
---|
| 628 |
|
---|
| 629 | reference at(size_type idx)
|
---|
| 630 | {
|
---|
[5072c67] | 631 | // TODO: bounds checking
|
---|
| 632 | return operator[](idx);
|
---|
[7a7ecbd] | 633 | }
|
---|
| 634 |
|
---|
[3a06cc6] | 635 | const_reference at(size_type idx) const
|
---|
[7a7ecbd] | 636 | {
|
---|
[5072c67] | 637 | // TODO: bounds checking
|
---|
| 638 | return operator[](idx);
|
---|
[7a7ecbd] | 639 | }
|
---|
| 640 |
|
---|
| 641 | reference front()
|
---|
| 642 | {
|
---|
[5072c67] | 643 | return *begin();
|
---|
[7a7ecbd] | 644 | }
|
---|
| 645 |
|
---|
| 646 | const_reference front() const
|
---|
| 647 | {
|
---|
[5072c67] | 648 | return *cbegin();
|
---|
[7a7ecbd] | 649 | }
|
---|
| 650 |
|
---|
| 651 | reference back()
|
---|
| 652 | {
|
---|
[5072c67] | 653 | auto tmp = end();
|
---|
| 654 |
|
---|
| 655 | return *(--tmp);
|
---|
[7a7ecbd] | 656 | }
|
---|
| 657 |
|
---|
| 658 | const_reference back() const
|
---|
| 659 | {
|
---|
[5072c67] | 660 | auto tmp = cend();
|
---|
| 661 |
|
---|
| 662 | return *(--tmp);
|
---|
[7a7ecbd] | 663 | }
|
---|
| 664 |
|
---|
| 665 | /**
|
---|
| 666 | * 23.3.3.4, modifiers:
|
---|
| 667 | */
|
---|
| 668 |
|
---|
| 669 | template<class... Args>
|
---|
| 670 | void emplace_front(Args&&... args)
|
---|
| 671 | {
|
---|
[db05684] | 672 | if (front_bucket_idx_ == 0)
|
---|
| 673 | add_new_bucket_front_();
|
---|
| 674 |
|
---|
| 675 | allocator_traits<allocator_type>::construct(
|
---|
| 676 | allocator_,
|
---|
| 677 | &data_[front_bucket_][--front_bucket_idx_],
|
---|
| 678 | forward<Args>(args)...
|
---|
| 679 | );
|
---|
| 680 |
|
---|
| 681 | ++size_;
|
---|
[7a7ecbd] | 682 | }
|
---|
| 683 |
|
---|
| 684 | template<class... Args>
|
---|
| 685 | void emplace_back(Args&&... args)
|
---|
| 686 | {
|
---|
[db05684] | 687 | allocator_traits<allocator_type>::construct(
|
---|
| 688 | allocator_,
|
---|
| 689 | &data_[back_bucket_][back_bucket_idx_++],
|
---|
| 690 | forward<Args>(args)...
|
---|
| 691 | );
|
---|
| 692 |
|
---|
| 693 | ++size_;
|
---|
| 694 |
|
---|
| 695 | if (back_bucket_idx_ >= bucket_size_)
|
---|
| 696 | add_new_bucket_back_();
|
---|
[7a7ecbd] | 697 | }
|
---|
| 698 |
|
---|
| 699 | template<class... Args>
|
---|
| 700 | iterator emplace(const_iterator position, Args&&... args)
|
---|
| 701 | {
|
---|
[9019d85] | 702 | auto idx = position.idx();
|
---|
| 703 | shift_right_(idx, 1);
|
---|
| 704 |
|
---|
| 705 | allocator_traits<allocator_type>::construct(
|
---|
| 706 | allocator_,
|
---|
| 707 | &data_[get_bucket_index_(idx)][get_element_index_(idx)],
|
---|
| 708 | forward<Args>(args)...
|
---|
| 709 | );
|
---|
| 710 | ++size_;
|
---|
| 711 |
|
---|
| 712 | return iterator{*this, idx};
|
---|
[7a7ecbd] | 713 | }
|
---|
| 714 |
|
---|
| 715 | void push_front(const value_type& value)
|
---|
| 716 | {
|
---|
[3a06cc6] | 717 | if (front_bucket_idx_ == 0)
|
---|
| 718 | add_new_bucket_front_();
|
---|
| 719 |
|
---|
| 720 | data_[front_bucket_][--front_bucket_idx_] = value;
|
---|
| 721 | ++size_;
|
---|
[7a7ecbd] | 722 | }
|
---|
| 723 |
|
---|
| 724 | void push_front(value_type&& value)
|
---|
| 725 | {
|
---|
[3a06cc6] | 726 | if (front_bucket_idx_ == 0)
|
---|
| 727 | add_new_bucket_front_();
|
---|
| 728 |
|
---|
| 729 | data_[front_bucket_][--front_bucket_idx_] = forward<value_type>(value);
|
---|
| 730 | ++size_;
|
---|
[7a7ecbd] | 731 | }
|
---|
| 732 |
|
---|
| 733 | void push_back(const value_type& value)
|
---|
| 734 | {
|
---|
[3a06cc6] | 735 | data_[back_bucket_][back_bucket_idx_++] = value;
|
---|
| 736 | ++size_;
|
---|
| 737 |
|
---|
| 738 | if (back_bucket_idx_ >= bucket_size_)
|
---|
| 739 | add_new_bucket_back_();
|
---|
[7a7ecbd] | 740 | }
|
---|
| 741 |
|
---|
| 742 | void push_back(value_type&& value)
|
---|
| 743 | {
|
---|
[3a06cc6] | 744 | data_[back_bucket_][back_bucket_idx_++] = forward<value_type>(value);
|
---|
| 745 | ++size_;
|
---|
| 746 |
|
---|
| 747 | if (back_bucket_idx_ >= bucket_size_)
|
---|
| 748 | add_new_bucket_back_();
|
---|
[7a7ecbd] | 749 | }
|
---|
| 750 |
|
---|
| 751 | iterator insert(const_iterator position, const value_type& value)
|
---|
| 752 | {
|
---|
[9019d85] | 753 | auto idx = position.idx();
|
---|
| 754 | shift_right_(idx, 1);
|
---|
| 755 |
|
---|
| 756 | /**
|
---|
| 757 | * Note: One may notice that when working with the deque
|
---|
| 758 | * iterator, we use its index without any checks.
|
---|
| 759 | * This is because a valid iterator will always have
|
---|
| 760 | * a valid index as functions like pop_back or erase
|
---|
| 761 | * invalidate iterators.
|
---|
| 762 | */
|
---|
| 763 | data_[get_bucket_index_(idx)][get_element_index_(idx)] = value;
|
---|
| 764 | ++size_;
|
---|
| 765 |
|
---|
| 766 | return iterator{*this, idx};
|
---|
[7a7ecbd] | 767 | }
|
---|
| 768 |
|
---|
| 769 | iterator insert(const_iterator position, value_type&& value)
|
---|
| 770 | {
|
---|
[9019d85] | 771 | auto idx = position.idx();
|
---|
| 772 | shift_right_(idx, 1);
|
---|
| 773 |
|
---|
| 774 | data_[get_bucket_index_(idx)][get_element_index_(idx)] = forward<value_type>(value);
|
---|
| 775 | ++size_;
|
---|
| 776 |
|
---|
| 777 | return iterator{*this, idx};
|
---|
[7a7ecbd] | 778 | }
|
---|
| 779 |
|
---|
| 780 | iterator insert(const_iterator position, size_type n, const value_type& value)
|
---|
| 781 | {
|
---|
[9019d85] | 782 | return insert(
|
---|
| 783 | position,
|
---|
[e5cf551] | 784 | aux::insert_iterator{0u, value},
|
---|
[9019d85] | 785 | aux::insert_iterator{n}
|
---|
| 786 | );
|
---|
[7a7ecbd] | 787 | }
|
---|
| 788 |
|
---|
| 789 | template<class InputIterator>
|
---|
| 790 | iterator insert(const_iterator position, InputIterator first, InputIterator last)
|
---|
| 791 | {
|
---|
[9019d85] | 792 | auto idx = position.idx();
|
---|
| 793 | auto count = distance(first, last);
|
---|
| 794 |
|
---|
| 795 | if (idx < size_ / 2)
|
---|
| 796 | {
|
---|
| 797 | shift_left_(idx, count);
|
---|
| 798 |
|
---|
| 799 | iterator it{*this, idx - 1};
|
---|
| 800 | while (first != last)
|
---|
| 801 | *it++ = *first++;
|
---|
| 802 | }
|
---|
| 803 | else
|
---|
| 804 | {
|
---|
| 805 | shift_right_(idx, count);
|
---|
| 806 |
|
---|
| 807 | iterator it{*this, idx};
|
---|
| 808 | while (first != last)
|
---|
| 809 | *it++ = *first++;
|
---|
| 810 | }
|
---|
| 811 |
|
---|
| 812 | size_ += count;
|
---|
| 813 |
|
---|
| 814 | return iterator{*this, idx};
|
---|
[7a7ecbd] | 815 | }
|
---|
| 816 |
|
---|
| 817 | iterator insert(const_iterator position, initializer_list<value_type> init)
|
---|
| 818 | {
|
---|
[9019d85] | 819 | return insert(position, init.begin(), init.end());
|
---|
[7a7ecbd] | 820 | }
|
---|
| 821 |
|
---|
| 822 | void pop_back()
|
---|
| 823 | {
|
---|
[3a06cc6] | 824 | if (empty())
|
---|
| 825 | return;
|
---|
| 826 |
|
---|
| 827 | if (back_bucket_idx_ == 0)
|
---|
[9019d85] | 828 | { // Means we gotta pop data_[back_bucket_ - 1][bucket_size_ - 1]!
|
---|
[3a06cc6] | 829 | if (data_[back_bucket_])
|
---|
| 830 | allocator_.deallocate(data_[back_bucket_], bucket_size_);
|
---|
| 831 |
|
---|
| 832 | --back_bucket_;
|
---|
| 833 | back_bucket_idx_ = bucket_size_ - 1;
|
---|
| 834 | }
|
---|
| 835 | else
|
---|
| 836 | --back_bucket_idx_;
|
---|
| 837 |
|
---|
| 838 | allocator_.destroy(&data_[back_bucket_][back_bucket_idx_]);
|
---|
| 839 | --size_;
|
---|
[7a7ecbd] | 840 | }
|
---|
| 841 |
|
---|
| 842 | void pop_front()
|
---|
| 843 | {
|
---|
[3a06cc6] | 844 | if (empty())
|
---|
| 845 | return;
|
---|
| 846 |
|
---|
| 847 | if (front_bucket_idx_ >= bucket_size_)
|
---|
[9019d85] | 848 | { // Means we gotta pop data_[front_bucket_ + 1][0]!
|
---|
[3a06cc6] | 849 | if (data_[front_bucket_])
|
---|
| 850 | allocator_.deallocate(data_[front_bucket_], bucket_size_);
|
---|
| 851 |
|
---|
| 852 | ++front_bucket_;
|
---|
| 853 | front_bucket_idx_ = 1;
|
---|
| 854 |
|
---|
| 855 | allocator_.destroy(&data_[front_bucket_][0]);
|
---|
| 856 | }
|
---|
| 857 | else
|
---|
| 858 | {
|
---|
| 859 | allocator_.destroy(&data_[front_bucket_][front_bucket_idx_]);
|
---|
| 860 |
|
---|
| 861 | ++front_bucket_idx_;
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | --size_;
|
---|
[7a7ecbd] | 865 | }
|
---|
| 866 |
|
---|
| 867 | iterator erase(const_iterator position)
|
---|
| 868 | {
|
---|
[f9ce7cd] | 869 | auto idx = position.idx();
|
---|
| 870 | copy(
|
---|
| 871 | iterator{*this, idx + 1},
|
---|
| 872 | end(),
|
---|
| 873 | iterator{*this, idx}
|
---|
| 874 | );
|
---|
| 875 |
|
---|
| 876 | /**
|
---|
| 877 | * Note: We need to not only decrement size,
|
---|
| 878 | * but also take care of any issues caused
|
---|
| 879 | * by decrement bucket indices, which pop_back
|
---|
| 880 | * does for us.
|
---|
| 881 | */
|
---|
| 882 | pop_back();
|
---|
| 883 |
|
---|
| 884 | return iterator{*this, idx};
|
---|
[7a7ecbd] | 885 | }
|
---|
| 886 |
|
---|
[3a06cc6] | 887 | iterator erase(const_iterator first, const_iterator last)
|
---|
[7a7ecbd] | 888 | {
|
---|
[f9ce7cd] | 889 | if (first == last)
|
---|
| 890 | return first;
|
---|
| 891 |
|
---|
| 892 | auto first_idx = first.idx();
|
---|
| 893 | auto last_idx = last.idx();
|
---|
| 894 | auto count = distance(first, last);
|
---|
| 895 |
|
---|
| 896 | copy(
|
---|
| 897 | iterator{*this, last_idx},
|
---|
| 898 | end(),
|
---|
| 899 | iterator{*this, first_idx}
|
---|
| 900 | );
|
---|
| 901 |
|
---|
| 902 | for (size_type i = 0; i < count; ++i)
|
---|
| 903 | pop_back();
|
---|
| 904 |
|
---|
| 905 | return iterator{*this, first_idx};
|
---|
[7a7ecbd] | 906 | }
|
---|
| 907 |
|
---|
| 908 | void swap(deque& other)
|
---|
| 909 | noexcept(allocator_traits<allocator_type>::is_always_equal::value)
|
---|
| 910 | {
|
---|
[f97ccd1] | 911 | std::swap(allocator_, other.allocator_);
|
---|
| 912 | std::swap(front_bucket_idx_, other.front_bucket_idx_);
|
---|
| 913 | std::swap(back_bucket_idx_, other.back_bucket_idx_);
|
---|
| 914 | std::swap(front_bucket_, other.front_bucket_);
|
---|
| 915 | std::swap(back_bucket_, other.back_bucket_);
|
---|
| 916 | std::swap(bucket_count_, other.bucket_count_);
|
---|
| 917 | std::swap(bucket_capacity_, other.bucket_capacity_);
|
---|
| 918 | std::swap(size_, other.size_);
|
---|
| 919 | std::swap(data_, other.data_);
|
---|
[7a7ecbd] | 920 | }
|
---|
| 921 |
|
---|
| 922 | void clear() noexcept
|
---|
| 923 | {
|
---|
[5072c67] | 924 | if (data_)
|
---|
| 925 | fini_();
|
---|
[3a06cc6] | 926 |
|
---|
| 927 | front_bucket_ = default_front_;
|
---|
| 928 | back_bucket_ = default_back_;
|
---|
| 929 | bucket_count_ = default_bucket_count_;
|
---|
| 930 | bucket_capacity_ = default_bucket_capacity_;
|
---|
| 931 | size_ = size_type{};
|
---|
| 932 |
|
---|
| 933 | init_();
|
---|
[7a7ecbd] | 934 | }
|
---|
| 935 |
|
---|
[35b706e8] | 936 | private:
|
---|
[7a7ecbd] | 937 | allocator_type allocator_;
|
---|
| 938 |
|
---|
[3a06cc6] | 939 | /**
|
---|
| 940 | * Note: In our implementation, front_bucket_idx_
|
---|
| 941 | * points at the first element and back_bucket_idx_
|
---|
| 942 | * points at the one past last element. Because of this,
|
---|
| 943 | * some operations may be done in inverse order
|
---|
| 944 | * depending on the side they are applied to.
|
---|
| 945 | */
|
---|
[7a7ecbd] | 946 | size_type front_bucket_idx_;
|
---|
| 947 | size_type back_bucket_idx_;
|
---|
[3a06cc6] | 948 | size_type front_bucket_;
|
---|
| 949 | size_type back_bucket_;
|
---|
[7a7ecbd] | 950 |
|
---|
| 951 | static constexpr size_type bucket_size_{16};
|
---|
[3a06cc6] | 952 | static constexpr size_type default_bucket_count_{2};
|
---|
| 953 | static constexpr size_type default_bucket_capacity_{4};
|
---|
| 954 | static constexpr size_type default_front_{1};
|
---|
| 955 | static constexpr size_type default_back_{2};
|
---|
[7a7ecbd] | 956 |
|
---|
| 957 | size_type bucket_count_;
|
---|
| 958 | size_type bucket_capacity_;
|
---|
[3a06cc6] | 959 | size_type size_{};
|
---|
[7a7ecbd] | 960 |
|
---|
| 961 | value_type** data_;
|
---|
[3a06cc6] | 962 |
|
---|
| 963 | void init_()
|
---|
| 964 | {
|
---|
| 965 | data_ = new value_type*[bucket_capacity_];
|
---|
| 966 |
|
---|
| 967 | for (size_type i = front_bucket_; i <= back_bucket_; ++i)
|
---|
| 968 | data_[i] = allocator_.allocate(bucket_size_);
|
---|
| 969 | }
|
---|
| 970 |
|
---|
[5072c67] | 971 | void prepare_for_size_(size_type size)
|
---|
| 972 | {
|
---|
| 973 | if (data_)
|
---|
| 974 | fini_();
|
---|
| 975 |
|
---|
[9019d85] | 976 | bucket_count_ = bucket_capacity_ = size / bucket_size_ + 2;
|
---|
[5072c67] | 977 |
|
---|
| 978 | front_bucket_ = 0;
|
---|
| 979 | back_bucket_ = bucket_capacity_ - 1;
|
---|
[db05684] | 980 |
|
---|
| 981 | front_bucket_idx_ = bucket_size_;
|
---|
| 982 | back_bucket_idx_ = size % bucket_size_;
|
---|
[5072c67] | 983 | }
|
---|
| 984 |
|
---|
| 985 | template<class Iterator>
|
---|
| 986 | void copy_from_range_(Iterator first, Iterator last)
|
---|
| 987 | {
|
---|
| 988 | size_ = distance(first, last);
|
---|
| 989 | prepare_for_size_(size_);
|
---|
| 990 | init_();
|
---|
| 991 |
|
---|
| 992 | auto it = begin();
|
---|
| 993 | while (first != last)
|
---|
| 994 | *it++ = *first++;
|
---|
| 995 | }
|
---|
| 996 |
|
---|
[9019d85] | 997 | void ensure_space_front_(size_type idx, size_type count)
|
---|
| 998 | {
|
---|
| 999 | auto free_space = bucket_size_ - elements_in_front_bucket_();
|
---|
| 1000 | if (front_bucket_idx_ == 0)
|
---|
| 1001 | free_space = 0;
|
---|
| 1002 |
|
---|
| 1003 | if (count <= free_space)
|
---|
| 1004 | {
|
---|
| 1005 | front_bucket_idx_ -= count;
|
---|
| 1006 | return;
|
---|
| 1007 | }
|
---|
| 1008 |
|
---|
| 1009 | count -= free_space;
|
---|
| 1010 |
|
---|
| 1011 | auto buckets_needed = count / bucket_size_;
|
---|
| 1012 | if (count % bucket_size_ != 0)
|
---|
| 1013 | ++buckets_needed;
|
---|
| 1014 |
|
---|
| 1015 | for (size_type i = size_type{}; i < buckets_needed; ++i)
|
---|
| 1016 | add_new_bucket_front_();
|
---|
| 1017 |
|
---|
| 1018 | front_bucket_idx_ = bucket_size_ - (count % bucket_size_);
|
---|
| 1019 | }
|
---|
| 1020 |
|
---|
| 1021 | void ensure_space_back_(size_type idx, size_type count)
|
---|
| 1022 | {
|
---|
| 1023 | auto free_space = bucket_size_ - back_bucket_idx_;
|
---|
| 1024 | if (count < free_space)
|
---|
| 1025 | return;
|
---|
| 1026 |
|
---|
| 1027 | count -= free_space;
|
---|
| 1028 | auto buckets_needed = count / bucket_size_;
|
---|
| 1029 | if (count % bucket_size_ != 0)
|
---|
| 1030 | ++buckets_needed;
|
---|
| 1031 |
|
---|
| 1032 | for (size_type i = size_type{}; i < buckets_needed; ++i)
|
---|
| 1033 | add_new_bucket_back_();
|
---|
| 1034 |
|
---|
| 1035 | back_bucket_idx_ = (back_bucket_idx_ + count) % bucket_size_;
|
---|
| 1036 | }
|
---|
| 1037 |
|
---|
| 1038 | void shift_right_(size_type idx, size_type count)
|
---|
| 1039 | {
|
---|
| 1040 | ensure_space_back_(idx, count);
|
---|
| 1041 |
|
---|
| 1042 | iterator it{*this, idx};
|
---|
| 1043 | copy_backward(it, end(), end() + count - 1);
|
---|
| 1044 |
|
---|
| 1045 | }
|
---|
| 1046 |
|
---|
| 1047 | void shift_left_(size_type idx, size_type count)
|
---|
| 1048 | {
|
---|
| 1049 | ensure_space_front_(idx, count);
|
---|
| 1050 |
|
---|
| 1051 | copy(
|
---|
| 1052 | iterator{*this, count},
|
---|
| 1053 | iterator{*this, idx + count - 1},
|
---|
| 1054 | iterator{*this, 0}
|
---|
| 1055 | );
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
[3a06cc6] | 1058 | void fini_()
|
---|
| 1059 | {
|
---|
| 1060 | for (size_type i = front_bucket_; i <= back_bucket_; ++i)
|
---|
| 1061 | allocator_.deallocate(data_[i], bucket_size_);
|
---|
| 1062 |
|
---|
| 1063 | delete[] data_;
|
---|
| 1064 | data_ = nullptr;
|
---|
| 1065 | }
|
---|
| 1066 |
|
---|
| 1067 | bool has_bucket_space_back_() const
|
---|
| 1068 | {
|
---|
| 1069 | return back_bucket_ < bucket_capacity_ - 1;
|
---|
| 1070 | }
|
---|
| 1071 |
|
---|
| 1072 | bool has_bucket_space_front_() const
|
---|
| 1073 | {
|
---|
| 1074 | return front_bucket_ > 0;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | void add_new_bucket_back_()
|
---|
| 1078 | {
|
---|
| 1079 | if (!has_bucket_space_back_())
|
---|
| 1080 | expand_();
|
---|
| 1081 |
|
---|
| 1082 | ++back_bucket_;
|
---|
[9019d85] | 1083 | ++bucket_count_;
|
---|
[3a06cc6] | 1084 | data_[back_bucket_] = allocator_.allocate(bucket_size_);
|
---|
| 1085 | back_bucket_idx_ = size_type{};
|
---|
| 1086 | }
|
---|
| 1087 |
|
---|
| 1088 | void add_new_bucket_front_()
|
---|
| 1089 | {
|
---|
| 1090 | if (!has_bucket_space_front_())
|
---|
| 1091 | expand_();
|
---|
| 1092 |
|
---|
| 1093 | --front_bucket_;
|
---|
[9019d85] | 1094 | ++bucket_count_;
|
---|
[3a06cc6] | 1095 | data_[front_bucket_] = allocator_.allocate(bucket_size_);
|
---|
| 1096 | front_bucket_idx_ = bucket_size_;
|
---|
| 1097 | }
|
---|
| 1098 |
|
---|
| 1099 | void expand_()
|
---|
| 1100 | {
|
---|
| 1101 | bucket_capacity_ *= 2;
|
---|
| 1102 | value_type** new_data = new value_type*[bucket_capacity_];
|
---|
| 1103 |
|
---|
[9019d85] | 1104 | /**
|
---|
| 1105 | * Note: This currently expands both sides whenever one reaches
|
---|
| 1106 | * its limit. Might be better to expand only one (or both when
|
---|
| 1107 | * the other is near its limit)?
|
---|
| 1108 | */
|
---|
[3a06cc6] | 1109 | size_type new_front = bucket_capacity_ / 4;
|
---|
[9019d85] | 1110 | size_type new_back = new_front + bucket_count_ - 1;
|
---|
[3a06cc6] | 1111 |
|
---|
| 1112 | for (size_type i = new_front, j = front_bucket_; i <= new_back; ++i, ++j)
|
---|
| 1113 | new_data[i] = move(data_[j]);
|
---|
| 1114 | std::swap(data_, new_data);
|
---|
| 1115 |
|
---|
| 1116 | delete[] new_data;
|
---|
| 1117 | front_bucket_ = new_front;
|
---|
| 1118 | back_bucket_ = new_back;
|
---|
| 1119 | }
|
---|
| 1120 |
|
---|
| 1121 | size_type get_bucket_index_(size_type idx) const
|
---|
| 1122 | {
|
---|
| 1123 | if (idx < elements_in_front_bucket_())
|
---|
| 1124 | return front_bucket_;
|
---|
| 1125 |
|
---|
| 1126 | idx -= elements_in_front_bucket_();
|
---|
| 1127 | return idx / bucket_size_ + front_bucket_ + 1;
|
---|
| 1128 | }
|
---|
| 1129 |
|
---|
| 1130 | size_type get_element_index_(size_type idx) const
|
---|
| 1131 | {
|
---|
| 1132 | if (idx < elements_in_front_bucket_())
|
---|
| 1133 | return bucket_size_ - elements_in_front_bucket_() + idx;
|
---|
| 1134 |
|
---|
| 1135 | idx -= elements_in_front_bucket_();
|
---|
| 1136 | return idx % bucket_size_;
|
---|
| 1137 | }
|
---|
| 1138 |
|
---|
| 1139 | size_type elements_in_front_bucket_() const
|
---|
| 1140 | {
|
---|
| 1141 | return bucket_size_ - front_bucket_idx_;
|
---|
| 1142 | }
|
---|
[7a7ecbd] | 1143 | };
|
---|
| 1144 |
|
---|
| 1145 | template<class T, class Allocator>
|
---|
| 1146 | bool operator==(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
| 1147 | {
|
---|
[6d8a63a] | 1148 | if (lhs.size() != rhs.size())
|
---|
| 1149 | return false;
|
---|
| 1150 |
|
---|
| 1151 | for (decltype(lhs.size()) i = 0; i < lhs.size(); ++i)
|
---|
| 1152 | {
|
---|
| 1153 | if (lhs[i] != rhs[i])
|
---|
| 1154 | return false;
|
---|
| 1155 | }
|
---|
| 1156 |
|
---|
| 1157 | return true;
|
---|
[7a7ecbd] | 1158 | }
|
---|
| 1159 |
|
---|
| 1160 | template<class T, class Allocator>
|
---|
| 1161 | bool operator<(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
| 1162 | {
|
---|
[6d8a63a] | 1163 | auto min_size = min(lhs.size(), rhs.size());
|
---|
| 1164 | for (decltype(lhs.size()) i = 0; i < min_size; ++i)
|
---|
| 1165 | {
|
---|
| 1166 | if (lhs[i] >= rhs[i])
|
---|
| 1167 | return false;
|
---|
| 1168 | }
|
---|
| 1169 |
|
---|
| 1170 | if (lhs.size() == rhs.size())
|
---|
| 1171 | return true;
|
---|
| 1172 | else
|
---|
| 1173 | return lhs.size() < rhs.size();
|
---|
[7a7ecbd] | 1174 | }
|
---|
| 1175 |
|
---|
| 1176 | template<class T, class Allocator>
|
---|
[3a06cc6] | 1177 | bool operator!=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
[7a7ecbd] | 1178 | {
|
---|
[6d8a63a] | 1179 | return !(lhs == rhs);
|
---|
[7a7ecbd] | 1180 | }
|
---|
| 1181 |
|
---|
| 1182 | template<class T, class Allocator>
|
---|
| 1183 | bool operator>(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
| 1184 | {
|
---|
[6d8a63a] | 1185 | return rhs < lhs;
|
---|
[7a7ecbd] | 1186 | }
|
---|
| 1187 |
|
---|
| 1188 | template<class T, class Allocator>
|
---|
| 1189 | bool operator<=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
| 1190 | {
|
---|
[6d8a63a] | 1191 | return !(rhs < lhs);
|
---|
[7a7ecbd] | 1192 | }
|
---|
| 1193 |
|
---|
| 1194 | template<class T, class Allocator>
|
---|
| 1195 | bool operator>=(const deque<T, Allocator>& lhs, const deque<T, Allocator>& rhs)
|
---|
| 1196 | {
|
---|
[6d8a63a] | 1197 | return !(lhs < rhs);
|
---|
[7a7ecbd] | 1198 | }
|
---|
| 1199 |
|
---|
| 1200 | /**
|
---|
| 1201 | * 23.3.3.5, deque specialized algorithms:
|
---|
| 1202 | */
|
---|
| 1203 |
|
---|
| 1204 | template<class T, class Allocator>
|
---|
| 1205 | void swap(deque<T, Allocator>& lhs, deque<T, Allocator>& rhs)
|
---|
| 1206 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 1207 | {
|
---|
| 1208 | lhs.swap(rhs);
|
---|
| 1209 | }
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | #endif
|
---|