diff --git a/core/src/processing/data/DoubleDict.java b/core/src/processing/data/DoubleDict.java index e310b079e..f2a9adf10 100644 --- a/core/src/processing/data/DoubleDict.java +++ b/core/src/processing/data/DoubleDict.java @@ -4,6 +4,7 @@ import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.NoSuchElementException; import processing.core.PApplet; @@ -610,21 +611,22 @@ public class DoubleDict { * @webref doubledict:method * @brief Remove a key/value pair */ - public int remove(String key) { + public double remove(String key) { int index = index(key); - if (index != -1) { - removeIndex(index); + if (index == -1) { + throw new NoSuchElementException("'" + key + "' not found"); } - return index; + double value = values[index]; + removeIndex(index); + return value; } - public String removeIndex(int index) { + public double removeIndex(int index) { if (index < 0 || index >= count) { throw new ArrayIndexOutOfBoundsException(index); } - String key = keys[index]; - //System.out.println("index is " + which + " and " + keys[which]); + double value = values[index]; indices.remove(keys[index]); for (int i = index; i < count-1; i++) { keys[i] = keys[i+1]; @@ -634,7 +636,7 @@ public class DoubleDict { count--; keys[count] = null; values[count] = 0; - return key; + return value; } diff --git a/core/src/processing/data/FloatDict.java b/core/src/processing/data/FloatDict.java index 2df14600c..9495563ad 100644 --- a/core/src/processing/data/FloatDict.java +++ b/core/src/processing/data/FloatDict.java @@ -3,6 +3,7 @@ package processing.data; import java.io.*; import java.util.HashMap; import java.util.Iterator; +import java.util.NoSuchElementException; import processing.core.PApplet; @@ -607,21 +608,22 @@ public class FloatDict { * @webref floatdict:method * @brief Remove a key/value pair */ - public int remove(String key) { + public float remove(String key) { int index = index(key); - if (index != -1) { - removeIndex(index); + if (index == -1) { + throw new NoSuchElementException("'" + key + "' not found"); } - return index; + float value = values[index]; + removeIndex(index); + return value; } - public String removeIndex(int index) { + public float removeIndex(int index) { if (index < 0 || index >= count) { throw new ArrayIndexOutOfBoundsException(index); } - String key = keys[index]; - //System.out.println("index is " + which + " and " + keys[which]); + float value = values[index]; indices.remove(keys[index]); for (int i = index; i < count-1; i++) { keys[i] = keys[i+1]; @@ -631,7 +633,7 @@ public class FloatDict { count--; keys[count] = null; values[count] = 0; - return key; + return value; } diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index 7acfd54b6..96913591f 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -3,6 +3,7 @@ package processing.data; import java.io.*; import java.util.HashMap; import java.util.Iterator; +import java.util.NoSuchElementException; import processing.core.PApplet; @@ -593,19 +594,20 @@ public class IntDict { */ public int remove(String key) { int index = index(key); - if (index != -1) { - removeIndex(index); + if (index == -1) { + throw new NoSuchElementException("'" + key + "' not found"); } - return index; + int value = values[index]; + removeIndex(index); + return value; } - public String removeIndex(int index) { + public int removeIndex(int index) { if (index < 0 || index >= count) { throw new ArrayIndexOutOfBoundsException(index); } - //System.out.println("index is " + which + " and " + keys[which]); - String key = keys[index]; + int value = values[index]; indices.remove(keys[index]); for (int i = index; i < count-1; i++) { keys[i] = keys[i+1]; @@ -615,7 +617,7 @@ public class IntDict { count--; keys[count] = null; values[count] = 0; - return key; + return value; } diff --git a/core/src/processing/data/LongDict.java b/core/src/processing/data/LongDict.java index 18ecac703..529246862 100644 --- a/core/src/processing/data/LongDict.java +++ b/core/src/processing/data/LongDict.java @@ -3,6 +3,7 @@ package processing.data; import java.io.*; import java.util.HashMap; import java.util.Iterator; +import java.util.NoSuchElementException; import processing.core.PApplet; @@ -580,21 +581,22 @@ public class LongDict { * @webref intdict:method * @brief Remove a key/value pair */ - public int remove(String key) { + public long remove(String key) { int index = index(key); - if (index != -1) { - removeIndex(index); + if (index == -1) { + throw new NoSuchElementException("'" + key + "' not found"); } - return index; + long value = values[index]; + removeIndex(index); + return value; } - public String removeIndex(int index) { + public long removeIndex(int index) { if (index < 0 || index >= count) { throw new ArrayIndexOutOfBoundsException(index); } - //System.out.println("index is " + which + " and " + keys[which]); - String key = keys[index]; + long value = values[index]; indices.remove(keys[index]); for (int i = index; i < count-1; i++) { keys[i] = keys[i+1]; @@ -604,7 +606,7 @@ public class LongDict { count--; keys[count] = null; values[count] = 0; - return key; + return value; } diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index 012e266d2..c66a61e4d 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -3,6 +3,7 @@ package processing.data; import java.io.*; import java.util.HashMap; import java.util.Iterator; +import java.util.NoSuchElementException; import processing.core.PApplet; @@ -433,12 +434,14 @@ public class StringDict { * @webref stringdict:method * @brief Remove a key/value pair */ - public int remove(String key) { + public String remove(String key) { int index = index(key); - if (index != -1) { - removeIndex(index); + if (index == -1) { + throw new NoSuchElementException("'" + key + "' not found"); } - return index; + String value = values[index]; + removeIndex(index); + return value; } @@ -446,9 +449,8 @@ public class StringDict { if (index < 0 || index >= count) { throw new ArrayIndexOutOfBoundsException(index); } - //System.out.println("index is " + which + " and " + keys[which]); - String key = keys[index]; - indices.remove(key); + String value = values[index]; + indices.remove(keys[index]); for (int i = index; i < count-1; i++) { keys[i] = keys[i+1]; values[i] = values[i+1]; @@ -457,10 +459,11 @@ public class StringDict { count--; keys[count] = null; values[count] = null; - return key; + return value; } + public void swap(int a, int b) { String tkey = keys[a]; String tvalue = values[a];