Skip to content

Commit db653b5

Browse files
committed
canonic boxing of floats and longs in array seqs
1 parent e4a76e0 commit db653b5

File tree

1 file changed

+32
-66
lines changed

1 file changed

+32
-66
lines changed

src/jvm/clojure/lang/ArraySeq.java

Lines changed: 32 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -210,30 +210,22 @@ public Object reduce(IFn f, Object start) throws Exception{
210210
}
211211

212212
public int indexOf(Object o) {
213-
if (o instanceof Integer) {
214-
int k = ((Integer) o).intValue();
213+
if (o instanceof Number) {
214+
int k = ((Number) o).intValue();
215215
for (int j = i; j < array.length; j++)
216216
if (k == array[j]) return j - i;
217217
}
218-
if (o == null) {
219-
return -1;
220-
}
221-
for (int j = i; j < array.length; j++)
222-
if (o.equals(array[j])) return j - i;
218+
223219
return -1;
224220
}
225221

226222
public int lastIndexOf(Object o) {
227-
if (o instanceof Integer) {
228-
int k = ((Integer) o).intValue();
223+
if (o instanceof Number) {
224+
int k = ((Number) o).intValue();
229225
for (int j = array.length - 1; j >= i; j--)
230226
if (k == array[j]) return j - i;
231227
}
232-
if (o == null) {
233-
return -1;
234-
}
235-
for (int j = array.length - 1; j >= i; j--)
236-
if (o.equals(array[j])) return j - i;
228+
237229
return -1;
238230
}
239231
}
@@ -250,7 +242,7 @@ static public class ArraySeq_float extends ASeq implements IndexedSeq, IReduce{
250242
}
251243

252244
public Object first(){
253-
return array[i];
245+
return Numbers.num(array[i]);
254246
}
255247

256248
public ISeq next(){
@@ -272,44 +264,34 @@ public ArraySeq_float withMeta(IPersistentMap meta){
272264
}
273265

274266
public Object reduce(IFn f) throws Exception{
275-
Object ret = array[i];
267+
Object ret = Numbers.num(array[i]);
276268
for(int x = i + 1; x < array.length; x++)
277-
ret = f.invoke(ret, array[x]);
269+
ret = f.invoke(ret, Numbers.num(array[x]));
278270
return ret;
279271
}
280272

281273
public Object reduce(IFn f, Object start) throws Exception{
282-
Object ret = f.invoke(start, array[i]);
274+
Object ret = f.invoke(start, Numbers.num(array[i]));
283275
for(int x = i + 1; x < array.length; x++)
284-
ret = f.invoke(ret, array[x]);
276+
ret = f.invoke(ret, Numbers.num(array[x]));
285277
return ret;
286278
}
287279

288280
public int indexOf(Object o) {
289-
if (o instanceof Float) {
290-
float f = ((Float) o).floatValue();
281+
if (o instanceof Number) {
282+
float f = ((Number) o).floatValue();
291283
for (int j = i; j < array.length; j++)
292284
if (f == array[j]) return j - i;
293285
}
294-
if (o == null) {
295-
return -1;
296-
}
297-
for (int j = i; j < array.length; j++)
298-
if (o.equals(array[j])) return j - i;
299286
return -1;
300287
}
301288

302289
public int lastIndexOf(Object o) {
303-
if (o instanceof Float) {
304-
float f = ((Float) o).floatValue();
290+
if (o instanceof Number) {
291+
float f = ((Number) o).floatValue();
305292
for (int j = array.length - 1; j >= i; j--)
306293
if (f == array[j]) return j - i;
307294
}
308-
if (o == null) {
309-
return -1;
310-
}
311-
for (int j = array.length - 1; j >= i; j--)
312-
if (o.equals(array[j])) return j - i;
313295
return -1;
314296
}
315297
}
@@ -361,30 +343,22 @@ public Object reduce(IFn f, Object start) throws Exception{
361343
}
362344

363345
public int indexOf(Object o) {
364-
if (o instanceof Double) {
365-
double d = ((Double) o).doubleValue();
346+
if (o instanceof Number) {
347+
double d = ((Number) o).doubleValue();
366348
for (int j = i; j < array.length; j++)
367349
if (d == array[j]) return j - i;
368350
}
369-
if (o == null) {
370-
return -1;
371-
}
372-
for (int j = i; j < array.length; j++)
373-
if (o.equals(array[j])) return j - i;
351+
374352
return -1;
375353
}
376354

377355
public int lastIndexOf(Object o) {
378-
if (o instanceof Double) {
379-
double d = ((Double) o).doubleValue();
356+
if (o instanceof Number) {
357+
double d = ((Number) o).doubleValue();
380358
for (int j = array.length - 1; j >= i; j--)
381359
if (d == array[j]) return j - i;
382360
}
383-
if (o == null) {
384-
return -1;
385-
}
386-
for (int j = array.length - 1; j >= i; j--)
387-
if (o.equals(array[j])) return j - i;
361+
388362
return -1;
389363
}
390364
}
@@ -400,7 +374,7 @@ static public class ArraySeq_long extends ASeq implements IndexedSeq, IReduce{
400374
}
401375

402376
public Object first(){
403-
return array[i];
377+
return Numbers.num(array[i]);
404378
}
405379

406380
public ISeq next(){
@@ -422,44 +396,36 @@ public ArraySeq_long withMeta(IPersistentMap meta){
422396
}
423397

424398
public Object reduce(IFn f) throws Exception{
425-
Object ret = array[i];
399+
Object ret = Numbers.num(array[i]);
426400
for(int x = i + 1; x < array.length; x++)
427-
ret = f.invoke(ret, array[x]);
401+
ret = f.invoke(ret, Numbers.num(array[x]));
428402
return ret;
429403
}
430404

431405
public Object reduce(IFn f, Object start) throws Exception{
432-
Object ret = f.invoke(start, array[i]);
406+
Object ret = f.invoke(start, Numbers.num(array[i]));
433407
for(int x = i + 1; x < array.length; x++)
434-
ret = f.invoke(ret, array[x]);
408+
ret = f.invoke(ret, Numbers.num(array[x]));
435409
return ret;
436410
}
437411

438412
public int indexOf(Object o) {
439-
if (o instanceof Long) {
440-
long l = ((Long) o).longValue();
413+
if (o instanceof Number) {
414+
long l = ((Number) o).longValue();
441415
for (int j = i; j < array.length; j++)
442416
if (l == array[j]) return j - i;
443417
}
444-
if (o == null) {
445-
return -1;
446-
}
447-
for (int j = i; j < array.length; j++)
448-
if (o.equals(array[j])) return j - i;
418+
449419
return -1;
450420
}
451421

452422
public int lastIndexOf(Object o) {
453-
if (o instanceof Long) {
454-
long l = ((Long) o).longValue();
423+
if (o instanceof Number) {
424+
long l = ((Number) o).longValue();
455425
for (int j = array.length - 1; j >= i; j--)
456426
if (l == array[j]) return j - i;
457427
}
458-
if (o == null) {
459-
return -1;
460-
}
461-
for (int j = array.length - 1; j >= i; j--)
462-
if (o.equals(array[j])) return j - i;
428+
463429
return -1;
464430
}
465431
}

0 commit comments

Comments
 (0)