v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
container-utils.h
Go to the documentation of this file.
1// Copyright 2022 the V8 project 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
5#ifndef V8_BASE_CONTAINER_UTILS_H_
6#define V8_BASE_CONTAINER_UTILS_H_
7
8#include <stddef.h>
9
10#include <algorithm>
11#include <iterator>
12#include <optional>
13#include <vector>
14
15namespace v8::base {
16
17// Returns true iff the {element} is found in the {container}.
18template <typename C, typename T>
19bool contains(const C& container, const T& element) {
20 const auto e = std::end(container);
21 return std::find(std::begin(container), e, element) != e;
22}
23
24// Returns the first index of {element} in {container}. Returns std::nullopt if
25// {container} does not contain {element}.
26template <typename C, typename T>
27std::optional<size_t> index_of(const C& container, const T& element) {
28 const auto b = std::begin(container);
29 const auto e = std::end(container);
30 if (auto it = std::find(b, e, element); it != e) {
31 return {std::distance(b, it)};
32 }
33 return std::nullopt;
34}
35
36// Returns the index of the first element in {container} that satisfies
37// {predicate}. Returns std::nullopt if no element satisfies {predicate}.
38template <typename C, typename P>
39std::optional<size_t> index_of_if(const C& container, const P& predicate) {
40 const auto b = std::begin(container);
41 const auto e = std::end(container);
42 if (auto it = std::find_if(b, e, predicate); it != e) {
43 return {std::distance(b, it)};
44 }
45 return std::nullopt;
46}
47
48// Removes {count} elements from {container} starting at {index}. If {count} is
49// larger than the number of elements after {index}, all elements after {index}
50// are removed. Returns the number of removed elements.
51template <typename C>
52inline size_t erase_at(C& container, size_t index, size_t count = 1) {
53 // TODO(C++20): Replace with std::erase.
54 if (std::size(container) <= index) return 0;
55 auto start = std::begin(container) + index;
56 count = std::min<size_t>(count, std::distance(start, std::end(container)));
57 container.erase(start, start + count);
58 return count;
59}
60
61// Removes all elements from {container} that satisfy {predicate}. Returns the
62// number of removed elements.
63// TODO(C++20): Replace with std::erase_if.
64template <typename C, typename P>
65inline size_t erase_if(C& container, const P& predicate) {
66 auto it =
67 std::remove_if(std::begin(container), std::end(container), predicate);
68 auto count = std::distance(it, std::end(container));
69 container.erase(it, std::end(container));
70 return count;
71}
72
73// Helper for std::count_if.
74template <typename C, typename P>
75inline size_t count_if(const C& container, const P& predicate) {
76 return std::count_if(std::begin(container), std::end(container), predicate);
77}
78
79// Helper for std::all_of.
80template <typename C, typename P>
81inline bool all_of(const C& container, const P& predicate) {
82 return std::all_of(std::begin(container), std::end(container), predicate);
83}
84template <typename C>
85inline bool all_of(const C& container) {
86 return std::all_of(
87 std::begin(container), std::end(container),
88 [](const auto& value) { return static_cast<bool>(value); });
89}
90
91// Helper for std::any_of.
92template <typename C, typename P>
93inline bool any_of(const C& container, const P& predicate) {
94 return std::any_of(std::begin(container), std::end(container), predicate);
95}
96template <typename C>
97inline bool any_of(const C& container) {
98 return std::any_of(
99 std::begin(container), std::end(container),
100 [](const auto& value) { return static_cast<bool>(value); });
101}
102
103// Helper for std::none_of.
104template <typename C, typename P>
105inline bool none_of(const C& container, const P& predicate) {
106 return std::none_of(std::begin(container), std::end(container), predicate);
107}
108
109// Helper for std::sort.
110template <typename C>
111inline void sort(C& container) {
112 std::sort(std::begin(container), std::end(container));
113}
114template <typename C, typename Comp>
115inline void sort(C& container, Comp comp) {
116 std::sort(std::begin(container), std::end(container), comp);
117}
118
119// Returns true iff all elements of {container} compare equal using operator==.
120template <typename C>
121inline bool all_equal(const C& container) {
122 if (std::size(container) <= 1) return true;
123 auto b = std::begin(container);
124 const auto& value = *b;
125 return std::all_of(++b, std::end(container),
126 [&](const auto& v) { return v == value; });
127}
128
129// Returns true iff all elements of {container} compare equal to {value} using
130// operator==.
131template <typename C, typename T>
132inline bool all_equal(const C& container, const T& value) {
133 return std::all_of(std::begin(container), std::end(container),
134 [&](const auto& v) { return v == value; });
135}
136
137// Appends to vector {v} all the elements in the range {std::begin(container)}
138// and {std::end(container)}.
139template <typename V, typename C>
140inline void vector_append(V& v, const C& container) {
141 v.insert(std::end(v), std::begin(container), std::end(container));
142}
143
144} // namespace v8::base
145
146#endif // V8_BASE_CONTAINER_UTILS_H_
#define V(Name)
int start
uint32_t count
OptionalOpIndex index
std::optional< size_t > index_of(const C &container, const T &element)
bool any_of(const C &container, const P &predicate)
bool none_of(const C &container, const P &predicate)
size_t erase_at(C &container, size_t index, size_t count=1)
bool all_equal(const C &container)
void vector_append(V &v, const C &container)
size_t count_if(const C &container, const P &predicate)
bool contains(const C &container, const T &element)
size_t erase_if(C &container, const P &predicate)
bool all_of(const C &container, const P &predicate)
std::optional< size_t > index_of_if(const C &container, const P &predicate)
void sort(C &container)
#define P(name, number_of_args, result_size)
Definition runtime.cc:22
std::unique_ptr< ValueMirror > value