From 90fc63ed2caf334bdc4f2830a05aff4e8bc4d186 Mon Sep 17 00:00:00 2001
From: Nicolas Dumoulin <nicolas.dumoulin@irstea.fr>
Date: Fri, 18 Sep 2015 09:54:39 +0000
Subject: [PATCH] optimization attempt

---
 src/main/java/pikelake/Grid.java | 30 ++++++++++++++++--------------
 1 file changed, 16 insertions(+), 14 deletions(-)

diff --git a/src/main/java/pikelake/Grid.java b/src/main/java/pikelake/Grid.java
index 1f3c8f5..29975fc 100644
--- a/src/main/java/pikelake/Grid.java
+++ b/src/main/java/pikelake/Grid.java
@@ -66,7 +66,7 @@ public class Grid extends Grid2D<Cell, Individual> {
     	
         // Lecture fichier contenant les HSI de toutes les mailles
     	String filePath = "data/input/HSI/hsi_BRO" + Time.getSeason() + marnageInit + ".txt";
-    	Scanner scanner = new Scanner(new File(filePath));
+        Scanner scanner = new Scanner(new File(filePath));
 
     	// Initialisation de chaque cellule contenant HSI
     	// On boucle sur chaque ligne detectée
@@ -171,7 +171,7 @@ public class Grid extends Grid2D<Cell, Individual> {
     	xPike = (position.getIndex() - (yPike * gridWidth));
     	
     	// Determination de la liste des mailles pour une distance donnee qui depend de la phase du jour et de la saison
-    	switch (Time.getSeason()) {
+        switch (Time.getSeason()) {
     		case "PRINTEMPS":	switch (Time.getPhaseJour()) {
     								case "AUBE" :	listeCoord = AreaMovement.areaPrinAube;		break;
     								case "JOUR" :	listeCoord = AreaMovement.areaPrinJour;		break;
@@ -221,21 +221,23 @@ public class Grid extends Grid2D<Cell, Individual> {
 
     	// Calcul des mailles comprises dans le lac
     	boolean stop = false;
-    	for (int cpt=0; cpt<listeCoord[0].length; cpt++) {
-    		
-    		if (listeCoord[0][cpt] == 0 & listeCoord[1][cpt] == 0 & stop == true) break;
-    		if (listeCoord[0][cpt] == 0 & listeCoord[1][cpt] == 0) stop = true;
-
-    		// Calcul de l'indice de la maille à partir des coordonnees (x,y)
-    		int newCell = xPike + listeCoord[0][cpt] + (yPike + listeCoord[1][cpt]) * gridWidth;
+        final int max = (gridWidth * gridHeight)-2;
+        for (int cpt=0; cpt<listeCoord[0].length; cpt++) {    		
+    		int newCell = computeIndex(xPike, listeCoord, cpt, yPike);
     		
     		// Test si l'indice calcule appartient à la grille totale
-    		if (newCell >= 0 & newCell <= (gridWidth * gridHeight)-2)
-    			
-    			// Si l'indice calcule est compris dans le lac on l'ajoute a la liste
-    			if (getCell(newCell).getHabitatQuality() >= 0)
-					neighbours.add(getCell(newCell));
+    		if (newCell >= 0 && newCell <= max) {
+                    final Cell cell = getCell(newCell);
+                    // Si l'indice calcule est compris dans le lac on l'ajoute a la liste
+                    if (cell.getHabitatQuality() >= 0) {
+                        neighbours.add(cell);
+                    }
+                }
     	}
         return neighbours;
     }
+
+    private int computeIndex(int xPike, int[][] listeCoord, int cpt, int yPike) {
+        return xPike + listeCoord[0][cpt] + (yPike + listeCoord[1][cpt]) * gridWidth;
+    }
 }
-- 
GitLab