mutable-stack.mth
· 1.5 KiB · Text
Originalformat
Playground
module float-buffer.main
import std.prelude
import std.world
import std.buffer
import std.maybe
struct +Stack(a) {
+buffer: +Buffer
top: IOffset
size: USize
item-size: USize
writer: [a UIndex +Buffer -- +Buffer]
reader: [ UIndex +Buffer -- a +Buffer]
--
def Empty! [
item-size: USize
writer: [a UIndex +Buffer -- +Buffer]
reader: [UIndex +Buffer -- a +Buffer]
--
+Stack(a)
] {
-1 >IOffset >top
0u >USize >size
@item-size >Nat 128 Nat.NatUnsafe * >USize +Buffer.new >+buffer
+Stack
}
def stack-size { size >Nat item-size >Nat * }
def insure-capcity! {
stack-size +buffer(size >Nat >= then(size 2* resize!))
}
def push! {
top:1+ size:1+ insure-capcity!
top >Int >UIndex-clamp writer +buffer(run)
}
def pop! {
top >UIndex-if(
reader +buffer(run) Some
top:1- size(>Int 1- >USize-clamp),
drop None
)
}
def peek! {
UOffset.>Int top >Int swap - >UIndex-if(
reader +buffer(run) Some,
drop None
)
}
def rdrop {
/+Stack
top> item-size> writer> reader> size> drop5
+buffer> rdrop
}
}
def float-stack {
|F64| >item-size
[ +Buffer.!F64 ] >writer
[ +Buffer.@F64 ] >reader
+Stack.Empty!
}
def main {
float-stack
10.0 push!
20.0 push!
30.0 push!
size >Nat count(>UOffset peek! then?(show rdip:print)) rdrop
}
| 1 | module float-buffer.main |
| 2 | |
| 3 | import std.prelude |
| 4 | import std.world |
| 5 | import std.buffer |
| 6 | import std.maybe |
| 7 | |
| 8 | struct +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 | |
| 62 | def float-stack { |
| 63 | |F64| >item-size |
| 64 | [ +Buffer.!F64 ] >writer |
| 65 | [ +Buffer.@F64 ] >reader |
| 66 | +Stack.Empty! |
| 67 | } |
| 68 | |
| 69 | def 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 | } |