GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Eliminate allocs - String Caches now have Prim subtypes (#1892) · FlyingAndroid/MPAndroidChart@424ee02

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • MPChartExample/src/com/xxmassdeveloper/mpchartexample
    • BarChartPositiveNegative.java
  • MPChartLib/src/main/java/com/github/mikephil/charting/formatter
    • DefaultAxisValueFormatter.java
    • FormattedStringCache.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java‎

Copy file name to clipboardExpand all lines: MPChartExample/src/com/xxmassdeveloper/mpchartexample/BarChartPositiveNegative.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,15 +165,15 @@ public Data(float xValue, float yValue, String xAxisValue) {
165165

166166
private class ValueFormatter implements com.github.mikephil.charting.formatter.ValueFormatter {
167167

168-
private FormattedStringCache<Integer, Float> mFormattedStringCache;
168+
private FormattedStringCache.PrimIntFloat mFormattedStringCache;
169169

170170
public ValueFormatter() {
171-
mFormattedStringCache = new FormattedStringCache<>(new DecimalFormat("######.0"));
171+
mFormattedStringCache = new FormattedStringCache.PrimIntFloat(new DecimalFormat("######.0"));
172172
}
173173

174174
@Override
175175
public String getFormattedValue(float value, Entry entry, int dataSetIndex, ViewPortHandler viewPortHandler) {
176-
return mFormattedStringCache.getFormattedString(value, dataSetIndex);
176+
return mFormattedStringCache.getFormattedValue(value, dataSetIndex);
177177
}
178178
}
179179
}
Collapse file

‎MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java‎

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/formatter/DefaultAxisValueFormatter.java
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class DefaultAxisValueFormatter implements AxisValueFormatter {
1212
/**
1313
* FormattedStringFormat for formatting and caching
1414
*/
15-
protected FormattedStringCache<Float, Float> mFormattedStringCache;
15+
protected FormattedStringCache.PrimFloat mFormattedStringCache;
1616

1717
/**
1818
* the number of decimal digits this formatter uses
@@ -35,14 +35,14 @@ public DefaultAxisValueFormatter(int digits) {
3535
b.append("0");
3636
}
3737

38-
mFormattedStringCache = new FormattedStringCache<>(new DecimalFormat("###,###,###,##0" + b.toString()));
38+
mFormattedStringCache = new FormattedStringCache.PrimFloat(new DecimalFormat("###,###,###,##0" + b.toString()));
3939
}
4040

4141
@Override
4242
public String getFormattedValue(float value, AxisBase axis) {
4343

4444
// TODO: There should be a better way to do this. Floats are not the best keys...
45-
return mFormattedStringCache.getFormattedString(value, value);
45+
return mFormattedStringCache.getFormattedValue(value);
4646

4747
}
4848

Collapse file

‎MPChartLib/src/main/java/com/github/mikephil/charting/formatter/FormattedStringCache.java‎

Copy file name to clipboardExpand all lines: MPChartLib/src/main/java/com/github/mikephil/charting/formatter/FormattedStringCache.java
+79-1Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.mikephil.charting.formatter;
22

33
import java.text.Format;
4+
import java.util.ArrayList;
45
import java.util.HashMap;
56

67
/**
@@ -17,7 +18,7 @@
1718
*/
1819
public class FormattedStringCache<K, V> {
1920

20-
private Format mFormat;
21+
protected Format mFormat;
2122
private HashMap<K, String> mCachedStringsHashMap = new HashMap<>();
2223
private HashMap<K, V> mCachedValuesHashMap = new HashMap<>();
2324

@@ -48,4 +49,81 @@ public String getFormattedString(V value, K key){
4849
return result;
4950
}
5051

52+
public static class PrimIntFloat extends FormattedStringCache{
53+
54+
public PrimIntFloat(Format format){
55+
super(format);
56+
}
57+
58+
private ArrayList<Float> cachedValues = new ArrayList<>();
59+
private ArrayList<String> cachedStrings = new ArrayList<>();
60+
61+
public String getFormattedValue(float value, int key) {
62+
63+
boolean hasValueAtdataSetIndex = true;
64+
if(cachedValues.size() <= key){
65+
int p = key;
66+
while(p >= 0){
67+
if(p == 0){
68+
cachedValues.add(value);
69+
cachedStrings.add("");
70+
}else{
71+
cachedValues.add(Float.NaN);
72+
cachedStrings.add("");
73+
}
74+
p--;
75+
}
76+
hasValueAtdataSetIndex = false;
77+
}
78+
79+
if(hasValueAtdataSetIndex) {
80+
Float cachedValue = cachedValues.get(key);
81+
hasValueAtdataSetIndex = !(cachedValue == null || cachedValue != value);
82+
}
83+
84+
if(!hasValueAtdataSetIndex){
85+
cachedValues.set(key, value);
86+
cachedStrings.set(key, mFormat.format(value));
87+
}
88+
89+
return cachedStrings.get(key);
90+
}
91+
92+
}
93+
94+
public static class PrimFloat extends FormattedStringCache{
95+
96+
public PrimFloat(Format format){
97+
super(format);
98+
}
99+
100+
101+
private ArrayList<Float> cachedValues = new ArrayList<>();
102+
private ArrayList<String> cachedStrings = new ArrayList<>();
103+
104+
public String getFormattedValue(float value) {
105+
106+
boolean alreadyHasValue = false;
107+
int vCount = cachedValues.size();
108+
int sIndex = -1;
109+
for(int i = 0 ; i < vCount ; i++){
110+
if(cachedValues.get(i) == value){
111+
sIndex = i;
112+
alreadyHasValue = true;
113+
break;
114+
}
115+
}
116+
if(!alreadyHasValue) {
117+
cachedValues.add(value);
118+
cachedStrings.add(mFormat.format(value));
119+
sIndex = cachedValues.size() - 1;
120+
}
121+
122+
return cachedStrings.get(sIndex);
123+
}
124+
125+
}
126+
127+
128+
51129
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.