From 6543ad07b75ed5efa6539f2ab68fa82791245134 Mon Sep 17 00:00:00 2001 From: Alex Birkett Date: Sat, 30 Jan 2016 13:20:56 +0100 Subject: [PATCH] Remove redundant gets and puts to the cells map --- src/main/java/no/birkett/kiwi/Row.java | 29 +++++++------------------- 1 file changed, 8 insertions(+), 21 deletions(-) diff --git a/src/main/java/no/birkett/kiwi/Row.java b/src/main/java/no/birkett/kiwi/Row.java index 9f2973d..5de55a9 100644 --- a/src/main/java/no/birkett/kiwi/Row.java +++ b/src/main/java/no/birkett/kiwi/Row.java @@ -70,29 +70,16 @@ public class Row { * is zero, the symbol will be removed from the row */ void insert(Symbol symbol, double coefficient) { + Double existingCoefficient = cells.get(symbol); - //this looks different than c++ code -// Double existingCoefficient = cells.get(symbol); -// -// if (existingCoefficient != null) { -// coefficient = existingCoefficient; -// } -// -// if (Util.nearZero(coefficient)) { -// cells.remove(symbol); -// } else { -// cells.put(symbol, coefficient); -// } - - //changes start here - Double value = this.cells.get(symbol); - if(value == null){ - this.cells.put(symbol, 0.0); + if (existingCoefficient != null) { + coefficient += existingCoefficient; } - double temp = this.cells.get(symbol) + coefficient; - this.cells.put(symbol, temp); - if(Util.nearZero(temp)){ - this.cells.remove(symbol); + + if (Util.nearZero(coefficient)) { + cells.remove(symbol); + } else { + cells.put(symbol, Double.valueOf(coefficient)); } }