• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_CONTAINERS_EXTEND_H_
6 #define BASE_CONTAINERS_EXTEND_H_
7 
8 #include <algorithm>
9 #include <iterator>
10 #include <ranges>
11 #include <type_traits>
12 #include <vector>
13 
14 namespace base {
15 
16 // Append to |dst| all elements of |src| by std::move-ing them out of |src|.
17 // After this operation, |src| will be empty.
18 template <typename T>
Extend(std::vector<T> & dst,std::vector<T> && src)19 void Extend(std::vector<T>& dst, std::vector<T>&& src) {
20   dst.insert(dst.end(), std::make_move_iterator(src.begin()),
21              std::make_move_iterator(src.end()));
22   src.clear();
23 }
24 
25 // Appends `range` to `dst`, copying them out of `range`.
26 template <typename T, typename Range, typename Proj = std::identity>
27   requires std::ranges::range<Range> &&
28            std::indirectly_unary_invocable<Proj, std::ranges::iterator_t<Range>>
29 void Extend(std::vector<T>& dst, Range&& range, Proj proj = {}) {
30   if constexpr (std::ranges::sized_range<Range>) {
31     dst.reserve(dst.size() + std::ranges::size(range));
32   }
33   std::ranges::transform(std::forward<Range>(range), std::back_inserter(dst),
34                          std::move(proj));
35 }
36 
37 }  // namespace base
38 
39 #endif  // BASE_CONTAINERS_EXTEND_H_
40