From e89448af704e254c35d6427b678dbe40739da101 Mon Sep 17 00:00:00 2001 From: q66 Date: Sat, 1 Aug 2015 20:16:03 +0100 Subject: [PATCH] add vector push_n --- ostd/vector.hh | 13 +++++++++++++ tests/vector.cc | 5 ++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/ostd/vector.hh b/ostd/vector.hh index 11386e2..ebd42d6 100644 --- a/ostd/vector.hh +++ b/ostd/vector.hh @@ -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()) { + 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 T &emplace_back(U &&...args) { if (p_len == p_cap) reserve(p_len + 1); diff --git a/tests/vector.cc b/tests/vector.cc index db094ef..e215a53 100644 --- a/tests/vector.cc +++ b/tests/vector.cc @@ -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; } \ No newline at end of file