ListBuffer.last is O(N)

68 views
Skip to first unread message

Chris Okasaki

unread,
Aug 26, 2016, 9:31:24 AM8/26/16
to scala-internals
Given the representation of ListBuffer, it should be dead simple to retrieve the last element in constant time, but the current (inherited) implementation traverses the entire list.  I'm guessing this is merely an oversight, but is there some technical reason NOT to override the inherited method with the obvious constant-time implementation?  If it is just an oversight, I'm happy to provide a PR.

(Of course, removing the last element would be an entirely different matter, but just looking at the last element should be fine.)

-- Chris

Rex Kerr

unread,
Aug 26, 2016, 11:12:00 AM8/26/16
to scala-i...@googlegroups.com
AFAIK this is just an oversight, and a fix would be welcome.  Normal use cases don't involve requesting the last element of a `ListBuffer`, but those that do would benefit.

  --Rex

--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-internals+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Simon Ochsenreither

unread,
Aug 27, 2016, 5:29:23 AM8/27/16
to scala-internals
Reply all
Reply to author
Forward
0 new messages