tikhomirov@288: /*
tikhomirov@545: * Copyright (c) 2011-2013 TMate Software Ltd
tikhomirov@288: *
tikhomirov@288: * This program is free software; you can redistribute it and/or modify
tikhomirov@288: * it under the terms of the GNU General Public License as published by
tikhomirov@288: * the Free Software Foundation; version 2 of the License.
tikhomirov@288: *
tikhomirov@288: * This program is distributed in the hope that it will be useful,
tikhomirov@288: * but WITHOUT ANY WARRANTY; without even the implied warranty of
tikhomirov@288: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
tikhomirov@288: * GNU General Public License for more details.
tikhomirov@288: *
tikhomirov@288: * For information on how to redistribute this software under
tikhomirov@288: * the terms of a license other than GNU General Public License
tikhomirov@288: * contact TMate Software at support@hg4j.com
tikhomirov@288: */
tikhomirov@288: package org.tmatesoft.hg.internal;
tikhomirov@288:
tikhomirov@561: import java.util.Arrays;
tikhomirov@561:
tikhomirov@288: /**
tikhomirov@328: * Vector of primitive values
tikhomirov@328: *
tikhomirov@288: * @author Artem Tikhomirov
tikhomirov@288: * @author TMate Software Ltd.
tikhomirov@288: */
tikhomirov@328: public class IntVector {
tikhomirov@288:
tikhomirov@288: private int[] data;
tikhomirov@288: private final int increment;
tikhomirov@288: private int count;
tikhomirov@288:
tikhomirov@288:
tikhomirov@288: public IntVector() {
tikhomirov@288: this(16, -1);
tikhomirov@288: }
tikhomirov@288:
tikhomirov@288: // increment == -1: grow by power of two.
tikhomirov@288: // increment == 0: no resize (Exception will be thrown on attempt to add past capacity)
tikhomirov@288: public IntVector(int initialCapacity, int increment) {
tikhomirov@288: data = new int[initialCapacity];
tikhomirov@288: this.increment = increment;
tikhomirov@288: }
tikhomirov@288:
tikhomirov@288: public void add(int v) {
tikhomirov@288: if (count == data.length) {
tikhomirov@551: grow(0);
tikhomirov@288: }
tikhomirov@288: data[count++] = v;
tikhomirov@288: }
tikhomirov@288:
tikhomirov@551: public void add(int... values) {
tikhomirov@551: if (count + values.length > data.length) {
tikhomirov@552: grow(count + values.length);
tikhomirov@551: }
tikhomirov@551: for (int v : values) {
tikhomirov@551: data[count++] = v;
tikhomirov@551: }
tikhomirov@551: }
tikhomirov@551:
tikhomirov@288: public int get(int i) {
tikhomirov@288: if (i < 0 || i >= count) {
tikhomirov@288: throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count));
tikhomirov@288: }
tikhomirov@288: return data[i];
tikhomirov@288: }
tikhomirov@288:
tikhomirov@545: public void set(int i, int v) {
tikhomirov@545: if (i < 0 || i >= count) {
tikhomirov@545: throw new IndexOutOfBoundsException(String.format("Index: %d, size: %d", i, count));
tikhomirov@545: }
tikhomirov@545: data[i] = v;
tikhomirov@545: }
tikhomirov@545:
tikhomirov@288: public int size() {
tikhomirov@288: return count;
tikhomirov@288: }
tikhomirov@329:
tikhomirov@545: public boolean isEmpty() {
tikhomirov@545: return count == 0;
tikhomirov@545: }
tikhomirov@545:
tikhomirov@329: public void clear() {
tikhomirov@329: count = 0;
tikhomirov@329: }
tikhomirov@329:
tikhomirov@573: public void trimTo(int newSize) {
tikhomirov@573: if (newSize < 0 || newSize > count) {
tikhomirov@573: throw new IllegalArgumentException(String.format("Can't trim vector of size %d to %d", count, newSize));
tikhomirov@573: }
tikhomirov@573: count = newSize;
tikhomirov@573: }
tikhomirov@573:
tikhomirov@329: public void trimToSize() {
tikhomirov@329: data = toArray(true);
tikhomirov@329: }
tikhomirov@329:
tikhomirov@288:
tikhomirov@288: public int[] toArray() {
tikhomirov@288: int[] rv = new int[count];
tikhomirov@288: System.arraycopy(data, 0, rv, 0, count);
tikhomirov@288: return rv;
tikhomirov@288: }
tikhomirov@552:
tikhomirov@552: public void reverse() {
tikhomirov@552: for (int a = 0, b = count-1; a < b; a++, b--) {
tikhomirov@552: int t = data[b];
tikhomirov@552: data[b] = data[a];
tikhomirov@552: data[a] = t;
tikhomirov@552: }
tikhomirov@552: }
tikhomirov@552:
tikhomirov@561: /**
tikhomirov@561: *
tikhomirov@561: * @param ascending true
to sort in ascending order, false
for descending
tikhomirov@561: */
tikhomirov@561: public void sort(boolean ascending) {
tikhomirov@561: Arrays.sort(data, 0, count);
tikhomirov@561: if (!ascending) {
tikhomirov@561: reverse();
tikhomirov@561: }
tikhomirov@561: }
tikhomirov@561:
tikhomirov@561:
tikhomirov@552: @Override
tikhomirov@552: public String toString() {
tikhomirov@552: return String.format("%s[%d]", IntVector.class.getSimpleName(), size());
tikhomirov@552: }
tikhomirov@288:
tikhomirov@288: /**
tikhomirov@288: * Use only when this instance won't be used any longer
tikhomirov@288: */
tikhomirov@288: @Experimental
tikhomirov@288: int[] toArray(boolean internalIfSizeMatchCapacity) {
tikhomirov@288: if (count == data.length) {
tikhomirov@288: return data;
tikhomirov@288: }
tikhomirov@288: return toArray();
tikhomirov@288: }
tikhomirov@288:
tikhomirov@551: private void grow(int newCapacityHint) {
tikhomirov@288: if (increment == 0) {
tikhomirov@329: throw new UnsupportedOperationException("This vector is not allowed to expand");
tikhomirov@288: }
tikhomirov@288: int newCapacity = increment < 0 ? data.length << 1 : data.length + increment;
tikhomirov@551: if (newCapacityHint > 0 && newCapacity < newCapacityHint) {
tikhomirov@551: newCapacity = newCapacityHint;
tikhomirov@551: }
tikhomirov@288: assert newCapacity > 0 && newCapacity != data.length : newCapacity;
tikhomirov@288: int[] newData = new int[newCapacity];
tikhomirov@288: System.arraycopy(data, 0, newData, 0, count);
tikhomirov@288: data = newData;
tikhomirov@288: }
tikhomirov@288: }