#lang typed/racket #:optimize
(provide filter remove
Queue queue enqueue head tail empty empty? queue->list
(rename-out [qmap map]) fold)
(require scheme/match)
(struct: (A) Reversing ([count : Integer]
[first : (Listof A)]
[second : (Listof A)]
[third : (Listof A)]
[fourth : (Listof A)]))
(struct: (A) Appending ([count : Integer]
[first : (Listof A)]
[second : (Listof A)]))
(struct: (A) Done ([first : (Listof A)]))
(define-type (RotationState A) (U Null (Reversing A) (Appending A) (Done A)))
(struct: (A) Queue ([lenf : Integer]
[front : (Listof A)]
[state : (RotationState A)]
[lenr : Integer]
[rear : (Listof A)]))
(: exec : (All (A) ((RotationState A) -> (RotationState A))))
(define (exec state)
(match state
[(struct Reversing (cnt (cons x first) second (cons y third) fourth))
(Reversing (add1 cnt) first (cons x second) third (cons y fourth))]
[(struct Reversing (cnt null second (list y) fourth))
(Appending cnt second (cons y fourth))]
[(struct Appending (0 first second)) (Done second)]
[(struct Appending (cnt (cons x first) second))
(Appending (sub1 cnt) first (cons x second))]
[else state]))
(: invalidate : (All (A) ((RotationState A) -> (RotationState A))))
(define (invalidate state)
(match state
[(struct Reversing (cnt first second third fourth))
(Reversing (sub1 cnt) first second third fourth)]
[(struct Appending (0 first (cons x second))) (Done second)]
[(struct Appending (cnt first second))
(Appending (sub1 cnt) first second)]
[else state]))
(: exec2 :
(All (A) (Integer (Listof A) (RotationState A) Integer (Listof A) ->
(Queue A))))
(define (exec2 lenf front state lenr rear)
(let ([newstate (exec (exec state))])
(match newstate
[(struct Done (newf)) (Queue lenf newf null lenr rear)]
[else (Queue lenf front newstate lenr rear)])))
(: check :
(All (A) (Integer (Listof A) (RotationState A) Integer (Listof A) ->
(Queue A))))
(define (check lenf front state lenr rear)
(if (<= lenr lenf)
(exec2 lenf front state lenr rear)
(exec2 (+ lenf lenr) front
(Reversing 0 front null rear null) 0 null)))
(: empty? : (All (A) ((Queue A) -> Boolean)))
(define (empty? que)
(zero? (Queue-lenf que)))
(define empty (Queue 0 null null 0 null))
(: enqueue : (All (A) (A (Queue A) -> (Queue A))))
(define (enqueue elem que)
(check (Queue-lenf que)
(Queue-front que)
(Queue-state que)
(add1 (Queue-lenr que))
(cons elem (Queue-rear que))))
(: head : (All (A) ((Queue A) -> A)))
(define (head que)
(let ([fr (Queue-front que)])
(if (null? fr)
(error 'head "given queue is empty")
(car fr))))
(: tail : (All (A) ((Queue A) -> (Queue A))))
(define (tail que)
(let ([fr (Queue-front que)])
(if (null? fr)
(error 'tail "given queue is empty")
(check (sub1 (Queue-lenf que))
(cdr fr)
(invalidate (Queue-state que))
(Queue-lenr que)
(Queue-rear que)))))
(: qmap :
(All (A C B ...)
(case-lambda
((A -> C) (Queue A) -> (Queue C))
((A B ... B -> C) (Queue A) (Queue B) ... B -> (Queue C)))))
(define qmap
(pcase-lambda: (A C B ...)
[([func : (A -> C)]
[deq : (Queue A)])
(map-single empty func deq)]
[([func : (A B ... B -> C)]
[deq : (Queue A)] . [deqs : (Queue B) ... B])
(apply map-multiple empty func deq deqs)]))
(: map-single : (All (A C) ((Queue C) (A -> C) (Queue A) -> (Queue C))))
(define (map-single accum func que)
(if (empty? que)
accum
(map-single (enqueue (func (head que)) accum) func (tail que))))
(: map-multiple :
(All (A C B ...)
((Queue C) (A B ... B -> C) (Queue A) (Queue B) ... B -> (Queue C))))
(define (map-multiple accum func que . ques)
(if (or (empty? que) (ormap empty? ques))
accum
(apply map-multiple
(enqueue (apply func (head que) (map head ques)) accum)
func
(tail que)
(map tail ques))))
(: fold :
(All (A C B ...)
(case-lambda ((C A -> C) C (Queue A) -> C)
((C A B ... B -> C) C (Queue A) (Queue B) ... B -> C))))
(define fold
(pcase-lambda: (A C B ...)
[([func : (C A -> C)]
[base : C]
[que : (Queue A)])
(if (empty? que)
base
(fold func (func base (head que)) (tail que)))]
[([func : (C A B ... B -> C)]
[base : C]
[que : (Queue A)] . [ques : (Queue B) ... B])
(if (or (empty? que) (ormap empty? ques))
base
(apply fold
func
(apply func base (head que) (map head ques))
(tail que)
(map tail ques)))]))
(: queue : (All (A) (A * -> (Queue A))))
(define (queue . lst)
(foldl (inst enqueue A) empty lst))
(: queue->list (All (A) ((Queue A) -> (Listof A))))
(define (queue->list que)
(if (empty? que)
null
(cons (head que) (queue->list (tail que)))))
(: filter : (All (A) ((A -> Boolean) (Queue A) -> (Queue A))))
(define (filter func que)
(: inner : (All (A) ((A -> Boolean) (Queue A) (Queue A) -> (Queue A))))
(define (inner func que accum)
(if (empty? que)
accum
(let ([head (head que)]
[tail (tail que)])
(if (func head)
(inner func tail (enqueue head accum))
(inner func tail accum)))))
(inner func que empty))
(: remove : (All (A) ((A -> Boolean) (Queue A) -> (Queue A))))
(define (remove func que)
(: inner : (All (A) ((A -> Boolean) (Queue A) (Queue A) -> (Queue A))))
(define (inner func que accum)
(if (empty? que)
accum
(let ([head (head que)]
[tail (tail que)])
(if (func head)
(inner func tail accum)
(inner func tail (enqueue head accum))))))
(inner func que empty))