
com.avsystem.commons.collection.MutableStack.scala Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of commons-core_2.13 Show documentation
Show all versions of commons-core_2.13 Show documentation
AVSystem commons library for Scala
package com.avsystem.commons
package collection
final class MutableStack[T] {
private[this] var ssize: Int = 0
private[this] var stack = List.empty[T]
def push(elem: T): Unit = {
stack ::= elem
ssize += 1
}
def pushAll(elems: IterableOnce[T]): Unit =
elems.iterator.foreach(push)
def pop(): T = stack match {
case head :: tail =>
stack = tail
ssize -= 1
head
case Nil => throw new NoSuchElementException("pop on empty stack")
}
def popOpt(): Opt[T] = stack match {
case head :: tail =>
stack = tail
ssize -= 1
Opt.some(head)
case Nil => Opt.Empty
}
def popOption(): Option[T] = stack match {
case head :: tail =>
stack = tail
ssize -= 1
Some(head)
case Nil => None
}
def top: T = stack match {
case head :: _ => head
case _ => throw new NoSuchElementException("top on empty stack")
}
def topOpt: Opt[T] = stack match {
case head :: _ => Opt.some(head)
case _ => Opt.Empty
}
def topOption: Option[T] =
stack.headOption
def asList: List[T] =
stack
def size: Int =
ssize
def isEmpty: Boolean =
stack.isEmpty
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy