diff options
author | chrisokasaki <cokasaki@gmail.com> | 2016-08-30 23:46:31 -0400 |
---|---|---|
committer | chrisokasaki <cokasaki@gmail.com> | 2016-08-30 23:46:31 -0400 |
commit | ef8360f243d7d840437f3383971898a66bf758c2 (patch) | |
tree | 8f9ede3112bf67e43d385c05681d7b76ef88d405 | |
parent | 5e91aa563dca2ea6b2aba2b767994c06a0fb2a61 (diff) | |
download | scala-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.scala | 19 |
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 |