Skip to content

Commit 11b1b23

Browse files
committed
normalize the behavior of remove() in the Dict classes
1 parent 2083040 commit 11b1b23

File tree

5 files changed

+50
-39
lines changed

5 files changed

+50
-39
lines changed

core/src/processing/data/DoubleDict.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.HashMap;
55
import java.util.Iterator;
66
import java.util.Map;
7+
import java.util.NoSuchElementException;
78

89
import processing.core.PApplet;
910

@@ -610,21 +611,22 @@ protected void create(String what, double much) {
610611
* @webref doubledict:method
611612
* @brief Remove a key/value pair
612613
*/
613-
public int remove(String key) {
614+
public double remove(String key) {
614615
int index = index(key);
615-
if (index != -1) {
616-
removeIndex(index);
616+
if (index == -1) {
617+
throw new NoSuchElementException("'" + key + "' not found");
617618
}
618-
return index;
619+
double value = values[index];
620+
removeIndex(index);
621+
return value;
619622
}
620623

621624

622-
public String removeIndex(int index) {
625+
public double removeIndex(int index) {
623626
if (index < 0 || index >= count) {
624627
throw new ArrayIndexOutOfBoundsException(index);
625628
}
626-
String key = keys[index];
627-
//System.out.println("index is " + which + " and " + keys[which]);
629+
double value = values[index];
628630
indices.remove(keys[index]);
629631
for (int i = index; i < count-1; i++) {
630632
keys[i] = keys[i+1];
@@ -634,7 +636,7 @@ public String removeIndex(int index) {
634636
count--;
635637
keys[count] = null;
636638
values[count] = 0;
637-
return key;
639+
return value;
638640
}
639641

640642

core/src/processing/data/FloatDict.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.util.HashMap;
55
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
67

78
import processing.core.PApplet;
89

@@ -607,21 +608,22 @@ protected void create(String what, float much) {
607608
* @webref floatdict:method
608609
* @brief Remove a key/value pair
609610
*/
610-
public int remove(String key) {
611+
public float remove(String key) {
611612
int index = index(key);
612-
if (index != -1) {
613-
removeIndex(index);
613+
if (index == -1) {
614+
throw new NoSuchElementException("'" + key + "' not found");
614615
}
615-
return index;
616+
float value = values[index];
617+
removeIndex(index);
618+
return value;
616619
}
617620

618621

619-
public String removeIndex(int index) {
622+
public float removeIndex(int index) {
620623
if (index < 0 || index >= count) {
621624
throw new ArrayIndexOutOfBoundsException(index);
622625
}
623-
String key = keys[index];
624-
//System.out.println("index is " + which + " and " + keys[which]);
626+
float value = values[index];
625627
indices.remove(keys[index]);
626628
for (int i = index; i < count-1; i++) {
627629
keys[i] = keys[i+1];
@@ -631,7 +633,7 @@ public String removeIndex(int index) {
631633
count--;
632634
keys[count] = null;
633635
values[count] = 0;
634-
return key;
636+
return value;
635637
}
636638

637639

core/src/processing/data/IntDict.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.util.HashMap;
55
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
67

78
import processing.core.PApplet;
89

@@ -593,19 +594,20 @@ protected void create(String what, int much) {
593594
*/
594595
public int remove(String key) {
595596
int index = index(key);
596-
if (index != -1) {
597-
removeIndex(index);
597+
if (index == -1) {
598+
throw new NoSuchElementException("'" + key + "' not found");
598599
}
599-
return index;
600+
int value = values[index];
601+
removeIndex(index);
602+
return value;
600603
}
601604

602605

603-
public String removeIndex(int index) {
606+
public int removeIndex(int index) {
604607
if (index < 0 || index >= count) {
605608
throw new ArrayIndexOutOfBoundsException(index);
606609
}
607-
//System.out.println("index is " + which + " and " + keys[which]);
608-
String key = keys[index];
610+
int value = values[index];
609611
indices.remove(keys[index]);
610612
for (int i = index; i < count-1; i++) {
611613
keys[i] = keys[i+1];
@@ -615,7 +617,7 @@ public String removeIndex(int index) {
615617
count--;
616618
keys[count] = null;
617619
values[count] = 0;
618-
return key;
620+
return value;
619621
}
620622

621623

core/src/processing/data/LongDict.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.util.HashMap;
55
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
67

78
import processing.core.PApplet;
89

@@ -580,21 +581,22 @@ protected void create(String what, long much) {
580581
* @webref intdict:method
581582
* @brief Remove a key/value pair
582583
*/
583-
public int remove(String key) {
584+
public long remove(String key) {
584585
int index = index(key);
585-
if (index != -1) {
586-
removeIndex(index);
586+
if (index == -1) {
587+
throw new NoSuchElementException("'" + key + "' not found");
587588
}
588-
return index;
589+
long value = values[index];
590+
removeIndex(index);
591+
return value;
589592
}
590593

591594

592-
public String removeIndex(int index) {
595+
public long removeIndex(int index) {
593596
if (index < 0 || index >= count) {
594597
throw new ArrayIndexOutOfBoundsException(index);
595598
}
596-
//System.out.println("index is " + which + " and " + keys[which]);
597-
String key = keys[index];
599+
long value = values[index];
598600
indices.remove(keys[index]);
599601
for (int i = index; i < count-1; i++) {
600602
keys[i] = keys[i+1];
@@ -604,7 +606,7 @@ public String removeIndex(int index) {
604606
count--;
605607
keys[count] = null;
606608
values[count] = 0;
607-
return key;
609+
return value;
608610
}
609611

610612

core/src/processing/data/StringDict.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.*;
44
import java.util.HashMap;
55
import java.util.Iterator;
6+
import java.util.NoSuchElementException;
67

78
import processing.core.PApplet;
89

@@ -433,22 +434,23 @@ protected void create(String key, String value) {
433434
* @webref stringdict:method
434435
* @brief Remove a key/value pair
435436
*/
436-
public int remove(String key) {
437+
public String remove(String key) {
437438
int index = index(key);
438-
if (index != -1) {
439-
removeIndex(index);
439+
if (index == -1) {
440+
throw new NoSuchElementException("'" + key + "' not found");
440441
}
441-
return index;
442+
String value = values[index];
443+
removeIndex(index);
444+
return value;
442445
}
443446

444447

445448
public String removeIndex(int index) {
446449
if (index < 0 || index >= count) {
447450
throw new ArrayIndexOutOfBoundsException(index);
448451
}
449-
//System.out.println("index is " + which + " and " + keys[which]);
450-
String key = keys[index];
451-
indices.remove(key);
452+
String value = values[index];
453+
indices.remove(keys[index]);
452454
for (int i = index; i < count-1; i++) {
453455
keys[i] = keys[i+1];
454456
values[i] = values[i+1];
@@ -457,10 +459,11 @@ public String removeIndex(int index) {
457459
count--;
458460
keys[count] = null;
459461
values[count] = null;
460-
return key;
462+
return value;
461463
}
462464

463465

466+
464467
public void swap(int a, int b) {
465468
String tkey = keys[a];
466469
String tvalue = values[a];

0 commit comments

Comments
 (0)