Skip to content

Commit 5a59be0

Browse files
hansonrhansonr
authored andcommitted
faster String.toCharArray(); Differences.txt
1 parent 4c966dd commit 5a59be0

File tree

10 files changed

+258
-26
lines changed

10 files changed

+258
-26
lines changed
-1.84 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190727175738
1+
20190814182015
-1.84 KB
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190727175738
1+
20190814182015
-1.84 KB
Binary file not shown.

sources/net.sf.j2s.java.core/doc/Differences.txt

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
Notes
22
=====
3+
updated 8/16/19 -- minor typos and added summary paragraph
34
updated 7/19/19 -- clarification that AWT and Swing classes are supported directly
45
updated 5/13/19 -- Mandarin U+79D8 reserved character; Missing Math methods; int and long
56
updated 5/10/19 -- adds a section on static issues in multi-(duplicate)-applet pages
67
updated 1/4/19 -- nio
7-
updated 9/15/18 -- adds integer 1/0 == 0
8+
updated 9/15/18 -- adds integer 1/0 == Infinity
89
updated 7/24/18 -- most classes replaced with https://github.com/frohoff/jdk8u-jdk
910
updated 6/5/17 -- reserved package name "window"
1011
updated 3/11/17 -- myClass.getField
@@ -537,14 +538,14 @@ qualified field and method names
537538

538539
In order to minimize the chance of added SwingJS field and method names colliding with ones
539540
developers might use in subclassing Java classes, we have added U+79D8 (first character of Mandarin
540-
"secrect") to the characters already disrecommended by Java documentation ("$" and "_"). The only problem
541+
"secret") to the characters already disrecommended by Java documentation ("$" and "_"). The only problem
541542
would be if you use that character followed by certain English words in certain classes. For example
542543
\u79D8canvas for JComponents (in java.awt.JSComponent) and \u79D8byte (in java.io.File).
543544

544545
missing Math methods
545546
--------------------
546547

547-
java.lang.Math is worked out, but some methods are missing, eithr because they
548+
java.lang.Math is worked out, but some methods are missing, either because they
548549
involve long integer value that are inaccessible in JavaScript, or because I just
549550
didn't implement them. This is a result of continued Java development.
550551
It is easy enough to add these methods if you have the source. They go into j2sClazz.js,
@@ -566,7 +567,7 @@ For example, this will definitely NOT work in SwingJS:
566567

567568
this.paint(getGraphics())
568569

569-
and really should not in Java, either, as it is technically a resource memory leak.
570+
and really should not work in Java, either, as it is technically a resource memory leak.
570571

571572
Instead, if you really do not want to use repaint(), use this:
572573

@@ -601,7 +602,7 @@ Static classes such as:
601602

602603
which are created using Class.forName are implemented using classes in the swingjs package.
603604

604-
AWTAccessor and AwtContext need to be customized
605+
AWTAccessor is not implemented.
605606

606607

607608
AWT component peers and component "ui" user interfaces
@@ -739,7 +740,7 @@ to BigInteger:
739740
* the integer storage bit length to 24, giving 48 for long and leaving
740741
* the last 16 bits clear for the exponent of the double number. This should
741742
* not affect performance significantly. It does increase the storage
742-
* size by about 33%. By bring an "int" to 3 bytes, we can easily construct
743+
* size by about 33%. By bringing an "int" to 3 bytes, we can easily construct
743744
* and use byte[] data intended for the original BitSet.
744745

745746
"Easily" may be a bit strong there. This was a serious challenge.
@@ -772,13 +773,11 @@ JEditorPane (JavaScript <div>)
772773

773774
For the initial implementation, we don't implement infinite undo/redo, and the abstract
774775
document model is much less elaborate. Only PlainDocument (in the form of JSPlainDocument)
775-
is implemented.
776-
The Document returned by JTextField.getDocument() is a javax.swing.text.Document.
776+
is implemented. The Document returned by JTextField.getDocument() is a javax.swing.text.Document.
777777

778-
all scrolling is handled by HTML5
779-
javax.swing.AutoScroller is not implemented
778+
All scrolling is handled by HTML5. javax.swing.AutoScroller is not implemented.
780779
public static methods .stop, .isRunning, .processMouseDragged require true Java threading
781-
javax.swing.text.View and its subclasses are not implemented.
780+
and so are not implmented. javax.swing.text.View and its subclasses are not implemented.
782781

783782
The JS document model does not allow two text fields to address the same underlying document.
784783

@@ -792,9 +791,23 @@ Matcher.start(groupID) is not supported.
792791

793792
java.util.Formatter will function correctly for all standard %... patterns.
794793

795-
integer 1/0 == 0
796-
----------------
794+
integer 1/0 == Infinity
795+
-----------------------
797796

798797
1/0 in Java throws "java.lang.ArithmeticException: / by zero", but in JavaScript is just Infinity.
799798

