forked from felix-lang/felix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rope.flx
60 lines (49 loc) · 1.28 KB
/
rope.flx
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
// define a rope as a list of strings in reverse order
struct rope {
r: list[&string];
}
// constructor from one string
ctor rope(s:string) => (new s).list[&string].rope;
// appending a string just prepends it to the list
fun + (r:rope,s:string) => rope ((new s)!r.r);
// appending a rope is just list concatenation
fun + (r:rope,s:rope) => rope (s.r + r.r);
fun count (r:rope) => r.r.len;
// the length of a rope is the sum of the lengths of its pieces
fun len (r:rope): size =>
fold_left (fun (acc:size) (s:&string) => acc + s.len) 0uz r.r
;
fun render (r:rope): &string {
var out = "";
reserve (&out, r.len + 1uz);
var x = rev r.r;
next:>
match x with
| Empty => return &out;
| head ! tail =>
x = tail;
out += head;
goto next;
endmatch;
}
proc test () {
var s = "Hello";
for i in 0..12 perform s += s;
println$ "String length = " + s.len.str;
begin
var t = #time;
var k = s;
for i in 0 .. 10 perform k = k + k + k;
println$ "Len = " + k.len.str;
println$ "Strings: Elapsed = " + (#time - t).str + "s";
end
begin
var t = #time;
var k = rope s;
for i in 0 .. 10 perform k = k + k + k;
var q = render k;
println$ "Len = " + q.len.str;
println$ "Ropes total: Elapsed = " + (#time - t).str + "s";
end
}
test;