| 391 | |
| 392 | template <typename InputIt> |
| 393 | iterator insert(const_iterator insert_at, InputIt first, InputIt last) { |
| 394 | const size_t n = storage_.size_; |
| 395 | const size_t it_size = static_cast<size_t>(last - first); // XXX might be O(n)? |
| 396 | const size_t pos = static_cast<size_t>(insert_at - const_data_ptr()); |
| 397 | storage_.bump_size(it_size); |
| 398 | auto* p = storage_.storage_ptr(); |
| 399 | if (it_size == 0) { |
| 400 | return p[pos].get(); |
| 401 | } |
| 402 | const size_t end_pos = pos + it_size; |
| 403 | |
| 404 | // Move [pos; n) to [end_pos; end_pos + n - pos) |
| 405 | size_t i = n; |
| 406 | size_t j = end_pos + n - pos; |
| 407 | while (j > std::max(n, end_pos)) { |
| 408 | p[--j].move_construct(&p[--i]); |
| 409 | } |
| 410 | while (j > end_pos) { |
| 411 | p[--j].move_assign(&p[--i]); |
| 412 | } |
| 413 | assert(j == end_pos); |
| 414 | // Copy [first; last) to [pos; end_pos) |
| 415 | j = pos; |
| 416 | while (j < std::min(n, end_pos)) { |
| 417 | p[j++].assign(*first++); |
| 418 | } |
| 419 | while (j < end_pos) { |
| 420 | p[j++].construct(*first++); |
| 421 | } |
| 422 | assert(first == last); |
| 423 | return p[pos].get(); |
| 424 | } |
| 425 | |
| 426 | void resize(size_t n) { |
| 427 | const size_t old_size = storage_.size_; |