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

Is it necessary to wipe in removeIf? #1723

Open
jmgimeno opened this issue Oct 24, 2024 · 0 comments
Open

Is it necessary to wipe in removeIf? #1723

jmgimeno opened this issue Oct 24, 2024 · 0 comments

Comments

@jmgimeno
Copy link

In the removeIf of primitive types, the template zeroes the remaining of the list.
Is it really necessary to do so?

    @Override
    public boolean removeIf(<name>Predicate predicate)
    {
        int currentFilledIndex = 0;
        for (int i = 0; i \< this.size; i++)
        {
            <type> item = this.items[i];
            if (!predicate.accept(item))
            {
                // keep it
                if (currentFilledIndex != i)
                {
                    this.items[currentFilledIndex] = item;
                }
                currentFilledIndex++;
            }
        }
        boolean changed = currentFilledIndex \< this.size;
        this.wipeAndResetTheEnd(currentFilledIndex);
        return changed;
    }

    private void wipeAndResetTheEnd(int newCurrentFilledIndex)
    {
        for (int i = newCurrentFilledIndex; i \< this.size; i++)
        {
            this.items[i] = <zero.(type)>;
        }
        this.size = newCurrentFilledIndex;
    }
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

No branches or pull requests

1 participant