📄 fourrussians.java
字号:
stepFWDBackTrack(showSteps);
break;
}
}
protected void stepBackward() {
CellElement currentCell;
FRTable dpTable = (FRTable)m_dpTable;
Point blocPos;
switch (m_currentPhase) {
case PHASE_CALC_GRID:
m_currentStep--;
blocPos = getCoordsByStep(m_currentStep);
dpTable.clearBloc(blocPos);
if (m_currentStep <= 0) {
stepZero();
return;
}
// Twice because the current will be added by the stepForward!
dpTable.clearLastBkIndex();
dpTable.clearLastBkIndex();
m_currentStep--;
stepForward(true);
break;
case PHASE_BACKTRACK:
if (m_backTrackList.size() <= 1) {
currentCell = (CellElement) m_backTrackList.getLast();
currentCell.clearColor();
m_currentStep--;
m_currentPhase = PHASE_CALC_GRID;
m_backTrackList.clear();
m_dpTable.clearInteractiveCells();
m_dpTable.clearHighlightColors();
m_dpTable.clearGridCircle();
m_dpTable.clearAllArrows();
dpTable.setShowBackgroundIndexes(true);
dpTable.clearLastBkIndex();
m_lDEqual.setText(" ");
m_l1Choiche.setText(" ");
m_l2Choiche.setText(" ");
m_l3Choiche.setText(" ");
m_dwPanel.setVisible(false);
stepForward(true);
}
else {
super.stepBackward();
}
break;
}
}
protected void stepFWDCalc(boolean showSteps) {
if (m_currentStep == 0) {
m_btnSetOne.setEnabled(false);
m_btnSetTwo.setEnabled(false);
m_btnSetGapOne.setEnabled(false);
m_btnSetGapTwo.setEnabled(false);
m_sldSetT.setEnabled(false);
m_btnPrev.setEnabled(true);
m_btnBeginning.setEnabled(true);
}
int i, x, y;
FRCellElement tmpCell;
Point currBloc;
currBloc = getCoordsByStep(m_currentStep);
x = (m_t - 1) * currBloc.x + 1;
y = (m_t - 1) * currBloc.y + 1;
//////////////////////////////////////////////////////
///// First encode the two alphabets
//////////////////////////////////////////////////////
int start, end;
start = (m_t-1) * currBloc.y;
end = (m_t-1) * (currBloc.y + 1);
// Substring will be of size m_t!!
String sub_s1 = m_s1.substring(start, end);
start = (m_t-1) * currBloc.x;
end = (m_t-1) * (currBloc.x + 1);
String sub_s2 = m_s2.substring(start, end);
// this local alphabet is guaranteed to be <= m_t
String localAlphabet = buildAlphabet(sub_s1, sub_s2);
Hashtable encodedAlphabet = new Hashtable(sub_s1.length());
byte count = 0;
Character ch;
// Assign to each unique value of the two strings a value
for (i = 0; i < sub_s1.length(); ++i) {
ch = new Character(sub_s2.charAt(i));
if (!encodedAlphabet.containsKey(ch)) {
encodedAlphabet.put(ch, new Byte(count++));
}
ch = new Character(sub_s1.charAt(i));
if (!encodedAlphabet.containsKey(ch)) {
encodedAlphabet.put(ch, new Byte(count++));
}
}
/////////////////////////////////////////
// The two encoded alphabets
byte D_encodedAlphaS1[] = new byte[m_t-1];
byte E_encodedAlphaS2[] = new byte[m_t-1];
Byte tmpEl;
for (i = 0; i < sub_s1.length(); ++i) {
ch = new Character(sub_s1.charAt(i));
tmpEl = (Byte) encodedAlphabet.get(ch);
D_encodedAlphaS1[i] = tmpEl.byteValue();
}
for (i = 0; i < sub_s2.length(); ++i) {
ch = new Character(sub_s2.charAt(i));
tmpEl = (Byte)encodedAlphabet.get(ch);
E_encodedAlphaS2[i] = tmpEl.byteValue();
}
//////////////////////////////////////////////////////
///// Now gets the gaps
//////////////////////////////////////////////////////
byte B_topGap[] = new byte[m_t-1];
byte C_leftGap[] = new byte[m_t-1];
////// Top gap
start = (m_t-1) * currBloc.x + 2;
for (i = 0; i < m_t-1; i++) {
tmpCell = (FRCellElement)m_dpTable.getCell(start + i, y);
if (tmpCell.hasAlternativeVal()) {
B_topGap[i] = (byte) tmpCell.getIntAlternativeVal();
}
else {
B_topGap[i] = (byte) tmpCell.getIntVal();
}
}
////// Left gap
start = (m_t-1) * currBloc.y + 2;
for (i = 0; i < m_t-1; i++) {
tmpCell = (FRCellElement)m_dpTable.getCell(x, start + i);
C_leftGap[i] = (byte)tmpCell.getIntVal();
}
//////////////////////////////////////////////////////
///// Now I have everything. Let's ask the hashtable!
//////////////////////////////////////////////////////
RBFParams param = new RBFParams(D_encodedAlphaS1, E_encodedAlphaS2,
B_topGap, C_leftGap);
MinimalistMatrix foundMat = (MinimalistMatrix)m_preprocTable.get(param);
m_lDEqual.setText("Found Bloc = " + foundMat.index);
///////////////////////////////////////////////////////
/// Now update the table
///////////////////////////////////////////////////////
tmpCell = (FRCellElement)m_dpTable.getCell(x + m_t-1, y);
int newOffsetVal = 0;
int newRealVal = tmpCell.getIntScoreVal();
MinimalistCellElement prevPrecompCell;
MinimalistCellElement currPrecompCell;
///// the right side ////////
byte c, r;
for (r = 1; r < m_t; ++r) {
tmpCell = (FRCellElement)m_dpTable.getCell(x + m_t-1, y + r);
prevPrecompCell = foundMat.mat[m_t-1][r-1];
currPrecompCell = foundMat.mat[m_t-1][r];
newOffsetVal = currPrecompCell.value - prevPrecompCell.value;
newRealVal += newOffsetVal;
tmpCell.setIntVal(newOffsetVal);
tmpCell.setIntScoreVal(newRealVal);
}
///// the bottom side ///////
tmpCell = (FRCellElement)m_dpTable.getCell(x, y + m_t-1);
newRealVal = tmpCell.getIntScoreVal();
for (c = 1; c < m_t; ++c) {
tmpCell = (FRCellElement)m_dpTable.getCell(x + c, y + m_t-1);
prevPrecompCell = foundMat.mat[c-1][m_t-1];
currPrecompCell = foundMat.mat[c][m_t-1];
newOffsetVal = currPrecompCell.value - prevPrecompCell.value;
newRealVal += newOffsetVal;
if (c == m_t - 1) {
tmpCell.setIntAlternativeVal(newOffsetVal);
}
else {
tmpCell.setIntVal(newOffsetVal);
tmpCell.setIntScoreVal(newRealVal);
}
}
// Now sets the pointers
FRCellElement pointed;
for (r = 1; r < m_t; ++r) {
for (c = 1; c < m_t; ++c) {
tmpCell = (FRCellElement)m_dpTable.getCell(x + c,
y + r);
currPrecompCell = foundMat.mat[c][r];
if (currPrecompCell.pLeft != null) {
pointed = (FRCellElement)m_dpTable.getCell(tmpCell.getColumn()-1,
tmpCell.getRow());
tmpCell.addLeftPointer(pointed);
}
if (currPrecompCell.pTop != null) {
pointed = (FRCellElement)m_dpTable.getCell(tmpCell.getColumn(),
tmpCell.getRow()-1);
tmpCell.addTopPointer(pointed);
}
if (currPrecompCell.pTopLeft != null) {
pointed = (FRCellElement)m_dpTable.getCell(tmpCell.getColumn()-1,
tmpCell.getRow()-1);
tmpCell.addDiagPointer(pointed);
}
}
}
/// Update the grid colors
((FRTable)m_dpTable).addBackgroundIndex(currBloc,
Integer.toString(foundMat.index));
((FRTable)m_dpTable).highlightBloc(currBloc);
m_currentStep++;
if (showSteps) {
m_dpTable.paint(m_dpTable.getGraphics());
}
}
protected void stepZero() {
FRTable dpTable = ( (FRTable) m_dpTable);
switch (m_currentPhase) {
case PHASE_CALC_GRID:
dpTable.clearAllBkIndexes();
m_dpTable.clearHighlightColors();
m_sldSetT.setEnabled(true);
setInfoMessage("Total blocs to precompute: " +
getNumPrecomputedBlocsString());
super.stepZero();
m_lDEqual.setText(" ");
m_l1Choiche.setText(" ");
m_l2Choiche.setText(" ");
m_l3Choiche.setText(" ");
break;
case PHASE_BACKTRACK:
dpTable.setShowBackgroundIndexes(true);
super.stepZero();
break;
}
}
protected void stepEnd() {
int i;
int totSize = m_nHBlocs * m_nVBlocs;
m_dpTable.clearDPHighlights();
m_dpTable.clearAllArrows();
m_dpTable.clearGridCircle();
switch (m_currentPhase) {
case PHASE_CALC_GRID:
for (i = m_currentStep; i < totSize; ++i) {
stepForward(false);
}
stepForward(true);
break;
case PHASE_BACKTRACK:
while (m_btnEnd.isEnabled()) {
stepForward(false);
}
m_dpTable.paint(m_dpTable.getGraphics());
}
}
protected void algoClear() {
super.algoClear();
m_btnShowPreproc.setEnabled(false);
m_dwPanel.setVisible(false);
m_sldSetT.setEnabled(false);
m_currentPhase = PHASE_PREPROCESSING;
}
///////////////////////////////////////////////////////////////////////
protected String buildAlphabet(String s1, String s2) {
HashSet hs = new HashSet();
int i;
String alphabet = "";
for (i = 0; i < s1.length(); ++i) {
hs.add(new Character(s1.charAt(i)));
}
for (i = 0; i < s2.length(); ++i) {
hs.add(new Character(s2.charAt(i)));
}
Iterator it = hs.iterator();
alphabet = "";
while (it.hasNext()) {
alphabet += ((Character)it.next()).charValue();
}
return alphabet;
//System.out.println("Alphabet: " + alphabet);
}
protected void buildEncodedAlphabet() {
// NOTE: MAX t SIZE = 256!!!!
// the dimension of the encoded alphabet values are
// min(size(alphabet), (t-1)*2)
m_encodedAlphSize = (Math.min(m_alphabet.length(), (m_t - 1) * 2));
m_stringsEncodedValues = new byte[m_encodedAlphSize];
for (byte i = 0; i < m_encodedAlphSize; ++i) {
m_stringsEncodedValues[i] = i;
}
}
/**
* Returns the number of forecasted precomputed blocs.
*/
protected long getNumPrecomputedBlocs() {
long totAlpha = (long)Math.pow(m_encodedAlphSize, m_t - 1);
// offset is always 3: {-1, 0, +1}
long totOffset = (long)Math.pow(3, m_t - 1);
return totAlpha * totAlpha *
totOffset * totOffset;
}
protected String getNumPrecomputedBlocsString() {
NumberFormat nf = NumberFormat.getInstance();
long nPrec = getNumPrecomputedBlocs();
return nf.format(nPrec);
}
/**
* Now returns the coordinate of the BLOC!!
*
* @param step The current Step
* @return The coordinates of the current bloc.
* Ie. 2,1 = second bloc on right and first on top
*/
protected Point getCoordsByStep(int step) {
Point ret = new Point();
// Column
int cols = m_nHBlocs;
ret.x = step % cols;
ret.y = step / cols;
return ret;
}
///////////////// EVENTS ////////////////////////////////
class SliderListener implements ChangeListener {
public void stateChanged(ChangeEvent e) {
JSlider source = (JSlider)e.getSource();
if (!source.getValueIsAdjusting()) {
int a = source.getValue();
Integer mappedT = (Integer)m_validTValues.get(source.getValue());
m_t = mappedT.intValue();
m_tValue.setText("t=" + m_t + ":");
resetFRGrid();
}
}
}
protected class ShowPrepListener
implements ActionListener {
public void actionPerformed(ActionEvent event) {
// Perform the action indicated by a mouse click on a button.
Object b = event.getSource(); // Get the component that was
// clicked.
if (b == m_btnShowPreproc) {
ShowPrepTables spt = new ShowPrepTables(m_preprocTable, m_orderedKeys,
0, m_preprocTable.size(), m_encodedAlphSize);
spt.show();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -