Sort disjoint sublist: Difference between revisions

Content added Content deleted
Line 576: Line 576:
let
let
typedef index = [i : nat | i < m] int i
typedef index = [i : nat | i < m] int i

val p_values = addr@ values


prval () = lemma_list_vt_param values
prval () = lemma_list_vt_param values
Line 594: Line 592:
val () = array_quicksort<index> (ix, num_indices)
val () = array_quicksort<index> (ix, num_indices)


(* Initialize a "refs" array with pointers to the relevant list
nodes. The pointers will be in ascending index order. *)
val @(pf_refs, pfgc_refs | p_refs) =
val @(pf_refs, pfgc_refs | p_refs) =
array_ptr_alloc<ptr> num_indices
array_ptr_alloc<ptr> num_indices
fun
macdef refs = !p_refs
init_refs {j : nat | j <= m}

{i : nat | i <= n}
(* Initialize the "refs" array with pointers to the relevant list
nodes. The pointers will be in ascending index order. *)
{p_refs : addr}
.<m - j>.
implement
(pf_refs : !array_v (ptr?, p_refs + (i * sizeof ptr),
array_initize$init<ptr> (i, x) =
n - i)
let
val @(pf1, fpf1 | p1) = $UN.ptr_vtake{list_vt (a, m)} p_values
>> array_v (ptr, p_refs + (i * sizeof ptr),
val i1 = g1ofg0 i
n - i) |
ix : &array (index, n),
prval () = lemma_g1uint_param i1
val () = assertloc (i1 < num_indices)
p_refs : ptr p_refs,
i : size_t i,
val @(pf2, fpf2 | p2) = $UN.ptr_vtake{@[index][n]} p_ix
macdef ix = !p_ix
values : &list_vt (a, m - j),
val () = x :=
j : size_t j)
:<!wrt> void =
$UN.castvwtp0{ptr} (list_vt_getref_at (!p1, ix[i1]))
prval () = fpf2 pf2
if i < num_indices then
prval () = fpf1 pf1
case+ values of
| list_vt_nil () => $effmask_exn assertloc (false)
in
| @ list_vt_cons (head, tail) =>
end
if j = ix[i] then
val () = array_initize<ptr> (refs, num_indices)
let
prval @(pf_elem, pf_rest) = array_v_uncons pf_refs
val p = ptr_add<ptr> (p_refs, i)
val () = ptr_set<ptr> (pf_elem | p, addr@ values)
val () = init_refs (pf_rest | ix, p_refs, succ i,
tail, succ j)
prval () = pf_refs := array_v_cons (pf_elem, pf_rest)
prval () = fold@ values
in
end
else
let
val () = init_refs (pf_refs | ix, p_refs, i,
tail, succ j)
prval () = fold@ values
in
end
else
let
prval () = pf_refs := array_v_unnil_nil{ptr?, ptr} pf_refs
in
end
val () = init_refs (pf_refs | ix, p_refs, i2sz 0, values, i2sz 0)


(* Sort through the "refs" pointers. Here we will do that by
(* Sort through the "refs" pointers. Here we will do that by
Line 647: Line 670:
fun
fun
write_back_values
write_back_values
{i : nat | i <= n}
{i : nat | i <= n}
(refs : &array (ptr, n),
{p_refs : addr}
vals : &array (a, n),
(pf_refs : !array_v (ptr, p_refs, n) |
i : size_t i)
p_refs : ptr p_refs,
vals : &array (a, n),
i : size_t i)
: void =
: void =
if i <> num_indices then
if i <> num_indices then
let
let
macdef refs = !p_refs
val @(pf1, fpf1 | p1) = $UN.ptr_vtake{List1_vt a} (refs[i])
val @(pf1, fpf1 | p1) = $UN.ptr_vtake{List1_vt a} (refs[i])
val+ @ list_vt_cons (head, tail) = !p1
val+ @ list_vt_cons (head, tail) = !p1
Line 660: Line 686:
prval () = fpf1 pf1
prval () = fpf1 pf1
in
in
write_back_values (refs, vals, succ i)
write_back_values (pf_refs | p_refs, vals, succ i)
end
end
val () = write_back_values (refs, vals, i2sz 0)
val () = write_back_values (pf_refs | p_refs, vals, i2sz 0)
in
in
array_ptr_free (pf_ix, pfgc_ix | p_ix);
array_ptr_free (pf_ix, pfgc_ix | p_ix);