eclipse-collections icon indicating copy to clipboard operation
eclipse-collections copied to clipboard

Is it necessary to wipe in removeIf?

Open jmgimeno opened this issue 1 year ago • 0 comments

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

jmgimeno avatar Oct 24 '24 17:10 jmgimeno