Stdlib:Data

This commit is contained in:
Dmitry Boulytchev 2020-08-06 14:56:41 +03:00
parent f6d4a475b4
commit 026158923f
12 changed files with 314 additions and 200 deletions

View file

@ -1,5 +1,5 @@
HashTab internal structure: MNode (-624426958, {[{1, 2, 3}, 100]}, 0, 0, 0)
HashTab internal structure: MNode (-624426958, {[{1, 2, 3}, 200], [{1, 2, 3}, 100]}, 0, 0, 0)
HashTab internal structure: [0, 0, {[{1, 2, 3}, 100]}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
HashTab internal structure: [0, 0, {[{1, 2, 3}, 200], [{1, 2, 3}, 100]}, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Searching: Some (200)
Searching: Some (200)
Replaced: Some (800)

View file

@ -98,3 +98,103 @@
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1

View file

@ -1,6 +1,6 @@
import Collection;
local s = emptySet (), i;
local s = emptySet (compare), i;
for i := 0, i < 100, i := i+1
do
@ -8,7 +8,7 @@ do
validateColl (s)
od;
printf ("Set internal structure: %s\n", s.string);
printf ("Set internal structure: %s\n", internalOf (s).string);
printf ("Set elements: %s\n", elements (s).string);
for i := 0, i < 100, i := i+1
@ -23,7 +23,7 @@ do
validateColl (s)
od;
printf ("Set internal structure: %s\n", s.string);
printf ("Set internal structure: %s\n", internalOf (s).string);
printf ("Set elements: %s\n", elements (s).string);
for i := 0, i < 100, i := i+1
@ -31,20 +31,20 @@ do
printf ("Testing %-3d => %d\n", i, memSet (s, i))
od;
printf ("List set: %s\n", listSet ({1, 2, 3, 4, 5}).string);
printf ("List set: %s\n", internalOf (listSet ({1, 2, 3, 4, 5}, compare)).string);
{
local u = union (listSet ({1, 2, 3, 4, 5}), listSet ({11, 22, 33, 44, 55})), u1;
local u = union (listSet ({1, 2, 3, 4, 5}, compare), listSet ({11, 22, 33, 44, 55}, compare)), u1;
validateColl (u);
printf ("Set union: %s\n", u.string);
printf ("Set union: %s\n", internalOf (u).string);
printf ("Elements: %s\n", elements (u).string);
u1 := diff (u, listSet ({1, 22, 3, 44, 5}));
u1 := diff (u, listSet ({1, 22, 3, 44, 5}, compare));
validateColl (u1);
printf ("Set difference: %s\n", u1.string);
printf ("Set difference: %s\n", internalOf (u1).string);
printf ("Elements: %s\n", elements (u1).string)
}

View file

@ -1,6 +1,6 @@
import Collection;
local s = emptyMap (), i;
local s = emptyMap (compare), i;
for i := 0, i < 100, i := i+1
do
@ -8,7 +8,7 @@ do
validateColl (s)
od;
printf ("Map internal structure: %s\n", s.string);
printf ("Map internal structure: %s\n", internalOf (s).string);
printf ("Map elements: %s\n", bindings (s).string);
for i := 0, i < 100, i := i+1
@ -23,7 +23,7 @@ do
validateColl (s)
od;
printf ("Map internal structure: %s\n", s.string);
printf ("Map internal structure: %s\n", internalOf (s).string);
printf ("Map elements: %s\n", bindings (s).string);
for i := 0, i < 100, i := i+1
@ -31,4 +31,4 @@ do
printf ("Testing %-3d => %s\n", i, findMap (s, i).string)
od;
printf ("List map: %s\n", listMap ({[1, 10], [2, 20], [3, 30], [4, 40], [5, 50]}).string)
printf ("List map: %s\n", internalOf (listMap ({[1, 10], [2, 20], [3, 30], [4, 40], [5, 50]}, compare)).string)

View file

@ -1,25 +1,21 @@
import Collection;
local a = {1, 2, 3}, b = {1, 2, 3}, t = emptyHashTab ();
local a = {1, 2, 3}, b = {1, 2, 3}, t = emptyHashTab (16, hash, compare);
t := addHashTab (t, a, 100);
validateColl (t);
printf ("HashTab internal structure: %s\n", t.string);
printf ("HashTab internal structure: %s\n", internalOf (t).string);
t := addHashTab (t, b, 200);
validateColl (t);
printf ("HashTab internal structure: %s\n", t.string);
printf ("HashTab internal structure: %s\n", internalOf (t).string);
printf ("Searching: %s\n", findHashTab (t, a).string);
printf ("Searching: %s\n", findHashTab (t, b).string);
t := addHashTab (t, a, 800);
validateColl (t);
printf ("Replaced: %s\n", findHashTab (t, a).string);
t := removeHashTab (t, a);
validateColl (t);
printf ("Restored: %s\n", findHashTab (t, a).string)

View file

@ -3,83 +3,7 @@ import Ref;
import Random;
import Array;
import Fun;
local m = emptyPtrMap ();
fun eq (x, y) {
local m = ref (emptyPtrMap ());
fun alreadyEq (x, y) {
fun find (x) {
fun walk (r) {
case r of
[#unboxed] -> r
| [x] -> walk (x)
esac
}
case findPtrMap (deref (m), x) of
Some (r) -> Some (walk (r))
| x -> x
esac
}
case [find (x), find (y)] of
[None, None] ->
local v = [1];
m ::= addPtrMap (addPtrMap (deref (m), x, v), y, v);
false
| [None, Some (ry)] ->
m ::= addPtrMap (deref (m), x, ry);
false
| [Some (rx), None] ->
m ::= addPtrMap (deref (m), y, rx);
false
| [Some (rx), Some (ry)] ->
if rx == ry
then true
else
rx [0] := ry;
false
fi
esac
}
fun eqargs (x, y, from) {
local continue = true;
if x.length != y.length
then false
else
for local i = from;, i<x.length && continue, i := i + 1 do
continue := eqrec (x[i], y[i])
od;
continue
fi
}
fun eqrec (x, y) {
if alreadyEq (x, y)
then true
else
if rawTag (x) != rawTag (y) then false
else
case x of
#array -> eqargs (x, y, 0)
| #fun -> if x[0] == y[0] then eqargs (x, y, 1) else false fi
| #sexp -> if compareTags (x, y) == 0 then eqargs (x, y, 0) else false fi
| _ -> compare (x, y) == 0
esac
fi
fi
}
eqrec (x, y)
}
import Data;
fun genCyclicArrays (n, eq, cross) {
local f = ref (true);
@ -154,34 +78,49 @@ fun genCyclicArrays (n, eq, cross) {
genrec (n, [], [], 0)
}
fun normalize (x) {
if x < 0 then -1
elif x > 0 then 1
else 0
fi
}
fun not (x) {0 - x}
disableGC ();
for local i=0;, i<25, i:=i+1
do
case genCyclicArrays (1000, true, false) of
[a, _] -> printf ("%d\n", eq (a, a) == true)
| [a, b] -> printf ("%d\n", eq (a, b) == true)
[a, b] ->
printf ("%d\n", (a =?= a) == 0);
printf ("%d\n", (a =?= b) == 0)
esac
od;
for local i=0;, i<25, i:=i+1
do
case genCyclicArrays (1000, true, true) of
[a, _] -> printf ("%d\n", eq (a, a) == true)
| [a, b] -> printf ("%d\n", eq (a, b) == true)
[a, b] ->
printf ("%d\n", (a =?= a) == 0);
printf ("%d\n", (a =?= b) == 0)
esac
od;
for local i=0;, i<25, i:=i+1
do
case genCyclicArrays (1000, false, false) of
[a, b] -> printf ("%d\n", eq (a, b) == false)
[a, b] -> local x = normalize (a =?= b);
printf ("%d\n", x != 0);
printf ("%d\n", not (x) == normalize (b =?= a))
esac
od;
for local i=0;, i<25, i:=i+1
do
case genCyclicArrays (1000, false, true) of
[a, b] -> printf ("%d\n", eq (a, b) == false)
[a, b] -> local x = normalize (a =?= b);
printf ("%d\n", x != 0);
printf ("%d\n", not (x) == normalize (b =?= a))
esac
od