CopyWithCheck method for arrays

Moderator: GZDoom Developers

Post Reply
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

CopyWithCheck method for arrays

Post by Apeirogon »

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 all

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]);
}

_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: CopyWithCheck method for arrays

Post by _mental_ »

Usually, this operation is called union. But why do you want it in the engine? What’s wrong with its implementation in ZScript?
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Re: CopyWithCheck method for arrays

Post by Apeirogon »

Loop inside loop inside loop inside another loop of a loop is looks bad IMO.

Code: Select all

    //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]);
                
                }

_mental_
 
 
Posts: 3812
Joined: Sun Aug 07, 2011 4:32 am

Re: CopyWithCheck method for arrays

Post by _mental_ »

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).
User avatar
Graf Zahl
Lead GZDoom+Raze Developer
Lead GZDoom+Raze Developer
Posts: 49067
Joined: Sat Jul 19, 2003 10:19 am
Location: Germany

Re: CopyWithCheck method for arrays

Post by Graf Zahl »

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.
Post Reply

Return to “Closed Feature Suggestions [GZDoom]”