mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 14:21:16 +00:00
Changed C++'s SharedVector::clear() to not preserve capacity when shared
This commit is contained in:
parent
d65af654d7
commit
a752c798b8
2 changed files with 8 additions and 6 deletions
|
@ -130,10 +130,9 @@ struct SharedVector
|
||||||
/// Clears the vector and removes all elements. The capacity remains unaffected.
|
/// Clears the vector and removes all elements. The capacity remains unaffected.
|
||||||
void clear()
|
void clear()
|
||||||
{
|
{
|
||||||
// Detach first to retain capacity, so that the begin() call doesn't detach
|
if (inner->refcount != 1) {
|
||||||
// (which it would to inner->size instead of capacity)
|
*this = SharedVector();
|
||||||
detach(inner->capacity);
|
} else {
|
||||||
{
|
|
||||||
auto b = begin(), e = end();
|
auto b = begin(), e = end();
|
||||||
inner->size = 0;
|
inner->size = 0;
|
||||||
for (auto it = b; it < e; ++it) {
|
for (auto it = b; it < e; ++it) {
|
||||||
|
|
|
@ -100,6 +100,7 @@ TEST_CASE("SharedVector")
|
||||||
using namespace sixtyfps;
|
using namespace sixtyfps;
|
||||||
|
|
||||||
SharedVector<SharedString> vec;
|
SharedVector<SharedString> vec;
|
||||||
|
vec.clear();
|
||||||
vec.push_back("Hello");
|
vec.push_back("Hello");
|
||||||
vec.push_back("World");
|
vec.push_back("World");
|
||||||
vec.push_back("of");
|
vec.push_back("of");
|
||||||
|
@ -112,11 +113,13 @@ TEST_CASE("SharedVector")
|
||||||
REQUIRE(orig_cap >= vec.size());
|
REQUIRE(orig_cap >= vec.size());
|
||||||
vec.clear();
|
vec.clear();
|
||||||
REQUIRE(vec.size() == 0);
|
REQUIRE(vec.size() == 0);
|
||||||
REQUIRE(vec.capacity() == orig_cap);
|
REQUIRE(vec.capacity() == 0); // vec was shared, so start with new empty vector.
|
||||||
vec.push_back("Welcome back");
|
vec.push_back("Welcome back");
|
||||||
REQUIRE(vec.size() == 1);
|
REQUIRE(vec.size() == 1);
|
||||||
REQUIRE(vec.capacity() == orig_cap);
|
REQUIRE(vec.capacity() >= vec.size());
|
||||||
|
|
||||||
REQUIRE(copy.size() == 4);
|
REQUIRE(copy.size() == 4);
|
||||||
REQUIRE(copy.capacity() == orig_cap);
|
REQUIRE(copy.capacity() == orig_cap);
|
||||||
|
copy.clear(); // copy is not shared (anymore), retain capacity.
|
||||||
|
REQUIRE(copy.capacity() == orig_cap);
|
||||||
}
|
}
|
Loading…
Add table
Add a link
Reference in a new issue