ArrayUtils.java

  1. /*
  2.  * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. This
  3.  * code is released under a tri EPL/GPL/LGPL license. You can use it,
  4.  * redistribute it and/or modify it under the terms of the:
  5.  *
  6.  * Eclipse Public License version 1.0
  7.  * GNU General Public License version 2
  8.  * GNU Lesser General Public License version 2.1
  9.  */
  10. package org.jruby.truffle.runtime.util;

  11. import com.oracle.truffle.api.CompilerAsserts;
  12. import org.jruby.truffle.nodes.RubyNode;
  13. import org.jruby.truffle.runtime.RubyArguments;

  14. import java.util.Arrays;

  15. public abstract class ArrayUtils {

  16.     public static boolean contains(int[] array, int value) {
  17.         for (int n = 0; n < array.length; n++) {
  18.             if (array[n] == value) {
  19.                 return true;
  20.             }
  21.         }

  22.         return false;
  23.     }

  24.     public static boolean contains(long[] array, long value) {
  25.         for (int n = 0; n < array.length; n++) {
  26.             if (array[n] == value) {
  27.                 return true;
  28.             }
  29.         }

  30.         return false;
  31.     }

  32.     public static boolean contains(double[] array, double value) {
  33.         for (int n = 0; n < array.length; n++) {
  34.             if (array[n] == value) {
  35.                 return true;
  36.             }
  37.         }

  38.         return false;
  39.     }

  40.     public static boolean contains(Object[] array, int length, Object value) {
  41.         for (int n = 0; n < length; n++) {
  42.             if (array[n].equals(value)) {
  43.                 return true;
  44.             }
  45.         }

  46.         return false;
  47.     }

  48.     public static Object[] box(int[] unboxed) {
  49.         return box(unboxed, 0);
  50.     }

  51.     public static Object[] box(long[] unboxed) {
  52.         return box(unboxed, 0);
  53.     }

  54.     public static Object[] box(double[] unboxed) {
  55.         return box(unboxed, 0);
  56.     }

  57.     public static Object[] box(Object array) {
  58.         return box(array, 0);
  59.     }

  60.     public static Object[] box(int[] unboxed, int extra) {
  61.         final Object[] boxed = new Object[unboxed.length + extra];

  62.         for (int n = 0; n < unboxed.length; n++) {
  63.             boxed[n] = unboxed[n];
  64.         }

  65.         return boxed;
  66.     }

  67.     public static Object[] box(long[] unboxed, int extra) {
  68.         final Object[] boxed = new Object[unboxed.length + extra];

  69.         for (int n = 0; n < unboxed.length; n++) {
  70.             boxed[n] = unboxed[n];
  71.         }

  72.         return boxed;
  73.     }

  74.     public static Object[] box(double[] unboxed, int extra) {
  75.         final Object[] boxed = new Object[unboxed.length + extra];

  76.         for (int n = 0; n < unboxed.length; n++) {
  77.             boxed[n] = unboxed[n];
  78.         }

  79.         return boxed;
  80.     }

  81.     public static Object[] boxUntil(int[] unboxed, int length) {
  82.         final Object[] boxed = new Object[length];

  83.         for (int n = 0; n < length; n++) {
  84.             boxed[n] = unboxed[n];
  85.         }

  86.         return boxed;
  87.     }

  88.     public static Object[] boxUntil(long[] unboxed, int length) {
  89.         final Object[] boxed = new Object[length];

  90.         for (int n = 0; n < length; n++) {
  91.             boxed[n] = unboxed[n];
  92.         }

  93.         return boxed;
  94.     }

  95.     public static Object[] boxUntil(double[] unboxed, int length) {
  96.         final Object[] boxed = new Object[length];

  97.         for (int n = 0; n < length; n++) {
  98.             boxed[n] = unboxed[n];
  99.         }

  100.         return boxed;
  101.     }

  102.     public static Object[] box(Object array, int extra) {
  103.         CompilerAsserts.neverPartOfCompilation();

  104.         if (array == null) {
  105.            return new Object[extra];
  106.         } if (array instanceof int[]) {
  107.             return box((int[]) array, extra);
  108.         } else if (array instanceof long[]) {
  109.             return box((long[]) array, extra);
  110.         } else if (array instanceof double[]) {
  111.             return box((double[]) array, extra);
  112.         } else if (array instanceof Object[]) {
  113.             final Object[] objectArray = (Object[]) array;
  114.             return Arrays.copyOf(objectArray, objectArray.length + extra);
  115.         } else {
  116.             throw new UnsupportedOperationException();
  117.         }
  118.     }

  119.     public static int[] unboxInteger(Object[] unboxed, int length) {
  120.         CompilerAsserts.neverPartOfCompilation();

  121.         final int[] boxed = new int[length];

  122.         for (int n = 0; n < length; n++) {
  123.             boxed[n] = (int) unboxed[n];
  124.         }

  125.         return boxed;
  126.     }

  127.     public static long[] unboxLong(Object[] unboxed, int length) {
  128.         CompilerAsserts.neverPartOfCompilation();

  129.         final long[] boxed = new long[length];

  130.         for (int n = 0; n < length; n++) {
  131.             final Object value = unboxed[n];

  132.             if (value instanceof Integer) {
  133.                 boxed[n] = (long) (int) unboxed[n];
  134.             } else if (value instanceof Long) {
  135.                 boxed[n] = (long) unboxed[n];
  136.             }
  137.         }

  138.         return boxed;
  139.     }

  140.     public static double[] unboxDouble(Object[] unboxed, int length) {
  141.         CompilerAsserts.neverPartOfCompilation();

  142.         final double[] boxed = new double[length];

  143.         for (int n = 0; n < length; n++) {
  144.             boxed[n] = (double) unboxed[n];
  145.         }

  146.         return boxed;
  147.     }

  148.     public static void copy(Object source, Object[] destination, int destinationStart, int length) {
  149.         RubyNode.notDesignedForCompilation();

  150.         if (length == 0) {
  151.             return;
  152.         }

  153.         if (source instanceof int[]) {
  154.             final int[] unboxedSource = (int[]) source;

  155.             for (int n = 0; n < length; n++) {
  156.                 destination[destinationStart + n] = unboxedSource[n];
  157.             }
  158.         } else if (source instanceof long[]) {
  159.             final long[] unboxedSource = (long[]) source;

  160.             for (int n = 0; n < length; n++) {
  161.                 destination[destinationStart + n] = unboxedSource[n];
  162.             }
  163.         } else if (source instanceof double[]) {
  164.             final double[] unboxedSource = (double[]) source;

  165.             for (int n = 0; n < length; n++) {
  166.                 destination[destinationStart + n] = unboxedSource[n];
  167.             }
  168.         } else if (source instanceof Object[]) {
  169.             RubyArguments.arraycopy((Object[]) source, 0, destination, destinationStart, length);
  170.         } else {
  171.             throw new UnsupportedOperationException();
  172.         }
  173.     }

  174.     public static long[] longCopyOf(int[] ints) {
  175.         final long[] longs = new long[ints.length];

  176.         for (int n = 0; n < ints.length; n++) {
  177.             longs[n] = ints[n];
  178.         }

  179.         return longs;
  180.     }

  181.     public static int capacity(int current, int needed) {
  182.         if (needed < 16) {
  183.             return 16;
  184.         } else {
  185.             int capacity = current;

  186.             while (capacity < needed) {
  187.                 capacity *= 2;
  188.             }

  189.             return capacity;
  190.         }
  191.     }

  192. }