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

Primitive iterator looses sync after too many elements are removed #481

Closed
alexo134 opened this issue Mar 16, 2018 · 2 comments
Closed

Primitive iterator looses sync after too many elements are removed #481

alexo134 opened this issue Mar 16, 2018 · 2 comments
Assignees

Comments

@alexo134
Copy link

Consider this code:

MutableIntSet s = IntSets.mutable.of();

int max = 100000;

for(int i = 0; i < max; i++) {
    s.add(i);
}

MutableIntIterator iterator = s.intIterator();

while (iterator.hasNext()) {
    // remove 2/3 of elements
    if (iterator.next() < ( max * 2 / 3)) {
        iterator.remove();
    }
}

this will cause this exception:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 262144
	at org.eclipse.collections.impl.set.mutable.primitive.IntHashSet$InternalIntIterator.next(IntHashSet.java:1624)

It works for smaller numbers of elements like max = 1k or 10k but starts to fail for 100k and more.

And I found the cause:
InternalIntIterator.remove() calls the standard methods IntHashSet.remove(); which does rehashing when there is too many entries occupied with sentinels.

...
if (this.occupiedWithSentinels > this.maxOccupiedWithSentinels()) {
    this.rehash();
}
...

and this makes iterator invalid.

@nikhilnanivadekar
Copy link
Contributor

@alexo134 thanks for reporting this!
We will take a look and come back on this issue.

@nikhilnanivadekar nikhilnanivadekar self-assigned this Mar 19, 2018
nikhilnanivadekar added a commit to nikhilnanivadekar/eclipse-collections that referenced this issue Mar 19, 2018
…iterator().remove(). Fixes eclipse#481

Signed-off-by: Nikhil Nanivadekar <nikhil.nanivadekar@gs.com>
nikhilnanivadekar added a commit to nikhilnanivadekar/eclipse-collections that referenced this issue Mar 19, 2018
…iterator().remove(). Fixes eclipse#481

Signed-off-by: Nikhil Nanivadekar <nikhil.nanivadekar@gs.com>
@nikhilnanivadekar
Copy link
Contributor

nikhilnanivadekar commented Mar 19, 2018

@alexo134 raised a #484 with a fix. Once merged it will be available with 9.2.0 release which is slated for 04/18: https://projects.eclipse.org/projects/technology.collections/releases/9.2.0

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

No branches or pull requests

2 participants