extend.h 915 B

12345678910111213141516171819202122232425262728293031
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_CONTAINERS_EXTEND_H_
  5. #define BASE_CONTAINERS_EXTEND_H_
  6. #include <iterator>
  7. #include <vector>
  8. namespace base {
  9. // Append to |dst| all elements of |src| by std::move-ing them out of |src|.
  10. // After this operation, |src| will be empty.
  11. template <typename T>
  12. void Extend(std::vector<T>& dst, std::vector<T>&& src) {
  13. dst.insert(dst.end(), std::make_move_iterator(src.begin()),
  14. std::make_move_iterator(src.end()));
  15. src.clear();
  16. }
  17. // Append to |dst| all elements of |src| by copying them out of |src|. |src| is
  18. // not changed.
  19. template <typename T>
  20. void Extend(std::vector<T>& dst, const std::vector<T>& src) {
  21. dst.insert(dst.end(), src.begin(), src.end());
  22. }
  23. } // namespace base
  24. #endif // BASE_CONTAINERS_EXTEND_H_