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 }