/**
 * Copyright (c) 2016-present, Facebook, Inc.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */ import * as List from './list' ; import * as Maybe from './maybe' ; export class Queue < T > { public forwardList : List . List < T > ; public backwardList : List . List < T > ; constructor ( forwardList , backwardList ) { this . forwardList = forwardList ; this . backwardList = backwardList ; } } export interface DequeueResult < T > { value : T | null ; queue : Queue < T > ; } export function Empty < T > ( ) { return new Queue < T > ( List . of < T > ( ) , List . of < T > ( ) ) ; } export function dequeue < T > ( queue ) { return queue . forwardList . match ( function ( ) { const newForwardList = List . reverse ( queue . backwardList ) ; return newForwardList . match ( function ( ) { const result = { value : null , queue : Empty < T > ( ) } ; return result ; } , function ( head , tail ) { const newQueue = new Queue < T > ( tail , List . of < T > ( ) ) ; const result = { value : head , queue : newQueue } ; return result ; } ) ; } , function ( head , tail ) { const newQueue = new Queue < T > ( tail , queue . backwardList ) ; const result = { value : head , queue : newQueue } ; return result ; } ) ; } export function of < T > ( ... args ) { var list = List . of < T > ( ) ; for ( var i = args . length - 1 ; i >= 0 ; i -- ) { list = List . cons ( args [ i ] , list ) ; } const queue = new Queue ( list , List . of < T > ( ) ) ; return queue ; } export function enqueue < T > ( value , queue ) { return new Queue < T > ( queue . forwardList , List . cons ( value , queue . backwardList ) ) ; }