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

Add more comments and make some small refactors and changes to PaigeTarjan folder #68

Merged
merged 8 commits into from
Jan 23, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public boolean isEmpty() {
* reset to {@code -1}.
* <p>
* Preconditions: this.ptr != -1.
* Post-conditions: this.ptr = -1.
*
* @param newId
* the ID of the newly created block, if applicable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,8 @@ public void computeCoarsestStablePartition() {
}

/**
* Move the state to the left of its Block ptr. This allows for the grouping of states with similar behavior.
* Move the state to the left of its Block ptr, and advance the ptr.
* This allows for the grouping of states with similar behavior.
*
* @param state
* state to be moved left within its Block.
Expand Down Expand Up @@ -312,7 +313,7 @@ private void moveLeft(int state) {
posData[posIdx] = ptr;
posData[posDataLow + other] = inBlockIdx;
}
b.ptr = ++ptr;
b.ptr = ptr + 1;
}
}

Expand All @@ -325,13 +326,19 @@ private void processTouched() {
if (splt != null) {
addToWorklist(splt);
}
b.ptr = -1;
b = next;
}

touchedHead = null;
}

/**
* Invoke Block's split, and if it is successful, update PaigeTarjan fields to reflect the new block.
*
* @param b block to split
* @return smaller split block, or null if split is not successful
* Post-conditions: b.ptr = -1.
*/
private @Nullable Block split(Block b) {
Block splt = b.split(numBlocks);
if (splt == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ private static void initCompleteDeterministicPrune(PaigeTarjan pt,
int init = absAutomaton.getIntInitialState();
Object initClass = initialClassification.apply(init);

Block initBlock = pt.createBlock();
initBlock.high = 1;
blockForState[init] = initBlock;
blockMap.put(initClass, initBlock);
blockForState[init] = getOrCreateBlock(blockMap, initClass, pt);

int[] statesBuff = new int[numStates];
statesBuff[0] = init;
Expand All @@ -131,7 +128,7 @@ private static void initCompleteDeterministicPrune(PaigeTarjan pt,
Block succBlock = blockForState[succ];
if (succBlock == null) {
Object succClass = initialClassification.apply(succ);
blockForState[succ] = getOrCreateSuccBlock(blockMap, succClass, pt);
blockForState[succ] = getOrCreateBlock(blockMap, succClass, pt);
statesBuff[reachableStates++] = succ;
}
data[predCountBase + succ]++;
Expand All @@ -146,10 +143,7 @@ private static void initCompleteDeterministicPrune(PaigeTarjan pt,

for (int i = 0; i < reachableStates; i++) {
int stateId = statesBuff[i];
Block b = blockForState[stateId];
int pos = --b.low;
data[pos] = stateId;
data[posDataLow + stateId] = pos;
updateBlockAndPosData(blockForState, stateId, data, posDataLow);

int predOfsBase = predOfsDataLow;

Expand All @@ -162,12 +156,7 @@ private static void initCompleteDeterministicPrune(PaigeTarjan pt,
}
}

pt.setBlockData(data);
pt.setPosData(data, posDataLow);
pt.setPredOfsData(data, predOfsDataLow);
pt.setPredData(data);
pt.setBlockForState(blockForState);
pt.setSize(numStates, numInputs);
updatePTFields(pt, data, posDataLow, predOfsDataLow, blockForState, numStates, numInputs);
}

private static void initCompleteDeterministicNoPrune(PaigeTarjan pt,
Expand All @@ -189,7 +178,7 @@ private static void initCompleteDeterministicNoPrune(PaigeTarjan pt,

for (int i = 0; i < numStates; i++) {
Object classification = initialClassification.apply(i);
blockForState[i] = getOrCreateSuccBlock(blockMap, classification, pt);
blockForState[i] = getOrCreateBlock(blockMap, classification, pt);

int predCountBase = predOfsDataLow;

Expand All @@ -210,10 +199,7 @@ private static void initCompleteDeterministicNoPrune(PaigeTarjan pt,
prefixSum(data, predOfsDataLow, predDataLow);

for (int i = 0; i < numStates; i++) {
Block b = blockForState[i];
int pos = --b.low;
data[pos] = i;
data[posDataLow + i] = pos;
updateBlockAndPosData(blockForState, i, data, posDataLow);
int predOfsBase = predOfsDataLow;

for (int j = 0; j < numInputs; j++) {
Expand All @@ -225,12 +211,7 @@ private static void initCompleteDeterministicNoPrune(PaigeTarjan pt,
}
}

pt.setBlockData(data);
pt.setPosData(data, posDataLow);
pt.setPredOfsData(data, predOfsDataLow);
pt.setPredData(data);
pt.setBlockForState(blockForState);
pt.setSize(numStates, numInputs);
updatePTFields(pt, data, posDataLow, predOfsDataLow, blockForState, numStates, numInputs);
}

public static void prefixSum(int[] array, int startInclusive, int endExclusive) {
Expand Down Expand Up @@ -277,10 +258,7 @@ public static void initDeterministic(PaigeTarjan pt,

Object initClass = initialClassification.apply(initId);

Block initBlock = pt.createBlock();
initBlock.high = 1;
blockForState[initId] = initBlock;
blockMap.put(initClass, initBlock);
blockForState[initId] = getOrCreateBlock(blockMap, initClass, pt);

int[] statesBuff = new int[numStatesWithSink];
statesBuff[0] = initId;
Expand Down Expand Up @@ -315,33 +293,36 @@ public static void initDeterministic(PaigeTarjan pt,
} else {
succClass = initialClassification.apply(succ);
}
blockForState[succId] = getOrCreateSuccBlock(blockMap, succClass, pt);
blockForState[succId] = getOrCreateBlock(blockMap, succClass, pt);
statesBuff[reachableStates++] = succId;
}
data[predCountBase + succId]++;
data[predCountBase + succId]++; // predOfsData
predCountBase += numStatesWithSink;
}
}

if (partial) {
int predCountIdx = predOfsDataLow + sinkId;
for (int i = 0; i < numInputs; i++) {
data[predCountIdx]++;
data[predCountIdx]++; // predOfsData - sink state has all its symbols pointing to itself
predCountIdx += numStatesWithSink;
}
}

// data[predOfsDataLow + j*numStatesWithSink+i] now contains the count of transitions to state i from input j

pt.canonizeBlocks();

// Make predOfsData cumulative
data[predOfsDataLow] += predDataLow;
prefixSum(data, predOfsDataLow, predDataLow);

// data[predOfsDataLow + j*numStatesWithSink+i] now contains
// the final predOfsData value plus the count of transitions to state i from input j

for (int i = 0; i < reachableStates; i++) {
int stateId = statesBuff[i];
Block b = blockForState[stateId];
int pos = --b.low;
data[pos] = stateId;
data[posDataLow + stateId] = pos;
updateBlockAndPosData(blockForState, stateId, data, posDataLow);

int predOfsBase = predOfsDataLow;

Expand All @@ -359,22 +340,24 @@ public static void initDeterministic(PaigeTarjan pt,
}
}

data[--data[predOfsBase + succId]] = stateId;
data[--data[predOfsBase + succId]] = stateId; // decrement predOfsData, set predData
predOfsBase += numStatesWithSink;
}
}

pt.setBlockData(data);
pt.setPosData(data, posDataLow);
pt.setPredOfsData(data, predOfsDataLow);
pt.setPredData(data);
pt.setSize(numStatesWithSink, numInputs);
pt.setBlockForState(blockForState);
updatePTFields(pt, data, posDataLow, predOfsDataLow, blockForState, numStatesWithSink, numInputs);

pt.removeEmptyBlocks();
}

private static Block getOrCreateSuccBlock(
private static void updateBlockAndPosData(Block[] blockForState, int i, int[] data, int posDataLow) {
Block b = blockForState[i];
int pos = --b.low;
data[pos] = i;
data[posDataLow + i] = pos;
}

private static Block getOrCreateBlock(
Map<@Nullable Object, Block> blockMap, Object classification, PaigeTarjan pt) {
Block block = blockMap.get(classification);
if (block == null) {
Expand All @@ -386,4 +369,14 @@ private static Block getOrCreateSuccBlock(
return block;
}

private static void updatePTFields(
PaigeTarjan pt, int[] data, int posDataLow, int predOfsDataLow,
Block[] blockForState, int numStates, int numInputs) {
pt.setBlockData(data);
pt.setPosData(data, posDataLow);
pt.setPredOfsData(data, predOfsDataLow);
pt.setPredData(data);
pt.setBlockForState(blockForState);
pt.setSize(numStates, numInputs);
}
}