All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.graphbuilder.curve.ValueVector Maven / Gradle / Ivy

Go to download

The Apache Commons Codec package contains simple encoder and decoders for various formats such as Base64 and Hexadecimal. In addition to these widely used encoders and decoders, the codec package also maintains a collection of phonetic encoding utilities.

The newest version!
/*
* Copyright (c) 2005, Graph Builder
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* * Neither the name of Graph Builder nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package com.graphbuilder.curve;

/**
A value-vector is a sequence of values that some curves use to define themselves,
sometimes called a knot-vector or a weight-vector.  The values are stored using an
array.
*/
public class ValueVector {

	protected int size = 0;
	protected double[] value = null;

	/**
	Creates a ValueVector with initial capacity of 2.
	*/
	public ValueVector() {
		value = new double[2];
	}

	/**
	Creates a ValueVector using the specified array and initial size.

	@throws IllegalArgumentException If the value array is null or size < 0 or size > data.length.
	*/
	public ValueVector(double[] value, int size) {
		if (value == null)
			throw new IllegalArgumentException("value array cannot be null.");

		if (size < 0 || size > value.length)
			throw new IllegalArgumentException("size >= 0 && size <= value.length required");

		this.value = value;
		this.size = size;
	}

	/**
	Creates a ValueVector with the specified initial capacity.
	*/
	public ValueVector(int initialCapacity) {
		value = new double[initialCapacity];
	}

	/**
	Returns the number of values in the value array.
	*/
	public int size() {
		return size;
	}

	/**
	Returns the value at the specified index.

	@throws IllegalArgumentException If index < 0 or index >= size.
	*/
	public double get(int index) {
		if (index < 0 || index >= size)
			throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")");

		return value[index];
	}

	/**
	Sets the value at the specified index.

	@throws IllegalArgumentException If index < 0 or index >= size.
	*/
	public void set(double d, int index) {
		if (index < 0 || index >= size)
			throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")");

		value[index] = d;
	}

	/**
	Removes the value at the specified index. Values at a higher index are shifted to fill the space.

	@throws IllegalArgumentException If index < 0 or index >= size.
	*/
	public void remove(int index) {
		if (index < 0 || index >= size)
			throw new IllegalArgumentException("required: (index >= 0 && index < size) but: (index = " + index + ", size = " + size + ")");

		for (int i = index + 1; i < size; i++)
			value[i-1] = value[i];

		size--;
	}

	/**
	Adds a value to the value array at index location size.
	*/
	public void add(double d) {
		insert(d, size);
	}

	/**
	Inserts the value at the specified index location.  Values at an equal or higher index are shifted to make space.

	@throws IllegalArgumentException If index < 0 or index > size.
	*/
	public void insert(double d, int index) {
		if (index < 0 || index > size)
			throw new IllegalArgumentException("required: (index >= 0 && index <= size) but: (index = " + index + ", size = " + size + ")");

		ensureCapacity(size + 1);

		for (int i = size; i > index; i--)
			value[i] = value[i-1];

		value[index] = d;
		size++;
	}

	/**
	Checks that the value array has the specified capacity, otherwise the capacity of the
	value array is increased to be the maximum between twice the current capacity and the
	specified capacity.
	*/
	public void ensureCapacity(int capacity) {
		if (value.length < capacity) {
			int x = 2 * value.length;
			if (x < capacity) x = capacity;
			double[] arr = new double[x];
			for (int i = 0; i < size; i++)
				arr[i] = value[i];
			value = arr;
		}
	}

	/**
	Creates a new value array of exact size, copying the values from the old array into the
	new one.
	*/
	public void trimArray() {
		if (size < value.length) {
			double[] arr = new double[size];
			for (int i = 0; i < size; i++)
				arr[i] = value[i];
			value = arr;
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy