Skip to content

Commit

Permalink
jgalgo-core: use fastutil-core insteand of fastutil
Browse files Browse the repository at this point in the history
  • Loading branch information
barakugav committed Dec 27, 2023
1 parent 3a7fe5b commit 86d55fd
Show file tree
Hide file tree
Showing 31 changed files with 280 additions and 321 deletions.
2 changes: 1 addition & 1 deletion jgalgo-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependencies>
<dependency>
<groupId>it.unimi.dsi</groupId>
<artifactId>fastutil</artifactId>
<artifactId>fastutil-core</artifactId>
<version>${fastutil.version}</version>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import com.jgalgo.graph.IntGraph;
import com.jgalgo.internal.util.Bitmap;
import com.jgalgo.internal.util.IntAdapters;
import it.unimi.dsi.fastutil.booleans.BooleanList;
import it.unimi.dsi.fastutil.ints.IntIterator;
import it.unimi.dsi.fastutil.ints.IntList;

Expand Down Expand Up @@ -148,7 +147,7 @@ static <V, E> boolean isEulerianTour(Graph<V, E> g, List<E> tour) {
} else {
int firstEdge = tour0.getInt(0);
boolean foundValidStartingEndpoint = false;
startingEndpointLoop: for (boolean startingEndpoint : BooleanList.of(true, false)) {
startingEndpointLoop: for (boolean startingEndpoint : new boolean[] { true, false }) {
visited.clear();
int u = startingEndpoint ? ig.edgeSource(firstEdge) : ig.edgeTarget(firstEdge);
IntIterator it = tour0.iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
*/
package com.jgalgo.alg;

import java.util.Map;
import java.util.function.IntPredicate;
import com.jgalgo.graph.IndexGraph;
import com.jgalgo.graph.IndexIntIdMap;
import com.jgalgo.graph.IntGraph;
import com.jgalgo.internal.util.Bitmap;
import it.unimi.dsi.fastutil.ints.Int2BooleanMap;
import it.unimi.dsi.fastutil.ints.IntSet;

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ default IntSet crossEdges() {
* @param map a map from vertex to either {@code true} or {@code false}
* @return a new vertex bi-partition
*/
static IVertexBiPartition fromMap(IntGraph g, Int2BooleanMap map) {
static IVertexBiPartition fromMap(IntGraph g, Map<Integer, Boolean> map) {
return fromMapping(g, map::get);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import com.jgalgo.internal.ds.ReferenceableHeap;
import com.jgalgo.internal.util.Assertions;
import com.jgalgo.internal.util.DebugPrinter;
import it.unimi.dsi.fastutil.objects.Reference2IntMap;
import it.unimi.dsi.fastutil.objects.Reference2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;

/**
* Blossom V implementation for maximum weighted matching.
Expand Down Expand Up @@ -2783,7 +2783,7 @@ private static class Debug {

private static class Impl {
private int nextBlossomId;
private final Reference2IntMap<Blossom> blossomIds = new Reference2IntOpenHashMap<>();
private final Object2IntMap<Blossom> blossomIds = new Object2IntOpenHashMap<>();
}

static String blossomId(Blossom b) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.jgalgo.alg;

import java.util.Map;
import java.util.Set;
import java.util.function.IntPredicate;
import java.util.function.Predicate;
Expand All @@ -23,7 +24,6 @@
import com.jgalgo.graph.IntGraph;
import com.jgalgo.internal.util.Bitmap;
import com.jgalgo.internal.util.IntAdapters;
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;

/**
* A partition of the vertices of a graph into two blocks.
Expand Down Expand Up @@ -149,8 +149,8 @@ default Set<E> crossEdges() {
* @param map a map from vertex to either {@code true} or {@code false}
* @return a new vertex bi-partition
*/
static <V, E> VertexBiPartition<V, E> fromMap(Graph<V, E> g, Object2BooleanMap<V> map) {
return fromMapping(g, map::getBoolean);
static <V, E> VertexBiPartition<V, E> fromMap(Graph<V, E> g, Map<V, Boolean> map) {
return fromMapping(g, map::get);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import java.util.EnumSet;
import java.util.Optional;
import java.util.function.Function;
import com.jgalgo.graph.IndexGraphBuilder.ReIndexedGraph;
import com.jgalgo.graph.IndexGraphBuilderImpl.ReIndexedGraphImpl;
import it.unimi.dsi.fastutil.booleans.Boolean2ObjectFunction;

class IndexGraphFactoryImpl implements IndexGraphFactory {

Expand Down Expand Up @@ -89,8 +89,9 @@ default IndexGraphBuilder.ReIndexedGraph newFromBuilderWithReIndex(IndexGraphBui

}

@SuppressWarnings("boxing")
MutableImpl mutableImpl() {
Boolean2ObjectFunction<MutableImpl> arrayImplFactory = selfEdges -> {
Function<Boolean, MutableImpl> arrayImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -125,10 +126,10 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl arrayImpl = arrayImplFactory.get(false);
MutableImpl arrayImplWithSelfEdges = arrayImplFactory.get(true);
MutableImpl arrayImpl = arrayImplFactory.apply(false);
MutableImpl arrayImplWithSelfEdges = arrayImplFactory.apply(true);

Boolean2ObjectFunction<MutableImpl> linkedImplFactory = selfEdges -> {
Function<Boolean, MutableImpl> linkedImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -163,9 +164,9 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl linkedImpl = linkedImplFactory.get(false);
MutableImpl linkedImplWithSelfEdges = linkedImplFactory.get(true);
Boolean2ObjectFunction<MutableImpl> linkedPtrImplFactory = selfEdges -> {
MutableImpl linkedImpl = linkedImplFactory.apply(false);
MutableImpl linkedImplWithSelfEdges = linkedImplFactory.apply(true);
Function<Boolean, MutableImpl> linkedPtrImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -200,9 +201,9 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl linkedPtrImpl = linkedPtrImplFactory.get(false);
MutableImpl linkedPtrImplWithSelfEdges = linkedPtrImplFactory.get(true);
Boolean2ObjectFunction<MutableImpl> hashtableImplFactory = selfEdges -> {
MutableImpl linkedPtrImpl = linkedPtrImplFactory.apply(false);
MutableImpl linkedPtrImplWithSelfEdges = linkedPtrImplFactory.apply(true);
Function<Boolean, MutableImpl> hashtableImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -237,9 +238,9 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl hashtableImpl = hashtableImplFactory.get(false);
MutableImpl hashtableImplWithSelfEdges = hashtableImplFactory.get(true);
Boolean2ObjectFunction<MutableImpl> hashtableMultiImplFactory = selfEdges -> {
MutableImpl hashtableImpl = hashtableImplFactory.apply(false);
MutableImpl hashtableImplWithSelfEdges = hashtableImplFactory.apply(true);
Function<Boolean, MutableImpl> hashtableMultiImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -274,9 +275,9 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl hashtableMultiImpl = hashtableMultiImplFactory.get(false);
MutableImpl hashtableMultiImplWithSelfEdges = hashtableMultiImplFactory.get(true);
Boolean2ObjectFunction<MutableImpl> matrixImplFactory = selfEdges -> {
MutableImpl hashtableMultiImpl = hashtableMultiImplFactory.apply(false);
MutableImpl hashtableMultiImplWithSelfEdges = hashtableMultiImplFactory.apply(true);
Function<Boolean, MutableImpl> matrixImplFactory = selfEdges -> {
return directed ? new MutableImpl() {

@Override
Expand Down Expand Up @@ -311,8 +312,8 @@ public IndexGraph newFromBuilder(IndexGraphBuilderImpl builder) {
}
};
};
MutableImpl matrixImpl = matrixImplFactory.get(false);
MutableImpl matrixImplWithSelfEdges = matrixImplFactory.get(true);
MutableImpl matrixImpl = matrixImplFactory.apply(false);
MutableImpl matrixImplWithSelfEdges = matrixImplFactory.apply(true);

if (impl != null) {
switch (impl) {
Expand Down Expand Up @@ -377,8 +378,9 @@ IndexGraphBuilder.ReIndexedGraph newFromBuilderWithReIndex(IndexGraphBuilderImpl

}

@SuppressWarnings("boxing")
ImmutableImpl immutableImpl() {
Boolean2ObjectFunction<ImmutableImpl> csrImplFactory = fastLookup -> {
Function<Boolean, ImmutableImpl> csrImplFactory = fastLookup -> {
return directed ? new ImmutableImpl() {
@Override
public IndexGraph newCopyOf(IndexGraph graph, boolean copyVerticesWeights, boolean copyEdgesWeights) {
Expand Down Expand Up @@ -450,8 +452,8 @@ public ReIndexedGraph newFromBuilderWithReIndex(IndexGraphBuilderImpl builder, b
}
};
};
ImmutableImpl csrImpl = csrImplFactory.get(false);
ImmutableImpl csrImplWithFastLookup = csrImplFactory.get(true);
ImmutableImpl csrImpl = csrImplFactory.apply(false);
ImmutableImpl csrImplWithFastLookup = csrImplFactory.apply(true);
if (hints.contains(GraphFactory.Hint.FastEdgeLookup)) {
return csrImplWithFastLookup;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,8 @@
package com.jgalgo.internal.ds;

import com.jgalgo.internal.util.Bitmap;
import it.unimi.dsi.fastutil.bytes.Byte2IntMap;
import it.unimi.dsi.fastutil.bytes.Byte2IntOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2IntMap;
import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap;
import it.unimi.dsi.fastutil.shorts.Short2IntMap;
import it.unimi.dsi.fastutil.shorts.Short2IntOpenHashMap;

abstract class RMQStaticLinearAbstract implements RMQStatic {

Expand Down Expand Up @@ -81,78 +77,31 @@ abstract class PreProcessor extends DsBase {
}

void preProcessInnerBlocks() {
int keySize = getBlockKeySize();
var innerBlocksIdx = new Object() {
int val = 0;
};
blockToInnerIdx = new int[blockNum];
final int innerBlockAllocSize = blockSize * (blockSize - 1) / 2;
if (keySize < Byte.SIZE) {
Byte2IntMap tables = new Byte2IntOpenHashMap();
for (int b = 0; b < blockNum; b++) {
byte key = (byte) calcBlockKey(b);
blockToInnerIdx[b] = key;
tables.computeIfAbsent(key, k -> innerBlocksIdx.val++);
}
final int innerBlockNum = tables.size();
Bitmap builtInnerBlocks = new Bitmap(innerBlockNum);
innerBlocks = new byte[innerBlockNum * innerBlockAllocSize];
for (int b = 0; b < blockNum; b++) {
byte key = (byte) blockToInnerIdx[b];
int innerIdx = tables.get(key);
blockToInnerIdx[b] = innerIdx;
if (!builtInnerBlocks.get(innerIdx)) {
byte[] demoBlock = calcDemoBlock(key & 0xff);
buildInnerBlock(innerIdx, demoBlock);
builtInnerBlocks.set(innerIdx);
}
}
tables.clear();

} else if (keySize < Short.SIZE) {
Short2IntMap tables = new Short2IntOpenHashMap();
for (int b = 0; b < blockNum; b++) {
short key = (short) calcBlockKey(b);
blockToInnerIdx[b] = key;
tables.computeIfAbsent(key, k -> innerBlocksIdx.val++);
}
final int innerBlockNum = tables.size();
Bitmap builtInnerBlocks = new Bitmap(innerBlockNum);
innerBlocks = new byte[innerBlockNum * innerBlockAllocSize];
for (int b = 0; b < blockNum; b++) {
short key = (short) blockToInnerIdx[b];
int innerIdx = tables.get(key);
blockToInnerIdx[b] = innerIdx;
if (!builtInnerBlocks.get(innerIdx)) {
byte[] demoBlock = calcDemoBlock(key & 0xffff);
buildInnerBlock(innerIdx, demoBlock);
builtInnerBlocks.set(innerIdx);
}
}
tables.clear();

} else {
Int2IntMap tables = new Int2IntOpenHashMap();
for (int b = 0; b < blockNum; b++) {
int key = calcBlockKey(b);
blockToInnerIdx[b] = key;
tables.computeIfAbsent(key, k -> innerBlocksIdx.val++);
}
final int innerBlockNum = tables.size();
Bitmap builtInnerBlocks = new Bitmap(innerBlockNum);
innerBlocks = new byte[innerBlockNum * innerBlockAllocSize];
for (int b = 0; b < blockNum; b++) {
int key = blockToInnerIdx[b];
int innerIdx = tables.get(key);
blockToInnerIdx[b] = innerIdx;
if (!builtInnerBlocks.get(innerIdx)) {
byte[] demoBlock = calcDemoBlock(key);
buildInnerBlock(innerIdx, demoBlock);
builtInnerBlocks.set(innerIdx);
}
Int2IntMap tables = new Int2IntOpenHashMap();
for (int b = 0; b < blockNum; b++) {
int key = calcBlockKey(b);
blockToInnerIdx[b] = key;
tables.computeIfAbsent(key, k -> innerBlocksIdx.val++);
}
final int innerBlockNum = tables.size();
Bitmap builtInnerBlocks = new Bitmap(innerBlockNum);
innerBlocks = new byte[innerBlockNum * innerBlockAllocSize];
for (int b = 0; b < blockNum; b++) {
int key = blockToInnerIdx[b];
int innerIdx = tables.get(key);
blockToInnerIdx[b] = innerIdx;
if (!builtInnerBlocks.get(innerIdx)) {
byte[] demoBlock = calcDemoBlock(key);
buildInnerBlock(innerIdx, demoBlock);
builtInnerBlocks.set(innerIdx);
}
tables.clear();
}
tables.clear();
}

private void buildInnerBlock(int innerBlock, byte[] demoBlock) {
Expand Down
43 changes: 25 additions & 18 deletions jgalgo-core/src/main/java/com/jgalgo/internal/util/Assertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
import com.jgalgo.internal.ds.Heap;
import com.jgalgo.internal.ds.ReferenceableHeap;
import it.unimi.dsi.fastutil.doubles.DoubleComparator;
import it.unimi.dsi.fastutil.ints.Int2ByteMap;
import it.unimi.dsi.fastutil.ints.Int2ByteOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntCollection;
import it.unimi.dsi.fastutil.ints.IntComparator;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;

public class Assertions {
private Assertions() {}
Expand Down Expand Up @@ -176,24 +176,31 @@ public static void sourcesSinksNotTheSame(IntCollection sources, IntCollection s
throw new IllegalArgumentException("no sources vertices provided");
if (sinks.isEmpty())
throw new IllegalArgumentException("no sinks vertices provided");
final byte UNSEEN = 0;
final byte SOURCE = 1;
final byte TARGET = 2;
Int2ByteMap types = new Int2ByteOpenHashMap(sources.size() + sinks.size());
types.defaultReturnValue(UNSEEN);
for (int v : sources) {
int vType = types.put(v, SOURCE);
if (vType != UNSEEN)
throw new IllegalArgumentException("Source vertex appear twice (idx=" + v + ")");
IntSet sourcesSet;
if (sources instanceof IntSet) {
sourcesSet = (IntSet) sources;
} else {
sourcesSet = new IntOpenHashSet(sources.size());
for (int v : sources) {
boolean added = sourcesSet.add(v);
if (!added)
throw new IllegalArgumentException("Source vertex appear twice (idx=" + v + ")");
}
}
for (int v : sinks) {
int vType = types.put(v, TARGET);
if (vType != UNSEEN) {
if (vType == SOURCE)
if (sinks instanceof IntSet) {
for (int sink : sinks) {
if (sourcesSet.contains(sink))
throw new IllegalArgumentException(
"A vertex can't be both a source and target (idx=" + v + ")");
if (vType == TARGET)
throw new IllegalArgumentException("Target vertex appear twice (idx=" + v + ")");
"A vertex can't be both a source and sink (idx=" + sink + ")");
}
} else {
IntSet verticesSet = new IntOpenHashSet(sinks.size());
for (int v : sinks) {
boolean added = verticesSet.add(v);
if (!added)
throw new IllegalArgumentException("Sink vertex appear twice (idx=" + v + ")");
if (sourcesSet.contains(v))
throw new IllegalArgumentException("A vertex can't be both a source and sink (idx=" + v + ")");
}
}
}
Expand Down
Loading

0 comments on commit 86d55fd

Please sign in to comment.