You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
852 B
37 lines
852 B
package nova.monadic_sfx.util
|
|
|
|
final class BoundedStack private (
|
|
val underlying: List[Int],
|
|
val maxSize: Int,
|
|
val sp: Int
|
|
) {
|
|
def push(v: Int): Either[String, BoundedStack] =
|
|
if (sp < maxSize) Right(new BoundedStack(v :: underlying, maxSize, sp + 1))
|
|
else Left("overflow")
|
|
|
|
def pushAll(v: List[Int]) =
|
|
if (sp + v.length < maxSize)
|
|
Right(new BoundedStack(v ::: underlying, maxSize, sp + v.length))
|
|
else Left("overflow")
|
|
|
|
def pop =
|
|
if (sp == 0) Left("Underflow")
|
|
else
|
|
Right(
|
|
underlying(sp) -> new BoundedStack(
|
|
underlying.splitAt(sp)._1,
|
|
maxSize,
|
|
sp - 1
|
|
)
|
|
)
|
|
}
|
|
|
|
object BoundedStack {
|
|
def apply(
|
|
// defaultValues: List[Int] = List.empty,
|
|
maxSize: Int = 10
|
|
// sp: Int = 0
|
|
) =
|
|
new BoundedStack(List.empty, maxSize, 0)
|
|
|
|
}
|