/usr/lib/ats2-postiats-0.2.6/libats/ATS1/DATS/funheap_braun.dats is in ats2-lang 0.2.6-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | (***********************************************************************)
(* *)
(* Applied Type System *)
(* *)
(* Hongwei Xi *)
(* *)
(***********************************************************************)
(*
** ATS - Unleashing the Potential of Types!
** Copyright (C) 2002-2011 Hongwei Xi, Boston University
** All rights reserved
**
** ATS is free software; you can redistribute it and/or modify it under
** the terms of the GNU GENERAL PUBLIC LICENSE (GPL) as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** ATS is distributed in the hope that it will be useful, but WITHOUT ANY
** WARRANTY; without even the implied warranty of MERCHANTABILITY or
** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
** for more details.
**
** You should have received a copy of the GNU General Public License
** along with ATS; see the file COPYING. If not, please write to the
** Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
** 02110-1301, USA.
*)
(* ****** ****** *)
(*
**
** A functional heap implementation based on Braun trees
**
** Contributed by Hongwei Xi (hwxi AT cs DOT bu DOT edu)
** Time: April, 2010 // based on a version done in November, 2008
**
*)
(* ****** ****** *)
//
// HX-2014-01-15: Porting to ATS2
//
(* ****** ****** *)
#define ATS_DYNLOADFLAG 0 // no dynloading
(* ****** ****** *)
staload "libats/ATS1/SATS/funheap_braun.sats"
(* ****** ****** *)
//
implement{a}
compare_elt_elt (x1, x2, cmp) = cmp (x1, x2)
//
(* ****** ****** *)
datatype
brauntree (a:t@ype+, int) =
| {n1,n2:nat | n2 <= n1; n1 <= n2+1}
B (a, n1+n2+1) of (a, brauntree (a, n1), brauntree (a, n2))
| E (a, 0) of ((*void*))
// end of [brauntree]
stadef bt = brauntree // an abbreviation
(* ****** ****** *)
assume
heap_t0ype_type (a:t@ype) = [n:nat] brauntree (a, n)
(* ****** ****** *)
implement{} funheap_make_nil () = E ()
(* ****** ****** *)
implement{a}
funheap_size (hp) = size (hp) where {
//
// this algorithm is taken from a paper by Chris Okasaki
//
fun diff
{nl,nr:nat |
nr <= nl && nl <= nr+1} .<nr>.
(nr: size_t nr, t: bt (a, nl)):<> int (nl-nr) =
(
case+ t of
| B (_, tl, tr) =>
(
if nr > 0 then let
val nr2 = half (nr)
in
if nr > nr2 + nr2 then diff (nr2, tl) else diff (nr2-1, tr)
end else begin
1 // return value
end // end of [if]
) (* end of [B] *)
| E ((*void*)) => 0
) (* end of [diff] *)
//
fun size {n:nat} .<n>.
(t: bt (a, n)):<> size_t n = case+ t of
| B (_, tl, tr) => let
val nr = size tr; val d1 = diff (nr, tl) + 1
in
nr + nr + i2sz(d1)
end // end of [B]
| E ((*void*)) => i2sz(0)
// end of [size]
} // end of [funheap_size]
(* ****** ****** *)
implement{a}
funheap_height (hp) =
loop (hp, 0) where {
//
fun loop {n:nat} .<n>.
(t: bt (a, n), res: intGte(0)):<> intGte(0) =
(
case+ t of B (_, tl, _) => loop (tl, res + 1) | E () => res
) (* end of [loop] *)
//
} (* end of [funheap_height] *)
(* ****** ****** *)
implement{a}
funheap_insert
(hp, x, cmp) = () where {
//
fun insert {n:nat} .<n>.
(t: bt (a, n), x: a):<cloref> bt (a, n+1) =
case+ t of
| E () => B{a}(x, E (), E ())
| B (x0, t1, t2) => let
val sgn = compare_elt_elt<a> (x0, x, cmp)
in
if sgn >= 0 then
B{a}(x, insert (t2, x0), t1) else B{a}(x0, insert (t2, x), t1)
// end of [if]
end // end of [B]
// end of [insert]
//
val () = hp := insert (hp, x)
//
} (* end of [funheap_insert] *)
(* ****** ****** *)
fun{a:t@ype}
brauntree_leftrem{n:pos} .<n>.
(t: bt (a, n), x_r: &a? >> a):<!wrt> bt (a, n-1) = let
//
val+B (x, t1, t2) = t
//
in
//
case+ t1 of
| B _ => let
val t1 = brauntree_leftrem (t1, x_r) in B{a}(x, t2, t1)
end // end of [B]
| E () => (x_r := x; E ())
//
end // end of [brauntree_leftrem]
(* ****** ****** *)
fn{a:t@ype}
brauntree_siftdn
{nl,nr:nat |
nr <= nl; nl <= nr+1}
(
x: a
, tl: bt (a, nl), tr: bt (a, nr)
, cmp: cmp a
) :<> bt (a, nl+nr+1) =
siftdn (x, tl, tr) where {
//
fun siftdn
{nl,nr:nat | nr <= nl; nl <= nr+1} .<nl+nr>.
(x: a, tl: bt (a, nl), tr: bt (a, nr))
:<cloref> bt (a, nl+nr+1) = case+ (tl, tr) of
| (B (xl, tll, tlr), B (xr, trl, trr)) =>
(
if compare_elt_elt<a> (xl, x, cmp) >= 0 then begin // xl >= x
if compare_elt_elt<a> (xr, x, cmp) >= 0
then B{a}(x, tl, tr) else B{a}(xr, tl, siftdn (x, trl, trr))
// end of [if]
end else begin // xl < x
if compare_elt_elt<a> (xr, x, cmp) >= 0 then B{a}(xl, siftdn (x, tll, tlr), tr)
else begin // xr < x
if compare_elt_elt<a> (xl, xr, cmp) >= 0
then B{a}(xr, tl, siftdn (x, trl, trr)) else B{a}(xl, siftdn (x, tll, tlr), tr)
// end of [if]
end // end of [if]
end (* end of [if] *)
) (* end of [B _, B _] *)
| (_, _) =>> (
case+ tl of
| B (xl, _, _) =>
if compare_elt_elt<a> (xl, x, cmp) >= 0 then B{a}(x, tl, E) else B{a}(xl, B{a}(x, E, E), E)
// end of [B]
| E ((*void*)) => B{a}(x, E (), E ())
) (* end of [_, _] *)
// end of [siftdn]
//
} (* end of [brauntree_siftdn] *)
(* ****** ****** *)
implement{a}
funheap_delmin
(hp0, cmp, res) = let
//
fun delmin{n:pos} .<>.
(
t: bt (a, n), res: &a? >> a
) :<!wrt> bt (a, n-1) = let
val+B (x, t1, t2) = t; val () = res := x in
case+ t1 of
| B _ => let
var x_lrm: a // uninitialized
val t1 = brauntree_leftrem<a> (t1, x_lrm) in
brauntree_siftdn<a> (x_lrm, t2, t1, cmp)
end // end of [B]
| E ((*void*)) => E ()
end // end of [demin]
//
in
//
case+ hp0 of
| B _ => let
val () = hp0 := delmin (hp0, res)
prval () = opt_some {a} (res) in true (*removed*)
end // end of [B_]
| E _ => let
prval () = opt_none {a} (res) in false(*notremoved*)
end // end of [E]
//
end // end of [funheap_delmin]
(* ****** ****** *)
implement{a}
funheap_delmin_opt
(hp0, cmp) = let
//
var res: a? // uninitized
val ans = funheap_delmin<a> (hp0, cmp, res)
//
in
//
if ans
then let
prval () = opt_unsome{a}(res) in Some_vt{a}(res)
end // end of [then]
else let
prval () = opt_unnone{a}(res) in None_vt{a}(*void*)
end // end of [else]
// end of [if]
//
end // end of [funheap_delmin_opt]
(* ****** ****** *)
(* end of [funheap_brauntree.dats] *)
|