add vector push_n

master
Daniel Kolesa 2015-08-01 20:16:03 +01:00
parent ef5951577d
commit e89448af70
2 changed files with 17 additions and 1 deletions

View File

@ -305,6 +305,19 @@ public:
return p_buf.first()[p_len++];
}
Range push_n(const T *v, Size n) {
reserve(p_len + n);
if (IsPod<T>()) {
memcpy(p_buf.first() + p_len, v, n * sizeof(T));
} else {
for (Size i = 0; i < n; ++i)
allocator_construct(p_buf.second(),
&p_buf.first()[p_len + i], v[i]);
}
p_len += n;
return Range(&p_buf.first()[p_len - n], &p_buf.first()[p_len]);
}
template<typename ...U>
T &emplace_back(U &&...args) {
if (p_len == p_cap) reserve(p_len + 1);

View File

@ -84,7 +84,10 @@ int main() {
assert(w.front() == 5);
assert(w.back() == 5);
assert(to_string(w) == "{5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5}");
int pushn[] = { 3, 2, 1 };
w.push_n(pushn, 3);
assert(to_string(w) == "{5, 5, 4, 5, 5, 5, 5, 5, 5, 5, 5, 3, 2, 1}");
return 0;
}