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

Improve Container Types #180

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Improve Container Types #180

wants to merge 8 commits into from

Conversation

Mozz3d
Copy link
Contributor

@Mozz3d Mozz3d commented Feb 7, 2025

No description provided.

include/RED4ext/Containers/DynArray.hpp Outdated Show resolved Hide resolved
include/RED4ext/Containers/DynArray.hpp Outdated Show resolved Hide resolved
include/RED4ext/Containers/DynArray.hpp Outdated Show resolved Hide resolved
include/RED4ext/Containers/DynArray.hpp Outdated Show resolved Hide resolved
include/RED4ext/Containers/DynArray.hpp Outdated Show resolved Hide resolved
include/RED4ext/Containers/DynArray.hpp Show resolved Hide resolved
include/RED4ext/Containers/Span.hpp Outdated Show resolved Hide resolved
include/RED4ext/Detail/Containers/ArrayIterator.hpp Outdated Show resolved Hide resolved
Comment on lines +81 to +90
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)
Copy link
Owner

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.

Copy link
Contributor Author

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

Copy link
Owner

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)

?

Copy link
Contributor Author

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

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@WSSDude WSSDude Feb 12, 2025

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)

Comment on lines +300 to +301
if (capacity)
std::destroy(begin(), end());
Copy link
Owner

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)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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()));
Copy link
Owner

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.

Comment on lines +129 to +135
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()));
}
Copy link
Owner

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.

Comment on lines +161 to +165
if constexpr (std::is_trivially_copyable_v<ValueType>)
{
auto begin = *aBegin;
std::memcpy(Data(), &begin, newSize * sizeof(ValueType));
}
Copy link
Owner

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.

Copy link
Collaborator

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.

Comment on lines +261 to +263
if (aSize > Capacity())
Reserve(aSize);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (aSize > Capacity())
Reserve(aSize);
if (aSize > Capacity())
{
Reserve(aSize);
}

Comment on lines +494 to +496
T* entries; // 00
uint32_t capacity; // 08
uint32_t size; // 0C
Copy link
Owner

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));
Copy link
Owner

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.

Copy link
Contributor Author

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?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

Comment on lines +101 to +123
[[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];
}
Copy link
Owner

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

@psiberx psiberx Feb 11, 2025

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?

Copy link
Owner

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.

Copy link
Collaborator

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.

Copy link
Collaborator

@WSSDude WSSDude Feb 12, 2025

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.

Copy link
Collaborator

@psiberx psiberx Feb 12, 2025

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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants