diff --git a/gs-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg b/gs-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg index fbbf03aee..027e29fbc 100644 --- a/gs-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg +++ b/gs-collections-code-generator/src/main/resources/impl/set/mutable/primitiveHashSet.stg @@ -289,14 +289,7 @@ public final class HashSet extends AbstractSet implements Mutable); - if (this.zeroToThirtyOne == initial) - { - return false; - } - this.zeroToThirtyOneOccupied--; - return true; + return this.removeZeroToThirtyOne(value); } int index = this.probe(value); if (<(equals.(type))("this.table[index]", "value")>) @@ -318,6 +311,18 @@ public final class HashSet extends AbstractSet implements Mutable value) + { + int initial = this.zeroToThirtyOne; + this.zeroToThirtyOne &= ~(1 \<\< <(castRealTypeToInt.(type))("value")>); + if (this.zeroToThirtyOne == initial) + { + return false; + } + this.zeroToThirtyOneOccupied--; + return true; + } + public boolean removeAll(Iterable source) { if (source.isEmpty()) @@ -1606,32 +1611,46 @@ public final class HashSet extends AbstractSet implements Mutable removeValue; - if (this.zeroToThirtyOne \<= <(literal.(type))("32")> && this.position == 0) - { - if (HashSet.this.zeroToThirtyOne != (HashSet.this.zeroToThirtyOne | 1 \<\< (<(castRealTypeToInt.(type))("this.zeroToThirtyOne")> - 1))) - { - throw new IllegalStateException(); - } - removeValue = <(castIntToNarrowTypeWithParens.(type))("this.zeroToThirtyOne - 1")>; - } - else if (<(equals.(type))({HashSet.this.table[this.position - 1]}, "REMOVED")>) - { - throw new IllegalStateException(); - } - else - { - removeValue = HashSet.this.table[this.position - 1]; - } - HashSet.this.remove(removeValue); - this.count--; - } + public void remove() + { + if (this.count == 0) + { + throw new IllegalStateException(); + } + removeValue; + if (this.zeroToThirtyOne \<= <(literal.(type))("32")> && this.position == 0) + { + if (HashSet.this.zeroToThirtyOne != (HashSet.this.zeroToThirtyOne | 1 \<\< (<(castRealTypeToInt.(type))("this.zeroToThirtyOne")> - 1))) + { + throw new IllegalStateException(); + } + removeValue = <(castIntToNarrowTypeWithParens.(type))("this.zeroToThirtyOne - 1")>; + } + else if (<(equals.(type))({HashSet.this.table[this.position - 1]}, "REMOVED")>) + { + throw new IllegalStateException(); + } + else + { + removeValue = HashSet.this.table[this.position - 1]; + } + if (HashSet.isBetweenZeroAndThirtyOne(removeValue)) + { + HashSet.this.removeZeroToThirtyOne(removeValue); + } + else if (<(equals.(type))({HashSet.this.table[this.position - 1]}, "removeValue")>) + { + if (HashSet.this.copyOnWrite) + { + HashSet.this.copyTable(); + } + HashSet.this.table[position -1] = REMOVED; + HashSet.this.occupiedWithData--; + HashSet.this.occupiedWithSentinels++; + } + + this.count--; + } } } @@ -1716,4 +1735,4 @@ public sum() return result; } ->> \ No newline at end of file +>> diff --git a/gs-collections-code-generator/src/main/resources/test/set/mutable/primitiveHashSetTest.stg b/gs-collections-code-generator/src/main/resources/test/set/mutable/primitiveHashSetTest.stg index 9baff7040..2fa2d5298 100644 --- a/gs-collections-code-generator/src/main/resources/test/set/mutable/primitiveHashSetTest.stg +++ b/gs-collections-code-generator/src/main/resources/test/set/mutable/primitiveHashSetTest.stg @@ -8,16 +8,18 @@ targetPath() ::= "com/gs/collections/impl/set/mutable/primitive" fileName(primitive) ::= "HashSetTest" class(primitive) ::= << - + >> -body(type, wrapperName, name) ::= << +body(type, wrapperName, name, charPrimitive, shortPrimitive) ::= << package com.gs.collections.impl.set.mutable.primitive; import java.lang.reflect.Field; +import com.gs.collections.api.iterator.MutableIterator; +import com.gs.collections.api.set.primitive.MutableSet; import com.gs.collections.impl.factory.primitive.Sets; import com.gs.collections.impl.list.mutable.primitive.ArrayList; import com.gs.collections.impl.test.Verify; @@ -177,6 +179,39 @@ public class HashSetTest extends AbstractSetTestCase Assert.assertEquals(0, occupiedWithSentinels.get(hashSet)); } + @Test + public void iterator_remove() + { + MutableSet set1 = Sets.mutable.empty(); + + int max = 65_536max = 10max = 100_000; + for (Integer i = 0; i \< max; i++) + { + set1.add(i.toString().charAt(0));set1.add(i.Value()); + } + + // set2 to verify copyTable() + MutableSet set2 = Sets.mutable.withAll(set1); + set2.freeze(); + + this.assertIteratorRemove(set1, max); + this.assertIteratorRemove(set2, max); + } + + private void assertIteratorRemove(MutableSet set, int max) + { + Verify.assertSize(max, set); + MutableIterator iterator = set.Iterator(); + Verify.assertThrows(IllegalStateException.class, () -> iterator.remove()); + + while (iterator.hasNext()) + { + iterator.next(); + iterator.remove(); + Verify.assertSize(--max, set); + } + } + @Test public void addEverySlot() {