Use a fixed flat set to store stopwords.
This is more memory efficient than std::unordered_set and requires no heap allocations. However, since the list of stop words is quite long, sorting it dynamically at compile time exceeds the maximum number of steps allowed in constexpr evaluation. To work around this, add MakeFixedFlatSetSorted() which requires already sorted input as an analogue to the already-existing MakeFixedFlatMapSorted(). Change-Id: I93e3cc685950ade5a1f054debb4638e5e1fc5d95 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4660000 Reviewed-by: danakj <danakj@chromium.org> Commit-Queue: Daniel Cheng <dcheng@chromium.org> Reviewed-by: Xiangdong Kong <xiangdongkong@google.com> Cr-Commit-Position: refs/heads/main@{#1164820}
This commit is contained in:

committed by
Chromium LUCI CQ

parent
33aca383a1
commit
5fac1ce5e1
@ -74,6 +74,25 @@ namespace base {
|
||||
template <class Key, size_t N, class Compare = std::less<>>
|
||||
using fixed_flat_set = base::flat_set<Key, Compare, std::array<const Key, N>>;
|
||||
|
||||
// Utility function to simplify constructing a fixed_flat_set from a fixed list
|
||||
// of keys and values. Requires that the passed in `data` contains unique keys
|
||||
// and be sorted by key. See `MakeFixedFlatSet` for a variant that sorts the
|
||||
// input automatically.
|
||||
//
|
||||
// Example usage:
|
||||
// constexpr auto kSet = base::MakeFixedFlatSetSorted<base::StringPiece>(
|
||||
// {"bar", "baz", "foo", "qux"});
|
||||
template <class Key, size_t N, class Compare = std::less<>>
|
||||
constexpr fixed_flat_set<Key, N, Compare> MakeFixedFlatSetSorted(
|
||||
std::common_type_t<Key> (&&data)[N],
|
||||
const Compare& comp = Compare()) {
|
||||
CHECK(internal::is_sorted_and_unique(data, comp));
|
||||
// Specify the value_type explicitly to ensure that the returned array has
|
||||
// immutable keys.
|
||||
return fixed_flat_set<Key, N, Compare>(
|
||||
sorted_unique, internal::ToArray<const Key>(data), comp);
|
||||
}
|
||||
|
||||
// Utility function to simplify constructing a fixed_flat_set from a fixed list
|
||||
// of keys. Requires that the passed in `data` contains unique keys.
|
||||
//
|
||||
|
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user