Advertisement
vmeansdev

DynArray

Sep 17th, 2019
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.Arrays;
  3.  
  4. public class DynArray<T> {
  5.     public T [] array;
  6.     public int count;
  7.     public int capacity;
  8.     private Class<T> clazz;
  9.  
  10.     private static final int DEFAULT_CAPACITY = 16;
  11.     private static final int CAPACITY_MULTIPLIER = 2;
  12.     private static final double CAPACITY_DENOMINATOR = 1.5;
  13.     private static final int OCCUPANCY_EDGE_PERCENTAGE = 50;
  14.  
  15.     public DynArray(Class<T> clazz) {
  16.         this.clazz = clazz;
  17.         this.count = 0;
  18.         this.capacity = DEFAULT_CAPACITY;
  19.         makeArray(capacity);
  20.     }
  21.  
  22.     public void makeArray(int new_capacity) {
  23.         if (count != 0) {
  24.             this.capacity = new_capacity;
  25.             array = Arrays.copyOf(array, new_capacity);
  26.             return;
  27.         }
  28.         array = _makeArray(new_capacity);
  29.     }
  30.  
  31.     public T getItem(int index) {
  32.         if (isOutOfBounds(index)) {
  33.             throw new IndexOutOfBoundsException();
  34.         }
  35.  
  36.         return array[index];
  37.     }
  38.  
  39.     public void append(T itm) {
  40.         if (count == capacity) {
  41.             // increase capacity
  42.             increaseBuffer();
  43.             array = Arrays.copyOf(array, capacity);
  44.         }
  45.         array[count] = itm;
  46.         increaseCount();
  47.     }
  48.  
  49.     public void insert(T itm, int index) {
  50.         if (index <= -1 || (count > 0 && index > count)) {
  51.             throw new IndexOutOfBoundsException();
  52.         }
  53.  
  54.         if (index == count) {
  55.             append(itm);
  56.             return;
  57.         }
  58.  
  59.         if (count == capacity) {
  60.             increaseBuffer();
  61.         }
  62.  
  63.         T[] newArr = _makeArray(capacity);
  64.         System.arraycopy(array, 0, newArr, 0, index);
  65.         newArr[index] = itm;
  66.         int elementsCount = (count - index);
  67.         System.arraycopy(array, index, newArr, index + 1, elementsCount);
  68.         array = newArr;
  69.         increaseCount();
  70.     }
  71.  
  72.     public void remove(int index) {
  73.         if (isOutOfBounds(index)) {
  74.             throw new IndexOutOfBoundsException();
  75.         }
  76.  
  77.         if (occupancyPercentage(count - 1, capacity) < OCCUPANCY_EDGE_PERCENTAGE) {
  78.             decreaseBuffer();
  79.         }
  80.         T[] newArr = _makeArray(capacity);
  81.         System.arraycopy(array, 0, newArr, 0, index);
  82.         System.arraycopy(array, index + 1,newArr, index, capacity - index - 1);
  83.         decreaseCount();
  84.         array = newArr;
  85.     }
  86.  
  87.     @SuppressWarnings("unchecked")
  88.     private T[] _makeArray(int capacity) {
  89.         return (T[]) Array.newInstance(this.clazz, capacity);
  90.     }
  91.  
  92.     private boolean isOutOfBounds(int index) {
  93.         return index >= count || index <= -1;
  94.     }
  95.  
  96.     private void increaseBuffer() {
  97.         this.capacity = this.capacity * CAPACITY_MULTIPLIER;
  98.     }
  99.  
  100.     private void increaseCount() {
  101.         count += 1;
  102.     }
  103.  
  104.     private void decreaseBuffer() {
  105.         if (capacity <= DEFAULT_CAPACITY) {
  106.             return;
  107.         }
  108.  
  109.         int newCapacity = (int)(this.capacity / CAPACITY_DENOMINATOR);
  110.         if (newCapacity < DEFAULT_CAPACITY) {
  111.             newCapacity = DEFAULT_CAPACITY;
  112.         }
  113.         this.capacity = newCapacity;
  114.     }
  115.  
  116.     private void decreaseCount() {
  117.         count -= 1;
  118.     }
  119.  
  120.     private int occupancyPercentage(int count, int capacity) {
  121.         return (100 * count) / capacity;
  122.     }
  123.  
  124.     @Override
  125.     public String toString() {
  126.         StringBuilder sb = new StringBuilder();
  127.         sb.append("[");
  128.         for (int i = 0; i < count; i++) {
  129.             sb.append(array[i]);
  130.             sb.append((i == count -1) ? "" : ", ");
  131.         }
  132.         sb.append("]");
  133.         return sb.toString();
  134.     }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement