mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 04:13:11 +00:00
AK: Teach Vector::insert() to use memmove() for trivial types
This commit is contained in:
parent
109727082c
commit
502626eecb
|
@ -295,4 +295,22 @@ TEST_CASE(nonnullownptrvector)
|
||||||
EXPECT_EQ(objects.size(), 2);
|
EXPECT_EQ(objects.size(), 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(insert_trivial)
|
||||||
|
{
|
||||||
|
Vector<int> ints;
|
||||||
|
ints.append(0);
|
||||||
|
ints.append(10);
|
||||||
|
ints.append(20);
|
||||||
|
ints.append(30);
|
||||||
|
ints.append(40);
|
||||||
|
ints.insert(2, 15);
|
||||||
|
EXPECT_EQ(ints.size(), 6);
|
||||||
|
EXPECT_EQ(ints[0], 0);
|
||||||
|
EXPECT_EQ(ints[1], 10);
|
||||||
|
EXPECT_EQ(ints[2], 15);
|
||||||
|
EXPECT_EQ(ints[3], 20);
|
||||||
|
EXPECT_EQ(ints[4], 30);
|
||||||
|
EXPECT_EQ(ints[5], 40);
|
||||||
|
}
|
||||||
|
|
||||||
TEST_MAIN(Vector)
|
TEST_MAIN(Vector)
|
||||||
|
|
17
AK/Vector.h
17
AK/Vector.h
|
@ -94,6 +94,8 @@ class TypedTransfer {
|
||||||
public:
|
public:
|
||||||
static void move(T* destination, T* source, size_t count)
|
static void move(T* destination, T* source, size_t count)
|
||||||
{
|
{
|
||||||
|
if (!count)
|
||||||
|
return;
|
||||||
if constexpr (Traits<T>::is_trivial()) {
|
if constexpr (Traits<T>::is_trivial()) {
|
||||||
memmove(destination, source, count * sizeof(T));
|
memmove(destination, source, count * sizeof(T));
|
||||||
return;
|
return;
|
||||||
|
@ -104,6 +106,8 @@ public:
|
||||||
|
|
||||||
static void copy(T* destination, const T* source, size_t count)
|
static void copy(T* destination, const T* source, size_t count)
|
||||||
{
|
{
|
||||||
|
if (!count)
|
||||||
|
return;
|
||||||
if constexpr (Traits<T>::is_trivial()) {
|
if constexpr (Traits<T>::is_trivial()) {
|
||||||
memmove(destination, source, count * sizeof(T));
|
memmove(destination, source, count * sizeof(T));
|
||||||
return;
|
return;
|
||||||
|
@ -114,6 +118,9 @@ public:
|
||||||
|
|
||||||
static bool compare(const T* a, const T* b, size_t count)
|
static bool compare(const T* a, const T* b, size_t count)
|
||||||
{
|
{
|
||||||
|
if (!count)
|
||||||
|
return true;
|
||||||
|
|
||||||
if constexpr (Traits<T>::is_trivial())
|
if constexpr (Traits<T>::is_trivial())
|
||||||
return !memcmp(a, b, count * sizeof(T));
|
return !memcmp(a, b, count * sizeof(T));
|
||||||
|
|
||||||
|
@ -335,9 +342,13 @@ public:
|
||||||
return append(move(value));
|
return append(move(value));
|
||||||
grow_capacity(size() + 1);
|
grow_capacity(size() + 1);
|
||||||
++m_size;
|
++m_size;
|
||||||
for (int i = size() - 1; i > index; --i) {
|
if constexpr (Traits<T>::is_trivial()) {
|
||||||
new (slot(i)) T(move(at(i - 1)));
|
TypedTransfer<T>::move(slot(index + 1), slot(index), m_size - index - 1);
|
||||||
at(i - 1).~T();
|
} else {
|
||||||
|
for (int i = size() - 1; i > index; --i) {
|
||||||
|
new (slot(i)) T(move(at(i - 1)));
|
||||||
|
at(i - 1).~T();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
new (slot(index)) T(move(value));
|
new (slot(index)) T(move(value));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue