Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADT] Introduce
map_to_vector
helper
The following pattern is common in the llvm codebase, as well as in downstream projects: ``` llvm::to_vector(llvm::map_range(container, lambda)) ``` This patch introduces a shortcut for this called `map_to_vector`. This template depends on both `llvm/ADT/SmallVector.h` and `llvm/ADT/STLExtras.h`, and since these are both relatively large and do not depend on each other, the `map_to_vector` helper is placed in a new header under `llvm/ADT/SmallVectorExtras.h`. Only a handful of use cases have been updated to use the new helper. Differential Revision: https://reviews.llvm.org/D145390
- Loading branch information
Laszlo Kindrat
committed
May 4, 2023
1 parent
7c57195
commit 17faae9
Showing
3 changed files
with
41 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//===- llvm/ADT/SmallVectorExtras.h -----------------------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
/// | ||
/// \file | ||
/// This file defines less commonly used SmallVector utilities. | ||
/// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_ADT_SMALLVECTOREXTRAS_H | ||
#define LLVM_ADT_SMALLVECTOREXTRAS_H | ||
|
||
#include "llvm/ADT/STLExtras.h" | ||
#include "llvm/ADT/SmallVector.h" | ||
|
||
namespace llvm { | ||
|
||
/// Map a range to a SmallVector with element types deduced from the mapping. | ||
template <class ContainerTy, class FuncTy> | ||
auto map_to_vector(ContainerTy &&C, FuncTy &&F) { | ||
return to_vector( | ||
map_range(std::forward<ContainerTy>(C), std::forward<FuncTy>(F))); | ||
} | ||
|
||
} // namespace llvm | ||
|
||
#endif // LLVM_ADT_SMALLVECTOREXTRAS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters