Class Queue<E, R>

  1. First In, First Out (FIFO): The core feature of a queue is its first in, first out nature. The element added to the queue first will be the one to be removed first.
  2. Operations: The main operations include enqueue (adding an element to the end of the queue) and dequeue (removing and returning the element at the front of the queue). Typically, there is also a peek operation (looking at the front element without removing it).
  3. Uses: Queues are commonly used to manage a series of tasks or elements that need to be processed in order. For example, managing task queues in a multi-threaded environment, or in algorithms for data structures like trees and graphs for breadth-first search.
  4. Task Scheduling: Managing the order of task execution in operating systems or applications.
  5. Data Buffering: Acting as a buffer for data packets in network communication.
  6. Breadth-First Search (BFS): In traversal algorithms for graphs and trees, queues store elements that are to be visited.
  7. Real-time Queuing: Like queuing systems in banks or supermarkets.

Type Parameters

  • E = any
  • R = any

Hierarchy (view full)

Accessors

  • get autoCompactRatio(): number
  • This function returns the value of the autoCompactRatio property.

    Returns number

    The autoCompactRatio property of the object, which is a number.

  • set autoCompactRatio(v): void
  • The above function sets the autoCompactRatio property to a specified number in TypeScript.

    Parameters

    • v: number

      The parameter v represents the value that will be assigned to the _autoCompactRatio property.

    Returns void

  • get elements(): E[]
  • The elements function returns the elements of this set.

    Returns E[]

    An array of the elements in the stack

  • get first(): undefined | E
  • Time Complexity: O(1) Space Complexity: O(1)

    The first function returns the first element of the array _elements if it exists, otherwise it returns undefined.

    Returns undefined | E

    The get first() method returns the first element of the data structure, represented by the _elements array at the _offset index. If the data structure is empty (size is 0), it returns undefined.

  • get last(): undefined | E
  • Time Complexity: O(1) Space Complexity: O(1)

    The last function returns the last element in an array-like data structure, or undefined if the structure is empty.

    Returns undefined | E

    The method get last() returns the last element of the _elements array if the array is not empty. If the array is empty, it returns undefined.

  • get offset(): number
  • The offset function returns the offset of the current page.

    Returns number

    The value of the protected variable _offset

  • get size(): number
  • The size function returns the number of elements in an array.

    Returns number

    The size of the array, which is the difference between the length of the array and the offset.

  • get toElementFn(): undefined | ((rawElement: R) => E)
  • The function returns the _toElementFn property, which is a function that converts a raw element to a specific type.

    Returns undefined | ((rawElement: R) => E)

    The function get toElementFn() is returning either a function that takes a raw element rawElement of type R and returns an element E, or undefined if no function is assigned to _toElementFn.

