1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
|
/*
* Copyright (C) 2014-2021 Brian L. Browning
*
* This file is part of Beagle
*
* Beagle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Beagle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package blbutil;
import java.util.Arrays;
/**
* Class {@code FloatList} represents a list of floats.
* Class {@code FloatList} supports a {@code clear()} method, but does not
* support a {@code remove()} method.
*
* @author Brian L. Browning {@code <browning@uw.edu>}
*/
public class FloatList {
/**
* The default initial capacity of an {@code FloatList}, which is 10.
*/
public static final int DEFAULT_INIT_CAPACITY = 10;
private int size;
private float[] values;
/**
* Constructs an {@code FloatList} object with the default
* initial capacity.
*
* @see #DEFAULT_INIT_CAPACITY
*/
public FloatList() {
this(DEFAULT_INIT_CAPACITY);
}
/**
* Constructs an {@code FloatList} object with the specified
* initial capacity.
*
* @param initCapacity the initial capacity of this list
* @throws IllegalArgumentException if {@code initCapacity<0}.
*/
public FloatList(int initCapacity) {
if (initCapacity < 0) {
throw new IllegalArgumentException(String.valueOf(initCapacity));
}
this.size = 0;
this.values = new float[initCapacity];
}
/**
* Adds the specified integer to the end of this list.
*
* @param element the value to be added to the end of this list.
*/
public void add(float element) {
if (size==values.length) {
int newCapacity = (values.length * 3)/2 + 1;
this.values = Arrays.copyOf(this.values, newCapacity);
}
this.values[size++] = element;
}
/**
* Adds the specified value to the specified element.
*
* @param index the index of the element to which the specified value
* will be added
* @param value the to be added
* @throws IndexOutOfBoundsException if
* {@code index < 0 || index >= this.size()}
*/
public void addToElement(int index, float value) {
if (index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
this.values[index] += value;
}
/**
* Returns the float at the specified position in this list.
* @param index the index of the returned float.
* @return the float at the specified position in this list.
* @throws IndexOutOfBoundsException if
* {@code index < 0 || index >= this.size()}
*/
public float get(int index) {
if (index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
return values[index];
}
/**
* Replaces the element at the specified position in this list with the
* specified element.
* @param index the index of the element to be replaced
* @param value the value to be stored at the specified position
* in this list
* @return the previous value at the specified position in this list
* @throws IndexOutOfBoundsException if
* {@code index < 0 || index >= this.size()}
*/
public float set(int index, float value) {
if (index >= size) {
throw new IndexOutOfBoundsException(String.valueOf(index));
}
float oldValue = values[index];
values[index] = value;
return oldValue;
}
/**
* Returns the number of elements in this list.
* @return the number of elements in this list.
*/
public int size() {
return size;
}
/**
* Returns {@code true} if this list has no elements, and returns
* {@code false} otherwise.
* @return {@code true} if this list has no elements, and returns
* {@code false} otherwise.
*/
public boolean isEmpty() {
return size==0;
}
/**
* Returns an integer array containing the sequence of elements in this
* list.
* @return an integer array containing the sequence of elements in this
* list.
*/
public float[] toArray() {
return Arrays.copyOf(values, size);
}
/**
* Removes all elements from this list.
*/
public void clear() {
this.size = 0;
}
/**
* Returns a string representation of this list. The
* exact details of the representation are unspecified and
* subject to change.
*
* @return a string representation of this list.
*/
@Override
public String toString() {
return Arrays.toString(Arrays.copyOf(values, size));
}
}
|