From 2aa7a33aca5b4b1edde9ad0a419a4bea75f0e0b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20=E2=80=98Touky=E2=80=99=20Huet?= Date: Wed, 17 Sep 2014 02:57:43 +0000 Subject: [PATCH] Added Insert in tuple array --- src/lol/base/array.h | 16 ++++++++++++++++ src/t/base/array.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/lol/base/array.h b/src/lol/base/array.h index 687e76d1..a9f85760 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -481,6 +481,22 @@ public: } ++this->m_count; } + inline void Insert(ptrdiff_t pos, T... args) + { + ASSERT(pos >= 0); + ASSERT(pos <= m_count); + + if (m_count >= m_reserved) + Grow(); + + for (ptrdiff_t i = m_count; i > pos; --i) + { + new (&m_data[i]) element_t(m_data[i - 1]); + m_data[i - 1].~element_t(); + } + new (&this->m_data[pos]) tuple({ args... }); + ++m_count; + } }; template diff --git a/src/t/base/array.cpp b/src/t/base/array.cpp index 47c21174..c6a6fc1d 100644 --- a/src/t/base/array.cpp +++ b/src/t/base/array.cpp @@ -188,6 +188,31 @@ lolunit_declare_fixture(ArrayTest) lolunit_assert_equal(7, a[2]); lolunit_assert_equal(2, a[3]); lolunit_assert_equal(6, a[4]); + + array b; + b.Insert(0, 5, 6, 7); + lolunit_assert_equal(5, b[0].m1); + lolunit_assert_equal(6, b[0].m2); + lolunit_assert_equal(7, b[0].m3); + + b.Insert(1, 8, 9, 6); + lolunit_assert_equal(5, b[0].m1); + lolunit_assert_equal(6, b[0].m2); + lolunit_assert_equal(7, b[0].m3); + lolunit_assert_equal(8, b[1].m1); + lolunit_assert_equal(9, b[1].m2); + lolunit_assert_equal(6, b[1].m3); + + b.Insert(1, 4, 5, 3); + lolunit_assert_equal(5, b[0].m1); + lolunit_assert_equal(6, b[0].m2); + lolunit_assert_equal(7, b[0].m3); + lolunit_assert_equal(4, b[2].m1); + lolunit_assert_equal(5, b[2].m2); + lolunit_assert_equal(3, b[2].m3); + lolunit_assert_equal(8, b[2].m1); + lolunit_assert_equal(9, b[2].m2); + lolunit_assert_equal(6, b[2].m3); } lolunit_declare_test(ArrayConcat)