[DIY] CopyWithCheck method for arrays

Moderator: GZDoom Developers

CopyWithCheck method for arrays

Postby Apeirogon » Sun Nov 24, 2019 2:21 pm

As you can expect from title, method which copy content of one array to another, but only if content of copied array does not exist in another.

Basically this
Code: Select allExpand view
array<actor> one;
array<actor> another;

fill arrays somewhere here

for(uint i = 0; i < another.size(); i++)
{
    if(one.find(another[i]) == one.size() )
        one.push(another[i]);
}

User avatar
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: CopyWithCheck method for arrays

Postby _mental_ » Sun Nov 24, 2019 2:43 pm

Usually, this operation is called union. But why do you want it in the engine? What’s wrong with its implementation in ZScript?
_mental_
 
 
 
Joined: 07 Aug 2011

Re: CopyWithCheck method for arrays

Postby Apeirogon » Sun Nov 24, 2019 2:51 pm

Loop inside loop inside loop inside another loop of a loop is looks bad IMO.
Code: Select allExpand view
    //check every line in sector
    for(uint i = 0; i < own_sector.lines.size(); i++)
    {
        line beginning = own_sector.lines[i];
        processed_lines.push(beginning);

        bool all_lines = false;

        while(all_lines == false)
        {
            for(uint j = 0; j < processed_lines.size(); j++)
            {
                line_array_wrapper neighborgs = neighborn_lines(processed_lines[j]);

                uint lines_amount = processed_lines.size();

                for(uint k = 0; k < neighborgs.lines_array.size(); k++)
                {
                    if(processed_lines.find(neighborgs.lines_array[k]) == processed_lines.size() )
                        processed_lines.push(neighborgs.lines_array[k]);
               
                }

User avatar
Apeirogon
I have a strange sense of humour
 
Joined: 12 Jun 2017

Re: CopyWithCheck method for arrays

Postby _mental_ » Mon Nov 25, 2019 12:05 am

If it’s about readability, create a function. If it’s about performance, native version will have the same loop, and it cannot be significantly faster (at least without knowing some specific details about those arrays).
_mental_
 
 
 
Joined: 07 Aug 2011

Re: CopyWithCheck method for arrays

Postby Graf Zahl » Mon Nov 25, 2019 1:04 am

In this particular case the native function will indeed not be significantly faster than the JITed version. And TBH, this particular use case is not a frequent one.
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
 
Joined: 19 Jul 2003
Location: Germany


Return to Closed Feature Suggestions

Who is online

Users browsing this forum: No registered users and 1 guest