Остання активність 1737485783

Версія 76ed06611e46268945305273e55a8c7939ae9c7d

mutable-stack.mth Неформатований Playground
1module float-buffer.main
2
3import std.prelude
4import std.world
5import std.buffer
6import std.maybe
7
8struct +Stack(a) {
9 +buffer: +Buffer
10 top: IOffset
11 size: USize
12 item-size: USize
13 writer: [a UIndex +Buffer -- +Buffer]
14 reader: [ UIndex +Buffer -- a +Buffer]
15 --
16 def Empty! [
17 item-size: USize
18 writer: [a UIndex +Buffer -- +Buffer]
19 reader: [UIndex +Buffer -- a +Buffer]
20 --
21 +Stack(a)
22 ] {
23 -1 >IOffset >top
24 0u >USize >size
25 @item-size >Nat 128 Nat.NatUnsafe * >USize +Buffer.new >+buffer
26 +Stack
27 }
28
29 def stack-size { size >Nat item-size >Nat * }
30
31 def insure-capcity! {
32 stack-size +buffer(size >Nat >= then(size 2* resize!))
33 }
34
35 def push! {
36 top:1+ size:1+ insure-capcity!
37 top >Int >UIndex-clamp writer +buffer(run)
38 }
39
40 def pop! {
41 top >UIndex-if(
42 reader +buffer(run) Some
43 top:1- size(>Int 1- >USize-clamp),
44 drop None
45 )
46 }
47
48 def peek! {
49 UOffset.>Int top >Int swap - >UIndex-if(
50 reader +buffer(run) Some,
51 drop None
52 )
53 }
54
55 def rdrop {
56 /+Stack
57 top> item-size> writer> reader> size> drop5
58 +buffer> rdrop
59 }
60}
61
62def float-stack {
63 |F32| >item-size
64 [ +Buffer.!F64 ] >writer
65 [ +Buffer.@F64 ] >reader
66 +Stack.Empty!
67}
68
69def main {
70 float-stack
71 10.0 push!
72 20.0 push!
73 30.0 push!
74 size >Nat count(>UOffset peek! then?(show rdip:print)) rdrop
75}