001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.math3.util; 018 019 020/** 021 * Provides a standard interface for double arrays. Allows different 022 * array implementations to support various storage mechanisms 023 * such as automatic expansion, contraction, and array "rolling". 024 * 025 */ 026public interface DoubleArray { 027 028 /** 029 * Returns the number of elements currently in the array. Please note 030 * that this may be different from the length of the internal storage array. 031 * 032 * @return number of elements 033 */ 034 int getNumElements(); 035 036 /** 037 * Returns the element at the specified index. Note that if an 038 * out of bounds index is supplied a ArrayIndexOutOfBoundsException 039 * will be thrown. 040 * 041 * @param index index to fetch a value from 042 * @return value stored at the specified index 043 * @throws ArrayIndexOutOfBoundsException if <code>index</code> is less than 044 * zero or is greater than <code>getNumElements() - 1</code>. 045 */ 046 double getElement(int index); 047 048 /** 049 * Sets the element at the specified index. If the specified index is greater than 050 * <code>getNumElements() - 1</code>, the <code>numElements</code> property 051 * is increased to <code>index +1</code> and additional storage is allocated 052 * (if necessary) for the new element and all (uninitialized) elements 053 * between the new element and the previous end of the array). 054 * 055 * @param index index to store a value in 056 * @param value value to store at the specified index 057 * @throws ArrayIndexOutOfBoundsException if <code>index</code> is less than 058 * zero. 059 */ 060 void setElement(int index, double value); 061 062 /** 063 * Adds an element to the end of this expandable array 064 * 065 * @param value to be added to end of array 066 */ 067 void addElement(double value); 068 069 /** 070 * Adds elements to the end of this expandable array 071 * 072 * @param values to be added to end of array 073 */ 074 void addElements(double[] values); 075 076 /** 077 * <p> 078 * Adds an element to the end of the array and removes the first 079 * element in the array. Returns the discarded first element. 080 * The effect is similar to a push operation in a FIFO queue. 081 * </p> 082 * <p> 083 * Example: If the array contains the elements 1, 2, 3, 4 (in that order) 084 * and addElementRolling(5) is invoked, the result is an array containing 085 * the entries 2, 3, 4, 5 and the value returned is 1. 086 * </p> 087 * 088 * @param value the value to be added to the array 089 * @return the value which has been discarded or "pushed" out of the array 090 * by this rolling insert 091 */ 092 double addElementRolling(double value); 093 094 /** 095 * Returns a double[] array containing the elements of this 096 * <code>DoubleArray</code>. If the underlying implementation is 097 * array-based, this method should always return a copy, rather than a 098 * reference to the underlying array so that changes made to the returned 099 * array have no effect on the <code>DoubleArray.</code> 100 * 101 * @return all elements added to the array 102 */ 103 double[] getElements(); 104 105 /** 106 * Clear the double array 107 */ 108 void clear(); 109 110}