Author: echatellier Date: 2011-11-25 14:47:23 +0100 (Fri, 25 Nov 2011) New Revision: 904 Url: http://forge.codelutin.com/repositories/revision/coser/904 Log: Improve duplicated line grouping algorithm (10 times faster now) Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDataTableModel.java trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDuplicatedLineTableModel.java trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/GlobalControlErrorModel.java Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDataTableModel.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDataTableModel.java 2011-11-25 13:45:07 UTC (rev 903) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDataTableModel.java 2011-11-25 13:47:23 UTC (rev 904) @@ -93,7 +93,7 @@ public int getColumnCount() { return header.length; } - + @Override public String getColumnName(int column) { String name = header[column]; @@ -109,9 +109,7 @@ */ @Override public Object getValueAt(int rowIndex, int columnIndex) { - Object result = getDataAt(rowIndex)[columnIndex]; - return result; } @@ -127,8 +125,7 @@ } /** - * Retourne l'index dans la liste des données du numero de ligne - * demandé. + * Retourne l'index dans la liste des données du numero de ligne demandée. * * @param lineNumber le numero de données * @return l'index Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDuplicatedLineTableModel.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDuplicatedLineTableModel.java 2011-11-25 13:45:07 UTC (rev 903) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/ControlDuplicatedLineTableModel.java 2011-11-25 13:47:23 UTC (rev 904) @@ -25,6 +25,8 @@ import java.util.EventListener; import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; import java.util.List; import java.util.Set; @@ -85,36 +87,59 @@ /** * Compute line indices. + * + * This algorithm has been optimized three times. + * Be carefull of performances when modify it, can take looooong times + * without optimisation. */ protected void computeLineIndices() { long timeBefore = System.currentTimeMillis(); int dataCount = getRowCount(); - Category category = getCategory(); + // we need to have a smaller list for performance reason + // next loop is while into another while + List<ControlError> localControlErrors = getCurrentModelErrors(); + // build indexed array linesIndex = new int[dataCount]; int i = 0; int j = 0; Set<Integer> alreadyDone = new HashSet<Integer>(); + + // look for first error line to manage in main loop + Iterator<ControlError> itControlErrors = localControlErrors.iterator(); + int nextErrorFirstLine = -1; + ControlError controlError = null; + if (itControlErrors.hasNext()) { + controlError = itControlErrors.next(); + // can return -1 here, after control cannot contains deleted lines + nextErrorFirstLine = dataTableModel.getRealIndexOfLine(controlError.getLineNumbers().get(0)); + } + + // main loop while (i<dataCount) { while (alreadyDone.contains(j)) { j++; } linesIndex[i++] = j; - for (ControlError controlError : controlErrors) { - if (CollectionUtils.size(controlError.getLineNumbers()) >= 2 && category.equals(controlError.getCategory())) { - // can return -1 here, after control can contains deleted lines - int errorFirstLine = dataTableModel.getRealIndexOfLine(controlError.getLineNumbers().get(0)); - if (j == errorFirstLine) { - for (int errorIndex = 1; errorIndex < controlError.getLineNumbers().size(); ++errorIndex) { - // can return -1 here, after control can contains deleted lines - int delegateIndex = dataTableModel.getRealIndexOfLine(controlError.getLineNumbers().get(errorIndex)); - linesIndex[i++] = delegateIndex; - alreadyDone.add(delegateIndex); - } - } + if (j == nextErrorFirstLine) { + for (int errorIndex = 1; errorIndex < controlError.getLineNumbers().size(); ++errorIndex) { + // can return -1 here, after control cannot contains deleted lines + int delegateIndex = dataTableModel.getRealIndexOfLine(controlError.getLineNumbers().get(errorIndex)); + linesIndex[i++] = delegateIndex; + alreadyDone.add(delegateIndex); } + + // remove it, useless for next iteration + itControlErrors.remove(); + + // look for next error line to manage in main loop + if (itControlErrors.hasNext()) { + controlError = itControlErrors.next(); + // can return -1 here, after control cannot contains deleted lines + nextErrorFirstLine = dataTableModel.getRealIndexOfLine(controlError.getLineNumbers().get(0)); + } // no else j == nextErrorFirstLine will become false } j++; } @@ -131,6 +156,24 @@ } } + /** + * Parse input control errors list, an build a new list with only errors + * that will be used by this model. + * + * @return new error list for model only + */ + protected List<ControlError> getCurrentModelErrors() { + Category category = getCategory(); + // linked list because no access with index + List<ControlError> resultControlErrors = new LinkedList<ControlError>(); + for (ControlError controlError : controlErrors) { + if (CollectionUtils.size(controlError.getLineNumbers()) >= 2 && category.equals(controlError.getCategory())) { + resultControlErrors.add(controlError); + } + } + return resultControlErrors; + } + /* * @see javax.swing.table.TableModel#getRowCount() */ Modified: trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/GlobalControlErrorModel.java =================================================================== --- trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/GlobalControlErrorModel.java 2011-11-25 13:45:07 UTC (rev 903) +++ trunk/coser-ui/src/main/java/fr/ifremer/coser/ui/control/GlobalControlErrorModel.java 2011-11-25 13:47:23 UTC (rev 904) @@ -53,9 +53,6 @@ */ public class GlobalControlErrorModel extends AbstractTreeTableModel { - /** serialVersionUID. */ - private static final long serialVersionUID = -32286733264427664L; - protected List<ControlError> controlErrors; protected List<Object> controlErrorCategory; protected Map<Object, List<ControlErrorGroup>> controlCategoryChild;