mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-10 10:01:13 +09:00
Add Vector::remove().
This commit is contained in:
parent
750b27cb07
commit
7777c8844b
Notes:
sideshowbarker
2024-07-19 18:49:24 +09:00
Author: https://github.com/awesomekling
Commit: 7777c8844b
2 changed files with 47 additions and 2 deletions
21
AK/Vector.h
21
AK/Vector.h
|
@ -29,6 +29,18 @@ public:
|
||||||
T& at(unsigned i) { return *slot(i); }
|
T& at(unsigned i) { return *slot(i); }
|
||||||
const T& at(unsigned i) const { return *slot(i); }
|
const T& at(unsigned i) const { return *slot(i); }
|
||||||
|
|
||||||
|
void remove(unsigned index)
|
||||||
|
{
|
||||||
|
ASSERT(index < m_size);
|
||||||
|
at(index).~T();
|
||||||
|
for (unsigned i = index + 1; i < m_size; ++i) {
|
||||||
|
new (slot(i - 1)) T(std::move(at(i)));
|
||||||
|
at(i).~T();
|
||||||
|
}
|
||||||
|
|
||||||
|
--m_size;
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Vector<T>;
|
friend class Vector<T>;
|
||||||
|
|
||||||
|
@ -95,6 +107,11 @@ public:
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void remove(unsigned index)
|
||||||
|
{
|
||||||
|
m_impl->remove(index);
|
||||||
|
}
|
||||||
|
|
||||||
void append(T&& value)
|
void append(T&& value)
|
||||||
{
|
{
|
||||||
ensureCapacity(size() + 1);
|
ensureCapacity(size() + 1);
|
||||||
|
@ -152,8 +169,8 @@ public:
|
||||||
unsigned m_index { 0 };
|
unsigned m_index { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
ConstIterator begin() const { return Iterator(*this, 0); }
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
||||||
ConstIterator end() const { return Iterator(*this, size()); }
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static unsigned paddedCapacity(unsigned capacity)
|
static unsigned paddedCapacity(unsigned capacity)
|
||||||
|
|
28
AK/test.cpp
28
AK/test.cpp
|
@ -127,5 +127,33 @@ int main(int, char**)
|
||||||
problem.append("test");
|
problem.append("test");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto printInts = [] (const Vector<int>& v) {
|
||||||
|
printf("Vector {\n size: %u\n capacity: %u\n elements: ", v.size(), v.capacity());
|
||||||
|
for (auto i : v)
|
||||||
|
printf("%d ", i);
|
||||||
|
printf("\n}\n");
|
||||||
|
};
|
||||||
|
|
||||||
|
Vector<int> v;
|
||||||
|
v.append(0);
|
||||||
|
v.append(1);
|
||||||
|
v.append(2);
|
||||||
|
v.append(3);
|
||||||
|
printInts(v);
|
||||||
|
|
||||||
|
v.remove(1);
|
||||||
|
printInts(v);
|
||||||
|
|
||||||
|
v.remove(0);
|
||||||
|
printInts(v);
|
||||||
|
|
||||||
|
v.remove(0);
|
||||||
|
printInts(v);
|
||||||
|
|
||||||
|
v.remove(0);
|
||||||
|
printInts(v);
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue