Skip to content

Commit 99ab612

Browse files
committed
add new String/Int/FloatDict constructors for easier initialization
1 parent 5915352 commit 99ab612

5 files changed

Lines changed: 73 additions & 0 deletions

File tree

core/src/processing/data/FloatDict.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,28 @@ public FloatDict(BufferedReader reader) {
6868
}
6969
}
7070

71+
72+
/**
73+
* Constructor to allow (more intuitive) inline initialization, e.g.:
74+
* <pre>
75+
* new FloatDict(new Object[][] {
76+
* { "key1", 1 },
77+
* { "key2", 2 }
78+
* });
79+
* </pre>
80+
*/
81+
public FloatDict(Object[][] pairs) {
82+
count = pairs.length;
83+
this.keys = new String[count];
84+
this.values = new float[count];
85+
for (int i = 0; i < count; i++) {
86+
keys[i] = (String) pairs[i][0];
87+
values[i] = (Float) pairs[i][1];
88+
indices.put(keys[i], i);
89+
}
90+
}
91+
92+
7193
/**
7294
* @nowebref
7395
*/

core/src/processing/data/IntDict.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,28 @@ public IntDict(String[] keys, int[] values) {
8383
}
8484
}
8585

86+
87+
/**
88+
* Constructor to allow (more intuitive) inline initialization, e.g.:
89+
* <pre>
90+
* new FloatDict(new Object[][] {
91+
* { "key1", 1 },
92+
* { "key2", 2 }
93+
* });
94+
* </pre>
95+
*/
96+
public IntDict(Object[][] pairs) {
97+
count = pairs.length;
98+
this.keys = new String[count];
99+
this.values = new int[count];
100+
for (int i = 0; i < count; i++) {
101+
keys[i] = (String) pairs[i][0];
102+
values[i] = (Integer) pairs[i][1];
103+
indices.put(keys[i], i);
104+
}
105+
}
106+
107+
86108
/**
87109
* Returns the number of key/value pairs
88110
*

core/src/processing/data/StringDict.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ public StringDict(BufferedReader reader) {
6969
}
7070
}
7171

72+
7273
/**
7374
* @nowebref
7475
*/
@@ -84,6 +85,29 @@ public StringDict(String[] keys, String[] values) {
8485
}
8586
}
8687

88+
89+
/**
90+
* Constructor to allow (more intuitive) inline initialization, e.g.:
91+
* <pre>
92+
* new StringDict(new String[][] {
93+
* { "key1", "value1" },
94+
* { "key2", "value2" }
95+
* });
96+
* </pre>
97+
* It's no Python, but beats a static { } block with HashMap.put() statements.
98+
*/
99+
public StringDict(String[][] pairs) {
100+
count = pairs.length;
101+
this.keys = new String[count];
102+
this.values = new String[count];
103+
for (int i = 0; i < count; i++) {
104+
keys[i] = pairs[i][0];
105+
values[i] = pairs[i][1];
106+
indices.put(keys[i], i);
107+
}
108+
}
109+
110+
87111
/**
88112
* @webref stringdict:method
89113
* @brief Returns the number of key/value pairs

core/todo.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ X pop() was not implemented correctly
33
X fairly major rewrite of createShape()
44
X prevents same code from appearing 5x (!) in the source
55
X improves bad api design with the static createShapeImpl() methods
6+
X add new String/Int/FloatDict constructors for easier initialization
67

78

89
_ need reference update for createShape()
@@ -128,6 +129,7 @@ _ https://github.com/processing/processing/issues/2778
128129
_ make join() work with Iterable?
129130
_ will this collide with the current String[] version?
130131
_ remove OPENGL constant (tell people to use P3D or P2D)
132+
_ exec() and open() to use varargs
131133

132134

133135
under consideration

todo.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ contribs
99
X Window size not passing into Tweak Mode
1010
X https://github.com/processing/processing/issues/3208
1111
X https://github.com/processing/processing/pull/3227
12+
X preferences window fixes for Linux
13+
X https://github.com/processing/processing/pull/3232
14+
X https://github.com/processing/processing/issues/3231
1215

1316

1417
_ we've lost arrow keys to expand items in the examples window

0 commit comments

Comments
 (0)