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

Backport PrimitiveHashSet#iterator()#remove() to not rehash. #37

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 50 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,54 @@
language: java
jdk:
- oraclejdk8
dist: trusty
sudo: false
addons:
apt:
packages:
- oracle-java8-installer

before_install:
- cat /etc/mavenrc
- sudo rm /etc/mavenrc
- export MAVEN_OPTS="-Dmaven.repo.local=$HOME/.m2/repository -Xmx2g -Dorg.slf4j.simpleLogger.showDateTime=true -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS"
- wget https://archive.apache.org/dist/maven/maven-3/3.5.0/binaries/apache-maven-3.5.0-bin.zip
- unzip -qq apache-maven-3.5.0-bin.zip
- export M2_HOME=$PWD/apache-maven-3.5.0
- export PATH=$M2_HOME/bin:$PATH
- echo "MAVEN_OPTS='-Dmaven.repo.local=$HOME/.m2/repository -Xmx1g -Dorg.slf4j.simpleLogger.showDateTime=true -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss:SSS'" > ~/.mavenrc

install:
-

matrix:
fast_finish: true
include:
- jdk: oraclejdk8
env:
- DESC="acceptance tests"
- JDK=Java8
- CMD="mvn install --projects acceptance-tests --also-make --activate-profiles all --batch-mode --show-version --errors"

- jdk: oraclejdk8
env:
- DESC="findbugs"
- JDK=Java8
- CMD="mvn install findbugs:check --projects '!scala-unit-tests,!jmh-scala-tests,!jmh-tests,!p2-repository' --activate-profiles all -DskipTests=true --batch-mode --show-version --errors"

- jdk: oraclejdk8
env:
- DESC="checkstyle"
- JDK=Java8
- CMD="mvn install checkstyle:check --activate-profiles all -DskipTests=true --batch-mode --show-version --errors"

- jdk: oraclejdk8
env:
- DESC="unit tests"
- JDK=Java8
- CMD="mvn install --batch-mode --show-version --errors"

- jdk: oraclejdk8
env:
- DESC="compile jmh-tests and performance-tests"
- JDK=Java8
- CMD="mvn install --projects jmh-tests,performance-tests --also-make --activate-profiles all -DskipTests=true --batch-mode --show-version --errors"

cache:
directories:
- ~/.m2
Original file line number Diff line number Diff line change
Expand Up @@ -289,14 +289,7 @@ public final class <name>HashSet extends Abstract<name>Set implements Mutable<na
{
if (isBetweenZeroAndThirtyOne(value))
{
int initial = this.zeroToThirtyOne;
this.zeroToThirtyOne &= ~(1 \<\< <(castRealTypeToInt.(type))("value")>);
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")>)
Expand All @@ -318,6 +311,18 @@ public final class <name>HashSet extends Abstract<name>Set implements Mutable<na
return false;
}

private boolean removeZeroToThirtyOne(<type> value)
{
int initial = this.zeroToThirtyOne;
this.zeroToThirtyOne &= ~(1 \<\< <(castRealTypeToInt.(type))("value")>);
if (this.zeroToThirtyOne == initial)
{
return false;
}
this.zeroToThirtyOneOccupied--;
return true;
}

public boolean removeAll(<name>Iterable source)
{
if (source.isEmpty())
Expand Down Expand Up @@ -1606,32 +1611,46 @@ public final class <name>HashSet extends Abstract<name>Set implements Mutable<na
return result;
}

public void remove()
{
if (this.count == 0)
{
throw new IllegalStateException();
}
<type> removeValue;
if (this.zeroToThirtyOne \<= <(literal.(type))("32")> && this.position == 0)
{
if (<name>HashSet.this.zeroToThirtyOne != (<name>HashSet.this.zeroToThirtyOne | 1 \<\< (<(castRealTypeToInt.(type))("this.zeroToThirtyOne")> - 1)))
{
throw new IllegalStateException();
}
removeValue = <(castIntToNarrowTypeWithParens.(type))("this.zeroToThirtyOne - 1")>;
}
else if (<(equals.(type))({<name>HashSet.this.table[this.position - 1]}, "REMOVED")>)
{
throw new IllegalStateException();
}
else
{
removeValue = <name>HashSet.this.table[this.position - 1];
}
<name>HashSet.this.remove(removeValue);
this.count--;
}
public void remove()
{
if (this.count == 0)
{
throw new IllegalStateException();
}
<type> removeValue;
if (this.zeroToThirtyOne \<= <(literal.(type))("32")> && this.position == 0)
{
if (<name>HashSet.this.zeroToThirtyOne != (<name>HashSet.this.zeroToThirtyOne | 1 \<\< (<(castRealTypeToInt.(type))("this.zeroToThirtyOne")> - 1)))
{
throw new IllegalStateException();
}
removeValue = <(castIntToNarrowTypeWithParens.(type))("this.zeroToThirtyOne - 1")>;
}
else if (<(equals.(type))({<name>HashSet.this.table[this.position - 1]}, "REMOVED")>)
{
throw new IllegalStateException();
}
else
{
removeValue = <name>HashSet.this.table[this.position - 1];
}
if (<name>HashSet.isBetweenZeroAndThirtyOne(removeValue))
{
<name>HashSet.this.removeZeroToThirtyOne(removeValue);
}
else if (<(equals.(type))({<name>HashSet.this.table[this.position - 1]}, "removeValue")>)
{
if (<name>HashSet.this.copyOnWrite)
{
<name>HashSet.this.copyTable();
}
<name>HashSet.this.table[position -1] = REMOVED;
<name>HashSet.this.occupiedWithData--;
<name>HashSet.this.occupiedWithSentinels++;
}

this.count--;
}
}
}

Expand Down Expand Up @@ -1716,4 +1735,4 @@ public <wideType.(type)> sum()
return result;
}

>>
>>
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ targetPath() ::= "com/gs/collections/impl/set/mutable/primitive"
fileName(primitive) ::= "<primitive.name>HashSetTest"

class(primitive) ::= <<
<body(primitive.type, primitive.wrapperName, primitive.name)>
<body(primitive.type, primitive.wrapperName, primitive.name, primitive.charPrimitive, primitive.shortPrimitive)>
>>

body(type, wrapperName, name) ::= <<
body(type, wrapperName, name, charPrimitive, shortPrimitive) ::= <<
<copyright()>

package com.gs.collections.impl.set.mutable.primitive;

import java.lang.reflect.Field;

import com.gs.collections.api.iterator.Mutable<name>Iterator;
import com.gs.collections.api.set.primitive.Mutable<name>Set;
import com.gs.collections.impl.factory.primitive.<name>Sets;
import com.gs.collections.impl.list.mutable.primitive.<name>ArrayList;
import com.gs.collections.impl.test.Verify;
Expand Down Expand Up @@ -177,6 +179,39 @@ public class <name>HashSetTest extends Abstract<name>SetTestCase
Assert.assertEquals(0, occupiedWithSentinels.get(hashSet));
}

@Test
public void iterator_remove()
{
Mutable<name>Set set1 = <name>Sets.mutable.empty();

int <if(shortPrimitive)>max = 65_536<elseif(charPrimitive)>max = 10<else>max = 100_000<endif>;
for (Integer i = 0; i \< max; i++)
{
<if(charPrimitive)>set1.add(i.toString().charAt(0));<else>set1.add(i.<type>Value());<endif>
}

// set2 to verify copyTable()
Mutable<name>Set set2 = <name>Sets.mutable.withAll(set1);
set2.freeze();

this.assertIteratorRemove(set1, max);
this.assertIteratorRemove(set2, max);
}

private void assertIteratorRemove(Mutable<name>Set set, int max)
{
Verify.assertSize(max, set);
Mutable<name>Iterator iterator = set.<type>Iterator();
Verify.assertThrows(IllegalStateException.class, () -> iterator.remove());

while (iterator.hasNext())
{
iterator.next();
iterator.remove();
Verify.assertSize(--max, set);
}
}

@Test
public void addEverySlot()
{
Expand Down