len, is_empty, capacity should all take constant-time #149
Replies: 4 comments
-
What is the guideline for when this isn't the case? Add an extra couple of bytes to store the length, or use a name like For reference I have a crate that currently implements an |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
I think it's more important that |
Beta Was this translation helpful? Give feedback.
-
Note, for linked lists (which are niche data structures), the tradeoff between constant time slice and constant time len is complicated. To make it less complicated, specifying len as being amortized constant time makes sense. |
Beta Was this translation helpful? Give feedback.
-
This is the case in all libstd collections and I think that it's reasonable to put this in the guidelines. Although it's stated that
is_empty
may be faster thanlen == 0
, it should be clarified thatlen
should always take constant time to execute regardless. Users should expect that even for collections likeLinkedList
andHashMap
, checking the length is constant-time.Beta Was this translation helpful? Give feedback.
All reactions