Is it constant or O(n)? If O(n) are there similar data structures with constant time size operations?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Strangely,
sizeandlengthhave different descriptions in the ListBuffer docs. For sure,ListBuffer.lengthis constant time. Prior to Scala 2.8,lengthwas indeed O(n), but this is now fixed. The implementation ofsizeinTraversableOncesuggests that it is O(n), but I may be missing something.Other performance characteristics of Scala collections are documented here. For
ListBufferspecifically,where C is constant and L is linear time.
Edit : Both ListBuffer length and size are now O(1) – The issue mentioned by @KiptonBarros was closed with Scala 2.9.1 see: https://issues.scala-lang.org/browse/SI-4933