Skip to content

Commit

Permalink
CSR: dont sore edges on construction, too expensive
Browse files Browse the repository at this point in the history
  • Loading branch information
barakugav committed Jul 25, 2023
1 parent bee2d88 commit 439dac7
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 225 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
*/
package com.jgalgo.graph;

import it.unimi.dsi.fastutil.ints.AbstractIntSet;

abstract class GraphCSRAbstractUnindexed extends GraphCSRBase {

final int[] edgesOut;
Expand Down Expand Up @@ -60,50 +58,4 @@ abstract class GraphCSRAbstractUnindexed extends GraphCSRBase {
}
}

abstract class EdgeSetSourceTarget extends AbstractIntSet implements EdgeSet {

final int source;
final int target;
private final int edgeIdxBegin;
private final int edgeIdxEnd;

EdgeSetSourceTarget(int source, int target, int edgeIdxBegin, int edgeIdxEnd) {
this.source = source;
this.target = target;
this.edgeIdxBegin = edgeIdxBegin;
this.edgeIdxEnd = edgeIdxEnd;
}

@Override
public int size() {
return edgeIdxEnd - edgeIdxBegin;
}

@Override
public EdgeIter iterator() {
return new EdgeIterSourceTarget(source, target, edgesOut, edgeIdxBegin, edgeIdxEnd);
}
}

private static class EdgeIterSourceTarget extends EdgeIterAbstract {
private final int source;
private final int target;

EdgeIterSourceTarget(int source, int target, int[] edges, int beginIdx, int endIdx) {
super(edges, beginIdx, endIdx);
this.source = source;
this.target = target;
}

@Override
public int source() {
return source;
}

@Override
public int target() {
return target;
}
}

}
40 changes: 0 additions & 40 deletions jgalgo-core/src/main/java/com/jgalgo/graph/GraphCSRBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.Set;
import com.jgalgo.graph.Graphs.ImmutableGraph;
import com.jgalgo.internal.util.Assertions;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.ints.IntSet;

