Skip to content
Merged
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
10 changes: 10 additions & 0 deletions 2SAT/TwoSAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public TwoSAT(int n) {
}

public void addClause(int x, boolean f, int y, boolean g) {
rangeCheck(x);
rangeCheck(y);
scc.addEdge(x << 1 | (f ? 0 : 1), y << 1 | (g ? 1 : 0));
scc.addEdge(y << 1 | (g ? 0 : 1), x << 1 | (f ? 1 : 0));
}
Expand Down Expand Up @@ -48,6 +50,14 @@ public boolean[] answer() {
return null;
}

private void rangeCheck(int x) {
if (0 < x || x >= n) {
throw new IndexOutOfBoundsException(
String.format("Index %d out of bounds for length %d", x, n)
);
}
}

private static final class EdgeList {
long[] a;
int ptr = 0;
Expand Down
11 changes: 11 additions & 0 deletions SCC/SCC.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public SCC(int n) {
}

public void addEdge(int from, int to) {
rangeCheck(from);
rangeCheck(to);
unorderedEdges.add(new Edge(from, to));
start[from + 1]++;
this.m++;
Expand All @@ -36,6 +38,7 @@ public int id(int i) {
"Graph hasn't been built."
);
}
rangeCheck(i);
return ids[i];
}

Expand Down Expand Up @@ -127,4 +130,12 @@ public int[][] build() {
hasBuilt = true;
return groups;
}

private void rangeCheck(int i) {
if (i < 0 || i >= n) {
throw new IndexOutOfBoundsException(
String.format("Index %d out of bounds for length %d", i, n)
);
}
}
}