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 <iterator>
9 #include <vector>
10
11 #include "base/containers/span.h"
12
13 namespace base {
14
15 // Append to |dst| all elements of |src| by std::move-ing them out of |src|.
16 // After this operation, |src| will be empty.
17 template <typename T>
Extend(std::vector<T> & dst,std::vector<T> && src)18 void Extend(std::vector<T>& dst, std::vector<T>&& src) {
19 dst.insert(dst.end(), std::make_move_iterator(src.begin()),
20 std::make_move_iterator(src.end()));
21 src.clear();
22 }
23
24 // Append to |dst| all elements of |src| by copying them out of |src|. |src| is
25 // not changed.
26 template <typename T>
Extend(std::vector<T> & dst,const std::vector<T> & src)27 void Extend(std::vector<T>& dst, const std::vector<T>& src) {
28 Extend(dst, make_span(src));
29 }
30
31 // Append to |dst| all elements of |src| by copying them out of |src|. |src| is
32 // not changed.
33 template <typename T, size_t N>
Extend(std::vector<T> & dst,span<T,N> src)34 void Extend(std::vector<T>& dst, span<T, N> src) {
35 dst.insert(dst.end(), src.begin(), src.end());
36 }
37
38 // Append to |dst| all elements of |src| by copying them out of |src|. |src| is
39 // not changed.
40 template <typename T, size_t N>
Extend(std::vector<T> & dst,span<const T,N> src)41 void Extend(std::vector<T>& dst, span<const T, N> src) {
42 dst.insert(dst.end(), src.begin(), src.end());
43 }
44
45 } // namespace base
46
47 #endif // BASE_CONTAINERS_EXTEND_H_
48