abstract class GraphCSRBase extends GraphBase implements IndexGraphImpl, ImmutableGraph {
Expand Down Expand Up @@ -119,12 +118,6 @@ public void removeVertex(int vertex) {
throw new UnsupportedOperationException();
}

@Override
public int getEdge(int source, int target) {
EdgeSet edges = getEdges(source, target);
return edges.isEmpty() ? -1 : edges.iterator().nextInt();
}

@Override
public int addEdge(int source, int target) {
throw new UnsupportedOperationException();
Expand Down Expand Up @@ -295,18 +288,6 @@ static class BuilderProcessEdgesUndirected extends BuilderProcessEdges {
for (int v = n - 1; v > 0; v--)
edgesOutBegin[v] = edgesOutBegin[v - 1];
edgesOutBegin[0] = 0;

for (int u = 0; u < n; u++) {
final int u0 = u;
IntArrays.quickSort(edgesOut, edgesOutBegin[u], edgesOutBegin[u + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(builder.edgeEndpoint(e1, u0), builder.edgeEndpoint(e2, u0))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
}

}
Expand Down Expand Up @@ -366,27 +347,6 @@ static class BuilderProcessEdgesDirected extends BuilderProcessEdges {
edgesInBegin[v] = edgesInBegin[v - 1];
}
edgesOutBegin[0] = edgesInBegin[0] = 0;

for (int u = 0; u < n; u++) {
IntArrays.quickSort(edgesOut, edgesOutBegin[u], edgesOutBegin[u + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(builder.edgeTarget(e1), builder.edgeTarget(e2))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
for (int v = 0; v < n; v++) {
IntArrays.quickSort(edgesIn, edgesInBegin[v], edgesInBegin[v + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(builder.edgeSource(e1), builder.edgeSource(e2))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
}

}
Expand Down
44 changes: 0 additions & 44 deletions jgalgo-core/src/main/java/com/jgalgo/graph/GraphCSRDirected.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

import com.jgalgo.graph.Graphs.GraphCapabilitiesBuilder;
import com.jgalgo.internal.util.Assertions;
import com.jgalgo.internal.util.JGAlgoUtils;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.ints.IntIntPair;

class GraphCSRDirected extends GraphCSRAbstractUnindexed {

Expand Down Expand Up @@ -47,35 +44,6 @@ class GraphCSRDirected extends GraphCSRAbstractUnindexed {
edgesIn[eIdx++] = e;
}
edgesInBegin[n] = m;

for (int u = 0; u < n; u++) {
IntArrays.quickSort(edgesOut, edgesOutBegin[u], edgesOutBegin[u + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(edgeTarget(e1), edgeTarget(e2))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
for (int v = 0; v < n; v++) {
IntArrays.quickSort(edgesIn, edgesInBegin[v], edgesInBegin[v + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(edgeSource(e1), edgeSource(e2))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
}

@Override
public EdgeSet getEdges(int source, int target) {
IntIntPair edgeRange = JGAlgoUtils.equalRange(edgesOutBegin[source], edgesOutBegin[source + 1], target,
eIdx -> edgeTarget(edgesOut[eIdx]));
return edgeRange == null ? Edges.EmptyEdgeSet
: new EdgeSetSourceTarget(source, target, edgeRange.firstInt(), edgeRange.secondInt());
}

@Override
Expand Down Expand Up @@ -118,18 +86,6 @@ public EdgeIter iterator() {
}
}

private class EdgeSetSourceTarget extends GraphCSRAbstractUnindexed.EdgeSetSourceTarget {

EdgeSetSourceTarget(int source, int target, int edgeIdxBegin, int edgeIdxEnd) {
super(source, target, edgeIdxBegin, edgeIdxEnd);
}

@Override
public boolean contains(int edge) {
return edgeSource(edge) == source && edgeTarget(edge) == target;
}
}

class EdgeSetIn extends GraphBase.EdgeSetInDirected {

EdgeSetIn(int target) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
import java.util.Optional;
import com.jgalgo.graph.Graphs.GraphCapabilitiesBuilder;
import com.jgalgo.internal.util.Assertions;
import com.jgalgo.internal.util.JGAlgoUtils;
import it.unimi.dsi.fastutil.ints.AbstractIntSet;
import it.unimi.dsi.fastutil.ints.IntIntPair;

class GraphCSRDirectedReindexed extends GraphCSRBase {

Expand Down Expand Up @@ -69,14 +66,6 @@ static IndexGraphBuilder.ReIndexedGraph newInstance(IndexGraphBuilderImpl builde
return new IndexGraphBuilderImpl.ReIndexedGraphImpl(g, Optional.empty(), Optional.of(edgesReIndexing));
}

@Override
public EdgeSet getEdges(int source, int target) {
IntIntPair edgeRange =
JGAlgoUtils.equalRange(edgesOutBegin[source], edgesOutBegin[source + 1], target, this::edgeTarget);
return edgeRange == null ? Edges.EmptyEdgeSet
: new EdgeSetSourceTarget(source, target, edgeRange.firstInt(), edgeRange.secondInt());
}

@Override
public EdgeSet outEdges(int source) {
return new EdgeSetOut(source);
Expand Down Expand Up @@ -139,36 +128,6 @@ public EdgeIter iterator() {
}
}

private class EdgeSetSourceTarget extends AbstractIntSet implements EdgeSet {

private final int source;
private final int target;
private final int begin;
private final int end;

EdgeSetSourceTarget(int source, int target, int start, int end) {
this.source = source;
this.target = target;
this.begin = start;
this.end = end;
}

@Override
public boolean contains(int edge) {
return source == edgeSource(edge) && target == edgeTarget(edge);
}

@Override
public int size() {
return end - begin;
}

@Override
public EdgeIter iterator() {
return new EdgeIterSourceTarget(source, target, begin, end);
}
}

private static abstract class EdgeIterOutAbstract implements EdgeIter {
private final int source;
int nextEdge;
Expand Down Expand Up @@ -234,19 +193,4 @@ public int target() {
}
}

private static class EdgeIterSourceTarget extends EdgeIterOutAbstract {

private final int target;

EdgeIterSourceTarget(int source, int target, int beginEdge, int endEnd) {
super(source, beginEdge, endEnd);
this.target = target;
}

@Override
public int target() {
return target;
}
}

}
37 changes: 0 additions & 37 deletions jgalgo-core/src/main/java/com/jgalgo/graph/GraphCSRUndirected.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@

import com.jgalgo.graph.Graphs.GraphCapabilitiesBuilder;
import com.jgalgo.internal.util.Assertions;
import com.jgalgo.internal.util.JGAlgoUtils;
import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.ints.IntIntPair;

class GraphCSRUndirected extends GraphCSRAbstractUnindexed {

Expand All @@ -30,27 +27,6 @@ class GraphCSRUndirected extends GraphCSRAbstractUnindexed {
GraphCSRUndirected(IndexGraph g) {
super(g);
Assertions.Graphs.onlyUndirected(g);

final int n = vertices().size();
for (int u = 0; u < n; u++) {
final int u0 = u;
IntArrays.quickSort(edgesOut, edgesOutBegin[u], edgesOutBegin[u + 1], (e1, e2) -> {
int c;
if ((c = Integer.compare(edgeEndpoint(e1, u0), edgeEndpoint(e2, u0))) != 0)
return c;
if ((c = Integer.compare(e1, e2)) != 0)
return c;
return 0;
});
}
}

@Override
public EdgeSet getEdges(int source, int target) {
IntIntPair edgeRange = JGAlgoUtils.equalRange(edgesOutBegin[source], edgesOutBegin[source + 1], target,
eIdx -> edgeEndpoint(edgesOut[eIdx], source));
return edgeRange == null ? Edges.EmptyEdgeSet
: new EdgeSetSourceTarget(source, target, edgeRange.firstInt(), edgeRange.secondInt());
}

@Override
Expand Down Expand Up @@ -115,19 +91,6 @@ public EdgeIter iterator() {
}
}

private class EdgeSetSourceTarget extends GraphCSRAbstractUnindexed.EdgeSetSourceTarget {

EdgeSetSourceTarget(int source, int target, int edgeIdxBegin, int edgeIdxEnd) {
super(source, target, edgeIdxBegin, edgeIdxEnd);
}

@Override
public boolean contains(int edge) {
int u = edgeSource(edge), v = edgeTarget(edge);
return (u == source && v == target) || (u == target && v == source);
}
}

private class EdgeIterOut extends EdgeIterAbstract {
private final int source;

Expand Down

0 comments on commit 439dac7

Please sign in to comment.