-
Notifications
You must be signed in to change notification settings - Fork 37
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
Improve Container Types #180
base: master
Are you sure you want to change the base?
Conversation
template<ContainerType TContainer> | ||
DynArray(const TContainer& aContainer, Memory::IAllocator* aAllocator = nullptr) | ||
: DynArray(aAllocator) | ||
{ | ||
Assign(aContainer.begin(), aContainer.end()); | ||
} | ||
|
||
template<ContainerType TContainer> | ||
DynArray(TContainer&& aContainer, Memory::IAllocator* aAllocator = nullptr) | ||
: DynArray(aAllocator) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a valid use case for this? I can't think of any, and you will have to handle special cases, such as Map
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I gave some of my reasons in discord, but yes I think so. Also the template using the ContainerType
concept to enforce what exactly can be used here, so things like Map
aren't a concern afaict
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not do something like:
template<typename InputIt>
requires std::input_iterator<InputIt>
DynArray(InputIt aFirst, InputIt aLast, Memory::IAllocator* aAllocator = nullptr)
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because I have a habit of over-complicating things when I haven't slept :)
Thank you
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You dont have to use requires in that case, just template <std::input_iterator InputIt>
is enough, maybe can be even included in the signature itself, getting rid of the template keyword alltogether if you do not need to do something with the type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And looking at the code now, think you should be able to do just that.
DynArray(std::input_iterator auto aFirst, std::input_iterator auto aLast, Memory::IAllocator* aAllocator = nullptr)
Unsure if it is wanted to be passed by value though, need to look at the implementation in full finally... If you need to pass by reference in the simplified case, just put & after the auto in the above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can actually do sth like that in multiple places from quick glance, you dont seem to be accessing the type explicitly in many of them. Using template keyword in such cases is a bit of a waste.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, rereading my comments, you may not want to do the simplified case without template in the end here as it would match 2 distinct input iterators on input... Still could be done in some other places.
This variant should be used here to ensure same type. #180 (comment)
if (capacity) | ||
std::destroy(begin(), end()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for the check. std::destory
is well defined in this case.
This is more like a comment, not a request for change, so if you want feel free to keep it.
Assign(aItems); | ||
} | ||
|
||
DynArray(uint32_t aSize, Memory::IAllocator* aAllocator = nullptr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DynArray(uint32_t aSize, Memory::IAllocator* aAllocator = nullptr) | |
DynArray(SizeType aSize, Memory::IAllocator* aAllocator = nullptr) |
{ | ||
if (this != std::addressof(aOther)) | ||
{ | ||
Assign(std::make_move_iterator(aOther.begin()), std::make_move_iterator(aOther.end())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is having a complexity of O(n)
, Before it was O(1)
, do the swap trick.
template<ContainerType TContainer> | ||
DynArray& operator=(TContainer&& aOther) | ||
{ | ||
if (this != std::addressof(aOther)) | ||
{ | ||
Assign(std::make_move_iterator(aOther.begin()), std::make_move_iterator(aOther.end())); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, no usecase for this.
if constexpr (std::is_trivially_copyable_v<ValueType>) | ||
{ | ||
auto begin = *aBegin; | ||
std::memcpy(Data(), &begin, newSize * sizeof(ValueType)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never do a memcpy
or memmove
. You might have pointers in the object that can point to itself, or to another variable in the object. Doing a copy
you will have undefined behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Never is a strong word I would say but yes, probably not good for collections. If copy is needed, std::copy family or ranges version of them should be used.
Which may end up as std::memcopy or memmove depending on underlying type. But it does call moves etc. when type requires it.
if (aSize > Capacity()) | ||
Reserve(aSize); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (aSize > Capacity()) | |
Reserve(aSize); | |
if (aSize > Capacity()) | |
{ | |
Reserve(aSize); | |
} |
T* entries; // 00 | ||
uint32_t capacity; // 08 | ||
uint32_t size; // 0C |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be private.
|
||
if constexpr (std::is_trivially_copyable_v<T>) | ||
{ | ||
std::memmove(aDst, aSrc, aCount * sizeof(ValueType)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to the previous memmove
comment. memcopy
and memmove
are C constructs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you suggesting I change this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
[[nodiscard]] constexpr Reference Front() | ||
{ | ||
assert(!Empty()); | ||
return Data()[0]; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstReference Front() const | ||
{ | ||
assert(!Empty()); | ||
return Data()[0]; | ||
} | ||
|
||
[[nodiscard]] constexpr Reference Back() | ||
{ | ||
assert(!Empty()); | ||
return Data()[Size() - 1]; | ||
} | ||
|
||
[[nodiscard]] constexpr ConstReference Back() const | ||
{ | ||
assert(!Empty()); | ||
return Data()[Size() - 1]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imo, things like begin
, front
, etc. should only be STL style to not have duplicated. Wdyt?
Same for DynArray, Span.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah definitely + from me on this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on what you mean a little? Im not sure if you're referring to just casing or something else aswell
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be either STL or our style (STL conformant or not), but not both, with only exceptions for compliance with named requirements, range-based for loop, etc. If we want to keep our naming, then it should be Size()
, Begin()
, Front()
, etc. Things like begin()
and end()
should be in a separate "compliance" region, not mixed in with the rest. What's the purpose of lowercase front()
btw?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think lowercase front
has any purpose, but I made a typo, I wanted to say being
, end
, etc. Basically, everything that is needed for range-based loop.
For these requirements, I would say to just use the lowercase (STL variant) naming, instead of having both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep Begin
and End
for consistent naming (you still need to use them explicitly) and not consider begin
and end
as part of our interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont know what you mean by consistent naming now - consistent with what? Other functions? Other collections not touched by this (if there are any)?
I would rather not have 2 functions doing the same thing personally, even if it would cause some function ToBeLikeThis and some like_this following std.
Mainly when they should be compatible with it, 2 of anything unnecessarily is a waste in my eyes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistent across the class at least, ideally across the project. begin
and end
should not be considered functions of a class, but unfortunately C++ doesn't have a way to handle it properly. For example, C# supports explicit interface implementation that would hide begin
and end
from class.
Begin
and begin
have different purposes, and that's what matters, how they're implemented is irrelevant. begin
exists to support range-based for loop, for everything else there's Begin
.
} | ||
|
||
template<ContainerType TContainer> | ||
DynArray(const TContainer& aContainer, Memory::IAllocator* aAllocator = nullptr) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No description provided.