Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pop_back function for vector on CppRuntime_Clang #16

Open
wants to merge 1 commit into
base: v1.x.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions source/stdcpp/allocator.d
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ extern(D):

///
enum size_t max_size = size_t.max / T.sizeof;

void __destroy(pointer __p)
{
destroy!false(__p);
}
}
else
{
Expand Down Expand Up @@ -266,6 +271,14 @@ struct allocator_traits(Alloc)
else
return a;
}

version (CppRuntime_Clang)
{
static void Destroy(ref Alloc alloc, pointer __p)
{
alloc.__destroy(__p);
}
}
}

private:
Expand Down
6 changes: 6 additions & 0 deletions source/stdcpp/test/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ else version (CppRuntime_Clang)
vec3.swap(vec);
assert(vec3.size == 9); // after swap
assert(vec.size == 7);
vec.pop_back();
assert(vec.size == 6);
vec.pop_back();
assert(vec.size == 5);
vec.pop_back();
assert(vec.size == 4);
}
}
34 changes: 30 additions & 4 deletions source/stdcpp/vector.d
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,6 @@ extern(D):
}
else version (CppRuntime_Clang)
{
pointer __begin_ = null;
pointer __end_ = null;
auto __end_cap_ = __compressed_pair!(pointer, allocator_type)(null, __default_init_tag());

inout(T)[] as_array() inout pure nothrow @trusted @nogc {return this.__begin_[0 .. size()];}

inout(value_type)* data() inout nothrow
Expand Down Expand Up @@ -908,6 +904,36 @@ extern(D):
}
///
extern(C++) void swap(ref vector x);
///
void pop_back()
{
assert(!empty(), "vector.pop_back called on an empty container");
this.__destruct_at_end(this.__end_ -1);
}

private:
pointer __begin_;
pointer __end_;
auto __end_cap_ = __compressed_pair!(pointer, allocator_type)(null, __default_init_tag());

extern(C++) ref inout(allocator_type) __alloc() inout nothrow
{
return this.__end_cap_.second();
}

void __base_destruct_at_end(pointer __new_last) nothrow
{
pointer __soon_to_be_end = this.__end_;
while (__new_last != __soon_to_be_end)
allocator_traits!(Alloc).Destroy(__alloc(), --__soon_to_be_end);
this.__end_ = __new_last;
}

void __destruct_at_end(pointer __new_last) nothrow
{
__base_destruct_at_end(__new_last);
// Todo: add an ASAN annotations usage of unused capacity
}
}
}

Expand Down
Loading