Import Upstream version 20180207
[hcoop/debian/mlton.git] / lib / mlton / heap / test.sml
CommitLineData
7f918cf1
CE
1(* Copyright (C) 1999-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8local
9 structure H = BinaryHeap(structure O = Int
10 open Int
11 fun inject x = x
12 fun project x = x
13 val largest = Int.maxInt
14 val smallest = Int.minInt)
15 open H
16 val h = new[(1, "1"), (2, "2"), (3, "3")]
17 val _ =
18 while not(isEmpty h) do
19 (print(deleteMin h)
20 ; print "\n")
21in
22end
23
24structure IntegerHeap = FibonacciHeap(structure Key' = Integer)
25
26open IntegerHeap ;
27
28fun p h = output h Integer.output print
29
30val h = new [(1, 1)] ;
31
32val w = isWellFormed h ;
33
34val _ = p h ;
35
36fun i n = insert h n n ;
37
38val _ = i 2 ;
39
40val _ = i 3 ;
41
42val h = new (ListUtil.reverse (ListUtil.map (ListUtil.fromTo 1 10)
43 (fn x => (x, x)))) ;
44
45val _ = p h ;
46
47val w = isWellFormed h ;
48
49val a = min h ;
50
51val a = deleteMin h ;
52
53val b = deleteMin h ;
54
55val c = deleteMin h ;
56
57val l = ListUtil.map (ListUtil.fromTo 1 7) (fn _ => deleteMin h) ;
58
59isEmpty h ;
60
61insert h 100 100 ;
62
63isEmpty h ;
64
65min h ;
66
67ListUtil.foreach (ListUtil.fromTo 1 10) (fn i =>
68 (insert h (11 * i) (11 * i) ;
69 ())) ;
70
71min h ;
72
73deleteMin h ;
74
75isEmpty h ;
76
77ListUtil.foreach (ListUtil.reverse (ListUtil.fromTo 1 1100))
78(fn i => (insert h i i ; ())) ;
79
80ListUtil.foreach (ListUtil.fromTo 1 1098) (fn _ => (deleteMin h ; ())) ;
81
82
83insert h 11 11 ;
84
85val _ = p h ;
86
87
88val h: int t = new [] ;
89
90val elts = ListUtil.map (ListUtil.fromTo 0 9) (fn x => insert h x x) ;
91
92val _ = p h ;
93
94fun dc i = decreaseKey h (ListUtil.nth elts i)
95fun d i = delete h (ListUtil.nth elts i)
96
97val _ = dc 6 ~1 ;
98
99 dc 7 0 ;
100
101val a = deleteMin h ;
102
103val _ = d 4 ;