800-
799+
800+
801+
Summary
802+
-------
803+
804+
These are all the known limitations of SwingJS. We have not found any of these limitations
805+
to be show-stoppers. The primary issue for newcomers to SwingJS is having the source code.
806+
You must check that source code for all your library jar files is available or, if you
807+
choose, you will need to decompile those classes. We have used decompilation on some projects,
808+
and it works just fine. So, technically, all we really need are JAR/class files. But the
809+
source is by far superior. It's generally prettier, and it has the license information that
810+
may or may not be present with the JAR or class files. Use class files at your own risk.
811+
812+
Bob Hanson
813+
2019.08.16

sources/net.sf.j2s.java.core/src/test/Test_Array.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ public static void main(String[] args) {
8888
double[] da = new double[3];
8989
System.out.println(da[iI]);
9090
assert (ii3[2] == 1 && j == 1);
91+
9192

9293
System.out.println("Test_Array OK");
9394
}
9495

96+
9597
}
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
package test;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Comparator;
6+
import java.util.List;
7+
import java.util.Random;
8+
9+
class Test_Interval extends Test_ {
10+
11+
static int[] i0 = new int[0];
12+
13+
static int[][] a = new int[][] {
14+
{ 1, 7 },
15+
{ 2, 4 },
16+
{ 3, 5 },
17+
{ 3, 6 },
18+
{ 4, 5 },
19+
{ 7, 8 },
20+
{ 9, 10 }
21+
22+
};
23+
24+
public static void main(String[] args) {
25+
26+
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
27+
int[] ap = new int[] { 10, 20, 30, 40, 50, 50, 50, 60, 70, 80, 90, 100, 110, 120 };
28+
29+
System.out.println(findClosestPoint(ap, 11, true)); // 0
30+
System.out.println(findClosestPoint(ap, 11, false)); // 1
31+
System.out.println(findClosestPoint(ap, 51, true)); // 6
32+
System.out.println(findClosestPoint(ap, 51, false)); // 7
33+
System.out.println(findClosestPoint(ap, 130, true));// -1
34+
System.out.println(findClosestPoint(ap, 130, false)); // -1
35+
System.out.println(findClosestPoint(ap, 0, true)); // -1
36+
System.out.println(findClosestPoint(ap, 0, false)); // -1
37+
System.out.println(findClosestPoint(ap, 30, true)); // 2
38+
System.out.println(findClosestPoint(ap, 60, true)); // 7
39+
System.out.println(findClosestPoint(ap, 50, true)); // 6
40+
System.out.println(findClosestPoint(ap, 50, false)); // 4
41+
42+
assert (findClosestPoint(ap, 11, true) == 0);
43+
assert (findClosestPoint(ap, 11, false) == 1);
44+
assert (findClosestPoint(ap, 51, true) == 6);
45+
assert (findClosestPoint(ap, 51, false) == 7);
46+
assert (findClosestPoint(ap, 130, true) == -1);
47+
assert (findClosestPoint(ap, 130, false) == -1);
48+
assert (findClosestPoint(ap, 0, true) == -1);
49+
assert (findClosestPoint(ap, 0, false) == -1);
50+
assert (findClosestPoint(ap, 30, true) == 2);
51+
assert (findClosestPoint(ap, 60, true) == 7);
52+
assert (findClosestPoint(ap, 50, true) == 6);
53+
assert (findClosestPoint(ap, 50, false) == 4);
54+
55+
new Test_Interval().findIntervals(a, 3);
56+
new Test_Interval().findIntervals(a, 5);
57+
new Test_Interval().findIntervals(a, 9);
58+
59+
System.out.println("Test_Interval OK");
60+
}
61+
62+
private static Interval[] ints;
63+
64+
private void findIntervals(int[][] a, int pos) {
65+
try {
66+
Thread.sleep(500);
67+
} catch (InterruptedException e) {
68+
// TODO Auto-generated catch block
69+
e.printStackTrace();
70+
}
71+
Random rand = new Random();
72+
int n = a.length;
73+
Interval[] intervals = new Interval[n];
74+
for (int i = 0; i < n;) {
75+
int r = rand.nextInt(n);
76+
if (intervals[r] == null) {
77+
intervals[r] = new Interval(a[i][0], a[i][1], i);
78+
i++;
79+
}
80+
}
81+
System.out.println("=====");
82+
//dumpIntervals(intervals);
83+
System.out.println("-----");
84+
ints = intervals;
85+
Arrays.sort(intervals, new IComparator());
86+
linkIntervals(intervals);
87+
//dumpIntervals(intervals);
88+
List<Interval> result = new ArrayList<>();
89+
findInterval(intervals, pos, result);
90+
for (int i = 0; i < result.size(); i++)
91+
System.out.println(pos + " " + result.get(i));
92+
93+
}
94+
95+
private void findInterval(Interval[] intervals, int pos, List<Interval> result) {
96+
Interval ithis = findClosestInterval(intervals, pos);
97+
while (ithis != null) {
98+
if (ithis.end >= pos)
99+
result.add(ithis);
100+
ithis = ithis.containedBy;
101+
}
102+
}
103+
104+
private Interval findClosestInterval(Interval[] l, int pos) {
105+
int low = 0;
106+
int high = l.length - 1;
107+
int mid = 0;
108+
while (low <= high) {
109+
mid = (low + high) >>> 1;
110+
int f = l[mid].begin;
111+
switch (Long.signum(f - pos)) {
112+
case -1:
113+
low = mid + 1;
114+
continue;
115+
case 1:
116+
high = mid - 1;
117+
continue;
118+
case 0:
119+
while (++mid <= high && l[mid].begin == pos) {
120+
;
121+
}
122+
mid--;
123+
return l[mid];
124+
}
125+
}
126+
return (high < 0 || low >= l.length ? null : l[high]);
127+
}
128+
129+
static void dumpIntervals(Interval[] intervals) {
130+
for (int i = 0; i < intervals.length; i++) {
131+
System.out.println(intervals[i]);
132+
}
133+
System.out.println("");
134+
}
135+
136+
public void linkIntervals(Interval[] intervals) {
137+
if (intervals.length < 2)
138+
return;
139+
int maxEnd = intervals[0].end;
140+
for (int i = 1, n = intervals.length; i < n; i++) {
141+
Interval ithis = intervals[i];
142+
if (ithis.begin <= maxEnd)
143+
ithis.containedBy = getContainedBy(intervals, i);
144+
if (ithis.end > maxEnd)
145+
maxEnd = ithis.end;
146+
}
147+
148+
}
149+
150+
private Interval getContainedBy(Interval[] intervals, int i) {
151+
Interval ithis = intervals[i];
152+
while (--i >= 0) {
153+
Interval ilast = intervals[i];
154+
if (ithis.begin <= ilast.end) {
155+
return ilast;
156+
}
157+
}
158+
return null;
159+
}
160+
161+
class Interval {
162+
int begin, end, index;
163+
Interval containedBy;
164+
165+
Interval(int begin, int end, int index) {
166+
this.begin = begin;
167+
this.end = end;
168+
this.index = index;
169+
}
170+
171+
@Override
172+
public String toString() {
173+
return "I" + index + " begin:" + begin + " end:" + end + " cont:"
174+
+ (containedBy == null ? "?" : containedBy.index);
175+
}
176+
177+
}
178+
179+
class IComparator implements Comparator<Interval> {
180+
181+
@Override
182+
public int compare(Interval a, Interval b) {
183+
int val = (a.begin < b.begin ? -1 : a.begin > b.begin ? 1 : a.end > b.end ? 1 : a.end < b.end ? -1 : 0);
184+
return val;
185+
}
186+
187+
}
188+
189+
static private int findClosestPoint(int[] l, int pos, boolean isStart) {
190+
int low = 0;
191+
int high = l.length - 1;
192+
int mid = 0;
193+
while (low <= high) {
194+
mid = (low + high) >>> 1;
195+
int f = l[mid];
196+
switch (Long.signum(f - pos)) {
197+
case -1:
198+
low = mid + 1;
199+
continue;
200+
case 1:
201+
high = mid - 1;
202+
continue;
203+
case 0:
204+
if (isStart) {
205+
while (--mid >= low && (f = l[mid]) != -1 && f == pos) {
206+
;
207+
}
208+
++mid;
209+
} else {
210+
while (++mid <= high && (f = l[mid]) != -1 && f == pos) {
211+
;
212+
}
213+
mid--;
214+
}
215+
return mid;
216+
}
217+
}
218+
// -1 here?
219+
System.out.println(isStart + " " + low + " " + mid + " " + high);
220+
return (high < 0 || low >= l.length ? -1 : isStart ? high : low);
221+
}
222+
223+
}

