summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchrisokasaki <cokasaki@gmail.com>2016-08-30 23:46:31 -0400
committerchrisokasaki <cokasaki@gmail.com>2016-08-30 23:46:31 -0400
commitef8360f243d7d840437f3383971898a66bf758c2 (patch)
tree8f9ede3112bf67e43d385c05681d7b76ef88d405
parent5e91aa563dca2ea6b2aba2b767994c06a0fb2a61 (diff)
downloadscala-ef8360f243d7d840437f3383971898a66bf758c2.tar.gz
scala-ef8360f243d7d840437f3383971898a66bf758c2.tar.bz2
scala-ef8360f243d7d840437f3383971898a66bf758c2.zip
SI-9906: override ListBuffer.last/lastOption to run in O(1) time
Also update scaladocs for those two methods.
-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