Data/Random/Buffer, spec updated

This commit is contained in:
Dmitry Boulytchev 2020-08-22 20:11:41 +03:00
parent 94e4b16267
commit f08cd8396f
9 changed files with 164 additions and 10 deletions

66
stdlib/Buffer.lama Normal file
View file

@ -0,0 +1,66 @@
-- Buffer.
-- (C) Dmitry Boulytchev, JetBrains Research, St. Petersburg State University, 2020
--
-- This unit provides a simple add-to-the-end buffer implemetation.
import List;
-- Creates an empty buffer
public fun emptyBuffer () {
{}
}
-- Creates a buffer with one element x
public fun singletonBuffer (x) {
local y = singleton (x);
[y, y]
}
-- Creates a buffer from a list x
public fun listBuffer (x) {
foldl (addBuffer, emptyBuffer (), x)
}
-- Adds x to the end of buffer buf
public fun addBuffer (buf, x) {
case buf of
{} -> local y = singleton (x);
[y, y]
| [head, last] ->
last[1] := singleton (x);
[head, last[1]]
esac
}
-- Adds the contents of buffer x to the end of buffer buf
public fun concatBuffer (buf, x) {
case buf of
{} -> x
| [head, last] ->
case x of
{} -> buf
| [h, l] ->
last[1] := h;
[head, l]
esac
esac
}
-- Infix synonym for concatBuffer
public infixl <+> before + (b1, b2) {
concatBuffer (b1, b2)
}
-- Infix synonym for addBuffer
public infix <+ at <+> (b, x) {
addBuffer (b, x)
}
-- Gets the contents of the buffer buf (as a list)
public fun getBuffer (buf) {
case buf of
{} -> {}
| [head, _] -> head
esac
}

View file

@ -79,12 +79,20 @@ public fun flatten (l) {
fun append (x) {
local new = x : {};
curr [0][1] := new;
curr [0] := new
}
fun traverse (l) {
case l of
_ : _ -> iter (traverse, l)
| {} -> skip
| _ -> append (l)
esac
}
iter (fun (x) {iter (append, x)}, l);
traverse (l);
res [1]
}

View file

@ -16,6 +16,8 @@ Array.o: List.o
Ostap.o: List.o Collection.o Ref.o Fun.o Matcher.o
Buffer.o: List.o
Expr.o: Ostap.o
%.o: %.lama

View file

@ -28,13 +28,12 @@ public fun randomString (len) {
-- Generates a random array of (deep) size n. f is element-generation
-- function which takes the size of the element
public fun randomArray (f, n) {
mapArray (f, split (n))
mapArray (f, split (n, random (n + 1)))
}
-- Splits a number in a random sequence of summands
public fun split (n) {
local k = random (n) + 1,
a = makeArray (k),
-- Splits a number n in a random sequence of k summands
public fun split (n, k) {
local a = makeArray (k),
m = n;
for local i = 0;, i < k, i := i + 1
@ -49,3 +48,22 @@ public fun split (n) {
a
}
-- Generates a (recursive) data structure of size n. nodeSpec is an array of pairs [n, fn] where
-- fn gets an array of values of length n and generates some node; leaf is a fucntion without
-- arguments which create a leaf.
public fun structure (n, nodeSpec, leaf) {
local k = nodeSpec.length;
fun rec (n) {
if n <= 1
then leaf ()
else
local ns = nodeSpec [random (k)];
ns [1] (mapArray (rec, split (n, ns [0])))
fi
}
rec (n)
}

View file

@ -24,7 +24,7 @@ fun genCyclicArrays (n, eq, cross) {
x -> [x, clone (x)]
esac
else
local a = split (n),
local a = split (n, random (n+1)),
b = mapArray (id, a),
index = initArray (random (a.length + 1), fun (_) {random (a.length)});