1- /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2-
3- /*
4- Part of the Processing project - http://processing.org
5-
6- Copyright (c) 2013-16 The Processing Foundation
7-
8- This library is free software; you can redistribute it and/or
9- modify it under the terms of the GNU Lesser General Public
10- License as published by the Free Software Foundation, version 2.
11-
12- This library is distributed in the hope that it will be useful,
13- but WITHOUT ANY WARRANTY; without even the implied warranty
14- of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15- See the GNU Lesser General Public License for more details.
16-
17- You should have received a copy of the GNU Lesser General
18- Public License along with this library; if not, write to the
19- Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20- Boston, MA 02111-1307 USA
21- */
22-
231package processing .data ;
242
253import java .io .*;
@@ -45,7 +23,7 @@ public class FloatDict {
4523 protected float [] values ;
4624
4725 /** Internal implementation for faster lookups */
48- private HashMap <String , Integer > indices = new HashMap <String , Integer >();
26+ private HashMap <String , Integer > indices = new HashMap <>();
4927
5028
5129 public FloatDict () {
@@ -137,6 +115,31 @@ public int size() {
137115 }
138116
139117
118+ /**
119+ * Resize the internal data, this can only be used to shrink the list.
120+ * Helpful for situations like sorting and then grabbing the top 50 entries.
121+ */
122+ public void resize (int length ) {
123+ if (length == count ) return ;
124+
125+ if (length > count ) {
126+ throw new IllegalArgumentException ("resize() can only be used to shrink the dictionary" );
127+ }
128+ if (length < 1 ) {
129+ throw new IllegalArgumentException ("resize(" + length + ") is too small, use 1 or higher" );
130+ }
131+
132+ String [] newKeys = new String [length ];
133+ float [] newValues = new float [length ];
134+ PApplet .arrayCopy (keys , newKeys , length );
135+ PApplet .arrayCopy (values , newValues , length );
136+ keys = newKeys ;
137+ values = newValues ;
138+ count = length ;
139+ resetIndices ();
140+ }
141+
142+
140143 /**
141144 * Remove all entries.
142145 *
@@ -145,7 +148,15 @@ public int size() {
145148 */
146149 public void clear () {
147150 count = 0 ;
148- indices = new HashMap <String , Integer >();
151+ indices = new HashMap <>();
152+ }
153+
154+
155+ private void resetIndices () {
156+ indices = new HashMap <>(count );
157+ for (int i = 0 ; i < count ; i ++) {
158+ indices .put (keys [i ], i );
159+ }
149160 }
150161
151162
@@ -183,8 +194,8 @@ public void remove() {
183194 }
184195
185196 public Entry next () {
197+ ++index ;
186198 Entry e = new Entry (keys [index ], values [index ]);
187- index ++;
188199 return e ;
189200 }
190201
@@ -368,6 +379,15 @@ public void set(String key, float amount) {
368379 }
369380
370381
382+ public void setIndex (int index , String key , float value ) {
383+ if (index < 0 || index >= count ) {
384+ throw new ArrayIndexOutOfBoundsException (index );
385+ }
386+ keys [index ] = key ;
387+ values [index ] = value ;
388+ }
389+
390+
371391 /**
372392 * @webref floatdict:method
373393 * @brief Check if a key is a part of the data structure
@@ -427,7 +447,7 @@ public void div(String key, float amount) {
427447 private void checkMinMax (String functionName ) {
428448 if (count == 0 ) {
429449 String msg =
430- String .format ("Cannot use %s() on an empty %s." ,
450+ String .format ("Cannot use %s() on an empty %s." ,
431451 functionName , getClass ().getSimpleName ());
432452 throw new RuntimeException (msg );
433453 }
@@ -544,6 +564,27 @@ public float maxValue() {
544564 }
545565
546566
567+ public float sum () {
568+ double amount = sumDouble ();
569+ if (amount > Float .MAX_VALUE ) {
570+ throw new RuntimeException ("sum() exceeds " + Float .MAX_VALUE + ", use sumDouble()" );
571+ }
572+ if (amount < -Float .MAX_VALUE ) {
573+ throw new RuntimeException ("sum() lower than " + -Float .MAX_VALUE + ", use sumDouble()" );
574+ }
575+ return (float ) amount ;
576+ }
577+
578+
579+ public double sumDouble () {
580+ double sum = 0 ;
581+ for (int i = 0 ; i < count ; i ++) {
582+ sum += values [i ];
583+ }
584+ return sum ;
585+ }
586+
587+
547588 public int index (String what ) {
548589 Integer found = indices .get (what );
549590 return (found == null ) ? -1 : found .intValue ();
@@ -717,10 +758,7 @@ public void swap(int a, int b) {
717758 s .run ();
718759
719760 // Set the indices after sort/swaps (performance fix 160411)
720- indices = new HashMap <String , Integer >();
721- for (int i = 0 ; i < count ; i ++) {
722- indices .put (keys [i ], i );
723- }
761+ resetIndices ();
724762 }
725763
726764
@@ -730,10 +768,7 @@ public void swap(int a, int b) {
730768 * @return a FloatDict with the original keys, mapped to their pct of the total
731769 */
732770 public FloatDict getPercent () {
733- double sum = 0 ;
734- for (int i = 0 ; i < count ; i ++) {
735- sum += values [i ];
736- }
771+ double sum = sum ();
737772 FloatDict outgoing = new FloatDict ();
738773 for (int i = 0 ; i < size (); i ++) {
739774 double percent = value (i ) / sum ;
@@ -791,4 +826,4 @@ public String toJSON() {
791826 public String toString () {
792827 return getClass ().getSimpleName () + " size=" + size () + " " + toJSON ();
793828 }
794- }
829+ }
0 commit comments