1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
package regtest.basic;
// native arrays
// Array<T> and T[] are synonyms
<T> void p(Array<T>);
p(a)
{
print("size = " + a.length + ", elements = ");
for(int i = 0; i<a.length; i++)
print(""+ String.valueOf(a[i]) + " ");
println("");
}
<T> Array<T> rev(Array<T>);
/* this is an interesting case to keep:
dispatch on Array for a method that only accepts (polymorphic) arrays.
The difficulty here is that the bytecode type for the argument is Object,
while it would be rawArray if the domain of the method was bigger.
*/
<T> rev(a@Array)
{
T[] res = fill(new T[a.length], int i => a[a.length - i - 1]);
return res;
}
void test_arrays();
test_arrays()
{
println("\n### Testing native arrays ###\n");
int[] a = test_native_arrays();
p(a);
p(rev(a));
test_collection_arrays(a);
test_polymorphism();
test_literal_arrays();
test_null_arrays();
}
int[] test_native_arrays();
test_native_arrays()
{
int[] aI = new int[10];
aI[0] = 0;
for(int i=1; i<aI.length; i++)
aI[i] = aI[i-1]+1;
long[] aL = new long[10];
aL[0] = 0L;
for(int i=1; i<aL.length; i++)
aL[i] = aL[i-1]+1;
float[] aF = new float[10];
aF[0] = 2;
for(int i=1; i<aF.length; i++)
aF[i] = aF[i-1]*1.5f;
double[] aD = new double[10];
aD[0] = 2;
for(int i=1; i<aD.length; i++)
aD[i] = aD[i-1]*1.5;
char[] aC = new char[10];
aC[0] = 'A';
for(int i=1; i<aC.length; i++)
aC[i] = aC[i-1];
short[] aS = new short[10];
aS[0] = 0;
for(int i=1; i<aS.length; i++)
aS[i] = aS[i-1];
byte[] aB = new byte[10];
aB[0] = 0;
for(int i=1; i<aB.length; i++)
aB[i] = aB[i-1];
boolean[] aZ = new boolean[10];
aZ[0] = true;
for(int i=1; i<aZ.length; i++)
aZ[i] = !aZ[i-1];
return aI;
}
// using collection functions on arrays
void test_collection_arrays(int[] a);
test_collection_arrays(a)
{
println(a.size());
int[] b = map(a, int i => i+4);
p(b);
p(filter(rev(b), int i=> i>6));
long[] ls = map(a, id);
ls[0] = Long.MAX_VALUE;
p(ls);
byte[] bs = new byte[1];
bs = map(bs, id);
bs[0] = 127;
short[] shs = map(bs, id);
shs[0] = 129;
int[] cs = new int[1];
cs[0] = 65;
double[] ds = map(cs, id);
p(ds);
Number[] ns = map(ds, id);
// idem with non primitive arrays
?String[] ss = new String[5];
ss[2] = "e2"; ss[4] = "e4";
p(ss);
?String[] ss2 = filter(ss, ?String s=>s!=null);
p(ss2);
p(map(ss2, ?String s =>{ String str = notNull(s); return str + str; }));
iter(id(ss), ?String s =>{});
// using an array that is the field of an object
Fields f = new Fields();
notNull(f.strings).iter(?String s =>println(s));
Collection<?String> c = ss;
println(c.size());
// multidimentional arrays
{
int N = 4;
int[][] aa = new int[N][N];
for (int i=0; i<N; i++)
{
for (int j=0; j<N; j++)
{
aa[i][j] = i*2+j;
print("" + aa[i][j] + " ");
}
println("");
}
}
// Literal arrays
String[] ts = [ "A", "B" ];
ts.iter( String s =>println(s));
float[] tf = [ 1.5f, 2f, 3.7f ];
tf.iter(float fl => println(fl + 1));
}
void longarray(long[]) { }
void test_literal_arrays()
{
// longarray must be passed an object of bytecode type long[]
longarray([0]);
// Calling native methods using arrays
let s1 = new String(['S', 'z', 'i', 'a']);
let s2 = new String(id(['S', 'z', 'i', 'a']));
}
// We test how functions on polymorphic arrays deal with native type components
<T> T getFirst(T[] a) = a[0];
// Use a polymorphic array as a collection after losing its type
// because of bytecode types' monomorphicity.
<T> void polyArrayAsCollection(T[] a) { a = id(a).map(id); }
class V<T>
{
T[] elements;
}
<T> V<T> makeV() = new V(elements: cast(new T[1]));
void test_polymorphism()
{
// polymorphic array field
V<String> v = makeV();
String[] elements = v.elements;
v.elements.size();
// Test that different array creation code is generated
// according to the context
byte[] b = [ 14, 17 ];
short[] s = [ 14, 17 ];
int[] i = [ 14, 17 ];
long[] l = [ 14, 17 ];
float[] f = [ 14, 17 ];
double[] d = [ 14, 17 ];
String[] S = [ "Quatorze", "Dix-sept" ];
byte bb = getFirst(b);
short ss = getFirst(s);
int ii = getFirst(i);
long ll = getFirst(l);
float ff = getFirst(f);
double dd = getFirst(d);
String SS = getFirst(S);
int[][] ddim = new int[1][1];
int iii = getFirst(getFirst(ddim));
println("Polymorphic arrays: " + bb + ss + ii + ll + ff + dd + SS + iii);
polyArrayAsCollection(S);
// empty array
// can be written with or without space inside the brackets
b = [];
d = [ ];
}
void test_null_arrays()
{
?Array<boolean> aB = id(null);
aB= arrayId(aB);
?Array<byte> ab = id(null);
ab = arrayId(ab);
?Array<short> as = id(null);
as = arrayId(as);
?Array<char> ac = id(null);
ac = arrayId(ac);
?Array<int> ai = id(null);
ai = arrayId(ai);
?Array<long> al = id(null);
al = arrayId(al);
?Array<float> af = id(null);
af = arrayId(af);
?Array<double> ad = id(null);
ad = arrayId(ad);
?Array<String> aS = id(null);
aS = arrayId(aS);
}
<T> ?Array<T> arrayId(?Array<T> a) = id(a);
// Using array -> Collection implicit conversion in the returned value
<T> Collection<T> wrap(T x) = [ x ];
|