Home | History | Annotate | Download | only in common
      1 package org.jbox2d.common;
      2 
      3 import java.lang.reflect.Array;
      4 
      5 import com.badlogic.gdx.utils.reflect.ArrayReflection;
      6 import com.badlogic.gdx.utils.reflect.ClassReflection;
      7 
      8 public class BufferUtils {
      9   /** Reallocate a buffer. */
     10   public static <T> T[] reallocateBuffer(Class<T> klass, T[] oldBuffer, int oldCapacity,
     11       int newCapacity) {
     12     assert (newCapacity > oldCapacity);
     13     @SuppressWarnings("unchecked")
     14     T[] newBuffer = (T[]) ArrayReflection.newInstance(klass, newCapacity);
     15     if (oldBuffer != null) {
     16       System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
     17     }
     18     for (int i = oldCapacity; i < newCapacity; i++) {
     19       try {
     20         newBuffer[i] = ClassReflection.newInstance(klass);
     21       } catch (Exception e) {
     22         throw new RuntimeException(e);
     23       }
     24     }
     25     return newBuffer;
     26   }
     27 
     28   /** Reallocate a buffer. */
     29   public static int[] reallocateBuffer(int[] oldBuffer, int oldCapacity, int newCapacity) {
     30     assert (newCapacity > oldCapacity);
     31     int[] newBuffer = new int[newCapacity];
     32     if (oldBuffer != null) {
     33       System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
     34     }
     35     return newBuffer;
     36   }
     37 
     38   /** Reallocate a buffer. */
     39   public static float[] reallocateBuffer(float[] oldBuffer, int oldCapacity, int newCapacity) {
     40     assert (newCapacity > oldCapacity);
     41     float[] newBuffer = new float[newCapacity];
     42     if (oldBuffer != null) {
     43       System.arraycopy(oldBuffer, 0, newBuffer, 0, oldCapacity);
     44     }
     45     return newBuffer;
     46   }
     47 
     48   /**
     49    * Reallocate a buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     50    * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     51    */
     52   public static <T> T[] reallocateBuffer(Class<T> klass, T[] buffer, int userSuppliedCapacity,
     53       int oldCapacity, int newCapacity, boolean deferred) {
     54     assert (newCapacity > oldCapacity);
     55     assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
     56     if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
     57       buffer = reallocateBuffer(klass, buffer, oldCapacity, newCapacity);
     58     }
     59     return buffer;
     60   }
     61 
     62   /**
     63    * Reallocate an int buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     64    * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     65    */
     66   public static int[] reallocateBuffer(int[] buffer, int userSuppliedCapacity, int oldCapacity,
     67       int newCapacity, boolean deferred) {
     68     assert (newCapacity > oldCapacity);
     69     assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
     70     if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
     71       buffer = reallocateBuffer(buffer, oldCapacity, newCapacity);
     72     }
     73     return buffer;
     74   }
     75 
     76   /**
     77    * Reallocate a float buffer. A 'deferred' buffer is reallocated only if it is not NULL. If
     78    * 'userSuppliedCapacity' is not zero, buffer is user supplied and must be kept.
     79    */
     80   public static float[] reallocateBuffer(float[] buffer, int userSuppliedCapacity, int oldCapacity,
     81       int newCapacity, boolean deferred) {
     82     assert (newCapacity > oldCapacity);
     83     assert (userSuppliedCapacity == 0 || newCapacity <= userSuppliedCapacity);
     84     if ((!deferred || buffer != null) && userSuppliedCapacity == 0) {
     85       buffer = reallocateBuffer(buffer, oldCapacity, newCapacity);
     86     }
     87     return buffer;
     88   }
     89 
     90   /** Rotate an array, see std::rotate */
     91   public static <T> void rotate(T[] ray, int first, int new_first, int last) {
     92     int next = new_first;
     93     while (next != first) {
     94       T temp = ray[first];
     95       ray[first] = ray[next];
     96       ray[next] = temp;
     97       first++;
     98       next++;
     99       if (next == last) {
    100         next = new_first;
    101       } else if (first == new_first) {
    102         new_first = next;
    103       }
    104     }
    105   }
    106 
    107   /** Rotate an array, see std::rotate */
    108   public static void rotate(int[] ray, int first, int new_first, int last) {
    109     int next = new_first;
    110     while (next != first) {
    111       int temp = ray[first];
    112       ray[first] = ray[next];
    113       ray[next] = temp;
    114       first++;
    115       next++;
    116       if (next == last) {
    117         next = new_first;
    118       } else if (first == new_first) {
    119         new_first = next;
    120       }
    121     }
    122   }
    123 
    124   /** Rotate an array, see std::rotate */
    125   public static void rotate(float[] ray, int first, int new_first, int last) {
    126     int next = new_first;
    127     while (next != first) {
    128       float temp = ray[first];
    129       ray[first] = ray[next];
    130       ray[next] = temp;
    131       first++;
    132       next++;
    133       if (next == last) {
    134         next = new_first;
    135       } else if (first == new_first) {
    136         new_first = next;
    137       }
    138     }
    139   }
    140 }
    141