kudzu-forest/elm-constant-time-queue/1.2.0

NOTE: the latest version is 1.4.0

Queue

One way queue with time-complexity of enqueueing and dequeueing O(1) even at worst case.

Type

type Queue a

Representation of First-In, First-Out(FIFO) data structure.

Creation

Returns empty queue.

head empty == Nothing
fromListLIFO : List x -> Queue x

Create queue from list. The newest element comes head of the queue.

["H","e","l","l","o"]
    |> fromListLIFO
    |> head
--> Just "H"
fromListFIFO : List x -> Queue x

Create queue from list. The oldest element of the list comes head of the queue.

["H"]
    |> (::) "e"
    |> (::) "l"
    |> (::) "l"
    |> (::) "o"
    |> fromListFIFO
    |> head
--> Just "H"

Query

isEmpty : Queue x -> Bool

Returns True if the argument has no element.

empty
    |> enqueue "a"
    |> isEmpty
--> False


empty
    |> enqueue "a"
    |> dequeue
    |> isEmpty
--> True
rear : Queue x -> Maybe x

Returns the newest element if exists, wrapped in Maybe. Worst-case time complexity of this operation is O(log N).

empty
    |> enqueue 100
    |> enqueue 200
    |> rear
--> Just 200


empty
    |> enqueue 100
    |> dequeue
    |> rear
--> Nothing
length : Queue x -> Int

Returns how many elements is contained in the queue. The time complexity is O(log N).

empty
    |> length
--> 0


empty
    |> enqueue 1
    |> enqueue 2
    |> enqueue 3
    |> dequeue
    |> length
--> 2

Modification

enqueue : x -> Queue x -> Queue x

Returns the queue as second argument with the element as the first argument injected into the rear side.

empty
    |> enqueue 100
    |> enqueue 200
    |> head
--> Just 100

Returns queue without the oldest element of the given queue.

empty
    |> enqueue "e"
    |> enqueue "l"
    |> enqueue "m"
    |> enqueue "i"
    |> enqueue "s"
    |> enqueue "h"
    |> dequeue
    |> head
--> Just "l"

Deconstruction

toListLIFO : Queue x -> List x

Convert a queue to List. The last element in the queue will become the head of the returned list.

empty
    |> enqueue 100
    |> enqueue 200
    |> enqueue 300
    |> toListLIFO
--> [300,200,100]
toListFIFO : Queue x -> List x

Convert a queue to List. The first element in the queue will become the head of the returned list.

empty
    |> enqueue 100
    |> enqueue 200
    |> enqueue 300
    |> toListFIFO
--> [100,200,300]