[289b2dd] | 1 | /*
|
---|
[c06328da] | 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
[289b2dd] | 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_VECTOR
|
---|
| 30 | #define LIBCPP_VECTOR
|
---|
| 31 |
|
---|
[35584b19] | 32 | #include <algorithm>
|
---|
[289b2dd] | 33 | #include <initializer_list>
|
---|
| 34 | #include <iterator>
|
---|
| 35 | #include <memory>
|
---|
[35584b19] | 36 | #include <utility>
|
---|
[289b2dd] | 37 |
|
---|
| 38 | namespace std
|
---|
| 39 | {
|
---|
| 40 | /**
|
---|
| 41 | * 23.3.6, vector:
|
---|
| 42 | */
|
---|
| 43 |
|
---|
| 44 | template<class T, class Allocator = allocator<T>>
|
---|
| 45 | class vector
|
---|
| 46 | {
|
---|
| 47 | public:
|
---|
| 48 | using value_type = T;
|
---|
| 49 | using reference = value_type&;
|
---|
| 50 | using const_reference = const value_type&;
|
---|
| 51 | using allocator_type = Allocator;
|
---|
| 52 | using size_type = size_t;
|
---|
| 53 | using difference_type = ptrdiff_t;
|
---|
| 54 | using pointer = typename allocator_traits<Allocator>::pointer;
|
---|
| 55 | using const_pointer = typename allocator_traits<Allocator>::pointer;
|
---|
| 56 | using iterator = pointer;
|
---|
| 57 | using const_iterator = const_pointer;
|
---|
| 58 | using reverse_iterator = std::reverse_iterator<iterator>;
|
---|
| 59 | using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
---|
| 60 |
|
---|
| 61 | vector() noexcept
|
---|
| 62 | : vector{Allocator{}}
|
---|
| 63 | { /* DUMMY BODY */ }
|
---|
| 64 |
|
---|
[35584b19] | 65 | explicit vector(const Allocator& alloc)
|
---|
| 66 | : data_{nullptr}, size_{}, capacity_{},
|
---|
| 67 | allocator_{alloc}
|
---|
| 68 | { /* DUMMY BODY */ }
|
---|
| 69 |
|
---|
| 70 | explicit vector(size_type n, const Allocator& alloc = Allocator{})
|
---|
| 71 | : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
|
---|
| 72 | {
|
---|
| 73 | data_ = allocator_.allocate(capacity_);
|
---|
| 74 |
|
---|
| 75 | for (size_type i = 0; i < size_; ++i)
|
---|
| 76 | allocator_traits<Allocator>::construct(allocator_, data_ + i);
|
---|
| 77 | }
|
---|
[289b2dd] | 78 |
|
---|
[35584b19] | 79 | vector(size_type n, const T& val, const Allocator& alloc = Allocator{})
|
---|
| 80 | : data_{}, size_{n}, capacity_{n}, allocator_{alloc}
|
---|
| 81 | {
|
---|
| 82 | data_ = allocator_.allocate(capacity_);
|
---|
[289b2dd] | 83 |
|
---|
[35584b19] | 84 | for (size_type i = 0; i < size_; ++i)
|
---|
| 85 | data_[i] = val;
|
---|
| 86 | }
|
---|
[289b2dd] | 87 |
|
---|
| 88 | template<class InputIterator>
|
---|
| 89 | vector(InputIterator first, InputIterator last,
|
---|
[35584b19] | 90 | const Allocator& alloc = Allocator{})
|
---|
| 91 | {
|
---|
| 92 | // TODO: research constraints and provide multiple
|
---|
| 93 | // implementations via enable_if
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | vector(const vector& other)
|
---|
| 97 | : data_{nullptr}, size_{other.size_}, capacity_{other.capacity_},
|
---|
| 98 | allocator_{other.allocator_}
|
---|
| 99 | {
|
---|
| 100 | data_ = allocator_.allocate(capacity_);
|
---|
| 101 |
|
---|
| 102 | for (size_type i = 0; i < size_; ++i)
|
---|
| 103 | data_[i] = other.data_[i];
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | vector(vector&& other) noexcept
|
---|
| 107 | : data_{other.data_}, size_{other.size_}, capacity_{other.capacity_},
|
---|
| 108 | allocator_{move(other.allocator_)}
|
---|
| 109 | {
|
---|
| 110 | // other is guaranteed to be empty()
|
---|
| 111 | other.size_ = other.capacity_ = 0;
|
---|
| 112 | other.data_ = nullptr;
|
---|
| 113 | }
|
---|
[289b2dd] | 114 |
|
---|
[35584b19] | 115 | vector(const vector& other, const Allocator& alloc)
|
---|
| 116 | : data_{nullptr}, size_{other.size_}, capacity_{other.capacity_},
|
---|
| 117 | allocator_{alloc}
|
---|
| 118 | {
|
---|
| 119 | data_ = allocator_.allocate(capacity_);
|
---|
[289b2dd] | 120 |
|
---|
[35584b19] | 121 | for (size_type i = 0; i < size_; ++i)
|
---|
| 122 | data_[i] = other.data_[i];
|
---|
| 123 | }
|
---|
[289b2dd] | 124 |
|
---|
[35584b19] | 125 | vector(initializer_list<T> init, const Allocator& alloc = Allocator{})
|
---|
| 126 | : data_{nullptr}, size_{init.size()}, capacity_{init.size()},
|
---|
| 127 | allocator_{alloc}
|
---|
| 128 | {
|
---|
| 129 | data_ = allocator_.allocate(capacity_);
|
---|
[289b2dd] | 130 |
|
---|
[35584b19] | 131 | auto it = init.begin();
|
---|
| 132 | for (size_type i = 0; it != init.end(); ++i, ++it)
|
---|
| 133 | {
|
---|
| 134 | data_[i] = *it;
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
[289b2dd] | 137 |
|
---|
[35584b19] | 138 | ~vector()
|
---|
| 139 | {
|
---|
| 140 | allocator_.deallocate(data_, capacity_);
|
---|
| 141 | }
|
---|
[289b2dd] | 142 |
|
---|
[35584b19] | 143 | vector& operator=(const vector& other)
|
---|
| 144 | {
|
---|
| 145 | vector tmp{other};
|
---|
| 146 | swap(tmp);
|
---|
| 147 |
|
---|
| 148 | return *this;
|
---|
| 149 | }
|
---|
[289b2dd] | 150 |
|
---|
| 151 | vector& operator=(vector&& other)
|
---|
| 152 | noexcept(allocator_traits<Allocator>::propagate_on_container_move_assignment::value ||
|
---|
| 153 | allocator_traits<Allocator>::is_always_equal::value)
|
---|
| 154 | {
|
---|
[c06328da] | 155 | if (data_)
|
---|
| 156 | allocator_.deallocate(data_, capacity_);
|
---|
| 157 |
|
---|
| 158 | // TODO: test this
|
---|
| 159 | data_ = other.data_;
|
---|
| 160 | size_ = other.size_;
|
---|
| 161 | capacity_ = other.capacity_;
|
---|
| 162 | allocator_ = move(other.allocator_);
|
---|
| 163 |
|
---|
| 164 | other.data_ = nullptr;
|
---|
| 165 | other.size_ = size_type{};
|
---|
| 166 | other.capacity_ = size_type{};
|
---|
| 167 | other.allocator_ = allocator_type{};
|
---|
[35584b19] | 168 | return *this;
|
---|
| 169 | }
|
---|
| 170 |
|
---|
| 171 | vector& operator=(initializer_list<T> init)
|
---|
| 172 | {
|
---|
| 173 | vector tmp{init, allocator_};
|
---|
| 174 | swap(tmp);
|
---|
| 175 |
|
---|
[289b2dd] | 176 | return *this;
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[35584b19] | 179 | template<class InputIterator>
|
---|
| 180 | void assign(InputIterator first, InputIterator last)
|
---|
| 181 | {
|
---|
| 182 | vector tmp{first, last};
|
---|
| 183 | swap(tmp);
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | void assign(size_type size, const T& val)
|
---|
| 187 | {
|
---|
| 188 | // Parenthesies required to avoid initializer list
|
---|
| 189 | // construction.
|
---|
| 190 | vector tmp(size, val);
|
---|
| 191 | swap(tmp);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | void assign(initializer_list<T> init)
|
---|
| 195 | {
|
---|
| 196 | vector tmp{init};
|
---|
| 197 | swap(tmp);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | allocator_type get_allocator() const noexcept
|
---|
| 201 | {
|
---|
[836ecad] | 202 | return allocator_type{allocator_};
|
---|
[35584b19] | 203 | }
|
---|
| 204 |
|
---|
| 205 | iterator begin() noexcept
|
---|
| 206 | {
|
---|
| 207 | return &data_[0];
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | const_iterator begin() const noexcept
|
---|
| 211 | {
|
---|
| 212 | return &data_[0];
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | iterator end() noexcept
|
---|
| 216 | {
|
---|
| 217 | return begin() + size_;
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | const_iterator end() const noexcept
|
---|
| 221 | {
|
---|
| 222 | return begin() + size_;
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 | reverse_iterator rbegin() noexcept
|
---|
| 226 | {
|
---|
[98c99ba] | 227 | return make_reverse_iterator(end());
|
---|
[35584b19] | 228 | }
|
---|
| 229 |
|
---|
| 230 | const_reverse_iterator rbegin() const noexcept
|
---|
| 231 | {
|
---|
[98c99ba] | 232 | return make_reverse_iterator(cend());
|
---|
[35584b19] | 233 | }
|
---|
| 234 |
|
---|
| 235 | reverse_iterator rend() noexcept
|
---|
| 236 | {
|
---|
[98c99ba] | 237 | return make_reverse_iterator(begin());
|
---|
[35584b19] | 238 | }
|
---|
| 239 |
|
---|
| 240 | const_reverse_iterator rend() const noexcept
|
---|
| 241 | {
|
---|
[98c99ba] | 242 | return make_reverse_iterator(cbegin());
|
---|
[35584b19] | 243 | }
|
---|
| 244 |
|
---|
| 245 | const_iterator cbegin() const noexcept
|
---|
| 246 | {
|
---|
| 247 | return &data_[0];
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | const_iterator cend() const noexcept
|
---|
| 251 | {
|
---|
| 252 | return cbegin() + size_;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
[73066e61] | 255 | const_reverse_iterator crbegin() const noexcept
|
---|
[35584b19] | 256 | {
|
---|
| 257 | return rbegin();
|
---|
| 258 | }
|
---|
| 259 |
|
---|
[73066e61] | 260 | const_reverse_iterator crend() const noexcept
|
---|
[35584b19] | 261 | {
|
---|
| 262 | return rend();
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | size_type size() const noexcept
|
---|
| 266 | {
|
---|
| 267 | return size_;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | size_type max_size() const noexcept
|
---|
| 271 | {
|
---|
| 272 | return std::allocator_traits<Allocator>::max_size(allocator_);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | void resize(size_type sz)
|
---|
| 276 | {
|
---|
| 277 | resize_with_copy_(size_, capacity_);
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | void resize(size_type sz, const value_type& val)
|
---|
| 281 | {
|
---|
| 282 | auto old_size = size_;
|
---|
| 283 | resize_with_copy_(sz, capacity_);
|
---|
| 284 |
|
---|
| 285 | for (size_type i = old_size - 1; i < size_; ++i)
|
---|
| 286 | data_[i] = val;
|
---|
| 287 | }
|
---|
| 288 |
|
---|
| 289 | size_type capacity() const noexcept
|
---|
| 290 | {
|
---|
| 291 | return capacity_;
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | bool empty() const noexcept
|
---|
| 295 | {
|
---|
| 296 | return size_ == 0;
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | void reserve(size_type new_capacity)
|
---|
| 300 | {
|
---|
| 301 | // TODO: if new_capacity > max_size() throw
|
---|
| 302 | // length_error (this function shall have no
|
---|
| 303 | // effect in such case)
|
---|
| 304 | if (new_capacity > capacity_)
|
---|
| 305 | resize_with_copy_(size_, new_capacity);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | void shrink_to_fit()
|
---|
| 309 | {
|
---|
| 310 | resize_with_copy_(size_, size_);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | reference operator[](size_type idx)
|
---|
| 314 | {
|
---|
| 315 | return data_[idx];
|
---|
| 316 | }
|
---|
| 317 |
|
---|
| 318 | const_reference operator[](size_type idx) const
|
---|
| 319 | {
|
---|
| 320 | return data_[idx];
|
---|
| 321 | }
|
---|
| 322 |
|
---|
| 323 | reference at(size_type idx)
|
---|
| 324 | {
|
---|
| 325 | // TODO: bounds checking
|
---|
| 326 | return data_[idx];
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | const_reference at(size_type idx) const
|
---|
| 330 | {
|
---|
| 331 | // TODO: bounds checking
|
---|
| 332 | return data_[idx];
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | reference front()
|
---|
| 336 | {
|
---|
| 337 | /**
|
---|
| 338 | * Note: Calling front/back on an empty container
|
---|
| 339 | * is undefined, we opted for at-like
|
---|
| 340 | * behavior to provide our users with means
|
---|
| 341 | * to protect their programs from accidental
|
---|
| 342 | * accesses to an empty vector.
|
---|
| 343 | */
|
---|
| 344 | return at(0);
|
---|
| 345 | }
|
---|
| 346 |
|
---|
| 347 | const_reference front() const
|
---|
| 348 | {
|
---|
| 349 | return at(0);
|
---|
| 350 | }
|
---|
| 351 |
|
---|
| 352 | reference back()
|
---|
| 353 | {
|
---|
| 354 | return at(size_ - 1);
|
---|
| 355 | }
|
---|
| 356 |
|
---|
| 357 | const_reference back() const
|
---|
| 358 | {
|
---|
| 359 | return at(size - 1);
|
---|
| 360 | }
|
---|
| 361 |
|
---|
| 362 | T* data() noexcept
|
---|
| 363 | {
|
---|
| 364 | return data_;
|
---|
| 365 | }
|
---|
| 366 |
|
---|
| 367 | const T* data() const noexcept
|
---|
| 368 | {
|
---|
| 369 | return data_;
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | template<class... Args>
|
---|
| 373 | reference emplace_back(Args&&... args)
|
---|
| 374 | {
|
---|
| 375 | if (size_ >= capacity_)
|
---|
| 376 | resize_with_copy_(size_, next_capacity_());
|
---|
| 377 |
|
---|
[d13b67a] | 378 | allocator_traits<Allocator>::construct(allocator_,
|
---|
| 379 | begin() + size_, forward<Args>(args)...);
|
---|
[35584b19] | 380 |
|
---|
| 381 | return back();
|
---|
| 382 | }
|
---|
| 383 |
|
---|
| 384 | void push_back(const T& x)
|
---|
| 385 | {
|
---|
| 386 | if (size_ >= capacity_)
|
---|
| 387 | resize_with_copy_(size_, next_capacity_());
|
---|
| 388 | data_[size_++] = x;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | void push_back(T&& x)
|
---|
| 392 | {
|
---|
| 393 | if (size_ >= capacity_)
|
---|
| 394 | resize_with_copy_(size_, next_capacity_());
|
---|
| 395 | data_[size_++] = forward<T>(x);
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 | void pop_back()
|
---|
| 399 | {
|
---|
| 400 | destroy_from_end_until_(end() - 1);
|
---|
[0bde223e] | 401 | --size_;
|
---|
[35584b19] | 402 | }
|
---|
| 403 |
|
---|
| 404 | template<class... Args>
|
---|
| 405 | iterator emplace(const_iterator position, Args&&... args)
|
---|
| 406 | {
|
---|
[56521a2] | 407 | auto pos = const_cast<iterator>(position);
|
---|
[35584b19] | 408 |
|
---|
[83aea53] | 409 | pos = shift_(pos, 1);
|
---|
| 410 | allocator_.construct(pos, forward<Args>(args)...);
|
---|
[56521a2] | 411 |
|
---|
| 412 | return pos;
|
---|
[35584b19] | 413 | }
|
---|
| 414 |
|
---|
| 415 | iterator insert(const_iterator position, const value_type& x)
|
---|
| 416 | {
|
---|
[56521a2] | 417 | auto pos = const_cast<iterator>(position);
|
---|
[35584b19] | 418 |
|
---|
[83aea53] | 419 | pos = shift_(pos, 1);
|
---|
[56521a2] | 420 | *pos = x;
|
---|
[35584b19] | 421 |
|
---|
[56521a2] | 422 | return pos;
|
---|
[35584b19] | 423 | }
|
---|
| 424 |
|
---|
| 425 | iterator insert(const_iterator position, value_type&& x)
|
---|
| 426 | {
|
---|
[56521a2] | 427 | auto pos = const_cast<iterator>(position);
|
---|
[35584b19] | 428 |
|
---|
[83aea53] | 429 | pos = shift_(pos, 1);
|
---|
[56521a2] | 430 | *pos = forward<value_type>(x);
|
---|
[35584b19] | 431 |
|
---|
[56521a2] | 432 | return pos;
|
---|
[35584b19] | 433 | }
|
---|
| 434 |
|
---|
| 435 | iterator insert(const_iterator position, size_type count, const value_type& x)
|
---|
| 436 | {
|
---|
[56521a2] | 437 | auto pos = const_cast<iterator>(position);
|
---|
[35584b19] | 438 |
|
---|
[83aea53] | 439 | pos = shift_(pos, count);
|
---|
[56521a2] | 440 | auto copy_target = pos;
|
---|
| 441 | for (size_type i = 0; i < count; ++i)
|
---|
| 442 | *copy_target++ = x;
|
---|
[35584b19] | 443 |
|
---|
[56521a2] | 444 | return pos;
|
---|
[35584b19] | 445 | }
|
---|
[289b2dd] | 446 |
|
---|
| 447 | template<class InputIterator>
|
---|
[35584b19] | 448 | iterator insert(const_iterator position, InputIterator first,
|
---|
| 449 | InputIterator last)
|
---|
| 450 | {
|
---|
[56521a2] | 451 | auto pos = const_cast<iterator>(position);
|
---|
| 452 | auto count = static_cast<size_type>(last - first);
|
---|
[35584b19] | 453 |
|
---|
[83aea53] | 454 | pos = shift_(pos, count);
|
---|
| 455 | copy(first, last, pos);
|
---|
[56521a2] | 456 |
|
---|
| 457 | return pos;
|
---|
[35584b19] | 458 | }
|
---|
| 459 |
|
---|
| 460 | iterator insert(const_iterator position, initializer_list<T> init)
|
---|
| 461 | {
|
---|
[56521a2] | 462 | auto pos = const_cast<iterator>(position);
|
---|
[35584b19] | 463 |
|
---|
[83aea53] | 464 | pos = shift_(pos, init.size());
|
---|
| 465 | copy(init.begin(), init.end(), pos);
|
---|
[56521a2] | 466 |
|
---|
| 467 | return pos;
|
---|
[35584b19] | 468 | }
|
---|
| 469 |
|
---|
| 470 | iterator erase(const_iterator position)
|
---|
| 471 | {
|
---|
| 472 | iterator pos = const_cast<iterator>(position);
|
---|
| 473 | copy(position + 1, cend(), pos);
|
---|
| 474 | --size_;
|
---|
| 475 |
|
---|
| 476 | return pos;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | iterator erase(const_iterator first, const_iterator last)
|
---|
| 480 | {
|
---|
| 481 | iterator pos = const_cast<iterator>(first);
|
---|
| 482 | copy(last, cend(), pos);
|
---|
| 483 | size_ -= static_cast<size_type>(last - first);
|
---|
| 484 |
|
---|
| 485 | return pos;
|
---|
| 486 | }
|
---|
| 487 |
|
---|
| 488 | void swap(vector& other)
|
---|
| 489 | noexcept(allocator_traits<Allocator>::propagate_on_container_swap::value ||
|
---|
| 490 | allocator_traits<Allocator>::is_always_equal::value)
|
---|
| 491 | {
|
---|
| 492 | std::swap(data_, other.data_);
|
---|
| 493 | std::swap(size_, other.size_);
|
---|
| 494 | std::swap(capacity_, other.capacity_);
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | void clear() noexcept
|
---|
| 498 | {
|
---|
| 499 | // Note: Capacity remains unchanged.
|
---|
| 500 | destroy_from_end_until_(begin());
|
---|
| 501 | size_ = 0;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | private:
|
---|
| 505 | value_type* data_;
|
---|
| 506 | size_type size_;
|
---|
| 507 | size_type capacity_;
|
---|
[836ecad] | 508 | allocator_type allocator_;
|
---|
[35584b19] | 509 |
|
---|
| 510 | void resize_without_copy_(size_type capacity)
|
---|
| 511 | {
|
---|
| 512 | if (data_)
|
---|
| 513 | allocator_.deallocate(data_, capacity_);
|
---|
| 514 |
|
---|
| 515 | data_ = allocator_.allocate(capacity);
|
---|
| 516 | size_ = 0;
|
---|
| 517 | capacity_ = capacity;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | void resize_with_copy_(size_type size, size_type capacity)
|
---|
| 521 | {
|
---|
| 522 | if (size < size_)
|
---|
| 523 | destroy_from_end_until_(begin() + size);
|
---|
| 524 |
|
---|
| 525 | if(capacity_ == 0 || capacity_ < capacity)
|
---|
| 526 | {
|
---|
| 527 | auto new_data = allocator_.allocate(capacity);
|
---|
| 528 |
|
---|
| 529 | auto to_copy = min(size, size_);
|
---|
| 530 | for (size_type i = 0; i < to_copy; ++i)
|
---|
| 531 | new_data[i] = move(data_[i]);
|
---|
| 532 |
|
---|
| 533 | std::swap(data_, new_data);
|
---|
| 534 |
|
---|
| 535 | allocator_.deallocate(new_data, capacity_);
|
---|
| 536 | }
|
---|
| 537 |
|
---|
| 538 | capacity_ = capacity;
|
---|
| 539 | size_ = size;
|
---|
| 540 | }
|
---|
| 541 |
|
---|
| 542 | void destroy_from_end_until_(iterator target)
|
---|
| 543 | {
|
---|
| 544 | if (!empty())
|
---|
| 545 | {
|
---|
| 546 | auto last = end();
|
---|
| 547 | while(last != target)
|
---|
| 548 | allocator_traits<Allocator>::destroy(allocator_, --last);
|
---|
| 549 | }
|
---|
| 550 | }
|
---|
[289b2dd] | 551 |
|
---|
[35584b19] | 552 | size_type next_capacity_(size_type hint = 0) const noexcept
|
---|
| 553 | {
|
---|
| 554 | if (hint != 0)
|
---|
| 555 | return max(capacity_ * 2, hint);
|
---|
| 556 | else
|
---|
| 557 | return max(capacity_ * 2, 2ul);
|
---|
| 558 | }
|
---|
[289b2dd] | 559 |
|
---|
[83aea53] | 560 | iterator shift_(iterator position, size_type count)
|
---|
[35584b19] | 561 | {
|
---|
[56521a2] | 562 | if (size_ + count < capacity_)
|
---|
[83aea53] | 563 | {
|
---|
| 564 | copy_backward(position, end(), end() + count);
|
---|
| 565 | size_ += count;
|
---|
| 566 |
|
---|
| 567 | return position;
|
---|
| 568 | }
|
---|
[56521a2] | 569 | else
|
---|
| 570 | {
|
---|
| 571 | auto start_idx = static_cast<size_type>(position - begin());
|
---|
| 572 | auto end_idx = start_idx + count;
|
---|
| 573 | auto new_size = size_ + count;
|
---|
[35584b19] | 574 |
|
---|
[56521a2] | 575 | // Auxiliary vector for easier swap.
|
---|
| 576 | vector tmp{};
|
---|
| 577 | tmp.resize_without_copy_(max(new_size, capacity_));
|
---|
| 578 | tmp.size_ = new_size;
|
---|
[35584b19] | 579 |
|
---|
[56521a2] | 580 | // Copy before insertion index.
|
---|
[83aea53] | 581 | copy(begin(), begin() + start_idx, tmp.begin());
|
---|
[35584b19] | 582 |
|
---|
[56521a2] | 583 | // Copy after insertion index.
|
---|
[83aea53] | 584 | copy(begin() + start_idx, end(), tmp.begin() + end_idx);
|
---|
[35584b19] | 585 |
|
---|
[56521a2] | 586 | swap(tmp);
|
---|
[83aea53] | 587 |
|
---|
| 588 | // Position was invalidated!
|
---|
| 589 | return begin() + start_idx;
|
---|
[56521a2] | 590 | }
|
---|
[35584b19] | 591 | }
|
---|
[289b2dd] | 592 | };
|
---|
[35584b19] | 593 |
|
---|
| 594 | template<class T, class Alloc>
|
---|
| 595 | bool operator==(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 596 | {
|
---|
[c06328da] | 597 | if (lhs.size() != rhs.size())
|
---|
| 598 | return false;
|
---|
| 599 |
|
---|
| 600 | for (decltype(lhs.size()) i = 0; i < lhs.size(); ++i)
|
---|
| 601 | {
|
---|
| 602 | if (lhs[i] != rhs[i])
|
---|
| 603 | return false;
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | return true;
|
---|
[35584b19] | 607 | }
|
---|
| 608 |
|
---|
| 609 | template<class T, class Alloc>
|
---|
| 610 | bool operator<(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 611 | {
|
---|
[c06328da] | 612 | auto min_size = min(lhs.size(), rhs.size());
|
---|
| 613 | for (decltype(lhs.size()) i = 0; i < min_size; ++i)
|
---|
| 614 | {
|
---|
| 615 | if (lhs[i] >= rhs[i])
|
---|
| 616 | return false;
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | if (lhs.size() == rhs.size())
|
---|
| 620 | return true;
|
---|
| 621 | else
|
---|
| 622 | return lhs.size() < rhs.size();
|
---|
[35584b19] | 623 | }
|
---|
| 624 |
|
---|
| 625 | template<class T, class Alloc>
|
---|
| 626 | bool operator!=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 627 | {
|
---|
| 628 | return !(lhs == rhs);
|
---|
| 629 | }
|
---|
| 630 |
|
---|
| 631 | template<class T, class Alloc>
|
---|
| 632 | bool operator>(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 633 | {
|
---|
[c06328da] | 634 | return rhs < lhs;
|
---|
[35584b19] | 635 | }
|
---|
| 636 |
|
---|
| 637 | template<class T, class Alloc>
|
---|
| 638 | bool operator>=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 639 | {
|
---|
[c06328da] | 640 | return !(lhs < rhs);
|
---|
[35584b19] | 641 | }
|
---|
| 642 |
|
---|
| 643 | template<class T, class Alloc>
|
---|
| 644 | bool operator<=(const vector<T, Alloc>& lhs, const vector<T, Alloc>& rhs)
|
---|
| 645 | {
|
---|
[c06328da] | 646 | return !(rhs < lhs);
|
---|
[35584b19] | 647 | }
|
---|
| 648 |
|
---|
| 649 | /**
|
---|
| 650 | * 23.3.6.6, specialized algorithms:
|
---|
| 651 | */
|
---|
| 652 | template<class T, class Alloc>
|
---|
| 653 | void swap(vector<T, Alloc>& lhs, vector<T, Alloc>& rhs)
|
---|
| 654 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 655 | {
|
---|
| 656 | lhs.swap(rhs);
|
---|
| 657 | }
|
---|
[c06328da] | 658 |
|
---|
| 659 | /**
|
---|
| 660 | * 23.3.7, class vector<bool>:
|
---|
| 661 | */
|
---|
| 662 |
|
---|
| 663 | // TODO: implement
|
---|
[289b2dd] | 664 | }
|
---|
| 665 |
|
---|
| 666 | #endif
|
---|