Class IntArrayList

  1 /*
  2  * Copyright (c) 2019 Martin Davis.
  3  *
  4  * All rights reserved. This program and the accompanying materials
  5  * are made available under the terms of the Eclipse Public License 2.0
  6  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  7  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
  8  * and the Eclipse Distribution License is available at
  9  *
 10  * http://www.eclipse.org/org/documents/edl-v10.php.
 11  */
 12 package org.locationtech.jts.util;
 13  
 14 import java.util.Arrays;
 15 import java.util.Objects;
 16  
 17 /**
 18  * An extendable array of primitive <code>int</code> values.
 19  * 
 20  * @author Martin Davis
 21  *
 22  */
 23 public class IntArrayList {
 24   private int[] data;
 25   private int size = 0;
 26  
 27   /**
 28    * Constructs an empty list.
 29    */
 30   public IntArrayList() {
 31     this(10);
 32   }
 33  
 34   /**
 35    * Constructs an empty list with the specified initial capacity
 36    * 
 37    * @param initialCapacity the initial capacity of the list
 38    */
 39   public IntArrayList(int initialCapacity) {
 40     data = new int[initialCapacity];
 41   }
 42  
 43   /**
 44    * Returns the number of values in this list.
 45    * 
 46    * @return the number of values in the list
 47    */
 48   public int size() {
 49     return size;
 50   }
 51  
 52   /**
 53    * Increases the capacity of this list instance, if necessary, 
 54    * to ensure that it can hold at least the number of elements 
 55    * specified by the capacity argument.
 56    * 
 57    * @param capacity the desired capacity
 58    */
 59   public void ensureCapacity(final int capacity) {
 60     if (capacity <= data.length) return;
 61     int newLength  = Math.max(capacity, data.length * 2);
 62     //System.out.println("IntArrayList: copying " + size + " ints to new array of length " + capacity);
 63     data = Arrays.copyOf(data, newLength);
 64   }
 65   /**
 66    * Adds a value to the end of this list.
 67    * 
 68    * @param value the value to add
 69    */
 70   public void add(final int value) {
 71     ensureCapacity(size + 1);
 72     data[size] = value;
 73     ++size;
 74   }
 75   
 76   /**
 77    * Adds all values in an array to the end of this list.
 78    * 
 79    * @param values an array of values
 80    */
 81   public void addAll(final int[] values) {
 82     if (values == nullreturn;
 83     if (values.length == 0return;
 84     ensureCapacity(size + values.length);
 85     System.arraycopy(values, 0, data, size, values.length);
 86     size += values.length;
 87    }
 88   
 89   /**
 90    * Returns a int array containing a copy of
 91    * the values in this list.
 92    * 
 93    * @return an array containing the values in this list
 94    */
 95   public int[] toArray() {
 96     int[] array = new int[size];
 97     System.arraycopy(data, 0, array, 0, size);
 98     return array;
 99   }
100 }
101