Skip to content

Commit

Permalink
Optimize any/all/noneSatisfy on UnifiedMapWithHashingStrategy - closes
Browse files Browse the repository at this point in the history
  • Loading branch information
Desislav-Petrov committed Oct 17, 2022
1 parent 6fbb964 commit 948d416
Showing 1 changed file with 113 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.eclipse.collections.api.block.function.Function;
import org.eclipse.collections.api.block.function.Function0;
import org.eclipse.collections.api.block.function.Function2;
import org.eclipse.collections.api.block.predicate.Predicate;
import org.eclipse.collections.api.block.predicate.Predicate2;
import org.eclipse.collections.api.block.procedure.Procedure;
import org.eclipse.collections.api.block.procedure.Procedure2;
import org.eclipse.collections.api.block.procedure.primitive.ObjectIntProcedure;
Expand Down Expand Up @@ -1711,6 +1713,117 @@ else if (this.table[i] != null)
return target;
}

private boolean shortCircuit(
Predicate<? super V> predicate,
boolean expected,
boolean onShortCircuit,
boolean atEnd)
{
for (int i = 0; i < this.table.length; i += 2)
{
if (this.table[i] == CHAINED_KEY)
{
Object[] chainedTable = (Object[]) this.table[i + 1];
for (int j = 0; j < chainedTable.length; j += 2)
{
if (chainedTable[j] != null)
{
V value = (V) chainedTable[j + 1];
if (predicate.accept(value) == expected)
{
return onShortCircuit;
}
}
}
}
else if (this.table[i] != null)
{
V value = (V) this.table[i + 1];

if (predicate.accept(value) == expected)
{
return onShortCircuit;
}
}
}

return atEnd;
}

private <P> boolean shortCircuitWith(
Predicate2<? super V, ? super P> predicate,
P parameter,
boolean expected,
boolean onShortCircuit,
boolean atEnd)
{
for (int i = 0; i < this.table.length; i += 2)
{
if (this.table[i] == CHAINED_KEY)
{
Object[] chainedTable = (Object[]) this.table[i + 1];
for (int j = 0; j < chainedTable.length; j += 2)
{
if (chainedTable[j] != null)
{
V value = (V) chainedTable[j + 1];
if (predicate.accept(value, parameter) == expected)
{
return onShortCircuit;
}
}
}
}
else if (this.table[i] != null)
{
V value = (V) this.table[i + 1];

if (predicate.accept(value, parameter) == expected)
{
return onShortCircuit;
}
}
}

return atEnd;
}

@Override
public boolean anySatisfy(Predicate<? super V> predicate)
{
return this.shortCircuit(predicate, true, true, false);
}

@Override
public <P> boolean anySatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.shortCircuitWith(predicate, parameter, true, true, false);
}

@Override
public boolean allSatisfy(Predicate<? super V> predicate)
{
return this.shortCircuit(predicate, false, false, true);
}

@Override
public <P> boolean allSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.shortCircuitWith(predicate, parameter, false, false, true);
}

@Override
public boolean noneSatisfy(Predicate<? super V> predicate)
{
return this.shortCircuit(predicate, true, false, true);
}

@Override
public <P> boolean noneSatisfyWith(Predicate2<? super V, ? super P> predicate, P parameter)
{
return this.shortCircuitWith(predicate, parameter, true, false, true);
}

protected class KeySet implements Set<K>, Serializable, BatchIterable<K>
{
private static final long serialVersionUID = 1L;
Expand Down

0 comments on commit 948d416

Please sign in to comment.