Methods

  • Time Complexity: O(n) Space Complexity: O(n)

    The function _getIterator returns an iterable iterator for the elements in the class.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(n) Space Complexity: O(1)

    The function is an implementation of the Symbol.iterator method that returns an IterableIterator.

    Parameters

    • Rest...args: any[]

      The args parameter in the code snippet represents a rest parameter. It allows the function to accept any number of arguments as an array. In this case, the args parameter is used to pass any number of arguments to the _getIterator method.

    Returns IterableIterator<E, any, any>

  • Time Complexity: O(1) Space Complexity: O(1)

    Parameters

    • index: number

    Returns undefined | E

  • Time Complexity: O(1) Space Complexity: O(1)

    The clear function resets the elements array and offset to their initial values.

    Returns void

  • Time Complexity: O(n) Space Complexity: O(n)

    The clone() function returns a new Queue object with the same elements as the original Queue.

    Returns Queue<E, R>

    The clone() method is returning a new instance of the Queue class.

  • The compact function in TypeScript slices the elements array based on the offset and resets the offset to zero.

    Returns boolean

    The compact() method is returning a boolean value of true.

  • The delete function removes an element from the list.

    Parameters

    • element: E

      Specify the element to be deleted

    Returns boolean

    A boolean value indicating whether the element was successfully deleted or not

  • The deleteAt function deletes the element at a given index.

    Parameters

    • index: number

      Determine the index of the element to be deleted

    Returns boolean

    A boolean value

  • Time Complexity: O(n) Space Complexity: O(1)

    The every function checks if every element in the array satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Queue<E, R>>

      The predicate parameter is a callback function that takes three arguments: the current element being processed, its index, and the array it belongs to. It should return a boolean value indicating whether the element satisfies a certain condition or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns boolean

    The every method is returning a boolean value. It returns true if every element in the array satisfies the provided predicate function, and false otherwise.

  • Time Complexity: O(n) Space Complexity: O(n)

    The filter function creates a new Queue object containing elements from the original Queue that satisfy a given predicate function.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Queue<E, R>>

      The predicate parameter is a callback function that takes three arguments: the current element being iterated over, the index of the current element, and the queue itself. It should return a boolean value indicating whether the element should be included in the filtered queue or not.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If thisArg is

    Returns Queue<E, R>

    The filter method is returning a new Queue object that contains the elements that satisfy the given predicate function.

  • Time Complexity: O(n) Space Complexity: O(1)

    The find function iterates over the elements of an array-like object and returns the first element that satisfies the provided callback function.

    Parameters

    • callbackfn: ElementCallback<E, R, boolean, Queue<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array itself. The function should return a boolean value indicating whether the current element matches the desired condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If thisArg @returns The findmethod returns the first element in the array that satisfies the provided callback function. If no element satisfies the callback function,undefined` is returned.

    Returns undefined | E

  • Time Complexity: O(n) Space Complexity: O(1)

    The forEach function iterates over each element in an array-like object and calls a callback function for each element.

    Parameters

    • callbackfn: ElementCallback<E, R, void, Queue<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes three arguments: the current element being processed, the index of the current element, and the array that forEach was called upon.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as this when executing the callbackfn function. If thisArg is provided, it will be passed as the this value to the callbackfn function. If `thisArg

    Returns void

  • Time Complexity: O(n) Space Complexity: O(1)

    The function checks if a given element exists in a collection.

    Parameters

    • element: E

      The parameter "element" is of type E, which means it can be any type. It represents the element that we want to check for existence in the collection.

    Returns boolean

    a boolean value. It returns true if the element is found in the collection, and false otherwise.

  • Time Complexity: O(1) Space Complexity: O(1)

    The function checks if a data structure is empty by comparing its size to zero.

    Returns boolean

    A boolean value indicating whether the size of the object is 0 or not.

  • Time Complexity: O(n) Space Complexity: O(n)

    Type Parameters

    • EM
    • RM

    Parameters

    • callback: ElementCallback<E, R, EM, Queue<E, R>>
    • OptionaltoElementFn: ((rawElement: RM) => EM)
        • (rawElement): EM
        • Parameters

          • rawElement: RM

          Returns EM

    • OptionalthisArg: any

    Returns Queue<EM, RM>

  • Time Complexity: O(1) Space Complexity: O(1)

    The push function adds an element to the end of the queue and returns true. Adds an element at the back of the queue.

    Parameters

    • element: E

      The element parameter represents the element that you want to add to the queue.

    Returns boolean

    Always returns true, indicating the element was successfully added.

  • Time Complexity: O(n) Space Complexity: O(1)

    The reduce function iterates over the elements of an array-like object and applies a callback function to reduce them into a single value.

    Type Parameters

    • U

    Parameters

    • callbackfn: ReduceElementCallback<E, R, U, Queue<E, R>>

      The callbackfn parameter is a function that will be called for each element in the array. It takes four arguments:

    • initialValue: U

      The initialValue parameter is the initial value of the accumulator. It is the value that the accumulator starts with before the reduction operation begins.

    Returns U

    The reduce method is returning the final value of the accumulator after iterating over all the elements in the array and applying the callback function to each element.

  • Time Complexity: O(1) Space Complexity: O(1)

    The shift function removes and returns the first element in the queue, and adjusts the internal data structure if necessary to optimize performance.

    Returns undefined | E

    The function shift() returns either the first element in the queue or undefined if the queue is empty.

  • Time Complexity: O(n) Space Complexity: O(1)

    The "some" function checks if at least one element in a collection satisfies a given predicate.

    Parameters

    • predicate: ElementCallback<E, R, boolean, Queue<E, R>>

      The predicate parameter is a callback function that takes three arguments: value, index, and array. It should return a boolean value indicating whether the current element satisfies the condition.

    • OptionalthisArg: any

      The thisArg parameter is an optional argument that specifies the value to be used as the this value when executing the predicate function. If thisArg is provided, it will be passed as the this value to the predicate function. If `thisArg

    Returns boolean

    a boolean value. It returns true if the predicate function returns true for any element in the collection, and false otherwise.

  • Time Complexity: O(1) Space Complexity: O(n)

    The toArray() function returns an array of elements from the current offset to the end of the _elements array.

    Returns E[]

    An array of type E is being returned.

  • Time Complexity: O(n) Space Complexity: O(n)

    The function "fromArray" creates a new Queue object from an array of elements.Creates a queue from an existing array.

    Type Parameters

    • E

    Parameters

    • elements: E[]

      The "elements" parameter is an array of elements of type E.

    Returns Queue<E, any>

    The method is returning a new instance of the Queue class, initialized with the elements from the input array.