From 66fe4bfdb4c320aeb22f04941ae4de966c7660b0 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 29 May 2013 06:52:41 -0400 Subject: [PATCH] fixes for Iterable inconsistency --- core/src/processing/data/IntDict.java | 118 +++++++++++++++++++------- core/todo.txt | 5 ++ 2 files changed, 90 insertions(+), 33 deletions(-) diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index 117b83d7b..d16cb10fe 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -9,7 +9,7 @@ import processing.core.PApplet; /** * A simple class to use a String as a lookup for an int value. - * + * * @webref data:composite */ public class IntDict { @@ -109,9 +109,9 @@ public class IntDict { } - /** + /** * Remove all entries. - * + * * @webref intdict:method * @brief Remove all entries */ @@ -126,25 +126,25 @@ public class IntDict { } - private void crop() { - if (count != keys.length) { - keys = PApplet.subset(keys, 0, count); - values = PApplet.subset(values, 0, count); - } - } +// private void crop() { +// if (count != keys.length) { +// keys = PApplet.subset(keys, 0, count); +// values = PApplet.subset(values, 0, count); +// } +// } /** * Return the internal array being used to store the keys. Allocated but * unused entries will be removed. This array should not be modified. - * + * * @webref intdict:method * @brief Return the internal array being used to store the keys */ - public String[] keys() { - crop(); - return keys; - } +// public String[] keys() { +// crop(); +// return keys; +// } // public Iterable keys() { @@ -152,6 +152,54 @@ public class IntDict { // // @Override // public Iterator iterator() { +// return new Iterator() { +// int index = -1; +// +// public void remove() { +// removeIndex(index); +// } +// +// public String next() { +// return key(++index); +// } +// +// public boolean hasNext() { +// return index+1 < size(); +// } +// }; +// } +// }; +// } + + + // Use this with 'for' loops + public Iterable keys() { + return new Iterable() { + + @Override + public Iterator iterator() { + return keyIterator(); +// return new Iterator() { +// int index = -1; +// +// public void remove() { +// removeIndex(index); +// } +// +// public String next() { +// return key(++index); +// } +// +// public boolean hasNext() { +// return index+1 < size(); +// } +// }; + } + }; + } + + + // Use this to iterate when you want to be able to remove elements along the way public Iterator keyIterator() { return new Iterator() { int index = -1; @@ -168,8 +216,6 @@ public class IntDict { return index+1 < size(); } }; -// } -// }; } @@ -197,7 +243,8 @@ public class IntDict { return values[index]; } - /** + + /** * @webref intdict:method * @brief To come... */ @@ -206,21 +253,26 @@ public class IntDict { @Override public Iterator iterator() { - return new Iterator() { - int index = -1; + return valueIterator(); + } + }; + } - public void remove() { - removeIndex(index); - } - public Integer next() { - return value(++index); - } + public Iterator valueIterator() { + return new Iterator() { + int index = -1; - public boolean hasNext() { - return index+1 < size(); - } - }; + public void remove() { + removeIndex(index); + } + + public Integer next() { + return value(++index); + } + + public boolean hasNext() { + return index+1 < size(); } }; } @@ -285,9 +337,9 @@ public class IntDict { } - /** - * Increase the value of a specific key by 1. - * + /** + * Increase the value of a specific key by 1. + * * @webref intdict:method * @brief Increase the value of a specific key by 1 */ @@ -414,7 +466,7 @@ public class IntDict { /** * Sort by values in descending order (largest value will be at [0]). - * + * * @webref intdict:method * @brief Sort by values in descending order */ diff --git a/core/todo.txt b/core/todo.txt index 55e229a53..1b896ca0d 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -1,4 +1,9 @@ 0218 core (2.0b10 or 2.0) +_ data decision stuff +_ key/valueIterator to get a version that can be removed in a loop +_ should keys() returns cropped internal array after all? +_ right now it's making a copy (which is safer, so folks don't modify) +_ but this means that we don't have a fast method for access before 2.0