Sort disjoint sublist: Difference between revisions
Content added Content deleted
m (→{{header|ATS}}) |
|||
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) |
||
⚫ | |||
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 |
||
⚫ | |||
⚫ | |||
init_refs {j : nat | j <= m} |
|||
{i : nat | i <= n} |
|||
⚫ | |||
{p_refs : addr} |
|||
.<m - j>. |
|||
implement |
|||
(pf_refs : !array_v (ptr?, p_refs + (i * sizeof ptr), |
|||
array_initize$init<ptr> (i, x) = |
|||
n - i) |
|||
⚫ | |||
>> array_v (ptr, p_refs + (i * sizeof ptr), |
|||
n - i) | |
|||
ix : &array (index, n), |
|||
prval () = lemma_g1uint_param i1 |
|||
p_refs : ptr p_refs, |
|||
i : size_t i, |
|||
val @(pf2, fpf2 | p2) = $UN.ptr_vtake{@[index][n]} p_ix |
|||
values : &list_vt (a, m - j), |
|||
j : size_t j) |
|||
:<!wrt> void = |
|||
$UN.castvwtp0{ptr} (list_vt_getref_at (!p1, ix[i1])) |
|||
if i < num_indices then |
|||
case+ values of |
|||
| list_vt_nil () => $effmask_exn assertloc (false) |
|||
⚫ | |||
| @ list_vt_cons (head, tail) => |
|||
⚫ | |||
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 |
|||
⚫ | |||
prval () = pf_refs := array_v_unnil_nil{ptr?, ptr} pf_refs |
|||
in |
|||
⚫ | |||
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} |
||
{p_refs : addr} |
|||
(pf_refs : !array_v (ptr, p_refs, n) | |
|||
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 |
||
⚫ | |||
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 ( |
write_back_values (pf_refs | p_refs, vals, succ i) |
||
end |
end |
||
val () = write_back_values ( |
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); |