程序包 org.hipparchus.util

类 ResizableDoubleArray

java.lang.Object
org.hipparchus.util.ResizableDoubleArray
所有已实现的接口:
Serializable

public class ResizableDoubleArray extends Object implements Serializable
一个可变长度的原始双精度数组实现,自动处理在添加和删除元素时扩展和收缩其内部存储数组。

内部存储数组的容量由initialCapacity属性确定,可以通过构造函数设置。默认初始容量为16。使用addElement(double)添加元素会将元素附加到数组的末尾。当内部存储数组末尾没有空位时,数组会扩展。扩展后的数组大小取决于expansionModeexpansionFactor属性。 expansionMode确定数组大小是乘以expansionFactorResizableDoubleArray.ExpansionMode.MULTIPLICATIVE)还是加法扩展(ResizableDoubleArray.ExpansionMode.ADDITIVE -- 添加expansionFactor个存储位置)。默认expansionModeMULTIPLICATIVE,默认expansionFactor为2。

addElementRolling(double)方法将一个新元素添加到内部存储数组的末尾,并将内部数组的“可用窗口”向前调整一个位置(有效地使原来的第二个元素成为第一个元素,依此类推)。多次激活此方法(或激活discardFrontElements(int))将使内部存储数组开头的存储位置被废弃。为了回收这些存储空间,每次激活这些方法之一时,内部存储数组的大小将与可寻址元素的数量(numElements属性)进行比较,如果差异太大,则将内部数组收缩到大小numElements + 1。内部存储数组何时“太大”取决于expansionModecontractionFactor属性。如果expansionModeMULTIPLICATIVE,则当存储数组长度与numElements的比率超过contractionFactor时,将触发收缩。如果expansionModeADDITIVE,则多余的存储位置数量将与contractionFactor进行比较。

为避免扩展和收缩的循环,expansionFactor不能超过contractionFactor。这两个属性的构造函数和修改器将强制执行此要求,如果违反将抛出MathIllegalArgumentException

注意:此类不是线程安全的。

另请参阅:
  • 嵌套类概要

    嵌套类
    修饰符和类型
    说明
    static enum 
    扩展算法的规范。
  • 构造器概要

    构造器
    构造器
    说明
    使用默认属性创建一个实例。
    ResizableDoubleArray(double[] initialArray)
    从现有的double[]创建一个实例,初始容量和numElements与提供的double[]数组的大小对应。
    ResizableDoubleArray(int initialCapacity)
    使用指定的初始容量创建一个实例。
    ResizableDoubleArray(int initialCapacity, double expansionFactor)
    使用指定的初始容量和扩展因子创建一个实例。
    ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion)
    使用指定的初始容量、扩展因子和收缩条件创建一个实例。
    ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data)
    使用指定的属性创建一个实例。
    复制构造函数。
  • 方法概要

    修饰符和类型
    方法
    说明
    void
    addElement(double value)
    将元素添加到可扩展数组的末尾。
    double
    addElementRolling(double value)
    将元素添加到数组的末尾,并移除数组中的第一个元素。
    void
    addElements(double[] values)
    将多个元素添加到可扩展数组的末尾。
    protected void
    checkContractExpand(double contraction, double expansion)
    检查扩展因子和收缩条件,如果收缩条件小于扩展条件,则引发异常。
    void
    clear()
    清除数组内容,将元素数量重置为零。
    double
    对数组的可寻址元素执行操作。
    void
    将存储数组收缩到(元素集的大小)+ 1,以避免零长度数组。
    copy()
    返回ResizableDoubleArray的副本。
    void
    丢弃数组的前i个初始元素。
    void
    丢弃数组的最后i个元素。
    boolean
    equals(Object object)
    如果对象是具有与此相同属性和相同内部存储数组的ResizableDoubleArray,则返回true。
    protected void
    expand()
    使用扩展因子扩展内部存储数组。
    protected double[]
    提供对内部存储数组的直接访问。
    int
    获取用于存储元素的内部数据结构的当前分配大小。
    double
    收缩条件定义内部数组何时会收缩,以仅存储元素数组中的元素数量。
    double
    getElement(int index)
    返回指定索引处的元素。
    double[]
    返回包含此ResizableArray元素的双精度数组。
    double
    扩展因子控制在需要扩展数组时新数组的大小。
    扩展模式确定内部存储数组在扩展时是加法增长还是乘法增长。
    int
    返回当前数组中的元素数量。
    protected int
    返回内部数组的“起始索引”。
    int
    返回与equals一致的哈希码。
    void
    setElement(int index, double value)
    设置指定索引处的元素。
    void
    setNumElements(int i)
    此函数允许您控制此数组中包含的元素数量,并可用于在数组中“丢弃”最后n个值。
    double
    value替换最近添加的值。

    从类继承的方法 java.lang.Object

    clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
  • 构造器详细资料

    • ResizableDoubleArray

      public ResizableDoubleArray()
      使用默认属性创建一个实例。
      • initialCapacity = 16
      • expansionMode = MULTIPLICATIVE
      • expansionFactor = 2.0
      • contractionCriterion = 2.5
    • ResizableDoubleArray

      public ResizableDoubleArray(int initialCapacity) throws MathIllegalArgumentException
      使用指定的初始容量创建一个实例。

      其他属性采用默认值:

      • expansionMode = MULTIPLICATIVE
      • expansionFactor = 2.0
      • contractionCriterion = 2.5
      参数:
      initialCapacity - 内部存储数组的初始大小。
      抛出:
      MathIllegalArgumentException - 如果initialCapacity <= 0
    • ResizableDoubleArray

      public ResizableDoubleArray(double[] initialArray)
      从现有的double[]创建一个实例,初始容量和numElements与提供的double[]数组的大小对应。

      如果提供的数组为null,则将创建一个具有默认初始容量的新空数组。输入数组将被复制,而不是引用。其他属性采用默认值:

      • expansionMode = MULTIPLICATIVE
      • expansionFactor = 2.0
      • contractionCriterion = 2.5
      参数:
      initialArray - 初始数组
    • ResizableDoubleArray

      public ResizableDoubleArray(int initialCapacity, double expansionFactor) throws MathIllegalArgumentException
      使用指定的初始容量和扩展因子创建一个实例。

      其余属性采用默认值:

      • expansionMode = MULTIPLICATIVE
      • contractionCriterion = 0.5 + expansionFactor

      如果不满足以下条件,则引发MathIllegalArgumentException:

      • initialCapacity > 0
      • expansionFactor > 1
      参数:
      initialCapacity - 内部存储数组的初始大小。
      expansionFactor - 根据此参数扩展数组。
      抛出:
      MathIllegalArgumentException - 如果参数无效。
    • ResizableDoubleArray

      public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion) throws MathIllegalArgumentException
      Creates an instance with the specified initial capacity, expansion factor, and contraction criteria.

      The expansion mode will default to MULTIPLICATIVE.

      Throws MathIllegalArgumentException if the following conditions are not met:

      • initialCapacity > 0
      • expansionFactor > 1
      • contractionCriterion >= expansionFactor
      参数:
      initialCapacity - Initial size of the internal storage array.
      expansionFactor - The array will be expanded based on this parameter.
      contractionCriterion - Contraction criterion.
      抛出:
      MathIllegalArgumentException - if the parameters are not valid.
    • ResizableDoubleArray

      public ResizableDoubleArray(int initialCapacity, double expansionFactor, double contractionCriterion, ResizableDoubleArray.ExpansionMode expansionMode, double... data) throws MathIllegalArgumentException
      Creates an instance with the specified properties.
      Throws MathIllegalArgumentException if the following conditions are not met:
      • initialCapacity > 0
      • expansionFactor > 1
      • contractionCriterion >= expansionFactor
      参数:
      initialCapacity - Initial size of the internal storage array.
      expansionFactor - The array will be expanded based on this parameter.
      contractionCriterion - Contraction criteria.
      expansionMode - Expansion mode.
      data - Initial contents of the array.
      抛出:
      MathIllegalArgumentException - if the parameters are not valid.
      NullArgumentException - if expansionMode is null
    • ResizableDoubleArray

      public ResizableDoubleArray(ResizableDoubleArray original) throws NullArgumentException
      Copy constructor.

      Creates a new ResizableDoubleArray that is a deep, fresh copy of the original. Original may not be null; otherwise a NullArgumentException is thrown.

      参数:
      original - array to copy
      抛出:
      NullArgumentException - if original is null
  • 方法详细资料

    • addElement

      public void addElement(double value)
      Adds an element to the end of this expandable array.
      参数:
      value - Value to be added to end of array.
    • addElements

      public void addElements(double[] values)
      Adds several element to the end of this expandable array.
      参数:
      values - Values to be added to end of array.
    • addElementRolling

      public double addElementRolling(double value)
      Adds an element to the end of the array and removes the first element in the array. Returns the discarded first element.

      The effect is similar to a push operation in a FIFO queue.

      Example: If the array contains the elements 1, 2, 3, 4 (in that order) and addElementRolling(5) is invoked, the result is an array containing the entries 2, 3, 4, 5 and the value returned is 1.

      参数:
      value - Value to be added to the array.
      返回:
      the value which has been discarded or "pushed" out of the array by this rolling insert.
    • substituteMostRecentElement

      public double substituteMostRecentElement(double value) throws MathIllegalStateException
      Substitutes value for the most recently added value.

      Returns the value that has been replaced. If the array is empty (i.e. if numElements is zero), an MathIllegalStateException is thrown.

      参数:
      value - New value to substitute for the most recently added value
      返回:
      the value that has been replaced in the array.
      抛出:
      MathIllegalStateException - if the array is empty
    • checkContractExpand

      protected void checkContractExpand(double contraction, double expansion) throws MathIllegalArgumentException
      Checks the expansion factor and the contraction criterion and raises an exception if the contraction criterion is smaller than the expansion criterion.
      参数:
      contraction - Criterion to be checked.
      expansion - Factor to be checked.
      抛出:
      MathIllegalArgumentException - if contraction < expansion.
      MathIllegalArgumentException - if contraction <= 1.
      MathIllegalArgumentException - if expansion <= 1 .
    • clear

      public void clear()
      Clear the array contents, resetting the number of elements to zero.
    • contract

      public void contract()
      Contracts the storage array to the (size of the element set) + 1 - to avoid a zero length array. This function also resets the startIndex to zero.
    • discardFrontElements

      public void discardFrontElements(int i) throws MathIllegalArgumentException
      Discards the i initial elements of the array.

      For example, if the array contains the elements 1,2,3,4, invoking discardFrontElements(2) will cause the first two elements to be discarded, leaving 3,4 in the array.

      参数:
      i - the number of elements to discard from the front of the array
      抛出:
      MathIllegalArgumentException - if i is greater than numElements.
    • discardMostRecentElements

      public void discardMostRecentElements(int i) throws MathIllegalArgumentException
      Discards the i last elements of the array.

      For example, if the array contains the elements 1,2,3,4, invoking discardMostRecentElements(2) will cause the last two elements to be discarded, leaving 1,2 in the array.

      参数:
      i - the number of elements to discard from the end of the array
      抛出:
      MathIllegalArgumentException - if i is greater than numElements.
    • expand

      protected void expand()
      Expands the internal storage array using the expansion factor.

      If expansionMode is set to MULTIPLICATIVE, the new array size will be internalArray.length * expansionFactor. If expansionMode is set to ADDITIVE, the length after expansion will be internalArray.length + expansionFactor.

    • getContractionCriterion

      public double getContractionCriterion()
      The contraction criterion defines when the internal array will contract to store only the number of elements in the element array.

      If the expansionMode is MULTIPLICATIVE, contraction is triggered when the ratio between storage array length and numElements exceeds contractionFactor. If the expansionMode is ADDITIVE, the number of excess storage locations is compared to contractionFactor.

      返回:
      the contraction criterion used to reclaim memory.
    • getElement

      public double getElement(int index)
      Returns the element at the specified index.
      参数:
      index - index to fetch a value from
      返回:
      value stored at the specified index
      抛出:
      ArrayIndexOutOfBoundsException - if index is less than zero or is greater than getNumElements() - 1.
    • getElements

      public double[] getElements()
      Returns a double array containing the elements of this ResizableArray.

      This method returns a copy, not a reference to the underlying array, so that changes made to the returned array have no effect on this ResizableArray.

      返回:
      the double array.
    • getExpansionFactor

      public double getExpansionFactor()
      The expansion factor controls the size of a new array when an array needs to be expanded.

      The expansionMode determines whether the size of the array is multiplied by the expansionFactor (MULTIPLICATIVE) or if the expansion is additive (ADDITIVE -- expansionFactor storage locations added). The default expansionMode is MULTIPLICATIVE and the default expansionFactor is 2.0.

      返回:
      the expansion factor of this expandable double array
    • getExpansionMode

      public ResizableDoubleArray.ExpansionMode getExpansionMode()
      The expansion mode determines whether the internal storage array grows additively or multiplicatively when it is expanded.
      返回:
      the expansion mode.
    • getCapacity

      public int getCapacity()
      Gets the currently allocated size of the internal data structure used for storing elements. This is not to be confused with the number of elements actually stored.
      返回:
      the length of the internal array.
    • getNumElements

      public int getNumElements()
      Returns the number of elements currently in the array. Please note that this is different from the length of the internal storage array.
      返回:
      the number of elements.
    • getArrayRef

      protected double[] getArrayRef()
      Provides direct access to the internal storage array. Please note that this method returns a reference to this object's storage array, not a copy.

      To correctly address elements of the array, the "start index" is required (available via the getStartIndex method.

      This method should only be used to avoid copying the internal array. The returned value must be used for reading only; other uses could lead to this object becoming inconsistent.

      The getElements() method has no such limitation since it returns a copy of this array's addressable elements.

      返回:
      the internal storage array used by this object.
    • getStartIndex

      protected int getStartIndex()
      Returns the "start index" of the internal array. This index is the position of the first addressable element in the internal storage array.

      The addressable elements in the array are at indices contained in the interval [getStartIndex(), getStartIndex() + getNumElements() - 1].

      返回:
      the start index.
    • compute

      public double compute(MathArrays.Function f)
      Performs an operation on the addressable elements of the array.
      参数:
      f - Function to be applied on this array.
      返回:
      the result.
    • setElement

      public void setElement(int index, double value)
      设置指定索引处的元素。

      如果指定的索引大于getNumElements() - 1,则numElements属性将增加到index +1,并为新元素和数组中前一个结束之间的所有(未初始化的)元素分配额外的存储空间(如果需要)。

      参数:
      index - 要存储值的索引
      value - 要存储在指定索引处的值
      抛出:
      ArrayIndexOutOfBoundsException - 如果index < 0
    • setNumElements

      public void setNumElements(int i) throws MathIllegalArgumentException
      此函数允许您控制此数组中包含的元素数量,并可用于在数组中“丢弃”最后n个值。此函数还将根据需要扩展内部数组。
      参数:
      i - 新元素数量
      抛出:
      MathIllegalArgumentException - 如果i为负数。
    • copy

      public ResizableDoubleArray copy()
      返回可调整大小的双精度数组的副本。在复制之前不会收缩,因此返回的对象是此对象的精确副本。
      返回:
      具有与此相同数据和配置属性的新ResizableDoubleArray
    • equals

      public boolean equals(Object object)
      当且仅当对象是具有与此相同属性和相同内部存储数组的ResizableDoubleArray时返回true。
      覆盖:
      equals 在类中 Object
      参数:
      object - 用于与此进行相等比较的对象
      返回:
      当且仅当对象是具有与此相同数据和属性的ResizableDoubleArray时返回true
    • hashCode

      public int hashCode()
      返回与equals一致的哈希码。
      覆盖:
      hashCode 在类中 Object
      返回:
      表示此ResizableDoubleArray的哈希码。