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     */
017    package 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     * @version $Id: DoubleArray.java 1416643 2012-12-03 19:37:14Z tn $
026     */
027    public interface DoubleArray {
028    
029        /**
030         * Returns the number of elements currently in the array.  Please note
031         * that this may be different from the length of the internal storage array.
032         *
033         * @return number of elements
034         */
035        int getNumElements();
036    
037        /**
038         * Returns the element at the specified index.  Note that if an
039         * out of bounds index is supplied a ArrayIndexOutOfBoundsException
040         * will be thrown.
041         *
042         * @param index index to fetch a value from
043         * @return value stored at the specified index
044         * @throws ArrayIndexOutOfBoundsException if <code>index</code> is less than
045         *         zero or is greater than <code>getNumElements() - 1</code>.
046         */
047        double getElement(int index);
048    
049        /**
050         * Sets the element at the specified index.  If the specified index is greater than
051         * <code>getNumElements() - 1</code>, the <code>numElements</code> property
052         * is increased to <code>index +1</code> and additional storage is allocated
053         * (if necessary) for the new element and all  (uninitialized) elements
054         * between the new element and the previous end of the array).
055         *
056         * @param index index to store a value in
057         * @param value value to store at the specified index
058         * @throws ArrayIndexOutOfBoundsException if <code>index</code> is less than
059         *         zero.
060         */
061        void setElement(int index, double value);
062    
063        /**
064         * Adds an element to the end of this expandable array
065         *
066         * @param value to be added to end of array
067         */
068        void addElement(double value);
069    
070        /**
071         * Adds elements to the end of this expandable array
072         *
073         * @param values to be added to end of array
074         */
075        void addElements(double[] values);
076    
077        /**
078         * <p>
079         * Adds an element to the end of the array and removes the first
080         * element in the array.  Returns the discarded first element.
081         * The effect is similar to a push operation in a FIFO queue.
082         * </p>
083         * <p>
084         * Example: If the array contains the elements 1, 2, 3, 4 (in that order)
085         * and addElementRolling(5) is invoked, the result is an array containing
086         * the entries 2, 3, 4, 5 and the value returned is 1.
087         * </p>
088         *
089         * @param value the value to be added to the array
090         * @return the value which has been discarded or "pushed" out of the array
091         *         by this rolling insert
092         */
093        double addElementRolling(double value);
094    
095        /**
096         * Returns a double[] array containing the elements of this
097         * <code>DoubleArray</code>.  If the underlying implementation is
098         * array-based, this method should always return a copy, rather than a
099         * reference to the underlying array so that changes made to the returned
100         *  array have no effect on the <code>DoubleArray.</code>
101         *
102         * @return all elements added to the array
103         */
104        double[] getElements();
105    
106        /**
107         * Clear the double array
108         */
109        void clear();
110    
111    }