iggs-utils - v2.4.0
    Preparing search index...

    Class CircularArray<T>

    A circular array implementation that allows for wrapping around the ends.

    import { collection } from 'iggs-utils';

    const circularArray = new collection.CircularArray<string>(monday, tuesday, wednesday, thursday, friday, saturday, sunday);

    // accessing elements by position
    circularArray.get() // monday
    circularArray.get(0) // monday
    circularArray.get(1) // tuesday
    circularArray.get(2) // wednesday
    circularArray.get(-1) // sunday
    circularArray.get(7) // monday
    circularArray.get(-7) // monday

    // navigating the circular array
    circularArray.next() // tuesday
    circularArray.next() // wednesday
    circularArray.next() // thursday
    circularArray.next(10) // sunday
    circularArray.get() // sunday
    circularArray.previous() // saturday
    circularArray.previous() // friday
    circularArray.previous() // thursday
    circularArray.previous(10) // monday
    circularArray.get() // monday

    // working with positions
    circularArray.setIndex(2) // wednesday
    circularArray.setPosition(10) // thursday
    circularArray.getIndex() // 3
    circularArray.getPosition() // 10

    Type Parameters

    • T
    Index

    Constructors

    Methods

    • Returns the element at the specified position in the circular array.

      Parameters

      • Optionalposition: number

        The position to peek at (defaults to the current position).

      Returns T

      The value at the specified position.

    • Moves the current position forward by the specified number of steps (1 by default).

      Parameters

      • steps: number = 1

        The number of steps to move forward (default is 1).

      Returns T

      The value at the new current position.

    • Moves the current position backward by the specified number of steps (1 by default).

      Parameters

      • steps: number = 1

        The number of steps to move backward (default is 1).

      Returns T

      The value at the new current position.

    • Set the current index in the circular array. Also updates the current position at the index value.

      Parameters

      • index: number

        The index to set.

      Returns T

    • Set the current position in the circular array.

      Parameters

      • position: number

        The position to set.

      Returns T

    • Returns a shallow copy of the circular array as a regular array.

      Returns T[]

      A regular array containing the elements of the circular array.