summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSeth Tisue <seth@tisue.net>2016-10-20 21:42:17 -0700
committerGitHub <noreply@github.com>2016-10-20 21:42:17 -0700
commit5b8e4d3436b4b22d67b99e37573957554f151dd9 (patch)
tree4b68eaec52fc19f941b06a4e82a8368f59349d34
parentaef9786669bb7bae4d46fd6edefb54e8fb0e6def (diff)
parentef8360f243d7d840437f3383971898a66bf758c2 (diff)
downloadscala-5b8e4d3436b4b22d67b99e37573957554f151dd9.tar.gz
scala-5b8e4d3436b4b22d67b99e37573957554f151dd9.tar.bz2
scala-5b8e4d3436b4b22d67b99e37573957554f151dd9.zip
Merge pull request #5371 from chrisokasaki/issue/9906
SI-9906: override ListBuffer.last/lastOption to run in O(1) time
-rw-r--r--src/library/scala/collection/mutable/ListBuffer.scala19
1 files changed, 19 insertions, 0 deletions
diff --git a/src/library/scala/collection/mutable/ListBuffer.scala b/src/library/scala/collection/mutable/ListBuffer.scala
index 02fcced3ac..3bb7004184 100644
--- a/src/library/scala/collection/mutable/ListBuffer.scala
+++ b/src/library/scala/collection/mutable/ListBuffer.scala
@@ -386,6 +386,25 @@ final class ListBuffer[A]
this
}
+ /** Selects the last element.
+ *
+ * Runs in constant time.
+ *
+ * @return the last element of this buffer.
+ * @throws NoSuchElementException if this buffer is empty.
+ */
+ override def last: A =
+ if (last0 eq null) throw new NoSuchElementException("last of empty ListBuffer")
+ else last0.head
+
+ /** Optionally selects the last element.
+ *
+ * Runs in constant time.
+ *
+ * @return `Some` of the last element of this buffer if the buffer is nonempty, `None` if it is empty.
+ */
+ override def lastOption: Option[A] = if (last0 eq null) None else Some(last0.head)
+
/** Returns an iterator over this `ListBuffer`. The iterator will reflect
* changes made to the underlying `ListBuffer` beyond the next element;
* the next element's value is cached so that `hasNext` and `next` are