MCPcopy Create free account
hub / github.com/pybind/pybind11 / small_vector

Class small_vector

include/pybind11/detail/argument_vector.h:128–218  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

126
127template <typename T, std::size_t InlineSize>
128struct small_vector {
129public:
130 small_vector() = default;
131
132 // Disable copy ctor and assignment.
133 small_vector(const small_vector &) = delete;
134 small_vector &operator=(const small_vector &) = delete;
135 small_vector(small_vector &&) noexcept = default;
136 small_vector &operator=(small_vector &&) noexcept = default;
137
138 std::size_t size() const {
139 if (is_inline()) {
140 return m_repr.iarray.size;
141 }
142 return m_repr.hvector.vec.size();
143 }
144
145 T const *data() const {
146 if (is_inline()) {
147 return m_repr.iarray.arr.data();
148 }
149 return m_repr.hvector.vec.data();
150 }
151
152 T &operator[](std::size_t idx) {
153 assert(idx < size());
154 if (is_inline()) {
155 return m_repr.iarray.arr[idx];
156 }
157 return m_repr.hvector.vec[idx];
158 }
159
160 T const &operator[](std::size_t idx) const {
161 assert(idx < size());
162 if (is_inline()) {
163 return m_repr.iarray.arr[idx];
164 }
165 return m_repr.hvector.vec[idx];
166 }
167
168 void push_back(const T &x) { emplace_back(x); }
169
170 void push_back(T &&x) { emplace_back(std::move(x)); }
171
172 template <typename... Args>
173 void emplace_back(Args &&...x) {
174 if (is_inline()) {
175 auto &ha = m_repr.iarray;
176 if (ha.size == InlineSize) {
177 move_to_heap_vector_with_reserved_size(InlineSize + 1);
178 m_repr.hvector.vec.emplace_back(std::forward<Args>(x)...);
179 } else {
180 ha.arr[ha.size++] = T(std::forward<Args>(x)...);
181 }
182 } else {
183 m_repr.hvector.vec.emplace_back(std::forward<Args>(x)...);
184 }
185 }

Callers

nothing calls this directly

Calls 1

is_inlineFunction · 0.85

Tested by

no test coverage detected