Fix indentation.
Add missing variable annotation. Remove unecessary annotations. Now pass. svn: r9522
This commit is contained in:
parent
a39a9ddfb7
commit
53709f1faf
|
@ -77,7 +77,7 @@
|
||||||
(define empty
|
(define empty
|
||||||
(case-lambda
|
(case-lambda
|
||||||
[() (make-heap-empty (current-compare))]
|
[() (make-heap-empty (current-compare))]
|
||||||
[(cmp) (make-heap-empty cmp)]))
|
[(#{cmp : comparator}) (make-heap-empty cmp)]))
|
||||||
|
|
||||||
(define: empty? : (pred heap-empty) heap-empty?)
|
(define: empty? : (pred heap-empty) heap-empty?)
|
||||||
|
|
||||||
|
@ -98,11 +98,8 @@
|
||||||
(cond
|
(cond
|
||||||
[(empty? h1) h2]
|
[(empty? h1) h2]
|
||||||
[(empty? h2) h1]
|
[(empty? h2) h1]
|
||||||
[else (let*: (;; added new bindings at simpler types
|
[else (let* ([x (heap-node-elm h1)]
|
||||||
[h1 : (heap-node a) h1]
|
[y (heap-node-elm h2)])
|
||||||
[h2 : (heap-node a) h2]
|
|
||||||
[x : a (heap-node-elm h1)] ;; FIXME - FUCK FUCK FUCK - why not x?
|
|
||||||
[y : a (heap-node-elm h2)])
|
|
||||||
(if<=? ((heap-compare h1) x y)
|
(if<=? ((heap-compare h1) x y)
|
||||||
(make x (heap-node-left h1) (union (heap-node-right h1) h2))
|
(make x (heap-node-left h1) (union (heap-node-right h1) h2))
|
||||||
(make y (heap-node-left h2) (union h1 (heap-node-right h2)))))]))
|
(make y (heap-node-left h2) (union h1 (heap-node-right h2)))))]))
|
||||||
|
@ -197,11 +194,8 @@
|
||||||
(heap-node-left h))
|
(heap-node-left h))
|
||||||
(heap-node-right h))))
|
(heap-node-right h))))
|
||||||
|
|
||||||
;; FIXME
|
|
||||||
(pdefine: (a) (elements [h : (Heap a)]) : (list-of a)
|
(pdefine: (a) (elements [h : (Heap a)]) : (list-of a)
|
||||||
(fold (lambda: ([x : a] [l : (list-of a)]) (cons x l))
|
(fold (lambda: ([x : a] [l : (list-of a)]) (cons x l)) '() h))
|
||||||
#;#{cons : (a (list-of a) -> (list-of a))}
|
|
||||||
#{'() :: (list-of a)} h))
|
|
||||||
|
|
||||||
(pdefine: (a) (count [x : a] [h : (Heap a)]) : number
|
(pdefine: (a) (count [x : a] [h : (Heap a)]) : number
|
||||||
(let ([cmp (heap-compare h)])
|
(let ([cmp (heap-compare h)])
|
||||||
|
@ -211,16 +205,20 @@
|
||||||
s))
|
s))
|
||||||
0 h)))
|
0 h)))
|
||||||
|
|
||||||
|
(pdefine: (a) (-heap . [xs : a]) : (Heap a)
|
||||||
|
(list->heap xs))
|
||||||
|
|
||||||
|
|
||||||
(define: list->heap : (All (a) (case-lambda (comparator (list-of a) -> (Heap a)) ((list-of a) -> (Heap a))))
|
(define: list->heap : (All (a) (case-lambda (comparator (list-of a) -> (Heap a)) ((list-of a) -> (Heap a))))
|
||||||
; time: O(n)
|
; time: O(n)
|
||||||
(pcase-lambda: (a)
|
(pcase-lambda: (a)
|
||||||
[([l : (list-of a)]) (list->heap (current-compare) l)]
|
[([l : (list-of a)]) (list->heap (current-compare) l)]
|
||||||
[([cmp : comparator] [l : (list-of a)])
|
[([cmp : comparator] [l : (list-of a)])
|
||||||
(let* ([e (empty cmp)]
|
(let* ([e (#{empty @ a} cmp)]
|
||||||
[hs (map (lambda: ([x : a]) (insert x e)) l)])
|
[hs (map (lambda: ([x : a]) (insert x e)) l)])
|
||||||
; (list heap) -> (list heap)
|
; (list heap) -> (list heap)
|
||||||
; merge adjacent pairs of heaps
|
; merge adjacent pairs of heaps
|
||||||
(define: (merge-pairs [hs : (list-of (Heap a))]) : (list-of (Heap a))
|
(define: (merge-pairs [hs : (Listof (Heap a))]) : (list-of (Heap a))
|
||||||
(cond
|
(cond
|
||||||
[(or (null? hs)
|
[(or (null? hs)
|
||||||
(null? (cdr hs))) hs]
|
(null? (cdr hs))) hs]
|
||||||
|
@ -236,9 +234,6 @@
|
||||||
[(null? (cdr hs)) (car hs)]
|
[(null? (cdr hs)) (car hs)]
|
||||||
[else (loop (merge-pairs hs))]))))]))
|
[else (loop (merge-pairs hs))]))))]))
|
||||||
|
|
||||||
;; FIXME - moved to after list->heap
|
|
||||||
(pdefine: (a) (-heap . [xs : a]) : (Heap a)
|
|
||||||
(list->heap xs))
|
|
||||||
|
|
||||||
|
|
||||||
(pdefine: (a) (insert* [xs : (list-of a)] [h : (Heap a)]) : (Heap a)
|
(pdefine: (a) (insert* [xs : (list-of a)] [h : (Heap a)]) : (Heap a)
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
#lang typed-scheme
|
||||||
;;; priority-queue.scm -- Jens Axel Søgaard
|
;;; priority-queue.scm -- Jens Axel Søgaard
|
||||||
;;; PURPOSE
|
;;; PURPOSE
|
||||||
|
|
||||||
; This file implements priority queues on top of
|
; This file implements priority queues on top of
|
||||||
; a heap library.
|
; a heap library.
|
||||||
#lang typed-scheme
|
|
||||||
(define-type-alias number Number)
|
(define-type-alias number Number)
|
||||||
(define-type-alias boolean Boolean)
|
(define-type-alias boolean Boolean)
|
||||||
(define-type-alias symbol Symbol)
|
(define-type-alias symbol Symbol)
|
||||||
|
@ -78,11 +78,9 @@
|
||||||
(pdefine: (a) (insert [x : a] [p : number] [pq : (priority-queue a)]) : (priority-queue a)
|
(pdefine: (a) (insert [x : a] [p : number] [pq : (priority-queue a)]) : (priority-queue a)
|
||||||
(make (heap:insert (#{cons :: (case-lambda (a (list-of a) -> (list-of a)) (number a -> (cons number a)))} p x) (heap pq))))
|
(make (heap:insert (#{cons :: (case-lambda (a (list-of a) -> (list-of a)) (number a -> (cons number a)))} p x) (heap pq))))
|
||||||
|
|
||||||
;; FIXME - had to insert extra binding to give the typechecker more help
|
;; FIXME -- too many annotations needed on cons
|
||||||
;; could have done this with annotation on map, probably
|
|
||||||
(pdefine: (a) (insert* [xs : (list-of a)] [ps : (list-of number)] [pq : (priority-queue a)]) : (priority-queue a)
|
(pdefine: (a) (insert* [xs : (list-of a)] [ps : (list-of number)] [pq : (priority-queue a)]) : (priority-queue a)
|
||||||
(let ([cons #{cons :: (case-lambda (a (list-of a) -> (list-of a)) (number a -> (cons number a)))}])
|
(make (heap:insert* (map #{#{cons @ number a} :: (number a -> (cons number a))} ps xs) (heap pq))))
|
||||||
(make (heap:insert* (map #{cons :: (number a -> (cons number a))} ps xs) (heap pq)))))
|
|
||||||
|
|
||||||
(pdefine: (a) (delete-min [pq : (priority-queue a)]) : (priority-queue a)
|
(pdefine: (a) (delete-min [pq : (priority-queue a)]) : (priority-queue a)
|
||||||
(let ([h (heap pq)])
|
(let ([h (heap pq)])
|
||||||
|
|
|
@ -19,8 +19,8 @@
|
||||||
(pdefine: (b) (heap-size [h : (Heap b)]) : number
|
(pdefine: (b) (heap-size [h : (Heap b)]) : number
|
||||||
(cond [(heap-empty? h) 0]
|
(cond [(heap-empty? h) 0]
|
||||||
[(heap-node? h)
|
[(heap-node? h)
|
||||||
(+ 1 (+ (#{heap-size @ b} (heap-node-left h))
|
(+ 1 (+ (heap-size (heap-node-left h))
|
||||||
(#{heap-size @ b} (heap-node-right h))))]
|
(heap-size (heap-node-right h))))]
|
||||||
;; FIXME - shouldn't need else clause
|
;; FIXME - shouldn't need else clause
|
||||||
[else (error "Never happens!")]))
|
[else (error "Never happens!")]))
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user