sources/net.sf.j2s.java.core/srcjs/js/j2sClazz.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4424,11 +4424,8 @@ sp.contains$S = function(a) {return this.indexOf(a) >= 0} // bh added
44244424
sp.compareTo$ = sp.compareTo$S = sp.compareTo$TT = function(a){return this > a ? 1 : this < a ? -1 : 0} // bh added
44254425

44264426
sp.toCharArray$=function(){
4427-
var result=new Array(this.length);
4428-
for(var i=0;i<this.length;i++){
4429-
result[i]=this.charAt(i);
4430-
}
4431-
return Clazz.array(Character.TYPE, -1, result);
4427+
var result = this.split("");
4428+
return setArray(result, Character.TYPE, "CA", -1);
44324429
};
44334430

44344431
String.valueOf$ = String.valueOf$Z = String.valueOf$C = String.valueOf$CA

sources/net.sf.j2s.java.core/srcjs/swingjs2.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18146,11 +18146,8 @@ sp.contains$S = function(a) {return this.indexOf(a) >= 0} // bh added
1814618146
sp.compareTo$ = sp.compareTo$S = sp.compareTo$TT = function(a){return this > a ? 1 : this < a ? -1 : 0} // bh added
1814718147

1814818148
sp.toCharArray$=function(){
18149-
var result=new Array(this.length);
18150-
for(var i=0;i<this.length;i++){
18151-
result[i]=this.charAt(i);
18152-
}
18153-
return Clazz.array(Character.TYPE, -1, result);
18149+
var result = this.split("");
18150+
return setArray(result, Character.TYPE, "CA", -1);
1815418151
};
1815518152

1815618153
String.valueOf$ = String.valueOf$Z = String.valueOf$C = String.valueOf$CA

0 commit comments

Comments
 (0)