Hacker News new | ask | show | jobs
by esrauch 950 days ago
The question the grandparent comment wrote was "Explain why popfront would not be implemented in the current vector in O(1) by simply pointing the front pointer forward one element?"

Answering that with "there are tradeoffs and STL had to pick one" is misunderstanding the point of this question; the _premise_ of the question is that there are tradeoffs and STL picked this one and we can safely assume they have some reason; the leading question is highlighting an interesting case where the tradeoff isn't trivially obvious regarding difficult-to-use capacity laying at the front of the vector after the popfront operation happens if you implement that operation in O(1).

1 comments

I guess I still don't clearly see what the leading question is pointing at.

Should that be: _why was it not important to optimize for vector pop front?_

If that is the case, then I feel like the overall philosophy of vector vs deque answers that question.

Vectors are tailored to be good at random access and back insertion/removal, at the expense of wasting capacity. Overall, that means vectors are focused for append-mostly workloads, thus why they often have and aggressive capacity reallocation factor.

Having a vector double down on unused capacity consumption by allowing constant time front removal was, I guess, deemed useless, since one would most likely be using a deque for such access patterns.

I'm thinking it's maybe it's "too obvious" for you that makes you misunderstand the point.

It's just:

- Look at these big-O, see that pop front isn't O(1)

- Here's a proposed pop front that is O(1) (move the pointer forward)

- Reason about why they didn't choose that.

It's already an aha moment for people to realize that the capacity left at front is generally harder to use than keeping unused capacity at the end. In the spirit of the leading question that aspect is something the reader can reasonably realize after thinking about it, not something that is intended to already be obvious before you start thinking.